Files
Bohan Jiang a9e82c7973 MUL-7282: fix(comments): delete only the selected comment and keep its replies (#8323)
Deleting a comment removed every reply under it, through the parent_id ON DELETE CASCADE. Now delete removes only that comment: one with replies becomes a tombstone (deleted_at set, body/attachments/reactions/resolution cleared) so the replies keep their parent, and one without replies is removed along with any tombstone ancestor left empty.

Tombstones are read-only and are never agent input. Every write to a comment's children takes its owners first (issue -> comment -> child), and a comment commits together with the attachments it was posted with. Clients only promise that replies are kept when the server declares comment_delete_keep_replies_supported, and then delete through DELETE /api/comments/{id}/keep-replies, which older servers do not route.
2026-09-13 19:13:43 +08:00

168 lines
4.8 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: reaction.sql
package db
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const addReaction = `-- name: AddReaction :one
WITH inserted AS (
INSERT INTO comment_reaction (comment_id, workspace_id, actor_type, actor_id, emoji)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (comment_id, actor_type, actor_id, emoji) DO NOTHING
RETURNING id, comment_id, workspace_id, actor_type, actor_id, emoji, created_at
), bumped AS (
UPDATE comment
SET revision = revision + 1
WHERE id IN (SELECT comment_id FROM inserted)
RETURNING revision
)
SELECT reaction.id, reaction.comment_id, reaction.workspace_id, reaction.actor_type, reaction.actor_id, reaction.emoji, reaction.created_at, COALESCE((SELECT revision FROM bumped), 0)::bigint AS comment_revision
FROM inserted reaction
UNION ALL
SELECT reaction.id, reaction.comment_id, reaction.workspace_id, reaction.actor_type, reaction.actor_id, reaction.emoji, reaction.created_at, 0::bigint AS comment_revision
FROM comment_reaction reaction
WHERE reaction.comment_id = $1
AND reaction.actor_type = $3
AND reaction.actor_id = $4
AND reaction.emoji = $5
AND NOT EXISTS (SELECT 1 FROM inserted)
LIMIT 1
`
type AddReactionParams struct {
CommentID pgtype.UUID `json:"comment_id"`
WorkspaceID pgtype.UUID `json:"workspace_id"`
ActorType string `json:"actor_type"`
ActorID pgtype.UUID `json:"actor_id"`
Emoji string `json:"emoji"`
}
type AddReactionRow struct {
ID pgtype.UUID `json:"id"`
CommentID pgtype.UUID `json:"comment_id"`
WorkspaceID pgtype.UUID `json:"workspace_id"`
ActorType string `json:"actor_type"`
ActorID pgtype.UUID `json:"actor_id"`
Emoji string `json:"emoji"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
CommentRevision int64 `json:"comment_revision"`
}
func (q *Queries) AddReaction(ctx context.Context, arg AddReactionParams) (AddReactionRow, error) {
row := q.db.QueryRow(ctx, addReaction,
arg.CommentID,
arg.WorkspaceID,
arg.ActorType,
arg.ActorID,
arg.Emoji,
)
var i AddReactionRow
err := row.Scan(
&i.ID,
&i.CommentID,
&i.WorkspaceID,
&i.ActorType,
&i.ActorID,
&i.Emoji,
&i.CreatedAt,
&i.CommentRevision,
)
return i, err
}
const deleteCommentReactions = `-- name: DeleteCommentReactions :exec
DELETE FROM comment_reaction
WHERE comment_id = $1 AND workspace_id = $2
`
type DeleteCommentReactionsParams struct {
CommentID pgtype.UUID `json:"comment_id"`
WorkspaceID pgtype.UUID `json:"workspace_id"`
}
// Part of the comment delete transaction: reactions go with the comment.
func (q *Queries) DeleteCommentReactions(ctx context.Context, arg DeleteCommentReactionsParams) error {
_, err := q.db.Exec(ctx, deleteCommentReactions, arg.CommentID, arg.WorkspaceID)
return err
}
const listReactionsByCommentIDs = `-- name: ListReactionsByCommentIDs :many
SELECT id, comment_id, workspace_id, actor_type, actor_id, emoji, created_at FROM comment_reaction
WHERE comment_id = ANY($1::uuid[])
ORDER BY created_at ASC
`
func (q *Queries) ListReactionsByCommentIDs(ctx context.Context, dollar_1 []pgtype.UUID) ([]CommentReaction, error) {
rows, err := q.db.Query(ctx, listReactionsByCommentIDs, dollar_1)
if err != nil {
return nil, err
}
defer rows.Close()
items := []CommentReaction{}
for rows.Next() {
var i CommentReaction
if err := rows.Scan(
&i.ID,
&i.CommentID,
&i.WorkspaceID,
&i.ActorType,
&i.ActorID,
&i.Emoji,
&i.CreatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const removeReaction = `-- name: RemoveReaction :one
WITH deleted AS (
DELETE FROM comment_reaction
WHERE comment_id = $1 AND actor_type = $2 AND actor_id = $3 AND emoji = $4
RETURNING comment_id
), bumped AS (
UPDATE comment
SET revision = revision + 1
WHERE id IN (SELECT comment_id FROM deleted)
RETURNING revision
)
SELECT EXISTS(SELECT 1 FROM deleted) AS changed,
COALESCE((SELECT revision FROM bumped), 0)::bigint AS comment_revision
`
type RemoveReactionParams struct {
CommentID pgtype.UUID `json:"comment_id"`
ActorType string `json:"actor_type"`
ActorID pgtype.UUID `json:"actor_id"`
Emoji string `json:"emoji"`
}
type RemoveReactionRow struct {
Changed bool `json:"changed"`
CommentRevision int64 `json:"comment_revision"`
}
func (q *Queries) RemoveReaction(ctx context.Context, arg RemoveReactionParams) (RemoveReactionRow, error) {
row := q.db.QueryRow(ctx, removeReaction,
arg.CommentID,
arg.ActorType,
arg.ActorID,
arg.Emoji,
)
var i RemoveReactionRow
err := row.Scan(&i.Changed, &i.CommentRevision)
return i, err
}