Repository navigation
Expand file tree
/
Copy pathmodels.go
More file actions
572 lines (453 loc) · 19.5 KB
/
Copy pathmodels.go
File metadata and controls
572 lines (453 loc) · 19.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
package lunogram
import (
"encoding/json"
"net/url"
"strconv"
"strings"
"time"
)
// ExternalID identifies a user or organization by an external system identifier.
type ExternalID struct {
// Source of the identifier (e.g. "default", "anonymous", or a custom source).
// Defaults to "default" if empty.
Source string `json:"source,omitempty"`
// ExternalID is the identifier value in the external system.
ExternalID string `json:"external_id"`
// Metadata is optional key-value data associated with this identifier.
Metadata map[string]any `json:"metadata,omitempty"`
}
// ExternalIDResponse is an external identifier as returned in API responses.
type ExternalIDResponse struct {
ID string `json:"id"`
Source string `json:"source"`
ExternalID string `json:"external_id"`
Metadata map[string]any `json:"metadata,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// User represents a user profile in Lunogram.
type User struct {
ID string `json:"id"`
ProjectID string `json:"project_id"`
Identifier []ExternalIDResponse `json:"identifier"`
Email string `json:"email,omitempty"`
Phone string `json:"phone,omitempty"`
Data map[string]any `json:"data"`
Timezone string `json:"timezone,omitempty"`
Locale string `json:"locale,omitempty"`
HasPushDevice bool `json:"has_push_device"`
Version int32 `json:"version"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// Organization represents an organization in Lunogram.
type Organization struct {
ID string `json:"id"`
ProjectID string `json:"project_id"`
Identifier []ExternalIDResponse `json:"identifier"`
Name string `json:"name,omitempty"`
Data map[string]any `json:"data"`
Version int32 `json:"version"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// UpsertUserRequest creates or updates a user profile.
type UpsertUserRequest struct {
// Identifier contains one or more external identifiers for the user.
Identifier []ExternalID `json:"identifier"`
// Email address of the user.
Email *string `json:"email,omitempty"`
// Phone number in E.164 format (e.g. "+31612345678").
Phone *string `json:"phone,omitempty"`
// Timezone in IANA format (e.g. "Europe/Amsterdam").
Timezone *string `json:"timezone,omitempty"`
// Locale as a BCP 47 tag (e.g. "nl-NL").
Locale *string `json:"locale,omitempty"`
// Data holds arbitrary user attributes.
Data map[string]any `json:"data,omitempty"`
}
// DeleteUserRequest deletes a user by identifier.
type DeleteUserRequest struct {
Identifier []ExternalID `json:"identifier"`
}
// Event represents a user event to be ingested.
type Event struct {
// Name of the event (e.g. "purchase_completed").
Name string `json:"name"`
// Identifier targets a specific user. Mutually exclusive with Match.
Identifier []ExternalID `json:"identifier,omitempty"`
// Match is a JSONB containment filter to target users by data attributes.
// Mutually exclusive with Identifier.
Match map[string]any `json:"match,omitempty"`
// Data holds event-specific attributes.
Data map[string]any `json:"data"`
}
// UpsertOrganizationRequest creates or updates an organization.
type UpsertOrganizationRequest struct {
Identifier []ExternalID `json:"identifier"`
Name *string `json:"name,omitempty"`
Data map[string]any `json:"data,omitempty"`
}
// DeleteOrganizationRequest deletes an organization by identifier.
type DeleteOrganizationRequest struct {
Identifier []ExternalID `json:"identifier"`
}
// OrganizationUserRequest adds a user to an organization.
type OrganizationUserRequest struct {
Organization OrganizationRef `json:"organization"`
User UserRef `json:"user"`
// Data holds organization-specific attributes for this user (e.g. role, department).
Data map[string]any `json:"data,omitempty"`
}
// RemoveOrganizationUserRequest removes a user from an organization.
type RemoveOrganizationUserRequest struct {
Organization OrganizationRef `json:"organization"`
User UserRef `json:"user"`
}
// OrganizationRef identifies an organization by its external identifiers.
type OrganizationRef struct {
Identifier []ExternalID `json:"identifier"`
}
// UserRef identifies a user by its external identifiers.
type UserRef struct {
Identifier []ExternalID `json:"identifier"`
}
// OrganizationEvent represents an organization event to be ingested.
type OrganizationEvent struct {
// Name of the event (e.g. "subscription_upgraded").
Name string `json:"name"`
// Identifier targets a specific organization. Mutually exclusive with Match.
Identifier []ExternalID `json:"identifier,omitempty"`
// Match is a JSONB containment filter to target organizations by data attributes.
// Mutually exclusive with Identifier.
Match map[string]any `json:"match,omitempty"`
// Data holds event-specific attributes.
Data map[string]any `json:"data,omitempty"`
}
// UpsertUserScheduledRequest creates or updates a scheduled resource for a user.
type UpsertUserScheduledRequest struct {
// ID of an existing schedule assignment to update in place. Omit to create a
// new assignment; the same name may be scheduled multiple times. The id is
// returned in the response.
ID *string `json:"id,omitempty"`
// Name of the scheduled resource (e.g. "renewal_date").
Name string `json:"name"`
// Identifier targets a specific user.
Identifier []ExternalID `json:"identifier,omitempty"`
// ScheduledAt is the trigger time for single schedules.
ScheduledAt *time.Time `json:"scheduled_at,omitempty"`
// StartAt is the start time for recurring schedules. Defaults to now if omitted.
StartAt *time.Time `json:"start_at,omitempty"`
// Interval for recurring schedules (e.g. "24h"). When set, the schedule becomes recurring.
Interval *string `json:"interval,omitempty"`
// Data holds scheduled resource attributes.
Data map[string]any `json:"data,omitempty"`
}
// DeleteUserScheduledRequest deletes scheduled instances for a user. Provide ID
// to delete a single assignment, or Name to delete every assignment with that
// name. Identifier is required.
type DeleteUserScheduledRequest struct {
// ID of a single schedule assignment to delete. When set, only that instance
// is removed; otherwise every assignment matching Name is deleted.
ID *string `json:"id,omitempty"`
// Name of the scheduled resource to delete (ignored when ID is set).
Name string `json:"name,omitempty"`
// Identifier targets a specific user.
Identifier []ExternalID `json:"identifier,omitempty"`
}
// UpsertOrganizationScheduledRequest creates or updates a scheduled resource for an organization.
type UpsertOrganizationScheduledRequest struct {
// ID of an existing schedule assignment to update in place. Omit to create a
// new assignment; the same name may be scheduled multiple times. The id is
// returned in the response.
ID *string `json:"id,omitempty"`
// Name of the scheduled resource (e.g. "contract_renewal").
Name string `json:"name"`
// Identifier targets a specific organization.
Identifier []ExternalID `json:"identifier"`
// ScheduledAt is the trigger time for single schedules.
ScheduledAt *time.Time `json:"scheduled_at,omitempty"`
// StartAt is the start time for recurring schedules. Defaults to now if omitted.
StartAt *time.Time `json:"start_at,omitempty"`
// Interval for recurring schedules (e.g. "24h"). When set, the schedule becomes recurring.
Interval *string `json:"interval,omitempty"`
// Data holds scheduled resource attributes.
Data map[string]any `json:"data,omitempty"`
}
// DeleteOrganizationScheduledRequest deletes scheduled instances for an
// organization. Provide ID to delete a single assignment, or Name to delete
// every assignment with that name. Identifier is required.
type DeleteOrganizationScheduledRequest struct {
// ID of a single schedule assignment to delete. When set, only that instance
// is removed; otherwise every assignment matching Name is deleted.
ID *string `json:"id,omitempty"`
// Name of the scheduled resource to delete (ignored when ID is set).
Name string `json:"name,omitempty"`
// Identifier targets a specific organization.
Identifier []ExternalID `json:"identifier"`
}
// ScheduledAccepted is returned when a scheduled resource is accepted for processing.
type ScheduledAccepted struct {
ID string `json:"id"`
Name string `json:"name"`
ScheduledAt time.Time `json:"scheduled_at"`
Data map[string]any `json:"data,omitempty"`
}
// Channel is the delivery channel of an inbox message.
type Channel string
// Supported delivery channels.
const (
ChannelEmail Channel = "email"
ChannelSMS Channel = "sms"
ChannelPush Channel = "push"
ChannelInbox Channel = "inbox"
)
// InboxStatus filters inbox messages by their read/archive state.
type InboxStatus string
// Inbox message statuses.
const (
InboxStatusUnread InboxStatus = "unread"
InboxStatusRead InboxStatus = "read"
InboxStatusArchived InboxStatus = "archived"
)
// InboxMessageCreate creates a single inbox message for a user.
type InboxMessageCreate struct {
// Target identifies the user the message is delivered to.
Target []ExternalID `json:"target"`
// Identifier is the external identifier of the message itself. It is
// required for idempotency and traceability back to the origin source.
Identifier ExternalID `json:"identifier"`
// Channel is the delivery channel (email, sms, push, inbox).
Channel Channel `json:"channel"`
// SenderIdentityID is required for email and sms messages. Push uses the
// project's push provider settings.
SenderIdentityID *string `json:"sender_identity_id,omitempty"`
// CampaignID associates the message with a campaign.
CampaignID *string `json:"campaign_id,omitempty"`
// BroadcastID associates the message with a broadcast.
BroadcastID *string `json:"broadcast_id,omitempty"`
// Content is the channel-specific payload content.
Content json.RawMessage `json:"content,omitempty"`
// Data holds arbitrary message attributes.
Data map[string]any `json:"data,omitempty"`
// Tags label the message for filtering.
Tags []string `json:"tags,omitempty"`
// Priority is a value from 1 to 5. Defaults to 3 when omitted.
Priority *int16 `json:"priority,omitempty"`
// Source is an optional origin label for the message.
Source *string `json:"source,omitempty"`
// ScheduledAt is when the message should be delivered.
ScheduledAt *time.Time `json:"scheduled_at,omitempty"`
// ExpiresAt is when the message stops being visible.
ExpiresAt *time.Time `json:"expires_at,omitempty"`
}
// OrganizationInboxMessageCreate creates a single inbox message for an organization.
type OrganizationInboxMessageCreate struct {
// Target identifies the organization the message is delivered to.
Target []ExternalID `json:"target"`
// Identifier is the external identifier of the message itself. It is
// required for idempotency and traceability back to the origin source.
Identifier ExternalID `json:"identifier"`
// Channel is the delivery channel (email, sms, push, inbox).
Channel Channel `json:"channel"`
// SenderIdentityID is required for email and sms messages. Push uses the
// project's push provider settings.
SenderIdentityID *string `json:"sender_identity_id,omitempty"`
// CampaignID associates the message with a campaign.
CampaignID *string `json:"campaign_id,omitempty"`
// BroadcastID associates the message with a broadcast.
BroadcastID *string `json:"broadcast_id,omitempty"`
// Content is the channel-specific payload content.
Content json.RawMessage `json:"content,omitempty"`
// Data holds arbitrary message attributes.
Data map[string]any `json:"data,omitempty"`
// Tags label the message for filtering.
Tags []string `json:"tags,omitempty"`
// Priority is a value from 1 to 5. Defaults to 3 when omitted.
Priority *int16 `json:"priority,omitempty"`
// Source is an optional origin label for the message.
Source *string `json:"source,omitempty"`
// ScheduledAt is when the message should be delivered.
ScheduledAt *time.Time `json:"scheduled_at,omitempty"`
// ExpiresAt is when the message stops being visible.
ExpiresAt *time.Time `json:"expires_at,omitempty"`
}
// InboxMessageRef references a single user inbox message to mark read or archived.
type InboxMessageRef struct {
// Target identifies the user the message belongs to.
Target []ExternalID `json:"target"`
// MessageID is the UUID of the message to mark.
MessageID string `json:"message_id"`
}
// OrganizationInboxMessageRef references a single organization inbox message to
// mark read or archived.
type OrganizationInboxMessageRef struct {
// Target identifies the organization the message belongs to.
Target []ExternalID `json:"target"`
// MessageID is the UUID of the message to mark.
MessageID string `json:"message_id"`
}
// InboxQuery holds the filters for querying user or organization inbox messages.
// Source, ExternalID, and Channel are required by the API; the remaining fields
// are optional filters.
type InboxQuery struct {
// Source of the target's external identifier (e.g. "default").
Source string
// ExternalID of the target whose inbox is queried.
ExternalID string
// Channel restricts the query to a single delivery channel. Required.
Channel Channel
// Status filters by read/archive state when set.
Status *InboxStatus
// Tags filters to messages carrying all of the given tags.
Tags []string
// MessageSource filters by the message's origin source label when set.
MessageSource *string
// Priority filters to messages with the given priority when set.
Priority *int
// Limit caps the number of messages returned (1–100, default 20).
Limit *int
// Offset skips the given number of messages for pagination.
Offset *int
}
// InboxCountQuery holds the filters for counting user or organization inbox
// messages. All three fields are required by the API.
type InboxCountQuery struct {
// Source of the target's external identifier (e.g. "default").
Source string
// ExternalID of the target whose inbox is counted.
ExternalID string
// Channel restricts the count to a single delivery channel.
Channel Channel
}
// values encodes the query as URL query parameters using the spec's parameter
// names (source, external_id, channel, status, tags, message_source, priority,
// limit, offset). Comma-separated tags are joined per the spec's tag filter.
func (q *InboxQuery) values() url.Values {
v := url.Values{}
v.Set("source", q.Source)
v.Set("external_id", q.ExternalID)
v.Set("channel", string(q.Channel))
if q.Status != nil {
v.Set("status", string(*q.Status))
}
if len(q.Tags) > 0 {
v.Set("tags", strings.Join(q.Tags, ","))
}
if q.MessageSource != nil {
v.Set("message_source", *q.MessageSource)
}
if q.Priority != nil {
v.Set("priority", strconv.Itoa(*q.Priority))
}
if q.Limit != nil {
v.Set("limit", strconv.Itoa(*q.Limit))
}
if q.Offset != nil {
v.Set("offset", strconv.Itoa(*q.Offset))
}
return v
}
// values encodes the count query as URL query parameters using the spec's
// parameter names (source, external_id, channel).
func (q *InboxCountQuery) values() url.Values {
v := url.Values{}
v.Set("source", q.Source)
v.Set("external_id", q.ExternalID)
v.Set("channel", string(q.Channel))
return v
}
// InboxMessage is an inbox message as returned by the API.
type InboxMessage struct {
ID string `json:"id"`
ProjectID string `json:"project_id"`
UserID *string `json:"user_id,omitempty"`
OrganizationID *string `json:"organization_id,omitempty"`
ExternalID *string `json:"external_id,omitempty"`
Channel Channel `json:"channel"`
SenderIdentityID *string `json:"sender_identity_id,omitempty"`
CampaignID *string `json:"campaign_id,omitempty"`
BroadcastID *string `json:"broadcast_id,omitempty"`
Content json.RawMessage `json:"content"`
Data json.RawMessage `json:"data"`
Tags []string `json:"tags"`
Priority int16 `json:"priority"`
Source *string `json:"source,omitempty"`
ScheduledAt time.Time `json:"scheduled_at"`
ExpiresAt *time.Time `json:"expires_at,omitempty"`
ReadAt *time.Time `json:"read_at,omitempty"`
ArchivedAt *time.Time `json:"archived_at,omitempty"`
SentAt *time.Time `json:"sent_at,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// InboxMessageList is a paginated list of inbox messages.
type InboxMessageList struct {
Results []InboxMessage `json:"results"`
Total int `json:"total"`
Limit int `json:"limit"`
Offset int `json:"offset"`
}
// InboxCount holds unread and total visible inbox message counts.
type InboxCount struct {
Unread int `json:"unread"`
Total int `json:"total"`
}
// DeviceOS is the operating system a registered device runs on.
type DeviceOS string
// Supported device operating systems.
const (
DeviceOSWeb DeviceOS = "web"
DeviceOSIOS DeviceOS = "ios"
DeviceOSAndroid DeviceOS = "android"
)
// DeviceKeys holds the Web Push subscription keys.
type DeviceKeys struct {
P256dh string `json:"p256dh"`
Auth string `json:"auth"`
}
// DeviceConfig is the transport configuration for a device's push subscription.
type DeviceConfig struct {
// Token is the device token for FCM or APNs.
Token *string `json:"token,omitempty"`
// Endpoint is the Web Push subscription endpoint URL.
Endpoint *string `json:"endpoint,omitempty"`
// ExpirationTime is when the subscription expires, if known.
ExpirationTime *time.Time `json:"expiration_time,omitempty"`
// Keys holds the Web Push subscription keys.
Keys *DeviceKeys `json:"keys,omitempty"`
}
// DeviceRegistration registers or updates a device's push subscription for a user.
type DeviceRegistration struct {
// Identifier targets the user the device belongs to.
Identifier []ExternalID `json:"identifier"`
// DeviceID is the caller's stable identifier for the device.
DeviceID string `json:"device_id"`
// Config is the device's push transport configuration.
Config DeviceConfig `json:"config"`
// OS is the device operating system (web, ios, android).
OS *DeviceOS `json:"os,omitempty"`
// OSVersion is the device operating system version.
OSVersion *string `json:"os_version,omitempty"`
// Model is the device model.
Model *string `json:"model,omitempty"`
// AppVersion is the version of the app the device runs.
AppVersion *string `json:"app_version,omitempty"`
// Data holds arbitrary device attributes.
Data json.RawMessage `json:"data,omitempty"`
}
// VapidPublicKey is the project's VAPID public key for Web Push.
type VapidPublicKey struct {
PublicKey string `json:"public_key"`
}
// CreateSessionRequest mints a session token for an end user.
type CreateSessionRequest struct {
// UserID is the end user's external identifier (the session subject).
UserID string `json:"user_id"`
}
// SessionToken is a minted session token: a short-lived bearer token for the
// Client API whose permissions are defined by the session auth method (policy).
type SessionToken struct {
Token string `json:"token"`
ExpiresAt time.Time `json:"expires_at"`
}