package web import ( "errors" "net/http" "sort" "strings" "time" "gitea.dooplex.hu/admin/felhom-controller/internal/backup" "gitea.dooplex.hu/admin/felhom-controller/internal/i18n" "gitea.dooplex.hu/admin/felhom-controller/internal/settings" ) // D72 / D84 (`09` §3 decisions 292 and 309; `07-backup-architecture.md` §8.2f, part 2) — THE HOUSEHOLD SEES THE DATE // AND CAN TAKE IT BACK. The off-site list on the restore page is keyed on the store, not on what is installed // (R-237), so a removed app stays on it while its copies exist: // // - an app with an end record reads „marked for deletion: ” and has ONE button, „keep it after all”; // - a removed app with copies and NO record (its backups were kept, or it was removed before this existed) reads // „the off-site copies of this app were kept” and has „delete in 30 days” — the household starts the clock itself; // - an end a clean-up window has carried out is one line of history. // // Nothing here deletes anything: both buttons only write or delete the end record (backup.StartOffsiteEnd, // backup.KeepOffsiteCopies). The deletion is the weekly window's, after the hub agreed. // offsiteEndDoneRow is one history line: „: the off-site copies were deleted on ”. type offsiteEndDoneRow struct { App string Line string } // applyOffsiteEnds puts the end records on the rows. offered = this box has the hub-provisioned tier. // // A record whose app has no row (the store could not be read, or its listing is cut) still gets a row: the // take-back button must not depend on a network read succeeding. func applyOffsiteEnds(rows []OffsiteRestoreRow, ends []settings.OffsiteEnd, offered bool, date func(e settings.OffsiteEnd) string) []OffsiteRestoreRow { byApp := make(map[string]settings.OffsiteEnd, len(ends)) for _, e := range ends { byApp[e.App] = e } seen := map[string]bool{} for i := range rows { r := &rows[i] seen[r.App] = true if e, ok := byApp[r.App]; ok && !r.Installed { r.EndDate = date(e) continue } // „delete in 30 days”: only for a removed app whose copies are really there, on a box where a press can // start the clock. Never for an installed app, never for a row we could not verify. r.EndOffered = offered && r.InStore && !r.Installed && !r.StoreUnknown } var extra []OffsiteRestoreRow for _, e := range ends { if !seen[e.App] { extra = append(extra, OffsiteRestoreRow{App: e.App, DisplayName: e.App, EndDate: date(e)}) } } sort.Slice(extra, func(i, j int) bool { return extra[i].App < extra[j].App }) return append(rows, extra...) } // offsiteEndData adds the end records to the restore page's data. func (s *Server) offsiteEndData(r *http.Request, data map[string]interface{}, rows []OffsiteRestoreRow) []OffsiteRestoreRow { if s.backupMgr == nil { return rows } lang := s.langFor(r) rows = applyOffsiteEnds(rows, s.backupMgr.PendingOffsiteEnds(), s.backupMgr.OffsiteEndOffered(), func(e settings.OffsiteEnd) string { return s.msgLang(lang, "offsite_end.marked", i18n.FormatDate(lang, e.Ends().Local())) }) var done []offsiteEndDoneRow for _, d := range s.backupMgr.OffsiteEndsHistory() { at, perr := time.Parse(time.RFC3339, d.DeletedAt) if perr != nil { continue } done = append(done, offsiteEndDoneRow{App: d.App, Line: s.msgLang(lang, "offsite_end.done", i18n.FormatDate(lang, at.Local()))}) } data["OffsiteEndsDone"] = done return rows } // offsiteEndKeepHandler is „keep it after all”: POST /backup/offsite-end/keep (app). It deletes the end record. // After the window has run there is no record, and the page says so. func (s *Server) offsiteEndKeepHandler(w http.ResponseWriter, r *http.Request) { if s.backupMgr == nil { offboxRedirectTo(w, r, "/backups/restore", "flash.offbox.mgr_unreachable", true) return } _ = r.ParseForm() app := strings.TrimSpace(r.FormValue("app")) kept, err := s.backupMgr.KeepOffsiteCopies(app) switch { case err != nil: s.logger.Printf("[WARN] [web] keep the off-site copies of %s: the end record could not be deleted: %v", app, err) offboxRedirectTo(w, r, "/backups/restore", "flash.offsite_end.failed", true) case !kept: offboxRedirectTo(w, r, "/backups/restore", "flash.offsite_end.nothing_to_keep", true) default: s.logger.Printf("[INFO] [web] the household keeps the off-site copies of %s after all (from %s)", app, r.RemoteAddr) offboxRedirectTo(w, r, "/backups/restore", "flash.offsite_end.kept", false) } } // offsiteEndStartHandler is „delete in 30 days”: POST /backup/offsite-end/start (app). The same press as „delete // backup data” at a removal — the same record, the same one mail — for an app that was removed without one. func (s *Server) offsiteEndStartHandler(w http.ResponseWriter, r *http.Request) { if s.backupMgr == nil { offboxRedirectTo(w, r, "/backups/restore", "flash.offbox.mgr_unreachable", true) return } _ = r.ParseForm() app := strings.TrimSpace(r.FormValue("app")) rec, _, err := s.backupMgr.StartOffsiteEnd(app) switch { case errors.Is(err, backup.ErrOffsiteEndNotOffered), errors.Is(err, backup.ErrOffsiteEndLiving): s.logger.Printf("[WARN] [web] delete-in-30-days for %q refused: %v", app, err) offboxRedirectTo(w, r, "/backups/restore", "flash.offsite_end.not_possible", true) case err != nil: s.logger.Printf("[WARN] [web] delete-in-30-days for %q: the end record was not written: %v", app, err) offboxRedirectTo(w, r, "/backups/restore", "flash.offsite_end.failed", true) default: // Also on a second press: the record stands as it was, and the page says its date again. offboxRedirectTo(w, r, "/backups/restore", "offsite_end.marked", false, i18n.FormatDate(s.langFor(r), rec.Ends().Local())) } }