Files
Docwell/Server/internal/httpapi/organize.go
T
tajniak81andClaude Opus 5.5 ec11abd8b8 Add page properties, tags, All docs, collections and journals
Phase B (items 1-6) of docs/AFFINE.md.

Properties: workspaces define custom page properties (text, number,
checkbox, date, select, multi-select, person, link) in the new
workspace_properties collection; pages keep their values in
pages.properties, checked against each property's type. The Info panel
under the title shows tags, custom and built-in properties, following
each property's visibility; the Info side panel shows them all, with the
page's collections and the journal. Settings gains Page properties and
Tags sections.

Tags: coloured, per workspace (tags collection, pages.tags); create from
the picker, rename, recolour, merge and delete. Search filters by tag.

All docs: GET /workspaces/{id}/docs lists the pages the caller can open
with their properties, narrowed by space, tag, collection or filter
rules evaluated on the server. The view shows a list, grid or cards,
groups and orders by any property, picks the properties shown, and acts
on several pages at once (tag, move, trash, collect).

Collections (page_collections): saved rules plus pages added or taken
out by hand, private to their creator until shared with the workspace,
listed in the sidebar.

Journals: one page per person and day in a private journal space
(spaces.journal_owner) that workspace admins do not see into, made on
first use. A week strip marks days with pages, new days can start from
a template, and dates in read mode open that day's journal.

The migration and schema snapshot, OpenAPI spec and generated Go, TS and
Dart types are updated (the Dart generator learns untyped values), the
mobile editor bundle is rebuilt, the new strings are translated into all
five languages, and the user guide covers the new features.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 16:35:42 +01:00

