CI / go (Media) (push) Successful in 14m33s
CI / go (Server) (push) Failing after 23m30s
CI / go (Web-App) (push) Canceled after 0s
CI / frontend (push) Failing after 9m30s
CI / desktop (ubuntu-latest) (push) Successful in 1h21m24s
CI / mobile (push) Failing after 4m16s
CI / docker (push) Successful in 1h1m25s
Docs / build (push) Successful in 14m59s
Docs / deploy (push) Skipped
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
Phase G of docs/AFFINE.md (ADR-17): - Invite links: one per workspace (member or guest, optional end date, optional admin approval) on a new /join page. Requests are approved (optionally as another role) or declined from workspace settings; join_request, join_approved and join_declined notifications reach the inbox, phones and the email digest. Workspace admins decide, else the organization's admins, else superadmins. Signup honours ?redirect=. - Workspace switches: allowSharing (off makes every share link answer 404 until it is back on) and urlPreview (chat apps may show private pages' titles). docwell-web writes title and Open Graph tags for known link-preview bots, asking docwell-server via DOCWELL_PREVIEW_API_URL; search engines are left out. - Page analytics: page_views counts opens per page, person and day from the existing visit call and share-link reads; editors see totals, a daily chart and recent viewers in the Info panel. analytics.prune drops rows after 400 days. - Unused files: attachments whose id appears in no page's Yjs state, page icon or comment (older than a day) are listed and deleted from workspace settings. - Migration, schema snapshot, OpenAPI, generated clients, translations in five languages, phone inbox, compose files and user docs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
193 lines
6.2 KiB
Go
193 lines
6.2 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package directory
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"docwell/server/internal/access"
|
|
"docwell/server/internal/store/pocketbase"
|
|
)
|
|
|
|
// ---- Favorites and recent pages --------------------------------------------
|
|
//
|
|
// Both are per user. Lists are filtered by the caller's current rights, so a
|
|
// page that was moved out of reach, or trashed, quietly drops out.
|
|
|
|
// maxRecent is how many recently visited pages a workspace keeps per user.
|
|
const maxRecent = 50
|
|
|
|
// PageItem is a page in a favorites or recent list.
|
|
type PageItem struct {
|
|
ID string `json:"id"`
|
|
Title string `json:"title"`
|
|
Icon string `json:"icon"`
|
|
Space string `json:"space"`
|
|
SpaceName string `json:"spaceName"`
|
|
// At is when it was added to favorites, or last visited.
|
|
At string `json:"at"`
|
|
}
|
|
|
|
// userPageRecord is a favorites or page_visits row with its page inlined
|
|
// (without the page's large content fields).
|
|
type userPageRecord struct {
|
|
ID string `json:"id"`
|
|
Page string `json:"page"`
|
|
Created string `json:"created"`
|
|
Visited string `json:"visited"`
|
|
Expand struct {
|
|
Page *pageRecord `json:"page"`
|
|
} `json:"expand"`
|
|
}
|
|
|
|
const userPageFields = "id,page,created,visited," +
|
|
"expand.page.id,expand.page.space,expand.page.title,expand.page.icon,expand.page.deleted_at"
|
|
|
|
// visibleItems keeps the rows whose page c can still open, with space names.
|
|
func (s *Service) visibleItems(ctx context.Context, c access.Caller, rows []userPageRecord, at func(userPageRecord) string) []PageItem {
|
|
type reach struct {
|
|
name string // "" = no access to the space
|
|
rules pageRules
|
|
}
|
|
spaces := map[string]reach{}
|
|
out := []PageItem{}
|
|
for _, r := range rows {
|
|
p := r.Expand.Page
|
|
if p == nil || p.DeletedAt != "" {
|
|
continue
|
|
}
|
|
sp, seen := spaces[p.Space]
|
|
if !seen {
|
|
if a, rules, err := s.spaceRules(ctx, c, p.Space); err == nil {
|
|
sp = reach{name: a.space.Name, rules: rules}
|
|
}
|
|
spaces[p.Space] = sp
|
|
}
|
|
name := sp.name
|
|
if name == "" || sp.rules.role(p.ID) == "" {
|
|
continue
|
|
}
|
|
out = append(out, PageItem{ID: p.ID, Title: p.Title, Icon: p.Icon, Space: p.Space, SpaceName: name, At: at(r)})
|
|
}
|
|
return out
|
|
}
|
|
|
|
// ListFavorites lists the caller's favorite pages in a workspace, newest first.
|
|
func (s *Service) ListFavorites(ctx context.Context, c access.Caller, wsID string) ([]PageItem, error) {
|
|
ws, _, err := s.workspaceFor(ctx, c, wsID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
rows, err := listAll[userPageRecord](ctx, s, "favorites", pocketbase.ListOptions{
|
|
Filter: fmt.Sprintf("user = '%s' && workspace = '%s'", c.ID, ws.ID), Sort: "-created",
|
|
Expand: "page", Fields: userPageFields,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return s.visibleItems(ctx, c, rows, func(r userPageRecord) string { return r.Created }), nil
|
|
}
|
|
|
|
func (s *Service) favoriteRow(ctx context.Context, userID, pageID string) (string, error) {
|
|
res, err := pocketbase.List[userPageRecord](ctx, s.pb, "favorites", pocketbase.ListOptions{
|
|
PerPage: 1, Filter: fmt.Sprintf("user = '%s' && page = '%s'", userID, pageID), Fields: "id",
|
|
})
|
|
if err != nil || len(res.Items) == 0 {
|
|
return "", err
|
|
}
|
|
return res.Items[0].ID, nil
|
|
}
|
|
|
|
// SetFavorite adds a page the caller can open to their favorites, or removes it.
|
|
func (s *Service) SetFavorite(ctx context.Context, c access.Caller, pageID string, on bool) error {
|
|
p, _, err := s.pageFor(ctx, c, pageID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
id, err := s.favoriteRow(ctx, c.ID, p.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
switch {
|
|
case on && id == "":
|
|
_, err = pocketbase.Create[userPageRecord](ctx, s.pb, "favorites", map[string]any{
|
|
"user": c.ID, "page": p.ID, "workspace": p.Workspace,
|
|
})
|
|
case !on && id != "":
|
|
err = s.pb.Delete(ctx, "favorites", id)
|
|
}
|
|
return mapPB(err, "page not found")
|
|
}
|
|
|
|
// RecordVisit notes that the caller opened a page, for their recent list.
|
|
func (s *Service) RecordVisit(ctx context.Context, c access.Caller, pageID string) error {
|
|
p, _, err := s.pageFor(ctx, c, pageID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// The same open counts towards the page's analytics (analytics.go).
|
|
if p.DeletedAt == "" {
|
|
s.recordView(ctx, p, c.ID)
|
|
}
|
|
res, err := pocketbase.List[userPageRecord](ctx, s.pb, "page_visits", pocketbase.ListOptions{
|
|
PerPage: 1, Filter: fmt.Sprintf("user = '%s' && page = '%s'", c.ID, p.ID), Fields: "id",
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
now := pbTime(time.Now())
|
|
if len(res.Items) > 0 {
|
|
_, err = pocketbase.Update[userPageRecord](ctx, s.pb, "page_visits", res.Items[0].ID, map[string]any{"visited": now})
|
|
return mapPB(err, "page not found")
|
|
}
|
|
if _, err := pocketbase.Create[userPageRecord](ctx, s.pb, "page_visits", map[string]any{
|
|
"user": c.ID, "page": p.ID, "workspace": p.Workspace, "visited": now,
|
|
}); err != nil {
|
|
return mapPB(err, "page not found")
|
|
}
|
|
s.trimVisits(ctx, c.ID, p.Workspace)
|
|
return nil
|
|
}
|
|
|
|
// trimVisits keeps the newest maxRecent visits per user and workspace (best effort).
|
|
func (s *Service) trimVisits(ctx context.Context, userID, wsID string) {
|
|
res, err := pocketbase.List[userPageRecord](ctx, s.pb, "page_visits", pocketbase.ListOptions{
|
|
Page: 2, PerPage: maxRecent, Filter: fmt.Sprintf("user = '%s' && workspace = '%s'", userID, wsID),
|
|
Sort: "-visited", Fields: "id",
|
|
})
|
|
if err != nil {
|
|
return
|
|
}
|
|
for _, r := range res.Items {
|
|
_ = s.pb.Delete(ctx, "page_visits", r.ID)
|
|
}
|
|
}
|
|
|
|
// ListRecent lists the pages the caller visited last in a workspace.
|
|
func (s *Service) ListRecent(ctx context.Context, c access.Caller, wsID string, limit int) ([]PageItem, error) {
|
|
ws, _, err := s.workspaceFor(ctx, c, wsID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if limit <= 0 || limit > maxRecent {
|
|
limit = 20
|
|
}
|
|
res, err := pocketbase.List[userPageRecord](ctx, s.pb, "page_visits", pocketbase.ListOptions{
|
|
PerPage: maxRecent, Filter: fmt.Sprintf("user = '%s' && workspace = '%s'", c.ID, ws.ID), Sort: "-visited",
|
|
Expand: "page", Fields: userPageFields,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := s.visibleItems(ctx, c, res.Items, func(r userPageRecord) string { return r.Visited })
|
|
return out[:min(limit, len(out))], nil
|
|
}
|
|
|
|
// isFavorite reports whether the caller has the page in their favorites.
|
|
func (s *Service) isFavorite(ctx context.Context, userID, pageID string) bool {
|
|
id, err := s.favoriteRow(ctx, userID, pageID)
|
|
return err == nil && id != ""
|
|
}
|