oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1package docs
2
3import (
4 "context"
5 "net/http"
6
7 "amadan.net/oficina/home/roster"
8 "amadan.net/oficina/home/teammode"
9 "amadan.net/rastrillo/rastrillo"
10 "gorm.io/gorm"
11)
12
13// deletePage is GET /d/{sqid}/delete's view data: the confirmation File
14// → Delete… opens. A page of its own rather than a dialog, the way Share…
15// is, so it works with no JavaScript and the destructive POST is only
16// ever one deliberate button away.
17type deletePage struct {
18 Frame Frame
19 Title string
20 Heading string
21 DocTitle string
22 Sqid string
23 Thumb thumbView
24 Body string
25 Confirm string
26 Cancel string
27}
28
29// deleteGet serves GET /d/{sqid}/delete. Owner-only, answering 404 to
30// anybody else exactly as the share page does: deleting is the owner's,
31// a writer may edit a document and still may not remove it.
32func (h *handlers) deleteGet(w http.ResponseWriter, r *http.Request) {
33 if !h.requireMemberRequest(w, r, true) {
34 return
35 }
36 owner, err := h.owner(r)
37 if err != nil {
38 h.logger.Error("docs: resolve owner for delete", "err", err)
39 h.fail(w, r, h.internalFailureStatus(), failStatus)
40 return
41 }
42 doc, err := h.store.Get(r.Context(), owner, r.PathValue("sqid"))
43 if err != nil {
44 h.ownerLookupFailure(w, r, err)
45 return
46 }
47 page := deletePage{
48 Title: rastrillo.T(r, "docs.delete.title"),
49 Heading: rastrillo.Tf(r, "docs.delete.heading", "title", doc.Title),
50 DocTitle: doc.Title,
51 Sqid: doc.Sqid,
52 Thumb: h.thumbnailer(r.Context())(doc),
53 Body: rastrillo.T(r, "docs.delete.body"),
54 Confirm: rastrillo.T(r, "docs.delete.confirm"),
55 Cancel: rastrillo.T(r, "docs.delete.cancel"),
56 }
57 page.Frame = h.frame(r, frameOptions{titleParts: []string{page.Title, doc.Title}, context: doc.Title})
58 if err := h.render.page(w, r, "delete", page); err != nil {
59 h.logger.Error("docs: render delete", "sqid", doc.Sqid, "err", err)
60 h.fail(w, r, h.internalFailureStatus(), failStatus)
61 }
62}
63
64// deletePost serves POST /d/{sqid}/delete: soft-delete the document
65// (Store.Delete) and go back to the list, where it is no longer listed.
66// Owner- and member-only, 404 to anybody else; a document already
67// deleted is a 404 too, since Get no longer finds it.
68//
69// Anybody else with the document open at that moment is not told: their
70// next save is refused with the 404 every write to a deleted document
71// gets (deleted_at IS NULL in each write's check), which save-queue.mjs
72// takes as permanent and reports as a failed save. A live "this
73// document was deleted" notice is not built; the suite feed already
74// carries the gone row one would need.
75func (h *handlers) deletePost(w http.ResponseWriter, r *http.Request) {
76 if !h.requireMemberRequest(w, r, true) {
77 return
78 }
79 owner, err := h.owner(r)
80 if err != nil {
81 h.logger.Error("docs: resolve owner for delete", "err", err)
82 h.fail(w, r, h.internalFailureStatus(), failStatus)
83 return
84 }
85 if err := h.store.Delete(r.Context(), owner, r.PathValue("sqid")); err != nil {
86 h.ownerLookupFailureOrStatus(w, r, err)
87 return
88 }
89 // The Mac and iPhone apps' Delete… (spec 2026-11-22-native-redesign
90 // §1): {"ok":true} once Store.Delete has committed, and nothing is
91 // read again (a re-read would find no document and answer 404 for a
92 // delete that happened). The client takes only this answer, or a 404
93 // from its own GET of the document afterwards, as deleted; an older
94 // server's 303 reaches it as "not JSON" and it checks with that GET,
95 // so a client released before this deploys is still safe.
96 //
97 // Written here, not with nativeJSON: in team mode its guarded write
98 // (team_emission.go) checks every document the request read again
99 // before sending, and this one is gone by design, so it would answer
100 // 404 for a delete that committed (TestTeamNativeDelete). The answer
101 // says nothing about the document, and the delete itself was
102 // authorized inside teammode.Guard before and after the write, so
103 // nothing is left to check; the browser's 303 is written the same way.
104 if wantsNative(r) {
105 w.Header().Set("Content-Type", "application/json")
106 w.Header().Set("Cache-Control", "no-store")
107 w.Header().Add("Vary", "Accept")
108 w.WriteHeader(http.StatusOK)
109 _, _ = w.Write([]byte(`{"ok":true}`))
110 return
111 }
112 http.Redirect(w, r, "/", http.StatusSeeOther)
113}
114
115// Delete soft-deletes one of owner's documents (spec §4.1: deleted_at;
116// a purge verb is separate and still to come) and announces it gone on
117// the suite feed, in one transaction under ownerMutation's guard, so a
118// person who is not the owner, or who lost membership mid-request,
119// deletes nothing.
120func (s *Store) Delete(ctx context.Context, owner int64, sqid string) error {
121 doc, err := s.Get(ctx, owner, sqid)
122 if err != nil {
123 return err
124 }
125 return s.ownerDeletion(ctx, owner, doc.ID)
126}
127
128// ownerDeletion is ownerMutation for the one write that ends a
129// document's life. teammode.Guard authorizes before the write and again
130// after it, and ownerMutation's check (checkPermission) only finds live
131// documents, so after a delete it finds nothing and the delete rolls
132// itself back. Here both checks ask only whether the actor holds owner
133// standing over the row, deleted or not: the same rule
134// (permissionScope), without the liveness the write is there to end.
135func (s *Store) ownerDeletion(ctx context.Context, owner, docID int64) error {
136 defer s.warm.drop(ctx, docID) // warm.go, rule 6
137 actor := s.ownerActor(ctx, owner)
138 return s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
139 authorize := func(roster.State) error {
140 var doc Document
141 err := s.permissionScope(ctx, tx, actor, permissionOwner).Where("documents.id = ?", docID).First(&doc).Error
142 return s.permissionError(ctx, actor, err)
143 }
144 return teammode.Guard(ctx, authorize, func() error {
145 return s.permissionError(ctx, actor, softDelete(tx, docID))
146 })
147 })
148}
149