Files
Docwell/Server/internal/httpapi/sharing.go
T
tajniak81andClaude Opus 5.5 9d674e9699
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
Add invite links with approval, sharing switches, page views and unused-file cleanup
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>
2026-11-16 15:59:55 +01:00

120 lines
5.5 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package httpapi
import (
"context"
"net/http"
"strconv"
"docwell/server/internal/access"
"docwell/server/internal/auth"
"docwell/server/internal/directory"
)
// SharingService is the part of directory.Service for the sharing extras
// (ADR-17): invite links and join requests, page analytics, unused-file
// cleanup and link previews for chat apps.
type SharingService interface {
GetJoinLink(ctx context.Context, c access.Caller, wsID string) (directory.JoinLink, bool, error)
SetJoinLink(ctx context.Context, c access.Caller, wsID string, in directory.SetJoinLinkInput) (directory.JoinLink, error)
DeleteJoinLink(ctx context.Context, c access.Caller, wsID string) error
GetJoinInfo(ctx context.Context, me *auth.User, token string) (directory.JoinInfo, error)
Join(ctx context.Context, me auth.User, token string) (directory.JoinResult, error)
ListJoinRequests(ctx context.Context, c access.Caller, wsID string) ([]directory.JoinRequest, error)
ReviewJoinRequest(ctx context.Context, c access.Caller, wsID, id string, approve bool, role string) error
GetPageAnalytics(ctx context.Context, c access.Caller, pageID string, days int) (directory.PageAnalytics, error)
ListUnusedFiles(ctx context.Context, c access.Caller, wsID string) (directory.UnusedFiles, error)
DeleteUnusedFiles(ctx context.Context, c access.Caller, wsID string, ids []string) (int, error)
GetPagePreview(ctx context.Context, pageID string) (directory.PagePreview, error)
GetSharePreview(ctx context.Context, token, pageID string) (directory.PagePreview, error)
}
func (a *api) sharingRoutes(mux *routeMux) {
// Invite links (workspace admins).
mux.HandleFunc("GET /api/v1/workspaces/{id}/invite-link", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
link, ok, err := a.dir.GetJoinLink(r.Context(), caller(r), r.PathValue("id"))
if err == nil && !ok {
writeJSON(w, http.StatusNotFound, map[string]string{"error": "not_found", "message": "no invite link"})
return
}
a.respond(w, link, err)
}))
mux.HandleFunc("PUT /api/v1/workspaces/{id}/invite-link", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
var in directory.SetJoinLinkInput
if !a.decode(w, r, &in) {
return
}
link, err := a.dir.SetJoinLink(r.Context(), caller(r), r.PathValue("id"), in)
a.respond(w, link, err)
}))
mux.HandleFunc("DELETE /api/v1/workspaces/{id}/invite-link", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
a.respondNoContent(w, a.dir.DeleteJoinLink(r.Context(), caller(r), r.PathValue("id")))
}))
mux.HandleFunc("GET /api/v1/workspaces/{id}/join-requests", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
list, err := a.dir.ListJoinRequests(r.Context(), caller(r), r.PathValue("id"))
a.respond(w, list, err)
}))
mux.HandleFunc("POST /api/v1/workspaces/{id}/join-requests/{requestId}/approve", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
var in struct{ Role string }
if r.ContentLength != 0 && !a.decode(w, r, &in) {
return
}
a.respondNoContent(w, a.dir.ReviewJoinRequest(r.Context(), caller(r), r.PathValue("id"), r.PathValue("requestId"), true, in.Role))
}))
mux.HandleFunc("POST /api/v1/workspaces/{id}/join-requests/{requestId}/decline", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
a.respondNoContent(w, a.dir.ReviewJoinRequest(r.Context(), caller(r), r.PathValue("id"), r.PathValue("requestId"), false, ""))
}))
// The /join page: anyone may look; joining needs an account.
mux.HandleFunc("GET /api/v1/auth/join/{token}", func(w http.ResponseWriter, r *http.Request) {
var me *auth.User
if token := auth.BearerToken(r); token != "" {
if u, err := a.auth.Verify(r.Context(), token); err == nil {
me = &u
}
}
info, err := a.dir.GetJoinInfo(r.Context(), me, r.PathValue("token"))
a.respond(w, info, err)
})
mux.HandleFunc("POST /api/v1/auth/join/{token}", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
me, _ := CurrentUser(r.Context())
res, err := a.dir.Join(r.Context(), me, r.PathValue("token"))
a.respond(w, res, err)
}))
mux.HandleFunc("GET /api/v1/pages/{id}/analytics", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
days, _ := strconv.Atoi(r.URL.Query().Get("days"))
out, err := a.dir.GetPageAnalytics(r.Context(), caller(r), r.PathValue("id"), days)
a.respond(w, out, err)
}))
mux.HandleFunc("GET /api/v1/workspaces/{id}/unused-files", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
out, err := a.dir.ListUnusedFiles(r.Context(), caller(r), r.PathValue("id"))
a.respond(w, out, err)
}))
mux.HandleFunc("POST /api/v1/workspaces/{id}/unused-files/delete", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
var in struct{ IDs []string }
if r.ContentLength != 0 && !a.decode(w, r, &in) {
return
}
n, err := a.dir.DeleteUnusedFiles(r.Context(), caller(r), r.PathValue("id"), in.IDs)
a.respond(w, map[string]int{"deleted": n}, err)
}))
// Link previews for chat apps, asked by docwell-web: no account, never indexed.
mux.HandleFunc("GET /api/v1/previews/pages/{id}", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Robots-Tag", "noindex")
out, err := a.dir.GetPagePreview(r.Context(), r.PathValue("id"))
a.respond(w, out, err)
})
mux.HandleFunc("GET /api/v1/previews/share/{token}", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Robots-Tag", "noindex")
out, err := a.dir.GetSharePreview(r.Context(), r.PathValue("token"), r.URL.Query().Get("page"))
a.respond(w, out, err)
})
}