Editor: - "/" opens a filterable block menu (text, headings, lists, quote, code, divider, and image or file upload when the host can store files). - Selecting text shows a formatting toolbar (bold, italic, strike, code, link) and, for anyone who may comment, a Comment button. - "@" finds people in the page's workspace; the mention node is always in the schema so documents with mentions open in every client. - The popups are plain DOM in the design system's slash-menu style, so the framework-free mobile bridge keeps building and can use them. - Menu words come from the host; the web app passes English or Polish. Inline comments (basic): commenters cannot edit the document, so a comment's anchor is a pair of Yjs relative positions stored with the comment rather than a mark in the text. Highlights follow local edits by mapping and are re-resolved when comments change or remote edits arrive. Pages get a comments panel (side column, or a bottom sheet on narrow screens). Authors and space admins can delete comments. Server: comments collection and endpoints, and a people lookup any workspace member may use for mentions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
145 lines
4.4 KiB
Go
145 lines
4.4 KiB
Go
// 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")
|
|
}
|