Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Prepare wall profile architecture

Anand Baburajan31c52b7bbbee
diff --git a/rust/wall/src/client.rs b/rust/wall/src/client.rs
index c2f7b0d1a3..cde54ce14e 100644
--- a/rust/wall/src/client.rs
+++ b/rust/wall/src/client.rs
@@ -192,7 +192,7 @@ impl AccountWallCtx {
})
}
- async fn default_owned_wall_access(&self) -> Result<(WallKeyResponse, Vec<u8>)> {
+ async fn default_profile_wall_access(&self) -> Result<(WallKeyResponse, Vec<u8>)> {
let root_wall_key = self
.get_root_wall_key()
.await?
@@ -988,7 +988,7 @@ impl AccountWallCtx {
}
async fn friend_actor_for_wall(&self, wall_id: &str) -> Result<WallActorResponse> {
- let (owned_wall, _) = self.default_owned_wall_access().await?;
+ let (owned_wall, _) = self.default_profile_wall_access().await?;
let friends = self.list_wall_friends(&owned_wall.wall_id).await?;
friends
.into_iter()
@@ -1024,7 +1024,7 @@ impl AccountWallCtx {
"target public key is required".into(),
));
}
- let (requester_wall, requester_wall_key) = self.default_owned_wall_access().await?;
+ let (requester_wall, requester_wall_key) = self.default_profile_wall_access().await?;
let target_share = sealed::seal(link.wall_key(), &self.public_key)?;
let requester_share = sealed::seal(&requester_wall_key, link.owner_public_key())?;
let payload = AddFriendPayload {
diff --git a/server/wall/controller/messages.go b/server/wall/controller/messages.go
index 834b392f90..176c1d88f7 100644
--- a/server/wall/controller/messages.go
+++ b/server/wall/controller/messages.go
@@ -203,14 +203,13 @@ func (c *MessagesController) requireFriendMessageTarget(ctx *gin.Context, userID
if targetWallID == "" {
return nil, nil, ente.NewBadRequestWithMessage("wallId is required")
}
- ownedWalls, err := c.WallsRepo.ListWallsByOwner(ctx.Request.Context(), userID)
+ senderWall, err := c.WallsRepo.GetDefaultWallByOwner(ctx.Request.Context(), userID)
if err != nil {
+ if errors.Is(stacktrace.RootCause(err), sql.ErrNoRows) {
+ return nil, nil, ente.NewBadRequestWithMessage("sender wall is missing")
+ }
return nil, nil, err
}
- if len(ownedWalls) == 0 {
- return nil, nil, ente.NewBadRequestWithMessage("sender wall is missing")
- }
- senderWall := ownedWalls[0]
recipientWall, err := c.WallsRepo.GetWallByID(ctx.Request.Context(), targetWallID)
if err != nil {
return nil, nil, err
@@ -224,7 +223,7 @@ func (c *MessagesController) requireFriendMessageTarget(ctx *gin.Context, userID
}
return nil, nil, err
}
- return &senderWall, recipientWall, nil
+ return senderWall, recipientWall, nil
}
func (c *MessagesController) validateReplyMessage(ctx *gin.Context, userID int64, replyMessageID, senderWallID, recipientWallID string) (sql.NullString, error) {
diff --git a/server/wall/repo/friends.go b/server/wall/repo/friends.go
index 5e6fa8c723..62009dc36d 100644
--- a/server/wall/repo/friends.go
+++ b/server/wall/repo/friends.go
@@ -169,7 +169,13 @@ func (r *FriendsRepository) DeleteFriendship(ctx context.Context, userID int64,
if err := tx.QueryRowContext(ctx, `
SELECT target_wall.owner_id, actor_wall.wall_id
FROM walls target_wall
- JOIN walls actor_wall ON actor_wall.owner_id = $2
+ JOIN LATERAL (
+ SELECT wall_id
+ FROM walls
+ WHERE owner_id = $2
+ ORDER BY created_at ASC
+ LIMIT 1
+ ) actor_wall ON TRUE
WHERE target_wall.wall_id = $1
`, targetWallID, userID).Scan(&targetOwnerID, &actorWallID); err != nil {
return stacktrace.Propagate(err, "")
@@ -212,10 +218,9 @@ func (r *FriendsRepository) DeleteShareByWallAndFriend(ctx context.Context, wall
func (r *FriendsRepository) GetShareForFriendAndWall(ctx context.Context, friendID int64, wallID string) (*WallShareRecord, error) {
return scanShareRecord(r.DB.QueryRowContext(ctx, `
- SELECT s.wall_id, s.friend_id, w.owner_id, owner_wall.wall_slug, s.encrypted_wall_key, s.key_version, s.created_at, ka.public_key
+ SELECT s.wall_id, s.friend_id, w.owner_id, w.wall_slug, s.encrypted_wall_key, s.key_version, s.created_at, ka.public_key
FROM wall_friend_shares s
JOIN walls w ON w.wall_id = s.wall_id
- JOIN walls owner_wall ON owner_wall.owner_id = w.owner_id
JOIN key_attributes ka ON ka.user_id = w.owner_id
WHERE s.friend_id = $1 AND s.wall_id = $2
`, friendID, wallID))
@@ -223,10 +228,9 @@ func (r *FriendsRepository) GetShareForFriendAndWall(ctx context.Context, friend
func (r *FriendsRepository) ListSharesForFriend(ctx context.Context, friendID int64) ([]WallShareRecord, error) {
rows, err := r.DB.QueryContext(ctx, `
- SELECT s.wall_id, s.friend_id, w.owner_id, owner_wall.wall_slug, s.encrypted_wall_key, s.key_version, s.created_at, ka.public_key
+ SELECT s.wall_id, s.friend_id, w.owner_id, w.wall_slug, s.encrypted_wall_key, s.key_version, s.created_at, ka.public_key
FROM wall_friend_shares s
JOIN walls w ON w.wall_id = s.wall_id
- JOIN walls owner_wall ON owner_wall.owner_id = w.owner_id
JOIN key_attributes ka ON ka.user_id = w.owner_id
WHERE s.friend_id = $1
ORDER BY s.created_at ASC
diff --git a/server/wall/repo/links.go b/server/wall/repo/links.go
index 73945c2797..67302fc032 100644
--- a/server/wall/repo/links.go
+++ b/server/wall/repo/links.go
@@ -152,10 +152,9 @@ func (r *LinksRepository) CreateSession(ctx context.Context, tokenHash []byte, w
func (r *LinksRepository) GetSession(ctx context.Context, tokenHash []byte) (*WallLinkSessionRecord, error) {
row := r.DB.QueryRowContext(ctx, `
SELECT s.token_hash, s.wall_id, s.owner_id, s.auth_key_hash, s.key_version, s.expires_at, s.created_at,
- w.wall_slug, owner_wall.wall_slug, l.encrypted_wall_key
+ w.wall_slug, w.wall_slug, l.encrypted_wall_key
FROM wall_link_sessions s
JOIN walls w ON w.wall_id = s.wall_id
- JOIN walls owner_wall ON owner_wall.owner_id = s.owner_id
JOIN wall_links l ON l.wall_id = s.wall_id
AND l.auth_key_hash = s.auth_key_hash
AND l.key_version = s.key_version
diff --git a/server/wall/repo/posts.go b/server/wall/repo/posts.go
index 4c74e583c4..173b882a11 100644
--- a/server/wall/repo/posts.go
+++ b/server/wall/repo/posts.go
@@ -59,11 +59,11 @@ func (r *PostsRepository) CreatePost(ctx context.Context, ownerID int64, wallID,
func (r *PostsRepository) GetPost(ctx context.Context, postID int64, viewerID int64) (*WallPostRecord, error) {
return scanPostRecord(r.DB.QueryRowContext(ctx, `
SELECT p.post_id, p.wall_id, w.wall_slug, p.owner_id,
- owner_wall.owner_id, owner_wall.wall_id, owner_wall.wall_slug, owner_ka.public_key,
- owner_wall.current_version, owner_wall.encrypted_profile, owner_wall.avatar_object_key,
- owner_wall.avatar_size, owner_wall.updated_at,
- (SELECT COUNT(*) FROM wall_friend_shares fs WHERE fs.wall_id = owner_wall.wall_id) AS author_friends,
- (SELECT COUNT(*) FROM wall_posts ap WHERE ap.wall_id = owner_wall.wall_id AND ap.is_deleted = FALSE) AS author_posts,
+ w.owner_id, w.wall_id, w.wall_slug, owner_ka.public_key,
+ w.current_version, w.encrypted_profile, w.avatar_object_key,
+ w.avatar_size, w.updated_at,
+ (SELECT COUNT(*) FROM wall_friend_shares fs WHERE fs.wall_id = w.wall_id) AS author_friends,
+ (SELECT COUNT(*) FROM wall_posts ap WHERE ap.wall_id = w.wall_id AND ap.is_deleted = FALSE) AS author_posts,
p.encrypted_post_key, p.caption_cipher,
p.key_version, p.created_at,
(SELECT COUNT(*) FROM wall_post_likes pl WHERE pl.post_id = p.post_id) AS likes,
@@ -71,8 +71,7 @@ func (r *PostsRepository) GetPost(ctx context.Context, postID int64, viewerID in
FALSE AS viewer_unread
FROM wall_posts p
JOIN walls w ON w.wall_id = p.wall_id
- JOIN walls owner_wall ON owner_wall.owner_id = p.owner_id
- JOIN key_attributes owner_ka ON owner_ka.user_id = p.owner_id
+ JOIN key_attributes owner_ka ON owner_ka.user_id = w.owner_id
WHERE p.post_id = $1 AND p.is_deleted = FALSE
`, postID, viewerID))
}
@@ -85,11 +84,11 @@ func (r *PostsRepository) ListPostsByWall(ctx context.Context, wallID string, vi
args := []any{wallID, viewerID}
query := `
SELECT p.post_id, p.wall_id, w.wall_slug, p.owner_id,
- owner_wall.owner_id, owner_wall.wall_id, owner_wall.wall_slug, owner_ka.public_key,
- owner_wall.current_version, owner_wall.encrypted_profile, owner_wall.avatar_object_key,
- owner_wall.avatar_size, owner_wall.updated_at,
- (SELECT COUNT(*) FROM wall_friend_shares fs WHERE fs.wall_id = owner_wall.wall_id) AS author_friends,
- (SELECT COUNT(*) FROM wall_posts ap WHERE ap.wall_id = owner_wall.wall_id AND ap.is_deleted = FALSE) AS author_posts,
+ w.owner_id, w.wall_id, w.wall_slug, owner_ka.public_key,
+ w.current_version, w.encrypted_profile, w.avatar_object_key,
+ w.avatar_size, w.updated_at,
+ (SELECT COUNT(*) FROM wall_friend_shares fs WHERE fs.wall_id = w.wall_id) AS author_friends,
+ (SELECT COUNT(*) FROM wall_posts ap WHERE ap.wall_id = w.wall_id AND ap.is_deleted = FALSE) AS author_posts,
p.encrypted_post_key, p.caption_cipher,
p.key_version, p.created_at,
(SELECT COUNT(*) FROM wall_post_likes pl WHERE pl.post_id = p.post_id) AS likes,
@@ -97,8 +96,7 @@ func (r *PostsRepository) ListPostsByWall(ctx context.Context, wallID string, vi
FALSE AS viewer_unread
FROM wall_posts p
JOIN walls w ON w.wall_id = p.wall_id
- JOIN walls owner_wall ON owner_wall.owner_id = p.owner_id
- JOIN key_attributes owner_ka ON owner_ka.user_id = p.owner_id
+ JOIN key_attributes owner_ka ON owner_ka.user_id = w.owner_id
WHERE p.wall_id = $1 AND p.is_deleted = FALSE`
if cursorCreatedAt, cursorPostID, ok := parsePostCursor(cursor); ok {
args = append(args, cursorCreatedAt, cursorPostID)
@@ -138,11 +136,11 @@ func (r *PostsRepository) ListFeed(ctx context.Context, viewerID int64, cursor s
args := []any{viewerID, readCreatedAt, readPostID}
query := `
SELECT p.post_id, p.wall_id, w.wall_slug, p.owner_id,
- owner_wall.owner_id, owner_wall.wall_id, owner_wall.wall_slug, owner_ka.public_key,
- owner_wall.current_version, owner_wall.encrypted_profile, owner_wall.avatar_object_key,
- owner_wall.avatar_size, owner_wall.updated_at,
- (SELECT COUNT(*) FROM wall_friend_shares fs WHERE fs.wall_id = owner_wall.wall_id) AS author_friends,
- (SELECT COUNT(*) FROM wall_posts ap WHERE ap.wall_id = owner_wall.wall_id AND ap.is_deleted = FALSE) AS author_posts,
+ w.owner_id, w.wall_id, w.wall_slug, owner_ka.public_key,
+ w.current_version, w.encrypted_profile, w.avatar_object_key,
+ w.avatar_size, w.updated_at,
+ (SELECT COUNT(*) FROM wall_friend_shares fs WHERE fs.wall_id = w.wall_id) AS author_friends,
+ (SELECT COUNT(*) FROM wall_posts ap WHERE ap.wall_id = w.wall_id AND ap.is_deleted = FALSE) AS author_posts,
p.encrypted_post_key, p.caption_cipher,
p.key_version, p.created_at,
(SELECT COUNT(*) FROM wall_post_likes pl WHERE pl.post_id = p.post_id) AS likes,
@@ -150,8 +148,7 @@ func (r *PostsRepository) ListFeed(ctx context.Context, viewerID int64, cursor s
((p.created_at, p.post_id) > ($2::bigint, $3::bigint)) AS viewer_unread
FROM wall_posts p
JOIN walls w ON w.wall_id = p.wall_id
- JOIN walls owner_wall ON owner_wall.owner_id = p.owner_id
- JOIN key_attributes owner_ka ON owner_ka.user_id = p.owner_id
+ JOIN key_attributes owner_ka ON owner_ka.user_id = w.owner_id
WHERE p.is_deleted = FALSE
AND p.owner_id <> $1
AND EXISTS (
diff --git a/server/wall/repo/walls.go b/server/wall/repo/walls.go
index 3e394769b8..d44ba7d8f9 100644
--- a/server/wall/repo/walls.go
+++ b/server/wall/repo/walls.go
@@ -70,6 +70,17 @@ func (r *WallsRepository) ListWallsByOwner(ctx context.Context, ownerID int64) (
return walls, stacktrace.Propagate(rows.Err(), "")
}
+func (r *WallsRepository) GetDefaultWallByOwner(ctx context.Context, ownerID int64) (*WallRecord, error) {
+ return scanWallRecord(r.DB.QueryRowContext(ctx, `
+ SELECT wall_id, owner_id, wall_slug, encrypted_wall_key, encrypted_profile, current_version,
+ avatar_object_key, avatar_bucket_id, avatar_size, created_at, updated_at
+ FROM walls
+ WHERE owner_id = $1
+ ORDER BY created_at ASC
+ LIMIT 1
+ `, ownerID))
+}
+
func (r *WallsRepository) GetWallByID(ctx context.Context, wallID string) (*WallRecord, error) {
return scanWallRecord(r.DB.QueryRowContext(ctx, `
SELECT wall_id, owner_id, wall_slug, encrypted_wall_key, encrypted_profile, current_version,
diff --git a/web/apps/social/src/services/socialProfile.ts b/web/apps/social/src/services/socialProfile.ts
index de557d12a2..92cd3f3844 100644
--- a/web/apps/social/src/services/socialProfile.ts
+++ b/web/apps/social/src/services/socialProfile.ts
@@ -108,6 +108,8 @@ const wallHTTPStatus = (error: unknown) => {
return typeof status == "number" ? status : undefined;
};
+const defaultOwnedWall = (walls: OwnedWall[]) => walls[0];
+
export const openCurrentWallContext = async () => {
const [authToken, baseUrl, masterKeyB64] = await Promise.all([
savedAuthToken(),
@@ -176,7 +178,7 @@ export const loadExistingSocialProfile = async () => {
try {
const walls = (await ctx.list_owned_walls()) as OwnedWall[];
- const wall = walls[0];
+ const wall = defaultOwnedWall(walls);
if (!wall) return null;
const wallProfile = (await ctx.get_wall_profile(
@@ -238,7 +240,7 @@ export const saveSocialProfile = async (
const existingWall =
(profile.wallId &&
walls.find((wall) => wall.wallId == profile.wallId)) ||
- walls[0];
+ defaultOwnedWall(walls);
const profilePayload = wallProfilePayloadFor({ ...profile, username });
let wallId: string;