Files
Docwell/Server/internal/directory/verify.go
T
tajniak81andClaude Opus 5.5 c98b127f1e
CI / go (Media) (push) Successful in 13m40s
CI / go (Server) (push) Failing after 23m20s
CI / go (Web-App) (push) Canceled after 0s
CI / frontend (push) Failing after 10m35s
Docs / build (push) Canceled after 0s
Docs / deploy (push) Canceled after 0s
CI / desktop (ubuntu-latest) (push) Failing after 3h4m42s
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
CI / mobile (push) Failing after 9m19s
CI / docker (push) Successful in 2h2m57s
Add columns, suggesting mode, verified pages, scanned PDF search and server translations
- Columns: /columns puts 2 to 4 columns side by side, with widths dragged
  between them; they stack on phones. HTML export and import keep them
  (also Notion column lists and Confluence page layouts).
- Suggesting mode: edits become suggested insertions and deletions that
  editors accept or reject one by one or all at once. Text only.
- Verified pages: editors verify a page for a while or without end; the
  verifier is notified when it runs out (migration 1764700000).
- Scanned PDFs attached to pages are read by AI for search where a
  workspace turns it on.
- The server writes error messages, emails, digests and phone
  notifications in English, Polish, German, French and Spanish; the apps
  send their language with each request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 08:38:08 +01:00

132 lines
4.4 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package directory
import (
"context"
"fmt"
"time"
"docwell/server/internal/access"
"docwell/server/internal/apperr"
"docwell/server/internal/store/pocketbase"
)
// ---- Verified pages --------------------------------------------------------
//
// An editor verifies a page to say it is correct, for some days or without
// end. Everyone sees who verified it and until when. When a verification
// runs out, the person who made it gets a "verify_expired" notification
// (once; verifying again starts over). Editing the page keeps it verified.
// NotifyVerifyExpired is the notification type of a verification that ran out.
const NotifyVerifyExpired = "verify_expired"
// maxVerifyDays is the longest a verification can last (about ten years).
const maxVerifyDays = 3660
// PageVerification is who verified a page and until when; At is empty when
// nobody has, Until when it does not run out.
type PageVerification struct {
By PageUser `json:"by"`
At string `json:"at"`
Until string `json:"until"`
Expired bool `json:"expired"`
}
// verification is a page's verification as of now.
func (s *Service) verification(ctx context.Context, p pageRecord, now time.Time) PageVerification {
if p.VerifiedAt == "" {
return PageVerification{}
}
v := PageVerification{By: s.pageUser(ctx, p.VerifiedBy), At: p.VerifiedAt, Until: p.VerifiedUntil}
v.Expired = verificationExpired(p.VerifiedUntil, now)
return v
}
// verificationExpired reports whether a verification lasting until (a
// PocketBase time, empty for no end) has run out at now.
func verificationExpired(until string, now time.Time) bool {
if until == "" {
return false
}
t, err := parsePBTime(until)
return err == nil && !t.After(now)
}
// VerifyPage verifies a page as the caller for days days, or without end
// for 0 (editor). It replaces an earlier verification.
func (s *Service) VerifyPage(ctx context.Context, c access.Caller, id string, days int) (Page, error) {
if days < 0 || days > maxVerifyDays {
return Page{}, apperr.Invalid(fmt.Sprintf("days must be between 0 and %d", maxVerifyDays))
}
p, a, err := s.pageFor(ctx, c, id)
if err != nil {
return Page{}, err
}
if err := access.EditPages(a.role); err != nil {
return Page{}, err
}
if p.DeletedAt != "" || p.ArchivedAt != "" {
return Page{}, apperr.Invalid("trashed and archived pages cannot be verified")
}
now := time.Now()
until := ""
if days > 0 {
until = pbTime(now.AddDate(0, 0, days))
}
body := map[string]any{
"verified_by": c.ID, "verified_at": pbTime(now), "verified_until": until, "verify_notified_at": "",
}
if _, err := pocketbase.Update[pageRecord](ctx, s.pb, "pages", p.ID, body); err != nil {
return Page{}, mapPB(err, "page not found")
}
return s.GetPage(ctx, c, p.ID)
}
// UnverifyPage takes a page's verification off (editor).
func (s *Service) UnverifyPage(ctx context.Context, c access.Caller, id string) (Page, error) {
p, a, err := s.pageFor(ctx, c, id)
if err != nil {
return Page{}, err
}
if err := access.EditPages(a.role); err != nil {
return Page{}, err
}
if p.VerifiedAt != "" {
body := map[string]any{"verified_by": "", "verified_at": "", "verified_until": "", "verify_notified_at": ""}
if _, err := pocketbase.Update[pageRecord](ctx, s.pb, "pages", p.ID, body); err != nil {
return Page{}, mapPB(err, "page not found")
}
}
return s.GetPage(ctx, c, p.ID)
}
// SendVerificationReminders tells each person whose verification of a page
// ran out, once. Each page is marked first, so a failure loses a reminder
// rather than sending it twice. Trashed pages are skipped (they are marked
// too, so restoring one does not remind about the past). It is a background
// job.
func (s *Service) SendVerificationReminders(ctx context.Context) error {
rows, err := listAll[pageRecord](ctx, s, "pages", pocketbase.ListOptions{
Filter: fmt.Sprintf("verified_at != '' && verified_until != '' && verify_notified_at = '' && verified_until <= '%s'", pbTime(time.Now())),
Sort: "verified_until",
Fields: pageFields,
})
if err != nil {
return err
}
for _, p := range rows {
if _, err := pocketbase.Update[struct{}](ctx, s.pb, "pages", p.ID, map[string]any{"verify_notified_at": pbTime(time.Now())}); err != nil {
return err
}
if p.DeletedAt != "" || !validID(p.VerifiedBy) {
continue
}
e := event{page: p, to: map[string]string{}}
e.add(p.VerifiedBy, NotifyVerifyExpired)
s.send(ctx, e)
}
return nil
}