feat(reactions): add emoji reactions for comments and issue descriptions

Add Slack-style emoji reactions to comments and issue descriptions with
full-stack support: database tables, REST API endpoints, real-time
WebSocket sync, optimistic UI updates, and inbox notifications.

- New `comment_reaction` and `issue_reaction` tables with migrations
- POST/DELETE endpoints for adding/removing reactions on both comments
  and issue descriptions
- Real-time WS events (reaction:added/removed, issue_reaction:added/removed)
- Shared ReactionBar component with quick emoji picker and full emoji-mart
  picker (lazy-loaded)
- Optimistic add/remove with rollback on failure
- Inbox notifications for comment author and issue creator when reacted to
- Reactions included in timeline, comment list, and issue detail responses
This commit is contained in:
Jiayuan 2026-03-30 22:37:59 +08:00
parent 72e3ccfe33
commit 7c1aabbe3a
32 changed files with 1221 additions and 49 deletions

View file

@ -6,6 +6,7 @@ import (
"sort"
"github.com/go-chi/chi/v5"
"github.com/jackc/pgx/v5/pgtype"
db "github.com/multica-ai/multica/server/pkg/db/generated"
)
@ -24,10 +25,11 @@ type TimelineEntry struct {
Details json.RawMessage `json:"details,omitempty"`
// Comment-only fields
Content *string `json:"content,omitempty"`
ParentID *string `json:"parent_id,omitempty"`
UpdatedAt *string `json:"updated_at,omitempty"`
CommentType *string `json:"comment_type,omitempty"`
Content *string `json:"content,omitempty"`
ParentID *string `json:"parent_id,omitempty"`
UpdatedAt *string `json:"updated_at,omitempty"`
CommentType *string `json:"comment_type,omitempty"`
Reactions []ReactionResponse `json:"reactions,omitempty"`
}
// ListTimeline returns a merged, chronologically-sorted timeline of activities
@ -77,6 +79,13 @@ func (h *Handler) ListTimeline(w http.ResponseWriter, r *http.Request) {
})
}
// Fetch reactions for all comments in one batch.
commentIDs := make([]pgtype.UUID, len(comments))
for i, c := range comments {
commentIDs[i] = c.ID
}
grouped := h.groupReactions(r, commentIDs)
for _, c := range comments {
content := c.Content
commentType := c.Type
@ -91,6 +100,7 @@ func (h *Handler) ListTimeline(w http.ResponseWriter, r *http.Request) {
ParentID: uuidToPtr(c.ParentID),
CreatedAt: timestampToString(c.CreatedAt),
UpdatedAt: &updatedAt,
Reactions: grouped[uuidToString(c.ID)],
})
}

View file

