| 1 | package slides |
| 2 | |
| 3 | // Team mode's per-request standing. With a.team nil nothing here runs: |
| 4 | // every helper below returns today's answer unchanged. |
| 5 | // |
| 6 | // Standing itself is home/teammode's Access. Identity is resolved from |
| 7 | // the actual session (a local address, an account link, or a Home-only |
| 8 | // user); the roster is asked about the Home subject, if there is one; |
| 9 | // and the answer is one of three: |
| 10 | // |
| 11 | // - Member: the team rung of visibleDecks — the person's own decks and |
| 12 | // every deck on the team rung — and ownership of their own decks. |
| 13 | // The roster's role says owner or member, and only the roster's. |
| 14 | // - NotMember: a guest. Only decks a grant names them on. |
| 15 | // - Unknown: the roster could not be read within its lease. Grants |
| 16 | // still work, and the deck list shows them with a "partial" notice |
| 17 | // (Paul, 2026-10-28); anything else that would have needed standing |
| 18 | // answers a retryable 503 rather than a 404 that would claim the |
| 19 | // deck is not theirs. |
| 20 | // |
| 21 | // A Member decision carries a lease. teamAccess.state() checks it every |
| 22 | // time it is asked, and a write checks it again inside its own |
| 23 | // transaction, before and after the write (team_guard.go). Streams |
| 24 | // re-decide before each delivery and at the lease (teamStreamAccess). |
| 25 | |
| 26 | import ( |
| 27 | "context" |
| 28 | "errors" |
| 29 | "net/http" |
| 30 | "strconv" |
| 31 | "strings" |
| 32 | "time" |
| 33 | |
| 34 | "amadan.net/oficina/home/connect" |
| 35 | "amadan.net/oficina/home/connect/linkstore" |
| 36 | "amadan.net/oficina/home/roster" |
| 37 | "amadan.net/oficina/home/teammode" |
| 38 | "amadan.net/rastrillo/rastrillo/sessions" |
| 39 | "gorm.io/gorm" |
| 40 | |
| 41 | "amadan.net/oficina/slides/internal/slides/teamstate" |
| 42 | ) |
| 43 | |
| 44 | // teamAccess is one request's standing for one user: teammode's Access |
| 45 | // for their Home subject (Member, NotMember or Unknown, with the lease |
| 46 | // re-checked and Unknown latched every time it is asked). Never stored; |
| 47 | // a stream asks for a new one before each delivery. A nil teamAccess, |
| 48 | // or one with a nil Access, is Unknown. |
| 49 | type teamAccess struct { |
| 50 | *teammode.Access |
| 51 | } |
| 52 | |
| 53 | var errNoIdentity = errors.New("slides: no team identity") |
| 54 | |
| 55 | func (t *teamAccess) state() roster.State { |
| 56 | if t == nil { |
| 57 | return roster.Unknown |
| 58 | } |
| 59 | return t.State() |
| 60 | } |
| 61 | |
| 62 | func (t *teamAccess) access() *teammode.Access { |
| 63 | if t == nil { |
| 64 | return nil |
| 65 | } |
| 66 | return t.Access |
| 67 | } |
| 68 | |
| 69 | // rosterOwner reports whether the roster says this person is one of the |
| 70 | // team's owners, now. "" (anything but Member) is not. |
| 71 | func (t *teamAccess) rosterOwner() bool { |
| 72 | return t != nil && t.Role() == "owner" |
| 73 | } |
| 74 | |
| 75 | // member reports whether u stands on the team rung: always outside team |
| 76 | // mode (the rung was never the roster's), and only while the roster says |
| 77 | // Member inside it. |
| 78 | func (a *app) member(u User) bool { |
| 79 | return a.team == nil || u.team.state() == roster.Member |
| 80 | } |
| 81 | |
| 82 | // teamIdentify resolves a session to its users row and the Home subject |
| 83 | // the roster knows them by ("" when there is none: a local account with |
| 84 | // no link, which is no team standing). A local session's row is created |
| 85 | // on first sight, exactly as currentUser always has; nothing else is. |
| 86 | func (a *app) teamIdentify(ctx context.Context, s sessions.Session) (User, string, error) { |
| 87 | switch s.Method { |
| 88 | case connect.HomeMethod: |
| 89 | if a.home == nil { |
| 90 | return User{}, "", errNoIdentity |
| 91 | } |
| 92 | p, err := a.home.Resolve(ctx, s.Subject) |
| 93 | if errors.Is(err, linkstore.ErrStorage) { |
| 94 | return User{}, "", errTeamUnavailable |
| 95 | } |
| 96 | if err != nil { |
| 97 | return User{}, "", errNoIdentity |
| 98 | } |
| 99 | var u User |
| 100 | if err := a.db.G.WithContext(ctx).Take(&u, p.ID).Error; err != nil { |
| 101 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 102 | return User{}, "", errNoIdentity |
| 103 | } |
| 104 | return User{}, "", errTeamUnavailable |
| 105 | } |
| 106 | return u, s.Subject, nil |
| 107 | case "magiclink", "keymail": |
| 108 | // No sample deck here: a local account made in team mode has no |
| 109 | // link yet, so it is a guest, and guests create nothing |
| 110 | // (teamMemberOnly). A Home-only member gets theirs at sign-in |
| 111 | // (team_home_only.go). |
| 112 | u, _, err := ensureUser(ctx, a.db.G, s.Subject) |
| 113 | if err != nil { |
| 114 | return User{}, "", errTeamUnavailable |
| 115 | } |
| 116 | subject, err := a.homeSubjectOf(ctx, u.ID) |
| 117 | if err != nil { |
| 118 | return User{}, "", err |
| 119 | } |
| 120 | return u, subject, nil |
| 121 | } |
| 122 | return User{}, "", errNoIdentity |
| 123 | } |
| 124 | |
| 125 | // homeSubjectOf is the Home subject the roster knows a user by: a |
| 126 | // Home-only row or an account link, "" for neither. |
| 127 | func (a *app) homeSubjectOf(ctx context.Context, id int64) (string, error) { |
| 128 | subject, err := teamstate.HomeSubject(ctx, a.team.db, a.team.issuer(), id) |
| 129 | switch { |
| 130 | case err == nil: |
| 131 | return subject, nil |
| 132 | case errors.Is(err, teamstate.ErrRefused): |
| 133 | return "", nil |
| 134 | } |
| 135 | return "", errTeamUnavailable |
| 136 | } |
| 137 | |
| 138 | // teamCurrentUser is currentUser in team mode: the session's user with |
| 139 | // the roster's standing attached. It signs nobody out for lacking |
| 140 | // standing — a removed member keeps a session that reaches only what was |
| 141 | // shared with them — but a session that resolves to nobody is ended as |
| 142 | // before, and one that cannot be resolved now is a retryable 503. |
| 143 | func (a *app) teamCurrentUser(w http.ResponseWriter, r *http.Request, s sessions.Session) (User, sessionOutcome) { |
| 144 | u, subject, err := a.teamIdentify(r.Context(), s) |
| 145 | switch { |
| 146 | case errors.Is(err, errTeamUnavailable): |
| 147 | a.teamUnavailable(w, r) |
| 148 | return User{}, sessionOffboarded |
| 149 | case err != nil && s.Method == connect.HomeMethod: |
| 150 | a.revokeHome(w, r, s.Subject) |
| 151 | return User{}, sessionOffboarded |
| 152 | case err != nil: |
| 153 | return User{}, sessionNone |
| 154 | } |
| 155 | u.team = &teamAccess{Access: a.team.Access(r.Context(), subject)} |
| 156 | return u, sessionOK |
| 157 | } |
| 158 | |
| 159 | // withStanding asks the roster about somebody other than the viewer — |
| 160 | // a deck's owner (team_owner.go), a presenter (team_present.go), an |
| 161 | // agent's operator — so what it says about them is what visibleDecks |
| 162 | // would decide for them. |
| 163 | func (a *app) withStanding(ctx context.Context, u User) User { |
| 164 | if a.team == nil { |
| 165 | return u |
| 166 | } |
| 167 | subject, err := a.homeSubjectOf(ctx, u.ID) |
| 168 | if err != nil { |
| 169 | u.team = nil // Unknown |
| 170 | return u |
| 171 | } |
| 172 | u.team = &teamAccess{Access: a.team.Access(ctx, subject)} |
| 173 | return u |
| 174 | } |
| 175 | |
| 176 | // streamDecision is what one stream delivery may do: deliver, hold (the |
| 177 | // stream stays open and delivers nothing, because standing cannot be read |
| 178 | // right now), or end (a definite "no"). |
| 179 | type streamDecision int |
| 180 | |
| 181 | const ( |
| 182 | streamDelivers streamDecision = iota |
| 183 | streamHolds |
| 184 | streamEnds |
| 185 | ) |
| 186 | |
| 187 | // streamHoldRetry is how often a held stream re-asks: the roster's own |
| 188 | // retry interval. A variable only so a test need not wait it out. |
| 189 | var streamHoldRetry = roster.RetryAfter |
| 190 | |
| 191 | // teamStreamAccess decides one deck-stream delivery afresh — session, |
| 192 | // user, roster, reach — and says how long a standing-backed answer may |
| 193 | // stand before it must be asked again: the Member lease's remaining |
| 194 | // time, or 0 when nothing expires on a timer (the answer rested on a |
| 195 | // grant). Unknown standing that a grant does not cover holds rather than |
| 196 | // ends: nothing is delivered, and nothing is decided against the person |
| 197 | // either. Team mode only. |
| 198 | func (a *app) teamStreamAccess(r *http.Request, deckID int64) (streamDecision, time.Duration) { |
| 199 | s, ok := sessions.Current(r) |
| 200 | if !ok { |
| 201 | return streamEnds, 0 |
| 202 | } |
| 203 | u, subject, err := a.teamIdentify(r.Context(), s) |
| 204 | switch { |
| 205 | case errors.Is(err, errTeamUnavailable): |
| 206 | return streamHolds, 0 |
| 207 | case err != nil: |
| 208 | return streamEnds, 0 |
| 209 | } |
| 210 | u.team = &teamAccess{Access: a.team.Access(r.Context(), subject)} |
| 211 | var n int64 |
| 212 | if err := a.visibleDecks(r.Context(), u).Where("decks.id = ?", deckID).Count(&n).Error; err != nil { |
| 213 | return streamHolds, 0 |
| 214 | } |
| 215 | // Asked after the reach, of the same Access: State latches, so a |
| 216 | // lease that lapsed (or was contended) after the query was built |
| 217 | // reads Unknown here, and then the deck may have been reached |
| 218 | // through the team rung that no longer stands. Without a lease only |
| 219 | // a grant holds, and nothing then expires on a timer. |
| 220 | state := u.team.state() |
| 221 | if state != roster.Member { |
| 222 | switch { |
| 223 | case a.grantedTo(r.Context(), u, Deck{ID: deckID}): |
| 224 | return streamDelivers, 0 |
| 225 | case state == roster.Unknown: |
| 226 | return streamHolds, 0 |
| 227 | } |
| 228 | return streamEnds, 0 |
| 229 | } |
| 230 | if n == 0 { |
| 231 | return streamEnds, 0 |
| 232 | } |
| 233 | renew := u.team.Remaining() |
| 234 | if renew <= 0 { |
| 235 | renew = time.Second |
| 236 | } |
| 237 | return streamDelivers, renew |
| 238 | } |
| 239 | |
| 240 | // teamJobOwner is the jobs registry's owner key in team mode. A |
| 241 | // Home-only user has no address to key on, so every team-mode session |
| 242 | // and every agent's operator keys on the user instead. |
| 243 | func teamJobOwner(id int64) string { return "slides-user:" + strconv.FormatInt(id, 10) } |
| 244 | |
| 245 | // teamLabel is how an addressless (Home-only) user is shown where an |
| 246 | // address would be: presence, a deck's last change, the account menu. |
| 247 | const teamLabel = "Oficina account" |
| 248 | |
| 249 | // homeOnlyLabel is teamLabel and the last four characters of the person |
| 250 | // ref ("Oficina account …k7qz"), as Home's Team page shows a ref, so two |
| 251 | // Home-only people are told apart. |
| 252 | func homeOnlyLabel(subject string) string { |
| 253 | if len(subject) <= 4 { |
| 254 | return teamLabel |
| 255 | } |
| 256 | return teamLabel + " …" + subject[len(subject)-4:] |
| 257 | } |
| 258 | |
| 259 | // label is how a person is shown: their name, else their address, else |
| 260 | // (only a team-mode Home-only user has neither) the Home-only label for |
| 261 | // subject. Outside team mode this is exactly the old Name-then-Email. |
| 262 | func (u User) label(subject string) string { |
| 263 | if u.Name != "" { |
| 264 | return u.Name |
| 265 | } |
| 266 | if strings.TrimSpace(u.Email) != "" || subject == "" { |
| 267 | return u.Email |
| 268 | } |
| 269 | return homeOnlyLabel(subject) |
| 270 | } |
| 271 | |
| 272 | // address is where a person's address is shown (an agent's principal): |
| 273 | // the address, or for a Home-only user, who has none, the Home-only |
| 274 | // label for the subject their own Access was asked about. Outside team |
| 275 | // mode it is exactly Email. |
| 276 | func (u User) address() string { |
| 277 | if u.Email != "" || u.team == nil || u.team.Access == nil { |
| 278 | return u.Email |
| 279 | } |
| 280 | return homeOnlyLabel(u.team.Subject()) |
| 281 | } |
| 282 | |
| 283 | // viewerLabel is u.label with the subject the request's own Access was |
| 284 | // asked about. |
| 285 | func (u User) viewerLabel() string { |
| 286 | if u.Email != "" || u.team == nil || u.team.Access == nil { |
| 287 | return u.label("") |
| 288 | } |
| 289 | return u.label(u.team.Subject()) |
| 290 | } |
| 291 | |
| 292 | // homeOnlySubjects maps each Home-only user to its subject, for labels on |
| 293 | // pages that name people without asking the roster. Nil outside team |
| 294 | // mode or on a read failure, which only costs the suffix. |
| 295 | func (a *app) homeOnlySubjects(ctx context.Context) map[int64]string { |
| 296 | if a.team == nil { |
| 297 | return nil |
| 298 | } |
| 299 | out, err := teamstate.HomeSubjects(ctx, a.team.db, a.team.issuer()) |
| 300 | if err != nil { |
| 301 | return nil |
| 302 | } |
| 303 | return out |
| 304 | } |
| 305 | |