Move pages to and from the vault
A page, including a journal day, can be moved into the unlocked vault with its id kept. Links to it elsewhere become [[locked:id]], shown as Locked Page until the vault is unlocked, so its title is kept only in the vault. Move out reverses it. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c08a8b8718
commit
994927c704
10 files changed
+443
-14
No files matched your search
@@ -289,3 +289,83 @@ func (s *Server) vaultLock(w http.ResponseWriter, r *http.Request, p principal)
|
||||
s.lockVault(w, r)
|
||||
writeJSON(w, map[string]bool{"exists": true, "unlocked": false})
|
||||
}
|
||||
|
||||
// moveToVault takes a page out of the ordinary notebook and puts it in the vault, which must be unlocked.
|
||||
// Links to it elsewhere become [[locked:id]], so the page's title stays in the vault only.
|
||||
func (s *Server) moveToVault(w http.ResponseWriter, r *http.Request, p principal) {
|
||||
c := s.cfg.Get()
|
||||
_, vst, ok := s.openVault(c, r, p.Sub)
|
||||
if !ok {
|
||||
failCode(w, http.StatusLocked, "unlock the vault first", "locked")
|
||||
return
|
||||
}
|
||||
id := r.PathValue("id")
|
||||
n, err := p.st.Get(id)
|
||||
if err == nil && n.Trashed {
|
||||
err = store.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
storeFail(w, err)
|
||||
return
|
||||
}
|
||||
if _, err := vst.Import(n); err != nil {
|
||||
if errors.Is(err, store.ErrExists) {
|
||||
failCode(w, http.StatusConflict, existsMessage(n, "your vault"), "exists")
|
||||
return
|
||||
}
|
||||
storeFail(w, err)
|
||||
return
|
||||
}
|
||||
if _, err := p.st.Detach(id); err != nil {
|
||||
vst.Discard(id) // the page stays where it was
|
||||
storeFail(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, map[string]string{"id": id})
|
||||
}
|
||||
|
||||
// moveFromVault brings a page back out of the vault into the ordinary notebook and points the
|
||||
// [[locked:id]] links to it at its title again.
|
||||
func (s *Server) moveFromVault(w http.ResponseWriter, r *http.Request, p principal) {
|
||||
main, err := s.notebook(s.cfg.Get(), p.Sub, p.Name, p.Email)
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, "could not open your notes; the server's log says why")
|
||||
return
|
||||
}
|
||||
id := r.PathValue("id")
|
||||
n, err := p.st.Get(id)
|
||||
if err == nil && n.Trashed {
|
||||
err = store.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
storeFail(w, err)
|
||||
return
|
||||
}
|
||||
in, err := main.Import(n)
|
||||
if err != nil {
|
||||
if errors.Is(err, store.ErrExists) {
|
||||
failCode(w, http.StatusConflict, existsMessage(n, "your notebook"), "exists")
|
||||
return
|
||||
}
|
||||
storeFail(w, err)
|
||||
return
|
||||
}
|
||||
if err := main.Reveal(id, in.Title); err != nil {
|
||||
main.Discard(id)
|
||||
storeFail(w, err)
|
||||
return
|
||||
}
|
||||
if err := p.st.Discard(id); err != nil {
|
||||
log.Printf("vault: moved %s out but could not remove it from the vault: %v", id, err)
|
||||
fail(w, http.StatusInternalServerError, "the page is back in your notebook, but a copy is still in the vault; the server's log says why")
|
||||
return
|
||||
}
|
||||
writeJSON(w, map[string]string{"id": id})
|
||||
}
|
||||
|
||||
func existsMessage(n store.Note, where string) string {
|
||||
if n.Daily != "" {
|
||||
return where + " already has a journal page for that day"
|
||||
}
|
||||
return where + " already has this page"
|
||||
}
|
||||
Reference in new issue
Block a user