π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
wall: use stable post pagination cursors
Neeraj Gupta5724c34e1154
diff --git a/server/wall/repo/module_test.go b/server/wall/repo/module_test.goindex b3d0d4d83c..1e1cd08b22 100644--- a/server/wall/repo/module_test.go+++ b/server/wall/repo/module_test.go@@ -593,13 +593,14 @@ func TestListPostsByWallPaginates(t *testing.T) {require.NoError(t, err)third, err := module.Posts.CreatePost(ctx, aliceID, wall.WallID, "post-key-3", nil, wall.CurrentVersion, nil)require.NoError(t, err)+ setPostCreatedAt(t, module, 1000, first, second, third)page, nextCursor, err := module.Posts.ListPostsByWall(ctx, wall.WallID, aliceID, "", 2)require.NoError(t, err)require.Len(t, page, 2)require.Equal(t, third, page[0].PostID)require.Equal(t, second, page[1].PostID)- require.Equal(t, strconv.FormatInt(second, 10), nextCursor)+ require.Equal(t, "1000:"+strconv.FormatInt(second, 10), nextCursor)page, nextCursor, err = module.Posts.ListPostsByWall(ctx, wall.WallID, aliceID, nextCursor, 2)require.NoError(t, err)@@ -608,6 +609,66 @@ func TestListPostsByWallPaginates(t *testing.T) {require.Empty(t, nextCursor)}+func TestListPostsByWallCursorUsesCreatedAtSortOrder(t *testing.T) {+ ctx := context.Background()+ module := newWallTestModule(t)++ aliceID := insertWallUser(t, module, "[email protected]", "alice-public")+ wall, err := module.Walls.CreateWall(ctx, aliceID, "alice", "alice-wall-key", "alice-profile")+ require.NoError(t, err)+ first, err := module.Posts.CreatePost(ctx, aliceID, wall.WallID, "post-key-1", nil, wall.CurrentVersion, nil)+ require.NoError(t, err)+ second, err := module.Posts.CreatePost(ctx, aliceID, wall.WallID, "post-key-2", nil, wall.CurrentVersion, nil)+ require.NoError(t, err)+ third, err := module.Posts.CreatePost(ctx, aliceID, wall.WallID, "post-key-3", nil, wall.CurrentVersion, nil)+ require.NoError(t, err)+ setPostCreatedAt(t, module, 3000, first)+ setPostCreatedAt(t, module, 2000, second)+ setPostCreatedAt(t, module, 1000, third)++ page, nextCursor, err := module.Posts.ListPostsByWall(ctx, wall.WallID, aliceID, "", 1)+ require.NoError(t, err)+ require.Len(t, page, 1)+ require.Equal(t, first, page[0].PostID)+ require.Equal(t, "3000:"+strconv.FormatInt(first, 10), nextCursor)++ page, nextCursor, err = module.Posts.ListPostsByWall(ctx, wall.WallID, aliceID, nextCursor, 1)+ require.NoError(t, err)+ require.Len(t, page, 1)+ require.Equal(t, second, page[0].PostID)+ require.Equal(t, "2000:"+strconv.FormatInt(second, 10), nextCursor)+}++func TestListFeedCursorUsesCreatedAtSortOrder(t *testing.T) {+ ctx := context.Background()+ module := newWallTestModule(t)++ aliceID := insertWallUser(t, module, "[email protected]", "alice-public")+ wall, err := module.Walls.CreateWall(ctx, aliceID, "alice", "alice-wall-key", "alice-profile")+ require.NoError(t, err)+ first, err := module.Posts.CreatePost(ctx, aliceID, wall.WallID, "post-key-1", nil, wall.CurrentVersion, nil)+ require.NoError(t, err)+ second, err := module.Posts.CreatePost(ctx, aliceID, wall.WallID, "post-key-2", nil, wall.CurrentVersion, nil)+ require.NoError(t, err)+ third, err := module.Posts.CreatePost(ctx, aliceID, wall.WallID, "post-key-3", nil, wall.CurrentVersion, nil)+ require.NoError(t, err)+ setPostCreatedAt(t, module, 3000, first)+ setPostCreatedAt(t, module, 2000, second)+ setPostCreatedAt(t, module, 1000, third)++ page, nextCursor, err := module.Posts.ListFeed(ctx, aliceID, "", 1)+ require.NoError(t, err)+ require.Len(t, page, 1)+ require.Equal(t, first, page[0].PostID)+ require.Equal(t, "3000:"+strconv.FormatInt(first, 10), nextCursor)++ page, nextCursor, err = module.Posts.ListFeed(ctx, aliceID, nextCursor, 1)+ require.NoError(t, err)+ require.Len(t, page, 1)+ require.Equal(t, second, page[0].PostID)+ require.Equal(t, "2000:"+strconv.FormatInt(second, 10), nextCursor)+}+func TestListTopLevelCommentsPaginates(t *testing.T) {ctx := context.Background()module := newWallTestModule(t)@@ -665,3 +726,11 @@ func requireQueuedTempObject(t *testing.T, module *Module, objectKey, purpose, brequire.Equal(t, bucketID, gotBucketID)require.True(t, expired)}++func setPostCreatedAt(t *testing.T, module *Module, createdAt int64, postIDs ...int64) {+ t.Helper()+ for _, postID := range postIDs {+ _, err := module.Posts.DB.Exec(`UPDATE wall_posts SET created_at = $1 WHERE post_id = $2`, createdAt, postID)+ require.NoError(t, err)+ }+}diff --git a/server/wall/repo/posts.go b/server/wall/repo/posts.goindex c672b8feaa..a129dd6dad 100644--- a/server/wall/repo/posts.go+++ b/server/wall/repo/posts.go@@ -82,15 +82,13 @@ func (r *PostsRepository) ListPostsByWall(ctx context.Context, wallID string, vi(SELECT COUNT(*) FROM wall_post_likes pl WHERE pl.post_id = p.post_id) AS likes,EXISTS (SELECT 1 FROM wall_post_likes pl WHERE pl.post_id = p.post_id AND pl.user_id = $2) AS viewer_liked,(SELECT COUNT(*) FROM wall_post_comments pc WHERE pc.post_id = p.post_id AND pc.is_deleted = FALSE) AS comments- 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- WHERE p.wall_id = $1 AND p.is_deleted = FALSE`- if trimmed := strings.TrimSpace(cursor); trimmed != "" {- if cursorID, err := strconv.ParseInt(trimmed, 10, 64); err == nil && cursorID > 0 {- args = append(args, cursorID)- query += ` AND p.post_id < $3`- }+ 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+ WHERE p.wall_id = $1 AND p.is_deleted = FALSE`+ if cursorCreatedAt, cursorPostID, ok := parsePostCursor(cursor); ok {+ args = append(args, cursorCreatedAt, cursorPostID)+ query += ` AND (p.created_at, p.post_id) < ($3, $4)`}args = append(args, limit+1)query += ` ORDER BY p.created_at DESC, p.post_id DESC LIMIT $` + strconv.Itoa(len(args))@@ -112,7 +110,7 @@ func (r *PostsRepository) ListPostsByWall(ctx context.Context, wallID string, vi}nextCursor := ""if len(out) > limit {- nextCursor = strconv.FormatInt(out[limit-1].PostID, 10)+ nextCursor = formatPostCursor(out[limit-1])out = out[:limit]}return out, nextCursor, nil@@ -140,12 +138,10 @@ func (r *PostsRepository) ListFeed(ctx context.Context, viewerID int64, cursor sSELECT 1 FROM wall_follow_shares fsWHERE fs.follower_id = $1 AND fs.wall_id = p.wall_id)- )`- if trimmed := strings.TrimSpace(cursor); trimmed != "" {- if cursorID, err := strconv.ParseInt(trimmed, 10, 64); err == nil && cursorID > 0 {- args = append(args, cursorID)- query += ` AND p.post_id < $2`- }+ )`+ if cursorCreatedAt, cursorPostID, ok := parsePostCursor(cursor); ok {+ args = append(args, cursorCreatedAt, cursorPostID)+ query += ` AND (p.created_at, p.post_id) < ($2, $3)`}args = append(args, limit+1)query += ` ORDER BY p.created_at DESC, p.post_id DESC LIMIT $` + strconv.Itoa(len(args))@@ -167,7 +163,7 @@ func (r *PostsRepository) ListFeed(ctx context.Context, viewerID int64, cursor s}nextCursor := ""if len(out) > limit {- nextCursor = strconv.FormatInt(out[limit-1].PostID, 10)+ nextCursor = formatPostCursor(out[limit-1])out = out[:limit]}return out, nextCursor, nil@@ -404,6 +400,26 @@ func scanPostRecord(scanner interface{ Scan(dest ...any) error }) (*WallPostRecoreturn &rec, nil}+func parsePostCursor(cursor string) (int64, int64, bool) {+ createdAtText, postIDText, ok := strings.Cut(strings.TrimSpace(cursor), ":")+ if !ok {+ return 0, 0, false+ }+ createdAt, err := strconv.ParseInt(createdAtText, 10, 64)+ if err != nil || createdAt <= 0 {+ return 0, 0, false+ }+ postID, err := strconv.ParseInt(postIDText, 10, 64)+ if err != nil || postID <= 0 {+ return 0, 0, false+ }+ return createdAt, postID, true+}++func formatPostCursor(post WallPostRecord) string {+ return strconv.FormatInt(post.CreatedAt, 10) + ":" + strconv.FormatInt(post.PostID, 10)+}+func scanCommentRecord(scanner interface{ Scan(dest ...any) error }) (*WallCommentRecord, error) {var rec WallCommentRecordif err := scanner.Scan(&rec.CommentID, &rec.PostID, &rec.AuthorID, &rec.Author, &rec.CommentCipher, &rec.ParentCommentID, &rec.CreatedAt, &rec.ViewerCanDelete); err != nil {