Skip to content

armgit / ente

Public Imported from GitHub
0

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

Bound Space comment visibility work to each page

Anand Baburajanc158a41cabde
diff --git a/server/space/repo/comments.go b/server/space/repo/comments.go
index 88654d484e..b97b397fb1 100644
--- a/server/space/repo/comments.go
+++ b/server/space/repo/comments.go
@@ -72,6 +72,47 @@ func hiddenCommentsCTE(postFilter string) string {
)`
}
+const commentAncestorsSQL = `WITH RECURSIVE comment_ancestors AS (
+ SELECT c.comment_id, c.reply_to_id, c.author_space_id, c.cipher IS NOT NULL AS has_content
+ FROM unnest($1::text[]) target(comment_id)
+ CROSS JOIN LATERAL (
+ SELECT c.comment_id, c.reply_to_id, c.author_space_id, c.cipher
+ FROM space_comments c WHERE c.comment_id = target.comment_id
+ OFFSET 0
+ ) c
+ UNION
+ SELECT parent.comment_id, parent.reply_to_id, parent.author_space_id, parent.cipher IS NOT NULL
+ FROM comment_ancestors child
+ CROSS JOIN LATERAL (
+ SELECT c.comment_id, c.reply_to_id, c.author_space_id, c.cipher
+ FROM space_comments c WHERE c.comment_id = child.reply_to_id
+ OFFSET 0
+ ) parent
+)
+SELECT c.comment_id, COALESCE(c.reply_to_id, ''), c.has_content AND account.encrypted_email IS NOT NULL
+FROM comment_ancestors c
+LEFT JOIN spaces author ON author.space_id = c.author_space_id
+LEFT JOIN users account ON account.user_id = author.owner_id`
+
+var commentsPageSQL = `SELECT c.comment_id, COALESCE(c.parent_id, ''), COALESCE(c.reply_to_id, ''),
+ CASE WHEN actor.space_id IS NOT NULL THEN c.cipher END, c.created_at,
+ (COALESCE(actor.space_id = $2, FALSE) OR EXISTS (SELECT 1 FROM space_friend_shares f WHERE f.space_id = actor.space_id AND f.friend_space_id = $2)),
+ COALESCE(actor.space_id, ''), COALESCE(actor.space_slug, ''), actor.public_key, COALESCE(actor.current_version, 0), actor.encrypted_profile,
+ avatar.object_id, avatar.key_version, avatar.size, COALESCE(actor.updated_at, 0)
+ FROM space_comments c LEFT JOIN spaces actor ON actor.space_id = c.author_space_id
+ AND EXISTS (SELECT 1 FROM users u WHERE u.user_id = actor.owner_id AND u.encrypted_email IS NOT NULL) ` + spaceActorAvatarJoin("actor", "avatar") + `
+ WHERE c.post_id = $1 AND (c.created_at, c.comment_id) > ($3, $4)
+ ORDER BY c.created_at, c.comment_id LIMIT 101`
+
+var commentsUnreadNotificationsSQL = `SELECT notification.comment_id, notification.notification_id
+ FROM unnest($2::text[]) target(comment_id)
+ CROSS JOIN LATERAL (
+ SELECT n.comment_id, n.notification_id ` + notificationSourcesSQL + `
+ WHERE n.recipient_space_id = $1 AND n.comment_id = target.comment_id AND n.read_at IS NULL
+ AND ` + notificationVisibleSQL("TRUE") + `
+ OFFSET 0
+ ) notification`
+
var commentRecipientsSQL = `WITH RECURSIVE ` + hiddenCommentsCTE("c.post_id = $2") + `
SELECT ARRAY(
SELECT participant.space_id FROM (
@@ -130,15 +171,7 @@ func (r *CommentsRepository) List(ctx context.Context, viewer string, postID int
return nil, "", err
}
createdAt, id, _ := parseMessageCursor(cursor)
- rows, err := tx.QueryContext(ctx, `WITH RECURSIVE `+hiddenCommentsCTE("c.post_id = $1")+`
- SELECT c.comment_id, COALESCE(c.parent_id, ''), COALESCE(c.reply_to_id, ''),
- CASE WHEN `+commentNotHiddenSQL+` THEN c.cipher END, c.created_at,
- (COALESCE(actor.space_id = $2, FALSE) OR EXISTS (SELECT 1 FROM space_friend_shares f WHERE f.space_id = actor.space_id AND f.friend_space_id = $2)),
- COALESCE(actor.space_id, ''), COALESCE(actor.space_slug, ''), actor.public_key, COALESCE(actor.current_version, 0), actor.encrypted_profile,
- avatar.object_id, avatar.key_version, avatar.size, COALESCE(actor.updated_at, 0)
- FROM space_comments c LEFT JOIN spaces actor ON actor.space_id = c.author_space_id
- AND EXISTS (SELECT 1 FROM users u WHERE u.user_id = actor.owner_id AND u.encrypted_email IS NOT NULL) `+spaceActorAvatarJoin("actor", "avatar")+`
- WHERE c.post_id = $1 AND (c.created_at, c.comment_id) > ($3, $4) ORDER BY c.created_at, c.comment_id LIMIT 101`, postID, viewer, createdAt, id)
+ rows, err := tx.QueryContext(ctx, commentsPageSQL, postID, viewer, createdAt, id)
if err != nil {
return nil, "", err
}
@@ -197,10 +230,7 @@ func (r *CommentsRepository) List(ctx context.Context, viewer string, postID int
return nil, "", err
}
rows.Close()
- const unreadFilter = `n.recipient_space_id = $1 AND n.comment_id = ANY($2) AND n.read_at IS NULL`
- rows, err = tx.QueryContext(ctx, `WITH RECURSIVE `+notificationHiddenCommentsCTE(unreadFilter)+`
- SELECT n.comment_id, n.notification_id `+notificationSourcesSQL+`
- WHERE `+unreadFilter+` AND `+notificationVisibleSQL(commentNotHiddenSQL), viewer, pq.Array(commentIDs))
+ rows, err = tx.QueryContext(ctx, commentsUnreadNotificationsSQL, viewer, pq.Array(commentIDs))
if err != nil {
return nil, "", err
}
@@ -216,9 +246,58 @@ func (r *CommentsRepository) List(ctx context.Context, viewer string, postID int
if err := rows.Err(); err != nil {
return nil, "", err
}
+ rows.Close()
+ visible, err := commentBatchVisibility(ctx, tx, commentIDs)
+ if err != nil {
+ return nil, "", err
+ }
+ for i := range items {
+ if !visible[items[i].ID] {
+ items[i].Cipher = nil
+ items[i].CanDelete = false
+ items[i].Reactions = []CommentReactionRecord{}
+ items[i].UnreadNotificationIDs = []string{}
+ }
+ }
return items, next, tx.Commit()
}
+func commentBatchVisibility(ctx context.Context, tx *sql.Tx, commentIDs []string) (map[string]bool, error) {
+ rows, err := tx.QueryContext(ctx, commentAncestorsSQL, pq.Array(commentIDs))
+ if err != nil {
+ return nil, err
+ }
+ defer rows.Close()
+ parents := make(map[string]string)
+ visible := make(map[string]bool)
+ for rows.Next() {
+ var id, parentID string
+ var hasContent bool
+ if err := rows.Scan(&id, &parentID, &hasContent); err != nil {
+ return nil, err
+ }
+ visible[id] = hasContent
+ if parentID != "" {
+ parents[id] = parentID
+ }
+ }
+ if err := rows.Err(); err != nil {
+ return nil, err
+ }
+ var isVisible func(string) bool
+ isVisible = func(id string) bool {
+ if parentID, ok := parents[id]; ok {
+ visible[id] = visible[id] && isVisible(parentID)
+ delete(parents, id)
+ }
+ return visible[id]
+ }
+ for _, id := range commentIDs {
+ isVisible(id)
+ }
+ return visible, nil
+}
+
func (r *CommentsRepository) Create(ctx context.Context, viewer string, postID int64, req models.CreateCommentRequest, cipher []byte) error {
_, err := r.CreateWithCreated(ctx, viewer, postID, req, cipher)
return err
diff --git a/server/space/repo/comments_test.go b/server/space/repo/comments_test.go
index f5cc2315c7..6b62475fb5 100644
--- a/server/space/repo/comments_test.go
+++ b/server/space/repo/comments_test.go
@@ -276,6 +276,97 @@ func TestDiscussionBatchReadsWithDeepReplyChain(t *testing.T) {
}
}
+func TestDiscussionPageWorkIsBoundedByPageAndAncestors(t *testing.T) {
+ for _, shape := range []string{"flat", "shared-parent", "hidden-parent"} {
+ t.Run(shape, func(t *testing.T) {
+ module := newSpaceTestModule(t)
+ ctx := t.Context()
+ owner, friend := notificationTestSpaces(t, module)
+ postID, err := testCreatePost(ctx, module, owner.OwnerID, owner.SpaceID, "post-key", nil, 1, nil)
+ require.NoError(t, err)
+ _, err = module.Comments.DB.ExecContext(ctx, `INSERT INTO space_comments
+ (comment_id, post_id, author_space_id, cipher, created_at)
+ VALUES ('root', $1, $2, CASE WHEN $3 = 'hidden-parent' THEN NULL ELSE '\x01'::bytea END, 1)`, postID, owner.SpaceID, shape)
+ require.NoError(t, err)
+ previousSize := 1
+ previousRows := make(map[string]float64)
+ for _, size := range []int{1000, 10000} {
+ _, err = module.Comments.DB.ExecContext(ctx, `INSERT INTO space_comments
+ (comment_id, post_id, author_space_id, parent_id, reply_to_id, cipher, created_at)
+ SELECT 'comment-' || i, $1, $2,
+ CASE WHEN $3 <> 'flat' THEN 'root' END,
+ CASE WHEN $3 <> 'flat' THEN 'root' END, '\x01'::bytea, i
+ FROM generate_series($4::int + 1, $5::int) i`, postID, owner.SpaceID, shape, previousSize, size)
+ require.NoError(t, err)
+ _, err = module.Comments.DB.ExecContext(ctx, `INSERT INTO space_notifications
+ (notification_id, recipient_space_id, actor_space_id, kind, comment_id, created_at)
+ SELECT 'notification-' || i, $1, $2, 'comment_reaction', 'comment-' || i, i
+ FROM generate_series($3::int + 1, $4::int) i`, owner.SpaceID, friend.SpaceID, previousSize, size)
+ require.NoError(t, err)
+ _, err = module.Comments.DB.ExecContext(ctx, `ANALYZE space_comments; ANALYZE space_notifications`)
+ require.NoError(t, err)
+ createdAt := size - 101
+ id := fmt.Sprintf("comment-%d", createdAt)
+ items, cursor, err := module.Comments.List(ctx, owner.SpaceID, postID, fmt.Sprintf("%d:%s", createdAt, id))
+ require.NoError(t, err)
+ require.Len(t, items, 100)
+ require.Equal(t, fmt.Sprintf("%d:comment-%d", size-1, size-1), cursor)
+ ids := make([]string, 0, len(items))
+ for i, item := range items {
+ require.Equal(t, fmt.Sprintf("comment-%d", createdAt+i+1), item.ID)
+ ids = append(ids, item.ID)
+ if shape == "hidden-parent" {
+ require.Empty(t, item.Cipher)
+ require.Empty(t, item.UnreadNotificationIDs)
+ } else {
+ require.Equal(t, []byte{1}, item.Cipher)
+ require.Equal(t, []string{fmt.Sprintf("notification-%d", createdAt+i+1)}, item.UnreadNotificationIDs)
+ }
+ }
+ for _, query := range []struct {
+ name, sql string
+ args []any
+ }{
+ {"page", commentsPageSQL, []any{postID, owner.SpaceID, createdAt, id}},
+ {"ancestors", commentAncestorsSQL, []any{pq.Array(ids)}},
+ {"unread", commentsUnreadNotificationsSQL, []any{owner.SpaceID, pq.Array(ids)}},
+ } {
+ var planJSON []byte
+ require.NoError(t, module.Comments.DB.QueryRowContext(ctx, `EXPLAIN (ANALYZE, FORMAT JSON) `+query.sql, query.args...).Scan(&planJSON))
+ type planNode struct {
+ Relation string `json:"Relation Name"`
+ Rows float64 `json:"Actual Rows"`
+ Filtered float64 `json:"Rows Removed by Filter"`
+ Loops float64 `json:"Actual Loops"`
+ Plans []planNode `json:"Plans"`
+ }
+ var plans []struct{ Plan planNode }
+ require.NoError(t, json.Unmarshal(planJSON, &plans))
+ require.Len(t, plans, 1)
+ var rowsExamined float64
+ var inspect func(planNode)
+ inspect = func(node planNode) {
+ if node.Relation == "space_comments" || node.Relation == "space_notifications" {
+ rowsExamined += (node.Rows + node.Filtered) * node.Loops
+ }
+ for _, child := range node.Plans {
+ inspect(child)
+ }
+ }
+ inspect(plans[0].Plan)
+ t.Logf("%d comments, %s: %.0f rows examined", size, query.name, rowsExamined)
+ require.Positive(t, rowsExamined, query.name)
+ if previousRows[query.name] > 0 {
+ require.LessOrEqual(t, rowsExamined, previousRows[query.name]*2, query.name)
+ }
+ previousRows[query.name] = rowsExamined
+ }
+ previousSize = size
+ }
+ })
+ }
+}
+
func TestDiscussionParentDeletionRemovesWholeThread(t *testing.T) {
for _, deleteAsOwner := range []bool{false, true} {
name := "author"