Files
Docwell/Server/internal/access/spaces.go
T
tajniak81andClaude Opus 5.5 2b78c167e2 Add comment threads, reactions, notifications and page watching
Comments are now threads: replies hang off a thread's first comment,
which alone points at text and can be resolved. Authors can edit their
own comments, and anyone who can comment can react with an emoji.
Resolved threads stop lighting text in the editor and fold away in the
panel. Comments can @mention people.

People watch the pages they create or comment on, and can watch or mute
a page from its menu. Muting is remembered. The inbox collects comments
on watched pages, replies in your threads, mentions (in comments and in
page text), pages shared with you and resolved threads. It shows an
unread badge in the sidebar, polled every minute, and links straight to
the thread. Nobody hears about their own actions or about pages they
cannot open, and a notification hides its text once access is lost.

Unread notifications are emailed as a digest every few minutes unless
the person turns that off. Read ones are pruned after 90 days. The jobs
registry can now run jobs on a timer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 22:27:55 +01:00

197 lines
6.0 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package access
import "docwell/server/internal/apperr"
// ---- Spaces and pages ------------------------------------------------------
//
// Space rights cascade from the workspace role:
//
// workspace owner/admin admin of every space in the workspace
// member explicit space role, else the space's default role in open spaces
// guest explicit space role only (guests never see open spaces by default)
//
// Space roles: admin > editor > commenter > viewer. Pages inherit their space's rights.
// Space roles stored in space_members.role.
const (
SpaceAdmin = "admin"
SpaceEditor = "editor"
SpaceCommenter = "commenter"
SpaceViewer = "viewer"
)
// Space visibilities stored in spaces.visibility.
const (
SpaceOpen = "open"
SpacePrivate = "private"
)
var spaceRank = map[string]int{SpaceViewer: 1, SpaceCommenter: 2, SpaceEditor: 3, SpaceAdmin: 4}
// ValidSpaceRole reports whether role is a space role.
func ValidSpaceRole(role string) bool { return spaceRank[role] > 0 }
// ValidDefaultRole reports whether role can be an open space's default role.
func ValidDefaultRole(role string) bool { return ValidSpaceRole(role) && role != SpaceAdmin }
// HigherSpaceRole returns the higher of two space roles ("" ranks lowest).
func HigherSpaceRole(a, b string) string {
if spaceRank[b] > spaceRank[a] {
return b
}
return a
}
// SpaceAtLeast reports whether space role has is at or above need.
func SpaceAtLeast(has, need string) bool { return has != "" && spaceRank[has] >= spaceRank[need] }
// SpaceRole is the effective role in a space for someone with workspace role
// wsRole (already resolved by WorkspaceRole) and explicit space role
// memberRole ("" when not a space member; the highest of the person's own
// and their groups' grants, see HigherSpaceRole). "" = no access.
func SpaceRole(wsRole, visibility, defaultRole, memberRole string) string {
switch {
case wsRole == "":
return ""
case AtLeast(wsRole, WorkspaceAdmin):
return SpaceAdmin
case ValidSpaceRole(memberRole):
return memberRole
case visibility == SpaceOpen && wsRole != WorkspaceGuest:
if ValidDefaultRole(defaultRole) {
return defaultRole
}
return SpaceEditor
}
return ""
}
// CreateSpace and DeleteSpace need workspace admin: spaces are the workspace's structure.
func CreateSpace(wsRole string) error {
if !AtLeast(wsRole, WorkspaceAdmin) {
return apperr.Forbidden("workspace admin role required")
}
return nil
}
func DeleteSpace(wsRole string) error { return CreateSpace(wsRole) }
// ManageSpace (settings and members) needs space admin.
func ManageSpace(spaceRole string) error {
if !SpaceAtLeast(spaceRole, SpaceAdmin) {
return apperr.Forbidden("space admin role required")
}
return nil
}
// ViewSpace needs any access; a space someone cannot see does not exist for them.
func ViewSpace(spaceRole string) error {
if spaceRole == "" {
return apperr.NotFound("space not found")
}
return nil
}
// EditPages (create, rename, move, trash, restore) needs editor.
func EditPages(spaceRole string) error {
if err := ViewSpace(spaceRole); err != nil {
return err
}
if !SpaceAtLeast(spaceRole, SpaceEditor) {
return apperr.Forbidden("editor role required")
}
return nil
}
// CommentPages (writing comments) needs commenter.
func CommentPages(spaceRole string) error {
if err := ViewSpace(spaceRole); err != nil {
return err
}
if !SpaceAtLeast(spaceRole, SpaceCommenter) {
return apperr.Forbidden("commenter role required")
}
return nil
}
// DeleteComment: authors delete their own comments, space admins any.
func DeleteComment(actorID, authorID, spaceRole string) error {
if actorID != "" && actorID == authorID {
return nil
}
if !SpaceAtLeast(spaceRole, SpaceAdmin) {
return apperr.Forbidden("only the author or a space admin can delete this comment")
}
return nil
}
// PurgePage (permanent delete from the trash) needs space admin.
func PurgePage(spaceRole string) error {
if err := ViewSpace(spaceRole); err != nil {
return err
}
return ManageSpace(spaceRole)
}
// SetSpaceMember checks granting role to someone whose workspace role is
// targetWsRole. The target must be able to open the workspace; workspace
// admins are already space admins everywhere, so they are not listed.
func SetSpaceMember(actorRole, targetWsRole, role string) error {
if !ValidSpaceRole(role) {
return apperr.Invalid("role must be admin, editor, commenter or viewer")
}
if err := ManageSpace(actorRole); err != nil {
return err
}
if targetWsRole == "" {
return apperr.Invalid("that user is not a member of this workspace")
}
if AtLeast(targetWsRole, WorkspaceAdmin) {
return apperr.Invalid("workspace admins already manage every space")
}
return nil
}
// SetSpaceGroup checks granting role in a space to a group of the space's
// workspace.
func SetSpaceGroup(actorRole, role string) error {
if !ValidSpaceRole(role) {
return apperr.Invalid("role must be admin, editor, commenter or viewer")
}
return ManageSpace(actorRole)
}
// ---- Groups ----------------------------------------------------------------
// ManageGroups (create, rename, delete, change members) needs workspace admin.
func ManageGroups(wsRole string) error {
if !AtLeast(wsRole, WorkspaceAdmin) {
return apperr.Forbidden("workspace admin role required")
}
return nil
}
// ViewGroups needs workspace membership above guest: guests see only what
// was shared with them, not the workspace's structure.
func ViewGroups(wsRole string) error {
if !AtLeast(wsRole, WorkspaceMember) {
return apperr.Forbidden("workspace member role required")
}
return nil
}
// EditComment: only the author changes what they wrote.
func EditComment(actorID, authorID string) error {
if actorID == "" || actorID != authorID {
return apperr.Forbidden("only the author can edit this comment")
}
return nil
}
// ResolveComment and ReactToComment need commenter, like writing one.
func ResolveComment(pageRole string) error { return CommentPages(pageRole) }
func ReactToComment(pageRole string) error { return CommentPages(pageRole) }