| 1 | package calendar |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "regexp" |
| 6 | "unicode/utf8" |
| 7 | |
| 8 | access "amadan.net/oficina/agent-access.git" |
| 9 | ) |
| 10 | |
| 11 | // Attendees through the agent operations are the rows quick add writes |
| 12 | // (models_attendee.go): addresses, normalized, sealed, keyed-fingerprinted, |
| 13 | // and RECORDED, NOT INVITED. Nothing here or downstream emails them; |
| 14 | // AGENTS.md "Attendees are recorded, never invited" and ruling B5 hold. |
| 15 | const ( |
| 16 | agentAttendeeLen = 254 |
| 17 | agentAttendeeMax = 50 |
| 18 | // agentAttendeeBytes bounds one event's attendees in a response. Quick |
| 19 | // add, import and booking store whatever they find, so the stored list |
| 20 | // has no length an agent response can rely on. |
| 21 | agentAttendeeBytes = 4096 |
| 22 | ) |
| 23 | |
| 24 | // reWholeAddress is quickadd_who.go's reAddress, anchored: an agent hands |
| 25 | // over a list, so anything beyond one bare address is refused rather than |
| 26 | // mined for whatever address it happens to contain. |
| 27 | var reWholeAddress = regexp.MustCompile(`^` + reAddress.String() + `$`) |
| 28 | |
| 29 | func agentAttendeesSchema() map[string]any { |
| 30 | return map[string]any{"type": "array", "maxItems": agentAttendeeMax, "items": map[string]any{"type": "string", "minLength": 3, "maxLength": agentAttendeeLen}} |
| 31 | } |
| 32 | |
| 33 | // agentAttendees normalizes as quick add does and refuses anything that |
| 34 | // is not an address. setAttendeesOn collapses duplicates by fingerprint. |
| 35 | func agentAttendees(in []string) ([]string, error) { |
| 36 | if len(in) > agentAttendeeMax { |
| 37 | return nil, access.Fail("invalid", "Supply at most 50 attendee email addresses.") |
| 38 | } |
| 39 | out := []string{} |
| 40 | for _, raw := range in { |
| 41 | addr := normalizeAddress(raw) |
| 42 | if len(addr) > agentAttendeeLen || !reWholeAddress.MatchString(addr) { |
| 43 | return nil, access.Fail("invalid", "Each attendee must be a single email address, such as name@example.com.") |
| 44 | } |
| 45 | out = append(out, addr) |
| 46 | } |
| 47 | return out, nil |
| 48 | } |
| 49 | |
| 50 | // agentAttendeeView is the part of a stored attendee list a response |
| 51 | // carries: the first addresses that fit agentAttendeeMax and |
| 52 | // agentAttendeeBytes, in stored order, and how many were left out. An |
| 53 | // address longer than any agent could have supplied is left out too. |
| 54 | func agentAttendeeView(all []string) ([]string, int) { |
| 55 | shown, size, full := []string{}, 2, false |
| 56 | for _, addr := range all { |
| 57 | encoded, _ := json.Marshal(addr) |
| 58 | if full || len(addr) > agentAttendeeLen || len(shown) == agentAttendeeMax || size+len(encoded)+1 > agentAttendeeBytes { |
| 59 | full = full || len(addr) <= agentAttendeeLen |
| 60 | continue |
| 61 | } |
| 62 | size += len(encoded) + 1 |
| 63 | shown = append(shown, addr) |
| 64 | } |
| 65 | return shown, len(all) - len(shown) |
| 66 | } |
| 67 | |
| 68 | // agentAttendeeFields adds attendees, and attendees_more when any were |
| 69 | // left out, to a response object. |
| 70 | func agentAttendeeFields(out map[string]any, all []string) map[string]any { |
| 71 | shown, more := agentAttendeeView(all) |
| 72 | out["attendees"] = shown |
| 73 | if more > 0 { |
| 74 | out["attendees_more"] = more |
| 75 | } |
| 76 | return out |
| 77 | } |
| 78 | |
| 79 | // agentAttendeesOutput is the response schema for those two fields. |
| 80 | func agentAttendeesOutput(properties map[string]any) map[string]any { |
| 81 | properties["attendees"] = map[string]any{"type": "array", "maxItems": agentAttendeeMax, "items": agentString(agentAttendeeLen)} |
| 82 | properties["attendees_more"] = map[string]any{"type": "integer", "minimum": 1} |
| 83 | return properties |
| 84 | } |
| 85 | |
| 86 | // agentClip shortens text from storage that the form never limited — |
| 87 | // imported or older rows — to what an operation's schema promises. |
| 88 | func agentClip(s string, runes int) string { |
| 89 | if utf8.RuneCountInString(s) <= runes { |
| 90 | return s |
| 91 | } |
| 92 | return string([]rune(s)[:runes]) |
| 93 | } |
| 94 | |