// SPDX-License-Identifier: AGPL-3.0-or-later package directory import ( "context" "encoding/json" "fmt" "strings" "docwell/server/internal/access" "docwell/server/internal/apperr" "docwell/server/internal/store/pocketbase" ) // ---- Inline comments ------------------------------------------------------- type Comment struct { ID string `json:"id"` Page string `json:"page"` Author MemberUser `json:"author"` Body string `json:"body"` Quote string `json:"quote"` // Anchor is the editor's encoding of the commented range; the server // stores it without looking inside. Anchor json.RawMessage `json:"anchor"` Created string `json:"created"` CanDelete bool `json:"canDelete"` } type commentRecord struct { ID string `json:"id"` Page string `json:"page"` Author string `json:"author"` Body string `json:"body"` Quote string `json:"quote"` Anchor json.RawMessage `json:"anchor"` Created string `json:"created"` Expand struct { Author *struct { ID string `json:"id"` Email string `json:"email"` Name string `json:"name"` } `json:"author"` } `json:"expand"` } func (r commentRecord) view(c access.Caller, role string) Comment { out := Comment{ ID: r.ID, Page: r.Page, Body: r.Body, Quote: r.Quote, Anchor: r.Anchor, Created: r.Created, CanDelete: access.DeleteComment(c.ID, r.Author, role) == nil, } if len(out.Anchor) == 0 { out.Anchor = json.RawMessage("null") } if a := r.Expand.Author; a != nil { out.Author = MemberUser{ID: a.ID, Email: a.Email, Name: a.Name} } return out } // ListComments lists a page's comments, oldest first (viewer). func (s *Service) ListComments(ctx context.Context, c access.Caller, pageID string) ([]Comment, error) { p, a, err := s.pageFor(ctx, c, pageID) if err != nil { return nil, err } recs, err := listAll[commentRecord](ctx, s, "comments", pocketbase.ListOptions{ Filter: fmt.Sprintf("page = '%s'", p.ID), Sort: "created", Expand: "author", }) if err != nil { return nil, err } out := make([]Comment, 0, len(recs)) for _, r := range recs { out = append(out, r.view(c, a.role)) } return out, nil } type CreateCommentInput struct { Body string `json:"body"` Quote string `json:"quote"` Anchor json.RawMessage `json:"anchor"` } // CreateComment adds a comment to a page (commenter; not in the trash). func (s *Service) CreateComment(ctx context.Context, c access.Caller, pageID string, in CreateCommentInput) (Comment, error) { p, a, err := s.pageFor(ctx, c, pageID) if err != nil { return Comment{}, err } if err := access.CommentPages(a.role); err != nil { return Comment{}, err } if p.DeletedAt != "" { return Comment{}, apperr.Invalid("restore the page from the trash first") } body := strings.TrimSpace(in.Body) switch { case body == "": return Comment{}, apperr.InvalidFields(map[string]string{"body": "Write something first."}) case len(body) > 5000: return Comment{}, apperr.InvalidFields(map[string]string{"body": "The comment is too long (max 5000)."}) case len(in.Anchor) > 4096: return Comment{}, apperr.InvalidFields(map[string]string{"anchor": "The anchor is too large."}) } quote := strings.TrimSpace(in.Quote) if r := []rune(quote); len(r) > 500 { quote = string(r[:500]) } fields := map[string]any{"page": p.ID, "workspace": p.Workspace, "author": c.ID, "body": body, "quote": quote} if len(in.Anchor) > 0 && json.Valid(in.Anchor) { fields["anchor"] = in.Anchor } rec, err := pocketbase.Create[commentRecord](ctx, s.pb, "comments", fields) if err != nil { return Comment{}, mapPB(err, "page not found") } rec, err = pocketbase.GetExpand[commentRecord](ctx, s.pb, "comments", rec.ID, "author") if err != nil { return Comment{}, mapPB(err, "comment not found") } return rec.view(c, a.role), nil } // DeleteComment removes a comment (its author, or a space admin). func (s *Service) DeleteComment(ctx context.Context, c access.Caller, id string) error { if !validID(id) { return apperr.NotFound("comment not found") } rec, err := pocketbase.Get[commentRecord](ctx, s.pb, "comments", id) if err != nil { return mapPB(err, "comment not found") } _, a, err := s.pageFor(ctx, c, rec.Page) if err != nil { return apperr.NotFound("comment not found") } if err := access.DeleteComment(c.ID, rec.Author, a.role); err != nil { return err } return mapPB(s.pb.Delete(ctx, "comments", rec.ID), "comment not found") }