oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package equipo
2
3import (
4 "context"
5 "errors"
6 "net/http"
7
8 "amadan.net/oficina/equipo/internal/equipo/conversation"
9 "amadan.net/oficina/equipo/internal/equipo/kernel"
10 "amadan.net/oficina/equipo/internal/equipo/personapi"
11 "github.com/go-chi/chi/v5"
12 "gorm.io/gorm"
13)
14
15type PersonPrincipal = personapi.PersonPrincipal
16type VerifyPersonBearer = personapi.VerifyPersonBearer
17
18func WithPersonBearerVerifier(verify VerifyPersonBearer) StoreOption {
19 return func(s *Store) { s.personBearer = verify }
20}
21func (s *Store) PersonAPIHandler() http.Handler { return s.personAPIRouter() }
22func (s *Store) personAPIRouter() chi.Router {
23 verify := s.personBearer
24 if verify == nil {
25 verify = s.personDevices().Verify
26 }
27 auth := personapi.Authenticator{LookupMember: s.Member, NotFound: ErrNotFound, Origin: s.personOrigin, Cookie: s.personCookie, Bearer: verify, Forbidden: ErrForbidden, Member: func(r *http.Request, p PersonPrincipal) error {
28 m, err := s.Member(r.Context(), p.Address)
29 if errors.Is(err, ErrNotFound) {
30 return ErrForbidden
31 }
32 if err != nil {
33 return err
34 }
35 if !m.Active || p.SessionEpoch < m.SessionEpoch {
36 return ErrForbidden
37 }
38 return nil
39 }}
40 h := &personapi.Handler{App: s, NotificationActions: s.Chat().NotificationActions, CursorCodec: kernel.CursorCodec{Key: s.personKey}, Authenticate: auth.Authenticate, AuthenticateRoute: auth.AuthenticateRoute, Invalid: ErrInvalid, Forbidden: ErrForbidden, NotFound: ErrNotFound, Conflict: ErrConflict, Unavailable: ErrUnavailable, CursorExpired: ErrCursorExpired}
41 r := h.Router()
42 // The thin task routes share three paths with the old engine's; until
43 // the slice 7 sweep, those keep serving the old tasks.
44 old := map[string]http.Handler{}
45 _ = chi.Walk(r, func(method, route string, handler http.Handler, _ ...func(http.Handler) http.Handler) error {
46 old[method+" "+route] = handler
47 return nil
48 })
49 c := s.chatHTTP(auth.Authenticate, h.Failure)
50 c.Old = old
51 c.Mount(r)
52 r.Post("/tasks/{id}/preview-pass", s.mintPreviewHTTP(auth))
53 return r
54}
55func (s *Store) personAPIReader(a Actor) *conversation.Application {
56 return s.coordinatorApplication().ReadOnly(func(ctx context.Context, fn func(*gorm.DB) error) error {
57 return s.readTeam(ctx, a, func(tx *gorm.DB, _ Team) error { return fn(tx) })
58 }, kernel.CursorCodec{Key: s.personKey})
59}
60
61func (s *Store) PersonAPIRead(ctx context.Context, a Actor, op, id, cursor string, limit int) (personapi.Result, error) {
62 if personapi.ExtendedRead(op) {
63 return s.nativeAPI().Read(ctx, a, op, id)
64 }
65 reader := personapi.Reader{Conversation: s.personAPIReader(a), TaskView: s.TaskView, Journey: s.Journey, NotFound: ErrNotFound, ReadTeam: func(ctx context.Context, a Actor, fn func(*gorm.DB, personapi.Team) error) error {
66 return s.readTeam(ctx, a, func(tx *gorm.DB, t Team) error {
67 return fn(tx, personapi.Team{ID: t.ID, Name: t.Name, RosterEpoch: t.RosterEpoch})
68 })
69 }}
70 return reader.Read(ctx, a, op, id, cursor, limit)
71}
72func (s *Store) PersonAPICancel(ctx context.Context, a Actor, id string, m Mutation) (personapi.Ack, error) {
73 _, err := s.cancelTask(ctx, a, id, &m)
74 return personapi.Ack{Revision: m.Revision + 1}, err
75}
76
77func (s *Store) currentPersonAPI(tx *gorm.DB, ctx context.Context) error {
78 p, ok := personapi.PrincipalFrom(ctx)
79 if !ok || p.Address == "" {
80 return nil
81 }
82 if p.CredentialID != "" && s.personBearer == nil {
83 if _, err := s.personDevices().VerifyAt(tx, p.CredentialID, ""); err != nil {
84 return err
85 }
86 }
87 member, err := currentRunnerMember(tx, p.Address)
88 if err != nil {
89 return err
90 }
91 if p.SessionEpoch < member.SessionEpoch {
92 return ErrForbidden
93 }
94 return nil
95}
96