| 1 | package sheets |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "sort" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "unicode/utf8" |
| 10 | |
| 11 | access "amadan.net/oficina/agent-access.git" |
| 12 | "amadan.net/oficina/sheets/internal/sheets/store" |
| 13 | "github.com/google/uuid" |
| 14 | "gorm.io/gorm" |
| 15 | ) |
| 16 | |
| 17 | type sheetAgentCell struct { |
| 18 | Row int `json:"row"` |
| 19 | Column int `json:"column"` |
| 20 | Raw string `json:"raw"` |
| 21 | } |
| 22 | type sheetAgentCreate struct { |
| 23 | Team string `json:"team"` |
| 24 | ClientID string `json:"client_id"` |
| 25 | Title string `json:"title"` |
| 26 | TabName string `json:"tab_name"` |
| 27 | Cells []sheetAgentCell `json:"cells"` |
| 28 | } |
| 29 | type sheetAgentCreated struct { |
| 30 | SheetID string `json:"sheet_id"` |
| 31 | TabID string `json:"tab_id"` |
| 32 | URL string `json:"url"` |
| 33 | Revision string `json:"revision"` |
| 34 | } |
| 35 | |
| 36 | func (a *sheetAgent) create(ctx context.Context, p access.Principal, member Member, input sheetAgentCreate) (any, error) { |
| 37 | if err := a.requireCreator(member); err != nil { |
| 38 | return nil, err |
| 39 | } |
| 40 | id, err := uuid.Parse(input.ClientID) |
| 41 | input.Title = strings.TrimSpace(input.Title) |
| 42 | input.TabName = strings.TrimSpace(input.TabName) |
| 43 | if err != nil || id == uuid.Nil || id.String() != input.ClientID || input.Title == "" || input.TabName == "" || !utf8.ValidString(input.Title) || !utf8.ValidString(input.TabName) || utf8.RuneCountInString(input.Title) > maxTitleRunes || utf8.RuneCountInString(input.TabName) > maxTitleRunes || len(input.Cells) > store.MaxCellBatch { |
| 44 | return nil, access.Fail("invalid", "Invalid initial sheet, tab name or client UUID.") |
| 45 | } |
| 46 | sort.Slice(input.Cells, func(i, j int) bool { |
| 47 | if input.Cells[i].Row != input.Cells[j].Row { |
| 48 | return input.Cells[i].Row < input.Cells[j].Row |
| 49 | } |
| 50 | return input.Cells[i].Column < input.Cells[j].Column |
| 51 | }) |
| 52 | writes := make([]store.CellWrite, len(input.Cells)) |
| 53 | for i, cell := range input.Cells { |
| 54 | if !inCellRange(cell.Row, cell.Column) || len(cell.Raw) > maxRawBytes || !utf8.ValidString(cell.Raw) || i > 0 && cell.Row == input.Cells[i-1].Row && cell.Column == input.Cells[i-1].Column { |
| 55 | return nil, access.Fail("invalid", "Invalid initial cell coordinate or input.") |
| 56 | } |
| 57 | writes[i] = store.CellWrite{Row: cell.Row, Col: cell.Column, Raw: cell.Raw} |
| 58 | } |
| 59 | if len(writes) > 0 { |
| 60 | if err := store.ValidateCellWrites(writes); err != nil { |
| 61 | return nil, access.Fail("invalid", "Invalid initial cell input.") |
| 62 | } |
| 63 | } |
| 64 | coordinate := a.digest("coordinate", []string{a.identityIssuer, a.issuer, p.Subject, p.Client, a.team, input.ClientID}) |
| 65 | payload := a.digest("content", struct { |
| 66 | Title, TabName string |
| 67 | Cells []sheetAgentCell |
| 68 | }{input.Title, input.TabName, input.Cells}) |
| 69 | var sheetID, tabID uint64 |
| 70 | err = a.app.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { |
| 71 | if e := a.bind(tx); e != nil { |
| 72 | return e |
| 73 | } |
| 74 | if e := a.creatorInTx(ctx, tx, p, member); e != nil { |
| 75 | return e |
| 76 | } |
| 77 | var receipt sheetAgentReceipt |
| 78 | e := tx.Where("coordinate=?", coordinate).First(&receipt).Error |
| 79 | if e == nil { |
| 80 | if receipt.Content != payload { |
| 81 | return access.Fail("conflict", "This client UUID already names different initial content.") |
| 82 | } |
| 83 | role, e := a.role(ctx, tx, receipt.SheetID, member) |
| 84 | if e != nil { |
| 85 | return e |
| 86 | } |
| 87 | if !role.Allows(SheetViewer) { |
| 88 | return errNoAccess |
| 89 | } |
| 90 | sheetID = receipt.SheetID |
| 91 | var tab store.Tab |
| 92 | if e = tx.Where("sheet_id=?", sheetID).Order("position ASC,id ASC").First(&tab).Error; e != nil { |
| 93 | return e |
| 94 | } |
| 95 | tabID = tab.ID |
| 96 | return a.creatorInTx(ctx, tx, p, member) |
| 97 | } |
| 98 | if !errors.Is(e, gorm.ErrRecordNotFound) { |
| 99 | return e |
| 100 | } |
| 101 | app := *a.app |
| 102 | app.db = tx |
| 103 | summary, e := app.newSheetWithTab(a.app.memberGuard(ctx), input.Title, input.TabName, member, false) |
| 104 | if e != nil { |
| 105 | return e |
| 106 | } |
| 107 | sheetID, tabID = summary.ID, summary.TabID |
| 108 | if e = store.InitializeTab(tx, a.app.sealer, tabID, writes, member.ID); e != nil { |
| 109 | return e |
| 110 | } |
| 111 | client, e := a.app.sealer.Seal("agent.client/"+strconv.FormatUint(sheetID, 10), []byte(p.Client)) |
| 112 | if e != nil { |
| 113 | return e |
| 114 | } |
| 115 | if e = tx.Create(&sheetAgentAttribution{sheetID, member.ID, client}).Error; e != nil { |
| 116 | return e |
| 117 | } |
| 118 | if e = tx.Create(&sheetAgentReceipt{coordinate, payload, sheetID}).Error; e != nil { |
| 119 | return e |
| 120 | } |
| 121 | var tab store.Tab |
| 122 | if e = tx.First(&tab, tabID).Error; e != nil { |
| 123 | return e |
| 124 | } |
| 125 | // A fresh tab's persisted watermark is the last assigned stamp; a fresh |
| 126 | // canonical handle will expose the next one. Check output size before commit. |
| 127 | if _, e = sheetAgentBounded(a.created(sheetID, tabID, tab.ContentVersion+1)); e != nil { |
| 128 | return e |
| 129 | } |
| 130 | return a.creatorInTx(ctx, tx, p, member) |
| 131 | }) |
| 132 | if err != nil { |
| 133 | return nil, sheetAgentFailure(err) |
| 134 | } |
| 135 | // Only a committed tab can enter the canonical cache. Retries observe edits. |
| 136 | if err = a.tab(ctx, a.app.db, sheetID, tabID, member); err != nil { |
| 137 | return nil, sheetAgentFailure(err) |
| 138 | } |
| 139 | handle, err := store.Open(a.app.db, a.app.sealer, tabID) |
| 140 | if err != nil { |
| 141 | return nil, sheetAgentFailure(err) |
| 142 | } |
| 143 | defer handle.Release() |
| 144 | a.remember(ctx, sheetID) |
| 145 | return sheetAgentBounded(a.created(sheetID, tabID, handle.Version())) |
| 146 | } |
| 147 | func (a *sheetAgent) created(sheetID, tabID, revision uint64) sheetAgentCreated { |
| 148 | id := strconv.FormatUint(sheetID, 10) |
| 149 | return sheetAgentCreated{id, strconv.FormatUint(tabID, 10), a.app.origin + "/sheets/" + id, strconv.FormatUint(revision, 10)} |
| 150 | } |
| 151 | |