diff --git a/Database/pb_migrations/1762600000_share_links.js b/Database/pb_migrations/1762600000_share_links.js new file mode 100644 index 0000000..9a49db9 --- /dev/null +++ b/Database/pb_migrations/1762600000_share_links.js @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later + +/// + +// Public share links: anyone with the link can read the page (and, if +// include_subpages, the pages under it) without signing in. The token is the +// link itself, so it is stored as is and shown to editors again; deleting the +// record revokes the link. + +migrate( + (app) => { + const users = app.findCollectionByNameOrId("users"); + const pages = app.findCollectionByNameOrId("pages"); + const workspaces = app.findCollectionByNameOrId("workspaces"); + + app.save( + new Collection({ + type: "base", + name: "share_links", + fields: [ + { name: "page", type: "relation", collectionId: pages.id, maxSelect: 1, required: true, cascadeDelete: true }, + { name: "workspace", type: "relation", collectionId: workspaces.id, maxSelect: 1, required: true, cascadeDelete: true }, + { name: "token", type: "text", required: true, min: 24, max: 64 }, + { name: "include_subpages", type: "bool" }, + { name: "created_by", type: "relation", collectionId: users.id, maxSelect: 1, cascadeDelete: false }, + { name: "created", type: "autodate", onCreate: true, onUpdate: false }, + { name: "updated", type: "autodate", onCreate: true, onUpdate: true }, + ], + indexes: [ + "CREATE UNIQUE INDEX idx_share_links_token ON share_links (token)", + "CREATE UNIQUE INDEX idx_share_links_page ON share_links (page)", + ], + }), + ); + }, + (app) => { + try { + app.delete(app.findCollectionByNameOrId("share_links")); + } catch (_) {} + }, +); diff --git a/Packages/api-client/src/index.ts b/Packages/api-client/src/index.ts index 61327ca..a35b4a3 100644 --- a/Packages/api-client/src/index.ts +++ b/Packages/api-client/src/index.ts @@ -37,6 +37,8 @@ export type Group = Schemas["Group"]; export type PageItem = Schemas["PageItem"]; export type Attachment = Schemas["Attachment"]; export type SearchHit = Schemas["SearchHit"]; +export type ShareLink = Schemas["ShareLink"]; +export type SharedPage = Schemas["SharedPage"]; export type SearchResult = Schemas["SearchResult"]; export type GroupRequest = Schemas["GroupRequest"]; export type SpaceGroup = Schemas["SpaceGroup"]; @@ -170,6 +172,15 @@ export function createClient(opts: ClientOptions) { removeAttachment: (id: string) => request("DELETE", `/api/v1/attachments/${encodeURIComponent(id)}`), }, + /** Reading pages through a public link (no session needed). */ + share: { + get: (token: string, page?: string) => + request( + "GET", + `/api/v1/share/${encodeURIComponent(token)}${page ? `/pages/${encodeURIComponent(page)}` : ""}`, + ), + }, + invites: { list: (params: { workspace?: string; organization?: string } = {}) => request("GET", `/api/v1/invites${query(params)}`), @@ -298,6 +309,11 @@ export function createClient(opts: ClientOptions) { form.append("file", file, name); return upload(`/api/v1/pages/${encodeURIComponent(id)}/attachments`, form); }, + /** The public link; rejects with a 404 ApiError when sharing is off. */ + shareLink: (id: string) => request("GET", `/api/v1/pages/${encodeURIComponent(id)}/share`), + setShareLink: (id: string, includeSubpages: boolean) => + request("PUT", `/api/v1/pages/${encodeURIComponent(id)}/share`, { includeSubpages }), + deleteShareLink: (id: string) => request("DELETE", `/api/v1/pages/${encodeURIComponent(id)}/share`), export: (id: string, format: "markdown" | "html") => download(`/api/v1/pages/${encodeURIComponent(id)}/export${query({ format })}`), /** Records that the user opened the page, for their recent list. */ diff --git a/Packages/api-client/src/schema.d.ts b/Packages/api-client/src/schema.d.ts index bba65b7..e071911 100644 --- a/Packages/api-client/src/schema.d.ts +++ b/Packages/api-client/src/schema.d.ts @@ -1207,6 +1207,89 @@ export interface paths { patch?: never; trace?: never; }; + "/api/v1/pages/{id}/share": { + parameters: { + query?: never; + header?: never; + path: { + id: string; + }; + cookie?: never; + }; + /** The page's public link (404 when sharing is off) */ + get: operations["getShareLink"]; + /** + * Turn the public link on, or change what it covers (editor) + * @description An existing link keeps its URL. + */ + put: operations["setShareLink"]; + post?: never; + /** Turn the public link off; its URL stops working (editor) */ + delete: operations["deleteShareLink"]; + options?: never; + head?: never; + patch?: never; + trace?: never; + }; + "/api/v1/share/{token}": { + parameters: { + query?: never; + header?: never; + path: { + token: string; + }; + cookie?: never; + }; + /** Read a shared page (no account needed) */ + get: operations["getSharedPage"]; + put?: never; + post?: never; + delete?: never; + options?: never; + head?: never; + patch?: never; + trace?: never; + }; + "/api/v1/share/{token}/pages/{pageId}": { + parameters: { + query?: never; + header?: never; + path: { + token: string; + pageId: string; + }; + cookie?: never; + }; + /** Read a page under the shared page, when the link includes subpages */ + get: operations["getSharedSubpage"]; + put?: never; + post?: never; + delete?: never; + options?: never; + head?: never; + patch?: never; + trace?: never; + }; + "/api/v1/share/{token}/files/{fileId}": { + parameters: { + query?: never; + header?: never; + path: { + token: string; + fileId: string; + }; + cookie?: never; + }; + /** Download an attachment of a page readable through the link */ + get: operations["downloadSharedFile"]; + put?: never; + post?: never; + delete?: never; + options?: never; + head?: never; + patch?: never; + trace?: never; + }; } export type webhooks = Record; export interface components { @@ -1587,6 +1670,26 @@ export interface components { items: components["schemas"]["SearchHit"][]; total: number; }; + ShareLink: { + token: string; + /** @description The link on docwell-web (DOCWELL_WEB_URL/share/) */ + url: string; + includeSubpages: boolean; + created: string; + }; + SharedPage: { + id: string; + title: string; + icon: string; + updated: string; + /** @description Tiptap JSON; files load from /api/v1/share/{token}/files/{fileId} */ + content: { + [key: string]: unknown; + } | null; + root: components["schemas"]["PageRef"]; + /** @description Pages under the shared page */ + pages: components["schemas"]["PageNode"][]; + }; }; responses: { /** @description Signed in */ @@ -3742,4 +3845,151 @@ export interface operations { 404: components["responses"]["Error"]; }; }; + getShareLink: { + parameters: { + query?: never; + header?: never; + path: { + id: string; + }; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description OK */ + 200: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": components["schemas"]["ShareLink"]; + }; + }; + 404: components["responses"]["Error"]; + }; + }; + setShareLink: { + parameters: { + query?: never; + header?: never; + path: { + id: string; + }; + cookie?: never; + }; + requestBody: { + content: { + "application/json": { + includeSubpages?: boolean; + }; + }; + }; + responses: { + /** @description OK */ + 200: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": components["schemas"]["ShareLink"]; + }; + }; + 400: components["responses"]["Error"]; + 403: components["responses"]["Error"]; + 404: components["responses"]["Error"]; + }; + }; + deleteShareLink: { + parameters: { + query?: never; + header?: never; + path: { + id: string; + }; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description Off */ + 204: { + headers: { + [name: string]: unknown; + }; + content?: never; + }; + 403: components["responses"]["Error"]; + 404: components["responses"]["Error"]; + }; + }; + getSharedPage: { + parameters: { + query?: never; + header?: never; + path: { + token: string; + }; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description OK */ + 200: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": components["schemas"]["SharedPage"]; + }; + }; + 404: components["responses"]["Error"]; + }; + }; + getSharedSubpage: { + parameters: { + query?: never; + header?: never; + path: { + token: string; + pageId: string; + }; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description OK */ + 200: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": components["schemas"]["SharedPage"]; + }; + }; + 404: components["responses"]["Error"]; + }; + }; + downloadSharedFile: { + parameters: { + query?: never; + header?: never; + path: { + token: string; + fileId: string; + }; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description The file */ + 200: { + headers: { + [name: string]: unknown; + }; + content: { + "application/octet-stream": string; + }; + }; + 404: components["responses"]["Error"]; + }; + }; } diff --git a/Phone-App/lib/api/generated/models.dart b/Phone-App/lib/api/generated/models.dart index f410cc8..5ef2eff 100644 --- a/Phone-App/lib/api/generated/models.dart +++ b/Phone-App/lib/api/generated/models.dart @@ -1730,3 +1730,87 @@ class SearchResult { 'total': total, }; } + +class ShareLink { + const ShareLink({ + required this.token, + required this.url, + required this.includeSubpages, + required this.created, + }); + + factory ShareLink.fromJson(Map json) => ShareLink( + token: json['token'] as String, + url: json['url'] as String, + includeSubpages: json['includeSubpages'] as bool, + created: json['created'] as String, + ); + + final String token; + + /// The link on docwell-web (DOCWELL_WEB_URL/share/) + final String url; + + final bool includeSubpages; + + final String created; + + Map toJson() => { + 'token': token, + 'url': url, + 'includeSubpages': includeSubpages, + 'created': created, + }; +} + +class SharedPage { + const SharedPage({ + required this.id, + required this.title, + required this.icon, + required this.updated, + required this.content, + required this.root, + required this.pages, + }); + + factory SharedPage.fromJson(Map json) => SharedPage( + id: json['id'] as String, + title: json['title'] as String, + icon: json['icon'] as String, + updated: json['updated'] as String, + content: json['content'] == null + ? null + : json['content'] as Map, + root: PageRef.fromJson(json['root'] as Map), + pages: (json['pages'] as List) + .map((e) => PageNode.fromJson(e as Map)) + .toList(), + ); + + final String id; + + final String title; + + final String icon; + + final String updated; + + /// Tiptap JSON; files load from /api/v1/share/{token}/files/{fileId} + final Map? content; + + final PageRef root; + + /// Pages under the shared page + final List pages; + + Map toJson() => { + 'id': id, + 'title': title, + 'icon': icon, + 'updated': updated, + 'content': content, + 'root': root.toJson(), + 'pages': pages.map((e) => e.toJson()).toList(), + }; +} diff --git a/Server/internal/api/api.gen.go b/Server/internal/api/api.gen.go index e73f7c3..bff654c 100644 --- a/Server/internal/api/api.gen.go +++ b/Server/internal/api/api.gen.go @@ -524,6 +524,30 @@ type Session struct { User User `json:"user"` } +// ShareLink defines model for ShareLink. +type ShareLink struct { + Created string `json:"created"` + IncludeSubpages bool `json:"includeSubpages"` + Token string `json:"token"` + + // Url The link on docwell-web (DOCWELL_WEB_URL/share/) + Url string `json:"url"` +} + +// SharedPage defines model for SharedPage. +type SharedPage struct { + // Content Tiptap JSON; files load from /api/v1/share/{token}/files/{fileId} + Content *map[string]interface{} `json:"content"` + Icon string `json:"icon"` + Id string `json:"id"` + + // Pages Pages under the shared page + Pages []PageNode `json:"pages"` + Root PageRef `json:"root"` + Title string `json:"title"` + Updated string `json:"updated"` +} + // SignupRequest defines model for SignupRequest. type SignupRequest struct { Email openapi_types.Email `json:"email"` @@ -793,6 +817,11 @@ type MovePageJSONBody struct { Parent *string `json:"parent,omitempty"` } +// SetShareLinkJSONBody defines parameters for SetShareLink. +type SetShareLinkJSONBody struct { + IncludeSubpages *bool `json:"includeSubpages,omitempty"` +} + // UpdateSpaceJSONBody defines parameters for UpdateSpace. type UpdateSpaceJSONBody struct { // DefaultRole Role that workspace members (not guests) get in an open space without an explicit role. @@ -940,6 +969,9 @@ type SavePageContentJSONRequestBody SavePageContentJSONBody // MovePageJSONRequestBody defines body for MovePage for application/json ContentType. type MovePageJSONRequestBody MovePageJSONBody +// SetShareLinkJSONRequestBody defines body for SetShareLink for application/json ContentType. +type SetShareLinkJSONRequestBody SetShareLinkJSONBody + // UpdateSpaceJSONRequestBody defines body for UpdateSpace for application/json ContentType. type UpdateSpaceJSONRequestBody UpdateSpaceJSONBody diff --git a/Server/internal/directory/shares.go b/Server/internal/directory/shares.go new file mode 100644 index 0000000..2e6c9ff --- /dev/null +++ b/Server/internal/directory/shares.go @@ -0,0 +1,210 @@ +// 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. + +// 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 + } + 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") +} + +// 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"` + // 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] + 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) { + 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 + } + root := t.byID[link.Page] + out := SharedPage{ + ID: p.ID, Title: p.Title, Icon: p.Icon, Updated: p.Updated, Content: doc, + 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] { + 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) +} diff --git a/Server/internal/httpapi/auth.go b/Server/internal/httpapi/auth.go index 8ff3ffa..2014c0b 100644 --- a/Server/internal/httpapi/auth.go +++ b/Server/internal/httpapi/auth.go @@ -150,6 +150,7 @@ func (a *api) routes(mux *routeMux) { a.favoriteRoutes(mux) a.searchRoutes(mux) a.transferRoutes(mux) + a.shareRoutes(mux) } func (a *api) decode(w http.ResponseWriter, r *http.Request, v any) bool { diff --git a/Server/internal/httpapi/directory.go b/Server/internal/httpapi/directory.go index 401f852..40b1bed 100644 --- a/Server/internal/httpapi/directory.go +++ b/Server/internal/httpapi/directory.go @@ -39,6 +39,7 @@ type DirectoryService interface { AttachmentService SearchService TransferService + ShareService } func caller(r *http.Request) access.Caller { diff --git a/Server/internal/httpapi/shares.go b/Server/internal/httpapi/shares.go new file mode 100644 index 0000000..ddcb72b --- /dev/null +++ b/Server/internal/httpapi/shares.go @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later + +package httpapi + +import ( + "context" + "net/http" + + "docwell/server/internal/access" + "docwell/server/internal/directory" +) + +// ShareService is the part of directory.Service for public share links. +type ShareService interface { + GetShareLink(ctx context.Context, c access.Caller, pageID string) (directory.ShareLink, bool, error) + SetShareLink(ctx context.Context, c access.Caller, pageID string, includeSubpages bool) (directory.ShareLink, error) + DeleteShareLink(ctx context.Context, c access.Caller, pageID string) error + GetSharedPage(ctx context.Context, token, pageID string) (directory.SharedPage, error) + OpenSharedFile(ctx context.Context, token, fileID string, header http.Header) (directory.FileDownload, error) +} + +func (a *api) shareRoutes(mux *routeMux) { + mux.HandleFunc("GET /api/v1/pages/{id}/share", a.requireUser(func(w http.ResponseWriter, r *http.Request) { + link, ok, err := a.dir.GetShareLink(r.Context(), caller(r), r.PathValue("id")) + if err == nil && !ok { + writeJSON(w, http.StatusNotFound, map[string]string{"error": "not_found", "message": "sharing is off"}) + return + } + a.respond(w, link, err) + })) + mux.HandleFunc("PUT /api/v1/pages/{id}/share", a.requireUser(func(w http.ResponseWriter, r *http.Request) { + var in struct{ IncludeSubpages bool } + if !a.decode(w, r, &in) { + return + } + link, err := a.dir.SetShareLink(r.Context(), caller(r), r.PathValue("id"), in.IncludeSubpages) + a.respond(w, link, err) + })) + mux.HandleFunc("DELETE /api/v1/pages/{id}/share", a.requireUser(func(w http.ResponseWriter, r *http.Request) { + a.respondNoContent(w, a.dir.DeleteShareLink(r.Context(), caller(r), r.PathValue("id"))) + })) + + // Public reads through a link: no account needed, never indexed. + mux.HandleFunc("GET /api/v1/share/{token}", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("X-Robots-Tag", "noindex") + p, err := a.dir.GetSharedPage(r.Context(), r.PathValue("token"), "") + a.respond(w, p, err) + }) + mux.HandleFunc("GET /api/v1/share/{token}/pages/{pageId}", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("X-Robots-Tag", "noindex") + p, err := a.dir.GetSharedPage(r.Context(), r.PathValue("token"), r.PathValue("pageId")) + a.respond(w, p, err) + }) + mux.HandleFunc("GET /api/v1/share/{token}/files/{fileId}", func(w http.ResponseWriter, r *http.Request) { + f, err := a.dir.OpenSharedFile(r.Context(), r.PathValue("token"), r.PathValue("fileId"), r.Header) + if err != nil { + a.writeError(w, err) + return + } + w.Header().Set("X-Robots-Tag", "noindex") + serveFile(w, f) + }) +} diff --git a/Web-App/frontend/src/components/ShareDialog.vue b/Web-App/frontend/src/components/ShareDialog.vue new file mode 100644 index 0000000..6e001e3 --- /dev/null +++ b/Web-App/frontend/src/components/ShareDialog.vue @@ -0,0 +1,203 @@ + + + + diff --git a/Web-App/frontend/src/locales/en.json b/Web-App/frontend/src/locales/en.json index a920352..97a2e58 100644 --- a/Web-App/frontend/src/locales/en.json +++ b/Web-App/frontend/src/locales/en.json @@ -278,5 +278,21 @@ "search": { "placeholder": "Search pages…", "none": "Nothing found for \"{q}\"." + }, + "share": { + "button": "Share", + "title": "Share this page", + "access": "Who can open it", + "privateSpace": "Only the people and groups added to {space}, plus workspace admins.", + "openSpace": "Everyone in the workspace, as {role} in {space}, plus the people and groups below.", + "manageAccess": "Change access in the space settings", + "copyPageLink": "Copy page link", + "public": "Public link", + "publicToggle": "Anyone with the link can read", + "publicHint": "No account needed. Turning it off makes the link stop working.", + "subpages": "Include the pages inside it", + "editorsOnly": "Editors of this space can turn the public link on or off.", + "readOnly": "Shared page · read only", + "gone": "This link does not work any more. Ask the person who shared it for a new one." } } diff --git a/Web-App/frontend/src/locales/pl.json b/Web-App/frontend/src/locales/pl.json index 0d652b7..00c8997 100644 --- a/Web-App/frontend/src/locales/pl.json +++ b/Web-App/frontend/src/locales/pl.json @@ -278,5 +278,21 @@ "search": { "placeholder": "Szukaj stron…", "none": "Nic nie znaleziono dla „{q}”." + }, + "share": { + "button": "Udostępnij", + "title": "Udostępnij tę stronę", + "access": "Kto może ją otworzyć", + "privateSpace": "Tylko osoby i grupy dodane do {space} oraz administratorzy obszaru roboczego.", + "openSpace": "Każdy w obszarze roboczym, z rolą {role} w {space}, oraz osoby i grupy poniżej.", + "manageAccess": "Zmień dostęp w ustawieniach przestrzeni", + "copyPageLink": "Kopiuj link do strony", + "public": "Link publiczny", + "publicToggle": "Każdy z linkiem może czytać", + "publicHint": "Konto nie jest potrzebne. Wyłączenie sprawia, że link przestaje działać.", + "subpages": "Uwzględnij strony wewnątrz", + "editorsOnly": "Edytorzy tej przestrzeni mogą włączać i wyłączać link publiczny.", + "readOnly": "Udostępniona strona · tylko do odczytu", + "gone": "Ten link już nie działa. Poproś osobę, która go udostępniła, o nowy." } } diff --git a/Web-App/frontend/src/router.ts b/Web-App/frontend/src/router.ts index 47a3044..61f8943 100644 --- a/Web-App/frontend/src/router.ts +++ b/Web-App/frontend/src/router.ts @@ -89,6 +89,21 @@ export function createDocwellRouter(platform: Platform) { component: () => import("./views/auth/VerifyEmailView.vue"), meta: { public: true }, }, + // Pages shared by public link; readable without an account. + { + path: "/share/:token", + name: "share", + component: () => import("./views/ShareView.vue"), + props: true, + meta: { public: true }, + }, + { + path: "/share/:token/p/:page", + name: "share-page", + component: () => import("./views/ShareView.vue"), + props: true, + meta: { public: true }, + }, // Linked from invite and sign-in emails sent by docwell-server and PocketBase. { path: "/invite", diff --git a/Web-App/frontend/src/views/ShareView.vue b/Web-App/frontend/src/views/ShareView.vue new file mode 100644 index 0000000..47b3ac6 --- /dev/null +++ b/Web-App/frontend/src/views/ShareView.vue @@ -0,0 +1,110 @@ + + + + diff --git a/Web-App/frontend/src/views/page/PageView.vue b/Web-App/frontend/src/views/page/PageView.vue index bf72dc4..0d98796 100644 --- a/Web-App/frontend/src/views/page/PageView.vue +++ b/Web-App/frontend/src/views/page/PageView.vue @@ -9,6 +9,7 @@ import { useRouter } from "vue-router"; import { useApi, useToken } from "../../api"; import PageHistory from "../../components/PageHistory.vue"; import PageList from "../../components/PageList.vue"; +import ShareDialog from "../../components/ShareDialog.vue"; import { useFileHost } from "../../composables/useFiles"; import { saveBlob } from "../../lib/download"; import { useRuntimeConfig } from "../../platform"; @@ -34,6 +35,7 @@ const notFound = ref(false); const title = ref(""); const busy = ref(false); const historyOpen = ref(false); +const shareOpen = ref(false); // Attachments: images and files load through a short-lived file token. const files = useFileHost(() => page.value?.id ?? props.id); @@ -284,8 +286,12 @@ const purge = () => > {{ $t("page.history.title") }} + + {{ $t("share.button") }} + +
{{ $t("page.notFound") }} diff --git a/api/openapi.yaml b/api/openapi.yaml index 8b9cab2..5b73975 100644 --- a/api/openapi.yaml +++ b/api/openapi.yaml @@ -1443,6 +1443,98 @@ paths: "400": { $ref: "#/components/responses/Error" } "403": { $ref: "#/components/responses/Error" } "404": { $ref: "#/components/responses/Error" } + # Public share links: read access for anyone with the link. + /api/v1/pages/{id}/share: + parameters: + - { name: id, in: path, required: true, schema: { type: string } } + get: + operationId: getShareLink + tags: [sharing] + summary: The page's public link (404 when sharing is off) + security: [{ bearer: [] }] + responses: + "200": + description: OK + content: + application/json: + schema: { $ref: "#/components/schemas/ShareLink" } + "404": { $ref: "#/components/responses/Error" } + put: + operationId: setShareLink + tags: [sharing] + summary: Turn the public link on, or change what it covers (editor) + description: An existing link keeps its URL. + security: [{ bearer: [] }] + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + includeSubpages: { type: boolean } + responses: + "200": + description: OK + content: + application/json: + schema: { $ref: "#/components/schemas/ShareLink" } + "400": { $ref: "#/components/responses/Error" } + "403": { $ref: "#/components/responses/Error" } + "404": { $ref: "#/components/responses/Error" } + delete: + operationId: deleteShareLink + tags: [sharing] + summary: Turn the public link off; its URL stops working (editor) + security: [{ bearer: [] }] + responses: + "204": { description: Off } + "403": { $ref: "#/components/responses/Error" } + "404": { $ref: "#/components/responses/Error" } + /api/v1/share/{token}: + parameters: + - { name: token, in: path, required: true, schema: { type: string } } + get: + operationId: getSharedPage + tags: [sharing] + summary: Read a shared page (no account needed) + responses: + "200": + description: OK + content: + application/json: + schema: { $ref: "#/components/schemas/SharedPage" } + "404": { $ref: "#/components/responses/Error" } + /api/v1/share/{token}/pages/{pageId}: + parameters: + - { name: token, in: path, required: true, schema: { type: string } } + - { name: pageId, in: path, required: true, schema: { type: string } } + get: + operationId: getSharedSubpage + tags: [sharing] + summary: Read a page under the shared page, when the link includes subpages + responses: + "200": + description: OK + content: + application/json: + schema: { $ref: "#/components/schemas/SharedPage" } + "404": { $ref: "#/components/responses/Error" } + /api/v1/share/{token}/files/{fileId}: + parameters: + - { name: token, in: path, required: true, schema: { type: string } } + - { name: fileId, in: path, required: true, schema: { type: string } } + get: + operationId: downloadSharedFile + tags: [sharing] + summary: Download an attachment of a page readable through the link + responses: + "200": + description: The file + content: + application/octet-stream: + schema: { type: string, format: binary } + "404": { $ref: "#/components/responses/Error" } components: securitySchemes: bearer: @@ -1885,3 +1977,24 @@ components: properties: items: { type: array, items: { $ref: "#/components/schemas/SearchHit" } } total: { type: integer } + ShareLink: + type: object + required: [token, url, includeSubpages, created] + properties: + token: { type: string } + url: { type: string, description: The link on docwell-web (DOCWELL_WEB_URL/share/) } + includeSubpages: { type: boolean } + created: { type: string } + SharedPage: + type: object + required: [id, title, icon, updated, content, root, pages] + properties: + id: { type: string } + title: { type: string } + icon: { type: string } + updated: { type: string } + content: + oneOf: [{ type: object, additionalProperties: true }, { type: "null" }] + description: Tiptap JSON; files load from /api/v1/share/{token}/files/{fileId} + root: { $ref: "#/components/schemas/PageRef" } + pages: { type: array, items: { $ref: "#/components/schemas/PageNode" }, description: Pages under the shared page, in tree order (only when the link includes subpages) }