// SPDX-License-Identifier: AGPL-3.0-or-later package directory import ( "context" "strings" "unicode/utf8" "docwell/server/internal/access" "docwell/server/internal/apperr" "docwell/server/internal/convert" "docwell/server/internal/store/pocketbase" ) // ---- What chat integrations (Slack) need from the directory ------------------ // ChatPage is a page as a chat app may show it: only pages every member of // the workspace can open (see OpenToMembers). type ChatPage struct { ID string Workspace string Title string Icon string SpaceName string URL string // Excerpt is the start of the page's text, one line. Excerpt string } // OpenToMembers returns the page when everyone in workspace wsID can open // it: it is not deleted, its space is open (not private, not a journal), // and neither it nor a page above it is restricted. Anything else is // reported as not shared, so a chat channel never shows what some members // of the workspace could not read. func (s *Service) OpenToMembers(ctx context.Context, wsID, pageID string) (ChatPage, bool) { if !validID(pageID) { return ChatPage{}, false } p, err := s.getPage(ctx, pageID) if err != nil || p.Workspace != wsID || p.DeletedAt != "" { return ChatPage{}, false } sp, err := pocketbase.Get[spaceRecord](ctx, s.pb, "spaces", p.Space) if err != nil || sp.Visibility != "open" || sp.JournalOwner != "" { return ChatPage{}, false } // The chain up to the space root; a cycle or a missing parent stops it. seen := map[string]bool{} for cur := p; ; { if cur.Restricted { return ChatPage{}, false } if cur.Parent == "" || seen[cur.Parent] { break } seen[cur.ID] = true next, err := s.getPage(ctx, cur.Parent) if err != nil { return ChatPage{}, false } cur = next } out := ChatPage{ ID: p.ID, Workspace: p.Workspace, Title: p.Title, Icon: p.Icon, SpaceName: sp.Name, URL: strings.TrimRight(s.mailer.WebURL, "/") + "/p/" + p.ID, } if doc, err := s.docs.Content(ctx, p.ID); err == nil { if node, err := convert.Parse(doc); err == nil { out.Excerpt = chatExcerpt(convert.Markdown(node, convert.Options{}), 240) } } return out, true } // chatExcerpt is the start of Markdown as one line of plain-ish text. func chatExcerpt(md string, n int) string { var parts []string for _, line := range strings.Split(md, "\n") { line = strings.TrimSpace(strings.TrimLeft(strings.TrimSpace(line), "#>*-+ ")) if line != "" && !strings.HasPrefix(line, "```") && !strings.HasPrefix(line, "|") { parts = append(parts, line) } if len(parts) > 6 { break } } s := strings.Join(strings.Fields(strings.Join(parts, " ")), " ") if utf8.RuneCountInString(s) <= n { return s } return strings.TrimSpace(string([]rune(s)[:n-1])) + "…" } // MemberByEmail is the member of workspace wsID with this email, as a caller // for work done on their behalf (a search from a chat app). func (s *Service) MemberByEmail(ctx context.Context, wsID, email string) (access.Caller, error) { u, err := s.findUserByEmail(ctx, email) if err != nil { return access.Caller{}, err } if u.Status == "disabled" { return access.Caller{}, apperr.Forbidden("the account is disabled") } c := u.Caller() if _, err := s.WorkspaceRole(ctx, c, wsID); err != nil { return access.Caller{}, err } return c, nil }