180 lines
7.7 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package httpapi
import (
"context"
"encoding/json"
"net/http"
"docwell/server/internal/access"
"docwell/server/internal/apperr"
"docwell/server/internal/directory"
)
// OrganizeService is the part of directory.Service for page properties,
// tags, the All docs list, collections and journals.
type OrganizeService interface {
ListProperties(ctx context.Context, c access.Caller, wsID string) ([]directory.Property, error)
CreateProperty(ctx context.Context, c access.Caller, wsID string, in directory.PropertyInput) (directory.Property, error)
UpdateProperty(ctx context.Context, c access.Caller, id string, in directory.PropertyInput) (directory.Property, error)
DeleteProperty(ctx context.Context, c access.Caller, id string) error
ListTags(ctx context.Context, c access.Caller, wsID string) ([]directory.Tag, error)
CreateTag(ctx context.Context, c access.Caller, wsID string, in directory.TagInput) (directory.Tag, error)
UpdateTag(ctx context.Context, c access.Caller, id string, in directory.TagInput) (directory.Tag, error)
DeleteTag(ctx context.Context, c access.Caller, id string) error
MergeTag(ctx context.Context, c access.Caller, id, into string) (directory.Tag, error)
ListDocs(ctx context.Context, c access.Caller, wsID string, q directory.DocQuery) (directory.DocList, error)
ListCollections(ctx context.Context, c access.Caller, wsID string) ([]directory.Collection, error)
CreateCollection(ctx context.Context, c access.Caller, wsID string, in directory.CollectionInput) (directory.Collection, error)
UpdateCollection(ctx context.Context, c access.Caller, id string, in directory.CollectionInput) (directory.Collection, error)
DeleteCollection(ctx context.Context, c access.Caller, id string) error
SetCollectionPage(ctx context.Context, c access.Caller, id, pageID string, in bool) (directory.Collection, error)
GetJournal(ctx context.Context, c access.Caller, wsID, from, to string) (directory.JournalInfo, error)
SetJournalTemplate(ctx context.Context, c access.Caller, wsID, template string) (directory.JournalInfo, error)
OpenJournal(ctx context.Context, c access.Caller, wsID, date, title string) (directory.Page, error)
}
func (a *api) organizeRoutes(mux *routeMux) {
// Properties.
mux.HandleFunc("GET /api/v1/workspaces/{id}/properties", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
list, err := a.dir.ListProperties(r.Context(), caller(r), r.PathValue("id"))
a.respond(w, list, err)
}))
mux.HandleFunc("POST /api/v1/workspaces/{id}/properties", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
var in directory.PropertyInput
if !a.decode(w, r, &in) {
return
}
p, err := a.dir.CreateProperty(r.Context(), caller(r), r.PathValue("id"), in)
a.respond(w, p, err)
}))
mux.HandleFunc("PATCH /api/v1/properties/{id}", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
var in directory.PropertyInput
if !a.decode(w, r, &in) {
return
}
p, err := a.dir.UpdateProperty(r.Context(), caller(r), r.PathValue("id"), in)
a.respond(w, p, err)
}))
mux.HandleFunc("DELETE /api/v1/properties/{id}", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
a.respondNoContent(w, a.dir.DeleteProperty(r.Context(), caller(r), r.PathValue("id")))
}))
// Tags.
mux.HandleFunc("GET /api/v1/workspaces/{id}/tags", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
list, err := a.dir.ListTags(r.Context(), caller(r), r.PathValue("id"))
a.respond(w, list, err)
}))
mux.HandleFunc("POST /api/v1/workspaces/{id}/tags", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
var in directory.TagInput
if !a.decode(w, r, &in) {
return
}
t, err := a.dir.CreateTag(r.Context(), caller(r), r.PathValue("id"), in)
a.respond(w, t, err)
}))
mux.HandleFunc("PATCH /api/v1/tags/{id}", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
var in directory.TagInput
if !a.decode(w, r, &in) {
return
}
t, err := a.dir.UpdateTag(r.Context(), caller(r), r.PathValue("id"), in)
a.respond(w, t, err)
}))
mux.HandleFunc("DELETE /api/v1/tags/{id}", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
a.respondNoContent(w, a.dir.DeleteTag(r.Context(), caller(r), r.PathValue("id")))
}))
mux.HandleFunc("POST /api/v1/tags/{id}/merge", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
var in struct {
Into string `json:"into"`
}
if !a.decode(w, r, &in) {
return
}
t, err := a.dir.MergeTag(r.Context(), caller(r), r.PathValue("id"), in.Into)
a.respond(w, t, err)
}))
// All docs.
mux.HandleFunc("GET /api/v1/workspaces/{id}/docs", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
dq := directory.DocQuery{Space: q.Get("space"), Tag: q.Get("tag"), Collection: q.Get("collection")}
if raw := q.Get("filter"); raw != "" {
var f directory.DocFilter
if err := json.Unmarshal([]byte(raw), &f); err != nil {
a.writeError(w, apperr.InvalidFields(map[string]string{"filter": "The filter is not valid JSON."}))
return
}
dq.Filter = &f
}
list, err := a.dir.ListDocs(r.Context(), caller(r), r.PathValue("id"), dq)
a.respond(w, list, err)
}))
// Collections.
mux.HandleFunc("GET /api/v1/workspaces/{id}/collections", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
list, err := a.dir.ListCollections(r.Context(), caller(r), r.PathValue("id"))
a.respond(w, list, err)
}))
mux.HandleFunc("POST /api/v1/workspaces/{id}/collections", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
var in directory.CollectionInput
if !a.decode(w, r, &in) {
return
}
col, err := a.dir.CreateCollection(r.Context(), caller(r), r.PathValue("id"), in)
a.respond(w, col, err)
}))
mux.HandleFunc("PATCH /api/v1/collections/{id}", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
var in directory.CollectionInput
if !a.decode(w, r, &in) {
return
}
col, err := a.dir.UpdateCollection(r.Context(), caller(r), r.PathValue("id"), in)
a.respond(w, col, err)
}))
mux.HandleFunc("DELETE /api/v1/collections/{id}", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
a.respondNoContent(w, a.dir.DeleteCollection(r.Context(), caller(r), r.PathValue("id")))
}))
mux.HandleFunc("PUT /api/v1/collections/{id}/pages/{page}", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
col, err := a.dir.SetCollectionPage(r.Context(), caller(r), r.PathValue("id"), r.PathValue("page"), true)
a.respond(w, col, err)
}))
mux.HandleFunc("DELETE /api/v1/collections/{id}/pages/{page}", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
col, err := a.dir.SetCollectionPage(r.Context(), caller(r), r.PathValue("id"), r.PathValue("page"), false)
a.respond(w, col, err)
}))
// Journals.
mux.HandleFunc("GET /api/v1/workspaces/{id}/journals", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
j, err := a.dir.GetJournal(r.Context(), caller(r), r.PathValue("id"), q.Get("from"), q.Get("to"))
a.respond(w, j, err)
}))
mux.HandleFunc("PATCH /api/v1/workspaces/{id}/journals", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
var in struct {
Template string `json:"template"`
}
if !a.decode(w, r, &in) {
return
}
j, err := a.dir.SetJournalTemplate(r.Context(), caller(r), r.PathValue("id"), in.Template)
a.respond(w, j, err)
}))
mux.HandleFunc("PUT /api/v1/workspaces/{id}/journals/{date}", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
var in struct {
Title string `json:"title"`
}
// The body is optional.
if r.ContentLength != 0 && !a.decode(w, r, &in) {
return
}
p, err := a.dir.OpenJournal(r.Context(), caller(r), r.PathValue("id"), r.PathValue("date"), in.Title)
a.respond(w, p, err)
}))
}