@ -13,18 +13,22 @@ import (
)
type CommentResponse struct {
ID string `json:"id"`
IssueID string `json:"issue_id"`
AuthorType string `json:"author_type"`
AuthorID string `json:"author_id"`
Content string `json:"content"`
Type string `json:"type"`
ParentID *string `json:"parent_id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
ID string `json:"id"`
IssueID string `json:"issue_id"`
AuthorType string `json:"author_type"`
AuthorID string `json:"author_id"`
Content string `json:"content"`
Type string `json:"type"`
ParentID *string `json:"parent_id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Reactions []ReactionResponse `json:"reactions"`
}
func commentToResponse(c db.Comment) CommentResponse {
func commentToResponse(c db.Comment, reactions []ReactionResponse) CommentResponse {
if reactions == nil {
reactions = []ReactionResponse{}
}
return CommentResponse{
ID: uuidToString(c.ID),
IssueID: uuidToString(c.IssueID),
@ -35,6 +39,7 @@ func commentToResponse(c db.Comment) CommentResponse {
ParentID: uuidToPtr(c.ParentID),
CreatedAt: timestampToString(c.CreatedAt),
UpdatedAt: timestampToString(c.UpdatedAt),
Reactions: reactions,
}
}
@ -54,9 +59,15 @@ func (h *Handler) ListComments(w http.ResponseWriter, r *http.Request) {
return
}
commentIDs := make([]pgtype.UUID, len(comments))
for i, c := range comments {
commentIDs[i] = c.ID
}
grouped := h.groupReactions(r, commentIDs)
resp := make([]CommentResponse, len(comments))
for i, c := range comments {
resp[i] = commentToResponse(c)
resp[i] = commentToResponse(c, grouped[uuidToString(c.ID)])
}
writeJSON(w, http.StatusOK, resp)
@ -122,7 +133,7 @@ func (h *Handler) CreateComment(w http.ResponseWriter, r *http.Request) {
return
}
resp := commentToResponse(comment)
resp := commentToResponse(comment, nil)
slog.Info("comment created", append(logger.RequestAttrs(r), "comment_id", uuidToString(comment.ID), "issue_id", issueID)...)
h.publish(protocol.EventCommentCreated, uuidToString(issue.WorkspaceID), authorType, authorID, map[string]any{
"comment": resp,
@ -197,7 +208,9 @@ func (h *Handler) UpdateComment(w http.ResponseWriter, r *http.Request) {
return
}
resp := commentToResponse(comment)
// Fetch reactions for the updated comment.
grouped := h.groupReactions(r, []pgtype.UUID{comment.ID})
resp := commentToResponse(comment, grouped[uuidToString(comment.ID)])
slog.Info("comment updated", append(logger.RequestAttrs(r), "comment_id", commentId)...)
h.publish(protocol.EventCommentUpdated, workspaceID, actorType, actorID, map[string]any{"comment": resp})
writeJSON(w, http.StatusOK, resp)

View file

@ -18,23 +18,24 @@ import (
// IssueResponse is the JSON response for an issue.
type IssueResponse struct {
ID string `json:"id"`
WorkspaceID string `json:"workspace_id"`
Number int32 `json:"number"`
Identifier string `json:"identifier"`
Title string `json:"title"`
Description *string `json:"description"`
Status string `json:"status"`
Priority string `json:"priority"`
AssigneeType *string `json:"assignee_type"`
AssigneeID *string `json:"assignee_id"`
CreatorType string `json:"creator_type"`
CreatorID string `json:"creator_id"`
ParentIssueID *string `json:"parent_issue_id"`
Position float64 `json:"position"`
DueDate *string `json:"due_date"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
ID string `json:"id"`
WorkspaceID string `json:"workspace_id"`
Number int32 `json:"number"`
Identifier string `json:"identifier"`
Title string `json:"title"`
Description *string `json:"description"`
Status string `json:"status"`
Priority string `json:"priority"`
AssigneeType *string `json:"assignee_type"`
AssigneeID *string `json:"assignee_id"`
CreatorType string `json:"creator_type"`
CreatorID string `json:"creator_id"`
ParentIssueID *string `json:"parent_issue_id"`
Position float64 `json:"position"`
DueDate *string `json:"due_date"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Reactions []IssueReactionResponse `json:"reactions,omitempty"`
}
type agentTriggerSnapshot struct {
@ -130,7 +131,18 @@ func (h *Handler) GetIssue(w http.ResponseWriter, r *http.Request) {
return
}
prefix := h.getIssuePrefix(r.Context(), issue.WorkspaceID)
writeJSON(w, http.StatusOK, issueToResponse(issue, prefix))
resp := issueToResponse(issue, prefix)
// Fetch issue reactions.
reactions, err := h.Queries.ListIssueReactions(r.Context(), issue.ID)
if err == nil && len(reactions) > 0 {
resp.Reactions = make([]IssueReactionResponse, len(reactions))
for i, rx := range reactions {
resp.Reactions[i] = issueReactionToResponse(rx)
}
}
writeJSON(w, http.StatusOK, resp)
}
type CreateIssueRequest struct {

View file

@ -0,0 +1,131 @@
package handler
import (
"encoding/json"
"log/slog"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/multica-ai/multica/server/internal/logger"
db "github.com/multica-ai/multica/server/pkg/db/generated"
"github.com/multica-ai/multica/server/pkg/protocol"
)
type IssueReactionResponse struct {
ID string `json:"id"`
IssueID string `json:"issue_id"`
ActorType string `json:"actor_type"`
ActorID string `json:"actor_id"`
Emoji string `json:"emoji"`
CreatedAt string `json:"created_at"`
}
func issueReactionToResponse(r db.IssueReaction) IssueReactionResponse {
return IssueReactionResponse{
ID: uuidToString(r.ID),
IssueID: uuidToString(r.IssueID),
ActorType: r.ActorType,
ActorID: uuidToString(r.ActorID),
Emoji: r.Emoji,
CreatedAt: timestampToString(r.CreatedAt),
}
}
func (h *Handler) AddIssueReaction(w http.ResponseWriter, r *http.Request) {
issueID := chi.URLParam(r, "id")
issue, ok := h.loadIssueForUser(w, r, issueID)
if !ok {
return
}
userID, ok := requireUserID(w, r)
if !ok {
return
}
var req struct {
Emoji string `json:"emoji"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "invalid request body")
return
}
if req.Emoji == "" {
writeError(w, http.StatusBadRequest, "emoji is required")
return
}
workspaceID := uuidToString(issue.WorkspaceID)
actorType, actorID := h.resolveActor(r, userID, workspaceID)
reaction, err := h.Queries.AddIssueReaction(r.Context(), db.AddIssueReactionParams{
IssueID: issue.ID,
WorkspaceID: issue.WorkspaceID,
ActorType: actorType,
ActorID: parseUUID(actorID),
Emoji: req.Emoji,
})
if err != nil {
slog.Warn("add issue reaction failed", append(logger.RequestAttrs(r), "error", err, "issue_id", issueID)...)
writeError(w, http.StatusInternalServerError, "failed to add reaction")
return
}
resp := issueReactionToResponse(reaction)
h.publish(protocol.EventIssueReactionAdded, workspaceID, actorType, actorID, map[string]any{
"reaction": resp,
"issue_id": uuidToString(issue.ID),
"issue_title": issue.Title,
"issue_status": issue.Status,
"creator_type": issue.CreatorType,
"creator_id": uuidToString(issue.CreatorID),
})
writeJSON(w, http.StatusCreated, resp)
}
func (h *Handler) RemoveIssueReaction(w http.ResponseWriter, r *http.Request) {
issueID := chi.URLParam(r, "id")
issue, ok := h.loadIssueForUser(w, r, issueID)
if !ok {
return
}
userID, ok := requireUserID(w, r)
if !ok {
return
}
var req struct {
Emoji string `json:"emoji"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "invalid request body")
return
}
if req.Emoji == "" {
writeError(w, http.StatusBadRequest, "emoji is required")
return
}
workspaceID := uuidToString(issue.WorkspaceID)
actorType, actorID := h.resolveActor(r, userID, workspaceID)
if err := h.Queries.RemoveIssueReaction(r.Context(), db.RemoveIssueReactionParams{
IssueID: issue.ID,
ActorType: actorType,
ActorID: parseUUID(actorID),
Emoji: req.Emoji,
}); err != nil {
slog.Warn("remove issue reaction failed", append(logger.RequestAttrs(r), "error", err, "issue_id", issueID)...)
writeError(w, http.StatusInternalServerError, "failed to remove reaction")
return
}
h.publish(protocol.EventIssueReactionRemoved, workspaceID, actorType, actorID, map[string]any{
"issue_id": uuidToString(issue.ID),
"emoji": req.Emoji,
"actor_type": actorType,
"actor_id": actorID,
})
w.WriteHeader(http.StatusNoContent)
}

View file

@ -0,0 +1,169 @@
package handler
import (
"encoding/json"
"log/slog"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/multica-ai/multica/server/internal/logger"
db "github.com/multica-ai/multica/server/pkg/db/generated"
"github.com/multica-ai/multica/server/pkg/protocol"
)
type ReactionResponse struct {
ID string `json:"id"`
CommentID string `json:"comment_id"`
ActorType string `json:"actor_type"`
ActorID string `json:"actor_id"`
Emoji string `json:"emoji"`
CreatedAt string `json:"created_at"`
}
func reactionToResponse(r db.CommentReaction) ReactionResponse {
return ReactionResponse{
ID: uuidToString(r.ID),
CommentID: uuidToString(r.CommentID),
ActorType: r.ActorType,
ActorID: uuidToString(r.ActorID),
Emoji: r.Emoji,
CreatedAt: timestampToString(r.CreatedAt),
}
}
func (h *Handler) AddReaction(w http.ResponseWriter, r *http.Request) {
commentId := chi.URLParam(r, "commentId")
userID, ok := requireUserID(w, r)
if !ok {
return
}
workspaceID := resolveWorkspaceID(r)
comment, err := h.Queries.GetCommentInWorkspace(r.Context(), db.GetCommentInWorkspaceParams{
ID: parseUUID(commentId),
WorkspaceID: parseUUID(workspaceID),
})
if err != nil {
writeError(w, http.StatusNotFound, "comment not found")
return
}
var req struct {
Emoji string `json:"emoji"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "invalid request body")
return
}
if req.Emoji == "" {
writeError(w, http.StatusBadRequest, "emoji is required")
return
}
actorType, actorID := h.resolveActor(r, userID, workspaceID)
reaction, err := h.Queries.AddReaction(r.Context(), db.AddReactionParams{
CommentID: comment.ID,
WorkspaceID: parseUUID(workspaceID),
ActorType: actorType,
ActorID: parseUUID(actorID),
Emoji: req.Emoji,
})
if err != nil {
slog.Warn("add reaction failed", append(logger.RequestAttrs(r), "error", err, "comment_id", commentId)...)
writeError(w, http.StatusInternalServerError, "failed to add reaction")
return
}
resp := reactionToResponse(reaction)
// Look up issue title for inbox notifications.
issueID := uuidToString(comment.IssueID)
var issueTitle, issueStatus string
if issue, err := h.Queries.GetIssue(r.Context(), comment.IssueID); err == nil {
issueTitle = issue.Title
issueStatus = issue.Status
}
h.publish(protocol.EventReactionAdded, workspaceID, actorType, actorID, map[string]any{
"reaction": resp,
"issue_id": issueID,
"issue_title": issueTitle,
"issue_status": issueStatus,
"comment_author_type": comment.AuthorType,
"comment_author_id": uuidToString(comment.AuthorID),
})
writeJSON(w, http.StatusCreated, resp)
}
func (h *Handler) RemoveReaction(w http.ResponseWriter, r *http.Request) {
commentId := chi.URLParam(r, "commentId")
userID, ok := requireUserID(w, r)
if !ok {
return
}
workspaceID := resolveWorkspaceID(r)
comment, err := h.Queries.GetCommentInWorkspace(r.Context(), db.GetCommentInWorkspaceParams{
ID: parseUUID(commentId),
WorkspaceID: parseUUID(workspaceID),
})
if err != nil {
writeError(w, http.StatusNotFound, "comment not found")
return
}
var req struct {
Emoji string `json:"emoji"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "invalid request body")
return
}
if req.Emoji == "" {
writeError(w, http.StatusBadRequest, "emoji is required")
return
}
actorType, actorID := h.resolveActor(r, userID, workspaceID)
if err := h.Queries.RemoveReaction(r.Context(), db.RemoveReactionParams{
CommentID: comment.ID,
ActorType: actorType,
ActorID: parseUUID(actorID),
Emoji: req.Emoji,
}); err != nil {
slog.Warn("remove reaction failed", append(logger.RequestAttrs(r), "error", err, "comment_id", commentId)...)
writeError(w, http.StatusInternalServerError, "failed to remove reaction")
return
}
h.publish(protocol.EventReactionRemoved, workspaceID, actorType, actorID, map[string]any{
"comment_id": commentId,
"issue_id": uuidToString(comment.IssueID),
"emoji": req.Emoji,
"actor_type": actorType,
"actor_id": actorID,
})
w.WriteHeader(http.StatusNoContent)
}
// groupReactions fetches reactions for the given comment IDs and groups them by comment_id.
func (h *Handler) groupReactions(r *http.Request, commentIDs []pgtype.UUID) map[string][]ReactionResponse {
if len(commentIDs) == 0 {
return nil
}
reactions, err := h.Queries.ListReactionsByCommentIDs(r.Context(), commentIDs)
if err != nil {
return nil
}
grouped := make(map[string][]ReactionResponse, len(commentIDs))
for _, rx := range reactions {
cid := uuidToString(rx.CommentID)
grouped[cid] = append(grouped[cid], reactionToResponse(rx))
}
return grouped
}