// SPDX-License-Identifier: AGPL-3.0-or-later package httpapi import ( "context" "net/http" "docwell/server/internal/access" "docwell/server/internal/apperr" "docwell/server/internal/directory" "docwell/server/internal/unfurl" ) // LinkPreviewer reads a web page's title and picture for link cards. type LinkPreviewer interface { Preview(ctx context.Context, url string) (unfurl.Preview, error) } // LinkService is the part of directory.Service for backlinks and templates. type LinkService interface { Backlinks(ctx context.Context, c access.Caller, pageID string) ([]directory.PageItem, error) ListTemplates(ctx context.Context, c access.Caller, wsID string) ([]directory.Template, error) ApplyTemplate(ctx context.Context, c access.Caller, pageID, templateID string) (directory.Page, error) } func (a *api) linkRoutes(mux *routeMux) { mux.HandleFunc("GET /api/v1/link-preview", a.requireUser(func(w http.ResponseWriter, r *http.Request) { if a.previews == nil { a.respond(w, nil, apperr.NotFound("link previews are off")) return } // With a workspace, its tokens reach private GitHub, Linear and Figma items. if ws := r.URL.Query().Get("workspace"); ws != "" && a.wsIntegrations != nil { p, err := a.wsIntegrations.Preview(r.Context(), caller(r), ws, r.URL.Query().Get("url")) a.respond(w, p, err) return } p, err := a.previews.Preview(r.Context(), r.URL.Query().Get("url")) a.respond(w, p, err) })) mux.HandleFunc("GET /api/v1/pages/{id}/backlinks", a.requireUser(func(w http.ResponseWriter, r *http.Request) { list, err := a.dir.Backlinks(r.Context(), caller(r), r.PathValue("id")) a.respond(w, list, err) })) mux.HandleFunc("GET /api/v1/workspaces/{id}/templates", a.requireUser(func(w http.ResponseWriter, r *http.Request) { list, err := a.dir.ListTemplates(r.Context(), caller(r), r.PathValue("id")) a.respond(w, list, err) })) mux.HandleFunc("POST /api/v1/pages/{id}/apply-template", a.requireUser(func(w http.ResponseWriter, r *http.Request) { var in struct{ Template string } if !a.decode(w, r, &in) { return } p, err := a.dir.ApplyTemplate(r.Context(), caller(r), r.PathValue("id"), in.Template) a.respond(w, p, err) })) }