// SPDX-License-Identifier: AGPL-3.0-or-later package directory import ( "context" "fmt" "strings" "docwell/server/internal/apperr" "docwell/server/internal/store/pocketbase" ) // ---- Link previews for chat apps ------------------------------------------- // // When a Docwell link is posted in Slack, Teams, Mattermost and the like, // their bots fetch it to show a preview. docwell-web asks these lookups for // what to put in the page's meta tags (ADR-17). A share link always gets its // page's title and the start of its text, since anyone can read it anyway. // A private page gets only its title and workspace, and only when the // workspace turned URL previews on; pages restricted to some people never // get one. // PagePreview is what a chat app's link preview shows. type PagePreview struct { Title string `json:"title"` Description string `json:"description"` // Site is the workspace's name. Site string `json:"site"` } func previewTitle(p pageRecord) string { t := strings.TrimSpace(p.Title) if t == "" { t = "Untitled" } if p.Icon != "" { t = p.Icon + " " + t } return t } // GetPagePreview previews a private page for a link; it is not found unless // the page's workspace turned URL previews on. func (s *Service) GetPagePreview(ctx context.Context, pageID string) (PagePreview, error) { notFound := apperr.NotFound("no preview") p, err := s.getPage(ctx, pageID) if err != nil || p.DeletedAt != "" || p.Restricted { return PagePreview{}, notFound } ws, err := s.getWorkspace(ctx, p.Workspace) if err != nil || !ws.URLPreview { return PagePreview{}, notFound } return PagePreview{Title: previewTitle(p), Site: ws.Name}, nil } // GetSharePreview previews a page reached through a share link (pageID "" // for the shared page itself). func (s *Service) GetSharePreview(ctx context.Context, token, pageID string) (PagePreview, error) { link, p, _, err := s.sharedScope(ctx, token, pageID) if err != nil { return PagePreview{}, err } out := PagePreview{Title: previewTitle(p)} if ws, err := s.getWorkspace(ctx, link.Workspace); err == nil { out.Site = ws.Name } res, err := pocketbase.List[struct { ContentText string `json:"content_text"` }](ctx, s.pb, "pages", pocketbase.ListOptions{PerPage: 1, Filter: fmt.Sprintf("id = '%s'", p.ID), Fields: "content_text"}) if err == nil && len(res.Items) > 0 { out.Description = excerpt(res.Items[0].ContentText) } return out, nil }