// pins.go - pinned documents (a reading list). Cookie-local. package main import ( "net/http" "net/url" "slices" ) const ( pinnedCookie = "pinned_docs" pinnedMax = 200 pinnedSearchesCookie = "pinned_searches" pinnedSearchesMax = 30 ) func (co *Corpus) readPinned(r *http.Request) []string { var out []string for _, id := range readIDListCookie(r, pinnedCookie, pinnedMax) { if co.docIndex[id] != nil { out = append(out, id) } } return out } func toggleEntry(list []string, e string, max int) []string { var next []string removed := false for _, x := range list { if x == e { removed = true continue } next = append(next, x) } if !removed && len(next) < max { next = append(next, e) } return next } func isPinned(r *http.Request, id string) bool { return slices.Contains(readIDListCookie(r, pinnedCookie, pinnedMax), id) } func (co *Corpus) handlePinToggle(w http.ResponseWriter, r *http.Request) { if !readPOSTForm(w, r) { return } id := r.PathValue("id") if co.docIndex[id] == nil { notFound(w, r) return } pinned := co.readPinned(r) stored := writeListCookie(w, pinnedCookie, toggleEntry(pinned, id, pinnedMax)) if pinRefused(pinned, stored, id) { http.Redirect(w, r, "/pinned?full=1", http.StatusSeeOther) return } back := safeBackPath(r.FormValue("back")) if back == "/" { back = docPath(id) } http.Redirect(w, r, back, http.StatusSeeOther) } func readPinnedSearchEntries(r *http.Request) []string { return readIDListCookie(r, pinnedSearchesCookie, pinnedSearchesMax) } func isPinnedSearch(r *http.Request, canonicalQS string) bool { return slices.Contains(readPinnedSearchEntries(r), url.QueryEscape(canonicalQS)) } func (co *Corpus) handleSearchPinToggle(w http.ResponseWriter, r *http.Request) { if !readPOSTForm(w, r) { return } qs := r.FormValue("qs") if qs == "" { http.Error(w, "bad query", http.StatusBadRequest) return } v, err := url.ParseQuery(qs) if err != nil { http.Error(w, "bad query", http.StatusBadRequest) return } entry := url.QueryEscape(qs) pinned := readPinnedSearchEntries(r) if !co.savableSearch(v) && !slices.Contains(pinned, entry) { http.Error(w, "this search cannot be pinned: it has no terms or filters, or is too long", http.StatusBadRequest) return } stored := writeListCookie(w, pinnedSearchesCookie, toggleEntry(pinned, entry, pinnedSearchesMax)) if pinRefused(pinned, stored, entry) { http.Redirect(w, r, "/pinned?full=1", http.StatusSeeOther) return } http.Redirect(w, r, safeBackPath(r.FormValue("back")), http.StatusSeeOther) } func pinRefused(before, stored []string, e string) bool { return !slices.Contains(before, e) && !slices.Contains(stored, e) } func (co *Corpus) handlePinnedPage(w http.ResponseWriter, r *http.Request) { prefs := readPrefs(r) var docs []*Document for _, id := range co.readPinned(r) { docs = append(docs, co.docIndex[id]) } searches := co.parseSearchEntries(readPinnedSearchEntries(r)) data := &pinnedPage{ Base: pageWith(prefs), Total: len(docs), Searches: searches, Full: r.URL.Query().Get("full") != "", Cards: newCardView(prefs, docs), } data.Cards.ShowUnpin = true noStore(w) render(w, r, data) } func (co *Corpus) handlePinnedExport(w http.ResponseWriter, r *http.Request) { w.Header().Set("Cache-Control", "private, no-store") w.Header().Set("Content-Disposition", `attachment; filename="snowchive-pinned.json"`) ids := co.readPinned(r) out := make([]apiDocSummary, 0, len(ids)) for _, id := range ids { out = append(out, docSummary(*co.docIndex[id])) } writeJSON(w, http.StatusOK, map[string]interface{}{ "count": len(out), "docs": out, }) }