// SPDX-License-Identifier: AGPL-3.0-or-later package httpapi import ( "context" "net/http" "strconv" "docwell/server/internal/access" "docwell/server/internal/directory" ) // CommentService is the part of directory.Service for comments, page // watching and notifications. type CommentService interface { ListComments(ctx context.Context, c access.Caller, pageID string) ([]directory.Comment, error) CreateComment(ctx context.Context, c access.Caller, pageID string, in directory.CreateCommentInput) (directory.Comment, error) UpdateComment(ctx context.Context, c access.Caller, id, body string) (directory.Comment, error) ResolveComment(ctx context.Context, c access.Caller, id string, resolved bool) (directory.Comment, error) ReactToComment(ctx context.Context, c access.Caller, id, emoji string, on bool) (directory.Comment, error) DeleteComment(ctx context.Context, c access.Caller, id string) error GetWatch(ctx context.Context, c access.Caller, pageID string) (directory.WatchState, error) SetWatch(ctx context.Context, c access.Caller, pageID string, on bool) (directory.WatchState, error) ListNotifications(ctx context.Context, c access.Caller, unreadOnly bool, limit int) (directory.NotificationList, error) MarkNotificationsRead(ctx context.Context, c access.Caller, ids []string, all bool) (int, error) GetNotificationSettings(ctx context.Context, c access.Caller) (directory.NotificationSettings, error) SetNotificationSettings(ctx context.Context, c access.Caller, in directory.NotificationSettings) (directory.NotificationSettings, error) } func (a *api) commentRoutes(mux *routeMux) { mux.HandleFunc("GET /api/v1/pages/{id}/comments", a.requireUser(func(w http.ResponseWriter, r *http.Request) { list, err := a.dir.ListComments(r.Context(), caller(r), r.PathValue("id")) a.respond(w, list, err) })) mux.HandleFunc("POST /api/v1/pages/{id}/comments", a.requireUser(func(w http.ResponseWriter, r *http.Request) { var in directory.CreateCommentInput if !a.decode(w, r, &in) { return } cm, err := a.dir.CreateComment(r.Context(), caller(r), r.PathValue("id"), in) a.respondStatus(w, http.StatusCreated, cm, err) })) mux.HandleFunc("PATCH /api/v1/comments/{id}", a.requireUser(func(w http.ResponseWriter, r *http.Request) { var in struct{ Body string } if !a.decode(w, r, &in) { return } cm, err := a.dir.UpdateComment(r.Context(), caller(r), r.PathValue("id"), in.Body) a.respond(w, cm, err) })) mux.HandleFunc("DELETE /api/v1/comments/{id}", a.requireUser(func(w http.ResponseWriter, r *http.Request) { a.respondNoContent(w, a.dir.DeleteComment(r.Context(), caller(r), r.PathValue("id"))) })) mux.HandleFunc("POST /api/v1/comments/{id}/resolve", a.requireUser(func(w http.ResponseWriter, r *http.Request) { var in struct{ Resolved bool } if !a.decode(w, r, &in) { return } cm, err := a.dir.ResolveComment(r.Context(), caller(r), r.PathValue("id"), in.Resolved) a.respond(w, cm, err) })) mux.HandleFunc("POST /api/v1/comments/{id}/reactions", a.requireUser(func(w http.ResponseWriter, r *http.Request) { var in struct { Emoji string On bool } if !a.decode(w, r, &in) { return } cm, err := a.dir.ReactToComment(r.Context(), caller(r), r.PathValue("id"), in.Emoji, in.On) a.respond(w, cm, err) })) mux.HandleFunc("GET /api/v1/pages/{id}/watch", a.requireUser(func(w http.ResponseWriter, r *http.Request) { st, err := a.dir.GetWatch(r.Context(), caller(r), r.PathValue("id")) a.respond(w, st, err) })) mux.HandleFunc("PUT /api/v1/pages/{id}/watch", a.requireUser(func(w http.ResponseWriter, r *http.Request) { var in struct{ Watching bool } if !a.decode(w, r, &in) { return } st, err := a.dir.SetWatch(r.Context(), caller(r), r.PathValue("id"), in.Watching) a.respond(w, st, err) })) mux.HandleFunc("GET /api/v1/notifications", a.requireUser(func(w http.ResponseWriter, r *http.Request) { q := r.URL.Query() limit, _ := strconv.Atoi(q.Get("limit")) list, err := a.dir.ListNotifications(r.Context(), caller(r), q.Get("unread") == "true", limit) a.respond(w, list, err) })) mux.HandleFunc("POST /api/v1/notifications/read", a.requireUser(func(w http.ResponseWriter, r *http.Request) { var in struct { IDs []string All bool } if !a.decode(w, r, &in) { return } n, err := a.dir.MarkNotificationsRead(r.Context(), caller(r), in.IDs, in.All) a.respond(w, map[string]int{"unread": n}, err) })) mux.HandleFunc("GET /api/v1/notifications/settings", a.requireUser(func(w http.ResponseWriter, r *http.Request) { st, err := a.dir.GetNotificationSettings(r.Context(), caller(r)) a.respond(w, st, err) })) mux.HandleFunc("PUT /api/v1/notifications/settings", a.requireUser(func(w http.ResponseWriter, r *http.Request) { var in directory.NotificationSettings if !a.decode(w, r, &in) { return } st, err := a.dir.SetNotificationSettings(r.Context(), caller(r), in) a.respond(w, st, err) })) }