mirror of
https://gh-proxy.org/https://github.com/klinsmaya/multica.git
synced 2026-11-09 01:46:46 +08:00
* feat: add issue event and time wakeups (MUL-7418) Co-authored-by: multica-agent <[email protected]> * feat(wakeups): capture issue collaboration event lifecycle Co-authored-by: multica-agent <[email protected]> * fix(wakeups): preserve reply source when reopening threads Co-authored-by: multica-agent <[email protected]> * feat(wakeups): summarize sidebar and issue activity cues Co-authored-by: multica-agent <[email protected]> * fix(wakeups): restore disabled configurations with a two-way toggle Co-authored-by: multica-agent <[email protected]> * feat(wakeups): manage workspace wakeups in Autopilot Co-authored-by: multica-agent <[email protected]> * chore(wakeups): reserve migrations after current main Co-authored-by: multica-agent <[email protected]> * test(wakeups): align inventory fixtures with shared contracts Co-authored-by: multica-agent <[email protected]> * fix(wakeups): distinguish trigger conditions and execution results Co-authored-by: multica-agent <[email protected]> * fix(wakeups): suppress registration feedback and drain bounded evidence Co-authored-by: multica-agent <[email protected]> * fix(wakeups): align sidebar actions with titles Co-authored-by: multica-agent <[email protected]> * fix(wakeups): bound database work and unify shared issue visibility Co-authored-by: multica-agent <[email protected]> * feat(wakeups): filter mutation events by member or agent actor Co-authored-by: multica-agent <[email protected]> * refactor(wakeups): merge structured evidence and narrow receipt locks Co-authored-by: multica-agent <[email protected]> * feat(wakeups): let people edit wakeup instructions Co-authored-by: multica-agent <[email protected]> * fix(wakeups): align inventory controls and compact prompt editing Co-authored-by: multica-agent <[email protected]> * fix(mobile): handle deferred agent run status Co-authored-by: multica-agent <[email protected]> --------- Co-authored-by: Lambda <[email protected]> Co-authored-by: multica-agent <[email protected]>
237 lines
6.8 KiB
Go
237 lines
6.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/jackc/pgx/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"`
|
|
CommentRevision *int64 `json:"comment_revision,omitempty"`
|
|
}
|
|
|
|
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 addedReactionToResponse(r db.AddReactionRow) ReactionResponse {
|
|
response := ReactionResponse{
|
|
ID: uuidToString(r.ID),
|
|
CommentID: uuidToString(r.CommentID),
|
|
ActorType: r.ActorType,
|
|
ActorID: uuidToString(r.ActorID),
|
|
Emoji: r.Emoji,
|
|
CreatedAt: timestampToString(r.CreatedAt),
|
|
}
|
|
if r.CommentRevision > 0 {
|
|
response.CommentRevision = &r.CommentRevision
|
|
}
|
|
return response
|
|
}
|
|
|
|
func (h *Handler) AddReaction(w http.ResponseWriter, r *http.Request) {
|
|
r = h.withWakeupActor(r)
|
|
commentId := chi.URLParam(r, "commentId")
|
|
|
|
userID, ok := requireUserID(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
workspaceID := h.resolveWorkspaceID(r)
|
|
commentUUID, ok := parseUUIDOrBadRequest(w, commentId, "comment id")
|
|
if !ok {
|
|
return
|
|
}
|
|
wsUUID, ok := parseUUIDOrBadRequest(w, workspaceID, "workspace id")
|
|
if !ok {
|
|
return
|
|
}
|
|
comment, err := h.Queries.GetCommentInWorkspace(r.Context(), db.GetCommentInWorkspaceParams{
|
|
ID: commentUUID,
|
|
WorkspaceID: wsUUID,
|
|
})
|
|
// A deleted comment's tombstone takes no reactions.
|
|
if err != nil || comment.DeletedAt.Valid {
|
|
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)
|
|
|
|
var reaction db.AddReactionRow
|
|
err = h.withLiveCommentLock(r.Context(), comment.ID, wsUUID, func(qtx *db.Queries) error {
|
|
var addErr error
|
|
reaction, addErr = qtx.AddReaction(r.Context(), db.AddReactionParams{
|
|
CommentID: comment.ID,
|
|
WorkspaceID: wsUUID,
|
|
ActorType: actorType,
|
|
ActorID: parseUUID(actorID),
|
|
Emoji: req.Emoji,
|
|
})
|
|
return addErr
|
|
})
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
// Deleted after the check above.
|
|
writeError(w, http.StatusNotFound, "comment not found")
|
|
return
|
|
}
|
|
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 := addedReactionToResponse(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
|
|
}
|
|
|
|
if reaction.CommentRevision > 0 {
|
|
h.publish(protocol.EventReactionAdded, workspaceID, actorType, actorID, map[string]any{
|
|
"reaction": resp,
|
|
"issue_id": issueID,
|
|
"issue_title": issueTitle,
|
|
"issue_status": issueStatus,
|
|
"comment_id": uuidToString(comment.ID),
|
|
"comment_author_type": comment.AuthorType,
|
|
"comment_author_id": uuidToString(comment.AuthorID),
|
|
"comment_revision": reaction.CommentRevision,
|
|
})
|
|
}
|
|
writeJSON(w, http.StatusCreated, resp)
|
|
}
|
|
|
|
func (h *Handler) RemoveReaction(w http.ResponseWriter, r *http.Request) {
|
|
r = h.withWakeupActor(r)
|
|
commentId := chi.URLParam(r, "commentId")
|
|
|
|
userID, ok := requireUserID(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
workspaceID := h.resolveWorkspaceID(r)
|
|
commentUUID, ok := parseUUIDOrBadRequest(w, commentId, "comment id")
|
|
if !ok {
|
|
return
|
|
}
|
|
wsUUID, ok := parseUUIDOrBadRequest(w, workspaceID, "workspace id")
|
|
if !ok {
|
|
return
|
|
}
|
|
comment, err := h.Queries.GetCommentInWorkspace(r.Context(), db.GetCommentInWorkspaceParams{
|
|
ID: commentUUID,
|
|
WorkspaceID: wsUUID,
|
|
})
|
|
// A deleted comment's tombstone has no reactions left to remove.
|
|
if err != nil || comment.DeletedAt.Valid {
|
|
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)
|
|
|
|
// Owner first, like every comment mutation: removing the reaction row and
|
|
// then bumping the comment would invert the delete transaction's order.
|
|
var removed db.RemoveReactionRow
|
|
err = h.withLiveCommentLock(r.Context(), comment.ID, wsUUID, func(qtx *db.Queries) error {
|
|
var removeErr error
|
|
removed, removeErr = qtx.RemoveReaction(r.Context(), db.RemoveReactionParams{
|
|
CommentID: comment.ID,
|
|
ActorType: actorType,
|
|
ActorID: parseUUID(actorID),
|
|
Emoji: req.Emoji,
|
|
})
|
|
return removeErr
|
|
})
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
writeError(w, http.StatusNotFound, "comment not found")
|
|
return
|
|
}
|
|
if 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
|
|
}
|
|
|
|
if removed.Changed {
|
|
h.publish(protocol.EventReactionRemoved, workspaceID, actorType, actorID, map[string]any{
|
|
"comment_id": uuidToString(comment.ID),
|
|
"issue_id": uuidToString(comment.IssueID),
|
|
"emoji": req.Emoji,
|
|
"actor_type": actorType,
|
|
"actor_id": actorID,
|
|
"comment_revision": removed.CommentRevision,
|
|
})
|
|
}
|
|
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
|
|
}
|