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>
236 lines
7.6 KiB
Go
236 lines
7.6 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package directory
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"docwell/server/internal/access"
|
|
"docwell/server/internal/apperr"
|
|
"docwell/server/internal/store/pocketbase"
|
|
)
|
|
|
|
// ---- Public share links ----------------------------------------------------
|
|
//
|
|
// A share link lets anyone read a page without an account; with subpages, the
|
|
// pages under it too. Editors turn it on and off. Reads through a link never
|
|
// see trashed pages, and attachments are reachable only for shared pages.
|
|
// A workspace admin can turn sharing off for the whole workspace: links can
|
|
// no longer be made, and existing ones stop working until it is back on.
|
|
|
|
// ShareLink is a page's public link, as editors see it.
|
|
type ShareLink struct {
|
|
Token string `json:"token"`
|
|
URL string `json:"url"`
|
|
IncludeSubpages bool `json:"includeSubpages"`
|
|
Created string `json:"created"`
|
|
}
|
|
|
|
type shareRecord struct {
|
|
ID string `json:"id"`
|
|
Page string `json:"page"`
|
|
Workspace string `json:"workspace"`
|
|
Token string `json:"token"`
|
|
IncludeSubpages bool `json:"include_subpages"`
|
|
Created string `json:"created"`
|
|
}
|
|
|
|
func (s *Service) shareView(r shareRecord) ShareLink {
|
|
return ShareLink{
|
|
Token: r.Token, URL: strings.TrimRight(s.mailer.WebURL, "/") + "/share/" + r.Token,
|
|
IncludeSubpages: r.IncludeSubpages, Created: r.Created,
|
|
}
|
|
}
|
|
|
|
func (s *Service) shareOf(ctx context.Context, pageID string) (shareRecord, error) {
|
|
res, err := pocketbase.List[shareRecord](ctx, s.pb, "share_links", pocketbase.ListOptions{
|
|
PerPage: 1, Filter: fmt.Sprintf("page = '%s'", pageID),
|
|
})
|
|
if err != nil || len(res.Items) == 0 {
|
|
return shareRecord{}, err
|
|
}
|
|
return res.Items[0], nil
|
|
}
|
|
|
|
// GetShareLink returns a page's link; ok is false when sharing is off (viewer).
|
|
func (s *Service) GetShareLink(ctx context.Context, c access.Caller, pageID string) (ShareLink, bool, error) {
|
|
p, _, err := s.pageFor(ctx, c, pageID)
|
|
if err != nil {
|
|
return ShareLink{}, false, err
|
|
}
|
|
rec, err := s.shareOf(ctx, p.ID)
|
|
if err != nil || rec.ID == "" {
|
|
return ShareLink{}, false, err
|
|
}
|
|
return s.shareView(rec), true, nil
|
|
}
|
|
|
|
// SetShareLink turns a page's link on (keeping its token if it exists) and
|
|
// sets whether it covers subpages (editor).
|
|
func (s *Service) SetShareLink(ctx context.Context, c access.Caller, pageID string, includeSubpages bool) (ShareLink, error) {
|
|
p, err := s.editablePage(ctx, c, pageID)
|
|
if err != nil {
|
|
return ShareLink{}, err
|
|
}
|
|
if off, err := s.sharingOff(ctx, p.Workspace); err != nil {
|
|
return ShareLink{}, err
|
|
} else if off {
|
|
return ShareLink{}, apperr.Forbidden("public links are turned off in this workspace")
|
|
}
|
|
rec, err := s.shareOf(ctx, p.ID)
|
|
if err != nil {
|
|
return ShareLink{}, err
|
|
}
|
|
if rec.ID != "" {
|
|
rec, err = pocketbase.Update[shareRecord](ctx, s.pb, "share_links", rec.ID, map[string]any{"include_subpages": includeSubpages})
|
|
return s.shareView(rec), mapPB(err, "page not found")
|
|
}
|
|
b := make([]byte, 24)
|
|
if _, err := rand.Read(b); err != nil {
|
|
return ShareLink{}, err
|
|
}
|
|
rec, err = pocketbase.Create[shareRecord](ctx, s.pb, "share_links", map[string]any{
|
|
"page": p.ID, "workspace": p.Workspace, "token": base64.RawURLEncoding.EncodeToString(b),
|
|
"include_subpages": includeSubpages, "created_by": c.ID,
|
|
})
|
|
if err != nil {
|
|
return ShareLink{}, mapPB(err, "page not found")
|
|
}
|
|
return s.shareView(rec), nil
|
|
}
|
|
|
|
// DeleteShareLink turns a page's link off; the old URL stops working (editor).
|
|
func (s *Service) DeleteShareLink(ctx context.Context, c access.Caller, pageID string) error {
|
|
// Unlike other edits this works in the trash too: hiding a page there
|
|
// should not need a restore first.
|
|
p, a, err := s.pageFor(ctx, c, pageID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := access.EditPages(a.role); err != nil {
|
|
return err
|
|
}
|
|
rec, err := s.shareOf(ctx, p.ID)
|
|
if err != nil || rec.ID == "" {
|
|
return err
|
|
}
|
|
return mapPB(s.pb.Delete(ctx, "share_links", rec.ID), "page not found")
|
|
}
|
|
|
|
// sharingOff reports whether a workspace turned public links off.
|
|
func (s *Service) sharingOff(ctx context.Context, wsID string) (bool, error) {
|
|
ws, err := s.getWorkspace(ctx, wsID)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return ws.SharingOff, nil
|
|
}
|
|
|
|
// SharedPage is what a link's visitors see.
|
|
type SharedPage struct {
|
|
ID string `json:"id"`
|
|
Title string `json:"title"`
|
|
Icon string `json:"icon"`
|
|
Updated string `json:"updated"`
|
|
Content json.RawMessage `json:"content"`
|
|
// Mode is the view the page opens in: "page" or "edgeless".
|
|
Mode string `json:"mode"`
|
|
// Root is the shared page; Pages lists the pages under it when the link
|
|
// includes subpages (tree order, empty otherwise).
|
|
Root PageRef `json:"root"`
|
|
Pages []PageNode `json:"pages"`
|
|
}
|
|
|
|
// sharedScope resolves a token and checks that pageID ("" = the shared page)
|
|
// is readable through it. It returns the link and the page.
|
|
func (s *Service) sharedScope(ctx context.Context, token, pageID string) (shareRecord, pageRecord, tree, error) {
|
|
notFound := apperr.NotFound("this link does not exist or was turned off")
|
|
token = strings.TrimSpace(token)
|
|
if len(token) < 24 || len(token) > 64 || !validID(token) {
|
|
return shareRecord{}, pageRecord{}, tree{}, notFound
|
|
}
|
|
res, err := pocketbase.List[shareRecord](ctx, s.pb, "share_links", pocketbase.ListOptions{
|
|
PerPage: 1, Filter: fmt.Sprintf("token = '%s'", token),
|
|
})
|
|
if err != nil {
|
|
return shareRecord{}, pageRecord{}, tree{}, err
|
|
}
|
|
if len(res.Items) == 0 {
|
|
return shareRecord{}, pageRecord{}, tree{}, notFound
|
|
}
|
|
link := res.Items[0]
|
|
if off, err := s.sharingOff(ctx, link.Workspace); err != nil || off {
|
|
return link, pageRecord{}, tree{}, notFound
|
|
}
|
|
root, err := s.getPage(ctx, link.Page)
|
|
if err != nil {
|
|
return link, pageRecord{}, tree{}, notFound
|
|
}
|
|
t, err := s.loadTree(ctx, root.Space)
|
|
if err != nil {
|
|
return link, pageRecord{}, tree{}, err
|
|
}
|
|
if !t.live(root.ID) {
|
|
return link, pageRecord{}, tree{}, notFound
|
|
}
|
|
if pageID == "" || pageID == root.ID {
|
|
return link, root, t, nil
|
|
}
|
|
p, ok := t.byID[pageID]
|
|
if !ok || !link.IncludeSubpages || !t.live(p.ID) || !t.within(p.ID, root.ID) || t.restrictedBelow(p.ID, root.ID) {
|
|
return link, pageRecord{}, tree{}, apperr.NotFound("page not found")
|
|
}
|
|
return link, p, t, nil
|
|
}
|
|
|
|
// GetSharedPage returns a page through a link: the shared page, or with
|
|
// pageID one of the pages under it.
|
|
func (s *Service) GetSharedPage(ctx context.Context, token, pageID string) (SharedPage, error) {
|
|
link, p, t, err := s.sharedScope(ctx, token, pageID)
|
|
if err != nil {
|
|
return SharedPage{}, err
|
|
}
|
|
doc, err := s.docs.Content(ctx, p.ID)
|
|
if err != nil {
|
|
return SharedPage{}, err
|
|
}
|
|
s.recordView(ctx, p, "")
|
|
root := t.byID[link.Page]
|
|
out := SharedPage{
|
|
ID: p.ID, Title: p.Title, Icon: p.Icon, Updated: p.Updated, Content: doc, Mode: pageMode(p.DefaultMode),
|
|
Root: PageRef{ID: root.ID, Title: root.Title, Icon: root.Icon}, Pages: []PageNode{},
|
|
}
|
|
if link.IncludeSubpages {
|
|
var walk func(parent string)
|
|
walk = func(parent string) {
|
|
for _, c := range t.children[parent] {
|
|
if c.Restricted {
|
|
continue
|
|
}
|
|
out.Pages = append(out.Pages, c.node())
|
|
walk(c.ID)
|
|
}
|
|
}
|
|
walk(root.ID)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// OpenSharedFile opens an attachment of a page readable through a link.
|
|
func (s *Service) OpenSharedFile(ctx context.Context, token, fileID string, header http.Header) (FileDownload, error) {
|
|
rec, err := s.getAttachment(ctx, fileID)
|
|
if err != nil {
|
|
return FileDownload{}, err
|
|
}
|
|
if _, _, _, err := s.sharedScope(ctx, token, rec.Page); err != nil {
|
|
return FileDownload{}, apperr.NotFound("file not found")
|
|
}
|
|
return s.openFile(ctx, rec, header)
|
|
}
|