| 1 | package memoria |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | |
| 7 | "amadan.net/oficina/suite" |
| 8 | |
| 9 | "amadan.net/oficina/memoria/internal/memoria/seal" |
| 10 | "amadan.net/oficina/memoria/internal/memoria/sibling" |
| 11 | ) |
| 12 | |
| 13 | // VisibleDocuments rules a set of documents for one person, the way search |
| 14 | // rules a set of passages (§4.1), for every surface that names a document |
| 15 | // outside search: the documents list, "Recently updated", a document's |
| 16 | // own page, its delete question. |
| 17 | // |
| 18 | // **Why every surface, not search alone** (suite revision 10, Paul, |
| 19 | // 2026-11-01: "Hold private too, check per person"). Memoria now holds a |
| 20 | // sibling's private and individually shared documents as well as its team |
| 21 | // ones, so a page that named a sibling document without asking its app |
| 22 | // would show one person somebody else's private document. Before revision |
| 23 | // 10 every sibling document was team-readable and the omission was |
| 24 | // harmless; the document page did not ask, and it was found by reading the |
| 25 | // page against the new rule. |
| 26 | // |
| 27 | // It returns the ids this person may see now, and §4.1's loud notices for |
| 28 | // anything an app did not answer for. Memoria's own documents (§4.2) are |
| 29 | // always visible: there is nobody else to ask. An id with no row is simply |
| 30 | // absent. |
| 31 | func (s *Store) VisibleDocuments(ctx context.Context, asker suite.Subject, ids []int64) (map[int64]bool, []string, error) { |
| 32 | visible := map[int64]bool{} |
| 33 | if len(ids) == 0 { |
| 34 | return visible, nil, nil |
| 35 | } |
| 36 | if asker.Value == "" { |
| 37 | return nil, nil, fmt.Errorf("visible documents: no asker; nothing can be authorized") |
| 38 | } |
| 39 | var docs []Document |
| 40 | if err := s.db.G.WithContext(ctx).Select("id", "source", "sibling_item_id", "sibling_revision"). |
| 41 | Where("id IN ?", ids).Find(&docs).Error; err != nil { |
| 42 | return nil, nil, err |
| 43 | } |
| 44 | all := make([]Result, 0, len(docs)) |
| 45 | for _, d := range docs { |
| 46 | r := Result{DocumentID: d.ID, Source: d.Source} |
| 47 | if len(d.SiblingItemID) > 0 { |
| 48 | id, err := s.sealer.Open(seal.PurposeItemID, d.SiblingItemID) |
| 49 | if err != nil { |
| 50 | return nil, nil, fmt.Errorf("open item id %d: %w", d.ID, err) |
| 51 | } |
| 52 | r.item = sibling.Candidate{Item: suite.ItemRevision{ItemID: string(id), Revision: d.SiblingRevision}} |
| 53 | } |
| 54 | all = append(all, r) |
| 55 | } |
| 56 | page := s.rule(ctx, asker, all) |
| 57 | for _, r := range page.Results { |
| 58 | visible[r.DocumentID] = true |
| 59 | } |
| 60 | return visible, page.Notices, nil |
| 61 | } |
| 62 | |
| 63 | // IsSiblingDocument reports whether a document's app of record is another |
| 64 | // app. Memoria may delete only what it owns (§4.2); a sibling's document is |
| 65 | // deleted in its own app and leaves through that app's feed. |
| 66 | func (s *Store) IsSiblingDocument(ctx context.Context, id int64) (bool, error) { |
| 67 | var d Document |
| 68 | if err := s.db.G.WithContext(ctx).Select("id", "sibling_item_id").Where("id = ?", id).Limit(1).Find(&d).Error; err != nil { |
| 69 | return false, err |
| 70 | } |
| 71 | return len(d.SiblingItemID) > 0, nil |
| 72 | } |
| 73 | |