// 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 }