| 1 | package calendar |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "encoding/json" |
| 7 | "strconv" |
| 8 | "time" |
| 9 | |
| 10 | access "amadan.net/oficina/agent-access.git" |
| 11 | "github.com/google/uuid" |
| 12 | "gorm.io/gorm" |
| 13 | ) |
| 14 | |
| 15 | type agentEventInput struct { |
| 16 | Team string `json:"team"` |
| 17 | ClientID string `json:"client_id"` |
| 18 | CalendarID int64 `json:"calendar_id"` |
| 19 | Title string `json:"title"` |
| 20 | Location string `json:"location"` |
| 21 | Notes string `json:"notes"` |
| 22 | AllDay bool `json:"all_day"` |
| 23 | StartLocal string `json:"start_local"` |
| 24 | EndLocal string `json:"end_local"` |
| 25 | TZ string `json:"tz"` |
| 26 | Kind string `json:"kind"` |
| 27 | Status string `json:"status"` |
| 28 | // omitempty keeps the receipt content of a create without attendees |
| 29 | // byte-identical to receipts written before attendees existed. |
| 30 | Attendees []string `json:"attendees,omitempty"` |
| 31 | } |
| 32 | |
| 33 | func (a *App) agentCreateEvent(ctx context.Context, principal access.Principal, person Person, raw json.RawMessage) (any, error) { |
| 34 | var in agentEventInput |
| 35 | if json.Unmarshal(raw, &in) != nil { |
| 36 | return nil, access.Fail("invalid", "Invalid event input.") |
| 37 | } |
| 38 | uid, err := uuid.Parse(in.ClientID) |
| 39 | if err != nil || uid == uuid.Nil || uid.String() != in.ClientID { |
| 40 | return nil, access.Fail("invalid", "Supply a nonzero UUID client_id.") |
| 41 | } |
| 42 | event := EventInput{CalendarID: in.CalendarID, Title: in.Title, Location: in.Location, Notes: in.Notes, AllDay: in.AllDay, StartLocal: in.StartLocal, EndLocal: in.EndLocal, TZ: in.TZ, Kind: in.Kind, Status: in.Status} |
| 43 | if err = ValidateEventInput(&event); err != nil { |
| 44 | return nil, err |
| 45 | } |
| 46 | attendees, err := agentAttendees(in.Attendees) |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | if in.AllDay && in.TZ != "" { |
| 51 | return nil, access.Fail("invalid", "An all-day event uses dates without a time zone.") |
| 52 | } |
| 53 | if !in.AllDay && event.TZ == "Local" { |
| 54 | return nil, access.Fail("invalid", "Supply an explicit IANA time zone, not the server local zone.") |
| 55 | } |
| 56 | if !in.AllDay && (!agentUnambiguous(event.StartLocal, event.TZ) || !agentUnambiguous(event.EndLocal, event.TZ)) { |
| 57 | return nil, access.Fail("invalid", "Choose unambiguous local times outside a DST gap or fold.") |
| 58 | } |
| 59 | coordinate, _ := json.Marshal([]string{a.cfg.Home.Issuer, a.agentIssuer, principal.Subject, principal.Client, in.Team, uid.String()}) |
| 60 | key, err := a.store.seal.Fingerprint("agent.event.coordinate.v1", string(coordinate)) |
| 61 | if err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | // Fingerprint the supplied fields, not normalized fields: changed retry input |
| 65 | // is a conflict even when the UI normalizer would make the same event. |
| 66 | in.ClientID = uid.String() |
| 67 | payload, _ := json.Marshal(in) |
| 68 | fingerprint, err := a.store.seal.Fingerprint("agent.event.content.v1", string(payload)) |
| 69 | if err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | var eventID int64 |
| 73 | err = a.store.g.WithContext(ctx).Transaction(func(tx *gorm.DB) error { |
| 74 | if e := a.bindAgent(tx); e != nil { |
| 75 | return e |
| 76 | } |
| 77 | fresh, e := a.checkAgentWriter(ctx, tx, principal, person, in.CalendarID) |
| 78 | if e != nil { |
| 79 | return e |
| 80 | } |
| 81 | // bindAgent reserved the writer before current authority was checked. |
| 82 | // The effect, reminder and receipt commit atomically. |
| 83 | if err := tx.Exec("INSERT INTO calendar_agent_events(coordinate,content) VALUES(?,?) ON CONFLICT(coordinate) DO NOTHING", key, fingerprint).Error; err != nil { |
| 84 | return err |
| 85 | } |
| 86 | current := a.agentStore(tx) |
| 87 | cal, err := current.Calendar(ctx, person.ID, in.CalendarID) |
| 88 | if err != nil { |
| 89 | return err |
| 90 | } |
| 91 | if cal.ArchivedAt != nil { |
| 92 | return ErrNotFound |
| 93 | } |
| 94 | if !CanWrite(cal.Role) { |
| 95 | return ErrReadOnly |
| 96 | } |
| 97 | var saved string |
| 98 | var id sql.NullInt64 |
| 99 | if err = tx.Raw("SELECT content,event_id FROM calendar_agent_events WHERE coordinate=?", key).Row().Scan(&saved, &id); err != nil { |
| 100 | return err |
| 101 | } |
| 102 | if saved != fingerprint { |
| 103 | return access.Fail("conflict", "This client_id was already used with different event fields.") |
| 104 | } |
| 105 | if id.Valid { |
| 106 | if _, err = current.Event(ctx, person.ID, id.Int64); err != nil { |
| 107 | return err |
| 108 | } |
| 109 | eventID = id.Int64 |
| 110 | _, e := a.checkAgentWriter(ctx, tx, principal, person, in.CalendarID) |
| 111 | return e |
| 112 | } |
| 113 | created, err := current.CreateEvent(ctx, person.ID, event) |
| 114 | if err != nil { |
| 115 | return err |
| 116 | } |
| 117 | if err = current.setAttendeesOn(tx, created.ID, attendees); err != nil { |
| 118 | return err |
| 119 | } |
| 120 | if err = current.EnsureDefaultReminder(ctx, fresh, created); err != nil { |
| 121 | return err |
| 122 | } |
| 123 | if err = tx.Exec("UPDATE calendar_agent_events SET event_id=? WHERE coordinate=?", created.ID, key).Error; err != nil { |
| 124 | return err |
| 125 | } |
| 126 | eventID = created.ID |
| 127 | _, e = a.checkAgentWriter(ctx, tx, principal, person, in.CalendarID) |
| 128 | return e |
| 129 | }) |
| 130 | if err != nil { |
| 131 | return nil, err |
| 132 | } |
| 133 | captureAgentEvent(ctx, eventID, in.CalendarID) |
| 134 | return map[string]any{"id": eventID, "url": a.cfg.Origin + "/events/" + strconv.FormatInt(eventID, 10), "invitations_sent": false}, nil |
| 135 | } |
| 136 | |
| 137 | // A machine caller must never silently acquire the store's default choice for |
| 138 | // a repeated or nonexistent local time. Adjacent zone offsets expose both |
| 139 | // candidates, including non-hour transitions. |
| 140 | func agentUnambiguous(local, zone string) bool { |
| 141 | if zone == "Local" { |
| 142 | return false |
| 143 | } |
| 144 | index, err := ToUTC(local, zone, false) |
| 145 | if err != nil { |
| 146 | return false |
| 147 | } |
| 148 | loc, err := Zone(zone) |
| 149 | if err != nil { |
| 150 | return false |
| 151 | } |
| 152 | actual := time.Unix(index, 0).In(loc) |
| 153 | if actual.Format(LayoutLocal) != local { |
| 154 | return false |
| 155 | } |
| 156 | naive, err := time.Parse(LayoutLocal, local) |
| 157 | if err != nil { |
| 158 | return false |
| 159 | } |
| 160 | from, to := actual.ZoneBounds() |
| 161 | for _, boundary := range []time.Time{from.Add(-time.Second), to} { |
| 162 | if boundary.IsZero() || boundary.Year() < 1 { |
| 163 | continue |
| 164 | } |
| 165 | _, offset := boundary.In(loc).Zone() |
| 166 | candidate := time.Unix(naive.Unix()-int64(offset), 0).In(loc) |
| 167 | if candidate.Unix() != index && candidate.Format(LayoutLocal) == local { |
| 168 | return false |
| 169 | } |
| 170 | } |
| 171 | return true |
| 172 | } |
| 173 | |