Skip to content

armgit / ente

Public Imported from GitHub
0

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

Simplify Space comment visibility for v1

Anand Baburajan82ce978c8c1f
diff --git a/server/space/repo/comments.go b/server/space/repo/comments.go
index b97b397fb1..88654d484e 100644
--- a/server/space/repo/comments.go
+++ b/server/space/repo/comments.go
@@ -72,47 +72,6 @@ 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 (
@@ -171,7 +130,15 @@ func (r *CommentsRepository) List(ctx context.Context, viewer string, postID int
return nil, "", err
}
createdAt, id, _ := parseMessageCursor(cursor)
- rows, err := tx.QueryContext(ctx, commentsPageSQL, postID, viewer, createdAt, id)
+ 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)
if err != nil {
return nil, "", err
}
@@ -230,7 +197,10 @@ func (r *CommentsRepository) List(ctx context.Context, viewer string, postID int
return nil, "", err
}
rows.Close()
- rows, err = tx.QueryContext(ctx, commentsUnreadNotificationsSQL, viewer, pq.Array(commentIDs))
+ 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))
if err != nil {
return nil, "", err
}
@@ -246,58 +216,9 @@ 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 6b62475fb5..79931ddc6c 100644
--- a/server/space/repo/comments_test.go
+++ b/server/space/repo/comments_test.go
@@ -4,7 +4,6 @@ import (
"context"
"database/sql"
"encoding/json"
- "fmt"
"net/http"
"testing"
"time"
@@ -175,198 +174,6 @@ func TestPostCommentCountWorkScalesWithReplyChainLength(t *testing.T) {
}
}
-func TestDiscussionBatchReadsWithDeepReplyChain(t *testing.T) {
- module := newSpaceTestModule(t)
- ctx := t.Context()
- owner, friend := notificationTestSpaces(t, module)
- rootAuthorID := insertSpaceUser(t, module, "[email protected]", "root-public")
- rootAuthor, err := testCreateSpace(ctx, module, rootAuthorID, "chain_root", "root", "public", "secret", "nonce", "profile")
- require.NoError(t, err)
- require.NoError(t, testAddFriend(ctx, module, rootAuthorID, rootAuthor.SpaceID, owner.SpaceID, "share", 1, "share", 1))
- 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, parent_id, reply_to_id, cipher, created_at)
- SELECT 'chain-' || i, $1, CASE WHEN i = 1 THEN $2 ELSE $3 END,
- CASE WHEN i > 1 THEN 'chain-1' END,
- CASE WHEN i > 1 THEN 'chain-' || (i - 1) END, '\x01'::bytea, i
- FROM generate_series(1, 1000) i`, postID, rootAuthor.SpaceID, owner.SpaceID)
- require.NoError(t, err)
- _, err = module.Comments.DB.ExecContext(ctx, `INSERT INTO space_comment_reactions (comment_id, actor_space_id, cipher)
- SELECT comment_id, $2, '\x01'::bytea FROM space_comments WHERE post_id = $1 AND created_at > 1`, postID, friend.SpaceID)
- 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', 'chain-' || i, i
- FROM generate_series(2, 1000) i`, owner.SpaceID, friend.SpaceID)
- require.NoError(t, err)
- _, err = module.Comments.DB.ExecContext(ctx, `ANALYZE space_comments; ANALYZE space_notifications`)
- require.NoError(t, err)
- notificationIDs := make([]string, 0, 999)
- for i := 2; i <= 1000; i++ {
- notificationIDs = append(notificationIDs, fmt.Sprintf("notification-%d", i))
- }
- for _, state := range []string{"visible", "deleted-comment", "inactive-author", "recovered"} {
- t.Run(state, func(t *testing.T) {
- _, err := module.Comments.DB.ExecContext(ctx, `UPDATE space_comments
- SET cipher = CASE WHEN $1 = 'deleted-comment' THEN NULL ELSE '\x01'::bytea END
- WHERE comment_id = 'chain-1'`, state)
- require.NoError(t, err)
- _, err = module.Comments.DB.ExecContext(ctx, `UPDATE users
- SET encrypted_email = CASE WHEN $2 = 'inactive-author' THEN NULL ELSE '\x00'::bytea END
- WHERE user_id = $1`, rootAuthorID, state)
- require.NoError(t, err)
- ctx, cancel := context.WithTimeout(t.Context(), 8*time.Second)
- defer cancel()
- hidden := state == "deleted-comment" || state == "inactive-author"
- started := time.Now()
- items, cursor, err := module.Comments.List(ctx, owner.SpaceID, postID, "900:chain-900")
- require.NoError(t, err)
- t.Logf("last 100 comments: %s", time.Since(started))
- require.Len(t, items, 100)
- require.Empty(t, cursor)
- for i, item := range items {
- require.Equal(t, fmt.Sprintf("chain-%d", i+901), item.ID)
- require.Equal(t, "chain-1", item.ParentID)
- require.Equal(t, fmt.Sprintf("chain-%d", i+900), item.ReplyToID)
- if hidden {
- require.Empty(t, item.Cipher)
- require.Empty(t, item.Reactions)
- require.Empty(t, item.UnreadNotificationIDs)
- require.False(t, item.CanDelete)
- } else {
- require.Equal(t, []byte{1}, item.Cipher)
- require.Len(t, item.Reactions, 1)
- require.Equal(t, []string{fmt.Sprintf("notification-%d", i+901)}, item.UnreadNotificationIDs)
- }
- }
- items, cursor, err = module.Comments.List(ctx, owner.SpaceID, postID, "")
- require.NoError(t, err)
- require.Len(t, items, 100)
- require.Equal(t, "100:chain-100", cursor)
- started = time.Now()
- notifications, cursor, err := module.Notifications.List(ctx, owner.SpaceID, "", 2)
- require.NoError(t, err)
- t.Logf("notifications: %s", time.Since(started))
- if hidden {
- require.Empty(t, notifications)
- require.Empty(t, cursor)
- } else {
- require.Len(t, notifications, 2)
- require.Equal(t, "chain-1000", notifications[0].CommentID)
- require.Equal(t, "chain-999", notifications[1].CommentID)
- require.Equal(t, "999:notification-999", cursor)
- }
- unread, err := module.Notifications.HasUnread(ctx, owner.SpaceID)
- require.NoError(t, err)
- require.Equal(t, !hidden, unread)
- require.NoError(t, module.Notifications.MarkRead(ctx, owner.SpaceID, notificationIDs))
- unread, err = module.Notifications.HasUnread(ctx, owner.SpaceID)
- require.NoError(t, err)
- require.False(t, unread)
- readCount := countSpaceRows(t, module, `SELECT count(*) FROM space_notifications WHERE read_at IS NOT NULL`)
- if hidden {
- require.Zero(t, readCount)
- } else {
- require.EqualValues(t, 999, readCount)
- }
- _, err = module.Notifications.DB.ExecContext(ctx, `UPDATE space_notifications SET read_at = NULL`)
- require.NoError(t, err)
- })
- }
-}
-
-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"