| ... | ... | @@ -3,7 +3,10 @@ import type { |
| 3 | 3 | RelayDeliveryKind, |
| 4 | 4 | RelayLiveActivityRegistrationRequest, |
| 5 | 5 | } from "@t3tools/contracts/relay"; |
| 6 | | import { RelayAgentActivityAggregateState as RelayAgentActivityAggregateStateSchema } from "@t3tools/contracts/relay"; |
| 6 | import { |
| 7 | RelayAgentActivityAggregateState as RelayAgentActivityAggregateStateSchema, |
| 8 | RelayDeliveryKind as RelayDeliveryKindSchema, |
| 9 | } from "@t3tools/contracts/relay"; |
| 7 | 10 | import * as Context from "effect/Context"; |
| 8 | 11 | import * as DateTime from "effect/DateTime"; |
| 9 | 12 | import * as Effect from "effect/Effect"; |
| ... | ... | @@ -17,28 +20,46 @@ import { relayLiveActivities, relayMobileDevices } from "../persistence/schema.t |
| 17 | 20 | |
| 18 | 21 | export class LiveActivityRegistrationPersistenceError extends Schema.TaggedErrorClass<LiveActivityRegistrationPersistenceError>()( |
| 19 | 22 | "LiveActivityRegistrationPersistenceError", |
| 20 | | { cause: Schema.Defect() }, |
| 23 | { |
| 24 | userId: Schema.String, |
| 25 | deviceId: Schema.String, |
| 26 | cause: Schema.Defect(), |
| 27 | }, |
| 21 | 28 | ) { |
| 22 | 29 | override get message(): string { |
| 23 | | return "Failed to persist Live Activity registration"; |
| 30 | return `Failed to persist Live Activity registration for user ${this.userId} and device ${this.deviceId}.`; |
| 24 | 31 | } |
| 25 | 32 | } |
| 26 | 33 | |
| 27 | 34 | export class LiveActivityTargetListPersistenceError extends Schema.TaggedErrorClass<LiveActivityTargetListPersistenceError>()( |
| 28 | 35 | "LiveActivityTargetListPersistenceError", |
| 29 | | { cause: Schema.Defect() }, |
| 36 | { |
| 37 | userId: Schema.String, |
| 38 | cause: Schema.Defect(), |
| 39 | }, |
| 30 | 40 | ) { |
| 31 | 41 | override get message(): string { |
| 32 | | return "Failed to list Live Activity delivery targets"; |
| 42 | return `Failed to list Live Activity delivery targets for user ${this.userId}.`; |
| 33 | 43 | } |
| 34 | 44 | } |
| 35 | 45 | |
| 36 | 46 | export class LiveActivityDeliveryMarkPersistenceError extends Schema.TaggedErrorClass<LiveActivityDeliveryMarkPersistenceError>()( |
| 37 | 47 | "LiveActivityDeliveryMarkPersistenceError", |
| 38 | | { cause: Schema.Defect() }, |
| 48 | { |
| 49 | operation: Schema.Literals([ |
| 50 | "mark-delivery", |
| 51 | "mark-start-queued", |
| 52 | "clear-start-queued", |
| 53 | "invalidate-delivery-token", |
| 54 | ]), |
| 55 | userId: Schema.String, |
| 56 | deviceId: Schema.String, |
| 57 | kind: Schema.NullOr(RelayDeliveryKindSchema), |
| 58 | cause: Schema.Defect(), |
| 59 | }, |
| 39 | 60 | ) { |
| 40 | 61 | override get message(): string { |
| 41 | | return "Failed to persist Live Activity delivery state"; |
| 62 | return `Failed to persist Live Activity state during ${this.operation} for user ${this.userId} and device ${this.deviceId}.`; |
| 42 | 63 | } |
| 43 | 64 | } |
| 44 | 65 | |
| ... | ... | @@ -110,11 +131,11 @@ export const make = Effect.gen(function* () { |
| 110 | 131 | const db = yield* RelayDb.RelayDb; |
| 111 | 132 | |
| 112 | 133 | return LiveActivities.of({ |
| 113 | | register: Effect.fn("relay.live_activities.register")( |
| 114 | | function* (input) { |
| 115 | | yield* Effect.annotateCurrentSpan({ |
| 116 | | "relay.mobile.device_id": input.registration.deviceId, |
| 117 | | }); |
| 134 | register: Effect.fn("relay.live_activities.register")(function* (input) { |
| 135 | yield* Effect.annotateCurrentSpan({ |
| 136 | "relay.mobile.device_id": input.registration.deviceId, |
| 137 | }); |
| 138 | yield* Effect.gen(function* () { |
| 118 | 139 | const updatedAt = DateTime.formatIso(yield* DateTime.now); |
| 119 | 140 | const registration = input.registration; |
| 120 | 141 | |
| ... | ... | @@ -155,9 +176,17 @@ export const make = Effect.gen(function* () { |
| 155 | 176 | updatedAt, |
| 156 | 177 | }, |
| 157 | 178 | }); |
| 158 | | }, |
| 159 | | Effect.mapError((cause) => new LiveActivityRegistrationPersistenceError({ cause })), |
| 160 | | ), |
| 179 | }).pipe( |
| 180 | Effect.mapError( |
| 181 | (cause) => |
| 182 | new LiveActivityRegistrationPersistenceError({ |
| 183 | userId: input.userId, |
| 184 | deviceId: input.registration.deviceId, |
| 185 | cause, |
| 186 | }), |
| 187 | ), |
| 188 | ); |
| 189 | }), |
| 161 | 190 | |
| 162 | 191 | listTargets: Effect.fn("relay.live_activities.list_targets")(function* (input) { |
| 163 | 192 | return yield* db |
| ... | ... | @@ -207,16 +236,22 @@ export const make = Effect.gen(function* () { |
| 207 | 236 | ), |
| 208 | 237 | ), |
| 209 | 238 | Effect.map((rows): ReadonlyArray<TargetRow> => rows), |
| 210 | | Effect.mapError((cause) => new LiveActivityTargetListPersistenceError({ cause })), |
| 239 | Effect.mapError( |
| 240 | (cause) => |
| 241 | new LiveActivityTargetListPersistenceError({ |
| 242 | userId: input.userId, |
| 243 | cause, |
| 244 | }), |
| 245 | ), |
| 211 | 246 | ); |
| 212 | 247 | }), |
| 213 | 248 | |
| 214 | | markDelivery: Effect.fn("relay.live_activities.mark_delivery")( |
| 215 | | function* (input) { |
| 216 | | yield* Effect.annotateCurrentSpan({ |
| 217 | | "relay.mobile.device_id": input.deviceId, |
| 218 | | "relay.delivery.kind": input.kind, |
| 219 | | }); |
| 249 | markDelivery: Effect.fn("relay.live_activities.mark_delivery")(function* (input) { |
| 250 | yield* Effect.annotateCurrentSpan({ |
| 251 | "relay.mobile.device_id": input.deviceId, |
| 252 | "relay.delivery.kind": input.kind, |
| 253 | }); |
| 254 | yield* Effect.gen(function* () { |
| 220 | 255 | const aggregateJson = |
| 221 | 256 | input.aggregate === null |
| 222 | 257 | ? null |
| ... | ... | @@ -257,9 +292,19 @@ export const make = Effect.gen(function* () { |
| 257 | 292 | updatedAt: input.deliveredAt, |
| 258 | 293 | }, |
| 259 | 294 | }); |
| 260 | | }, |
| 261 | | Effect.mapError((cause) => new LiveActivityDeliveryMarkPersistenceError({ cause })), |
| 262 | | ), |
| 295 | }).pipe( |
| 296 | Effect.mapError( |
| 297 | (cause) => |
| 298 | new LiveActivityDeliveryMarkPersistenceError({ |
| 299 | operation: "mark-delivery", |
| 300 | userId: input.userId, |
| 301 | deviceId: input.deviceId, |
| 302 | kind: input.kind, |
| 303 | cause, |
| 304 | }), |
| 305 | ), |
| 306 | ); |
| 307 | }), |
| 263 | 308 | |
| 264 | 309 | markStartQueued: Effect.fn("relay.live_activities.mark_start_queued")(function* (input) { |
| 265 | 310 | yield* Effect.annotateCurrentSpan({ |
| ... | ... | @@ -287,7 +332,18 @@ export const make = Effect.gen(function* () { |
| 287 | 332 | updatedAt: input.queuedAt, |
| 288 | 333 | }, |
| 289 | 334 | }) |
| 290 | | .pipe(Effect.mapError((cause) => new LiveActivityDeliveryMarkPersistenceError({ cause }))); |
| 335 | .pipe( |
| 336 | Effect.mapError( |
| 337 | (cause) => |
| 338 | new LiveActivityDeliveryMarkPersistenceError({ |
| 339 | operation: "mark-start-queued", |
| 340 | userId: input.userId, |
| 341 | deviceId: input.deviceId, |
| 342 | kind: null, |
| 343 | cause, |
| 344 | }), |
| 345 | ), |
| 346 | ); |
| 291 | 347 | }), |
| 292 | 348 | |
| 293 | 349 | clearStartQueued: Effect.fn("relay.live_activities.clear_start_queued")(function* (input) { |
| ... | ... | @@ -303,7 +359,18 @@ export const make = Effect.gen(function* () { |
| 303 | 359 | eq(relayLiveActivities.deviceId, input.deviceId), |
| 304 | 360 | ), |
| 305 | 361 | ) |
| 306 | | .pipe(Effect.mapError((cause) => new LiveActivityDeliveryMarkPersistenceError({ cause }))); |
| 362 | .pipe( |
| 363 | Effect.mapError( |
| 364 | (cause) => |
| 365 | new LiveActivityDeliveryMarkPersistenceError({ |
| 366 | operation: "clear-start-queued", |
| 367 | userId: input.userId, |
| 368 | deviceId: input.deviceId, |
| 369 | kind: null, |
| 370 | cause, |
| 371 | }), |
| 372 | ), |
| 373 | ); |
| 307 | 374 | }), |
| 308 | 375 | |
| 309 | 376 | invalidateDeliveryToken: Effect.fn("relay.live_activities.invalidate_delivery_token")( |
| ... | ... | @@ -312,39 +379,58 @@ export const make = Effect.gen(function* () { |
| 312 | 379 | "relay.mobile.device_id": input.deviceId, |
| 313 | 380 | "relay.delivery.kind": input.kind, |
| 314 | 381 | }); |
| 315 | | if (input.kind === "push_notification") { |
| 316 | | yield* db |
| 317 | | .update(relayMobileDevices) |
| 318 | | .set({ |
| 319 | | pushToken: null, |
| 320 | | updatedAt: input.invalidatedAt, |
| 321 | | }) |
| 322 | | .where( |
| 323 | | and( |
| 324 | | eq(relayMobileDevices.userId, input.userId), |
| 325 | | eq(relayMobileDevices.deviceId, input.deviceId), |
| 326 | | ), |
| 327 | | ); |
| 328 | | return; |
| 329 | | } |
| 382 | yield* Effect.gen(function* () { |
| 383 | if (input.kind === "push_notification") { |
| 384 | yield* db |
| 385 | .update(relayMobileDevices) |
| 386 | .set({ |
| 387 | pushToken: null, |
| 388 | updatedAt: input.invalidatedAt, |
| 389 | }) |
| 390 | .where( |
| 391 | and( |
| 392 | eq(relayMobileDevices.userId, input.userId), |
| 393 | eq(relayMobileDevices.deviceId, input.deviceId), |
| 394 | ), |
| 395 | ); |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | if (input.kind === "live_activity_start") { |
| 400 | yield* db |
| 401 | .update(relayMobileDevices) |
| 402 | .set({ |
| 403 | pushToStartToken: null, |
| 404 | updatedAt: input.invalidatedAt, |
| 405 | }) |
| 406 | .where( |
| 407 | and( |
| 408 | eq(relayMobileDevices.userId, input.userId), |
| 409 | eq(relayMobileDevices.deviceId, input.deviceId), |
| 410 | ), |
| 411 | ); |
| 412 | yield* db |
| 413 | .update(relayLiveActivities) |
| 414 | .set({ |
| 415 | remoteStartQueuedAt: null, |
| 416 | updatedAt: input.invalidatedAt, |
| 417 | }) |
| 418 | .where( |
| 419 | and( |
| 420 | eq(relayLiveActivities.userId, input.userId), |
| 421 | eq(relayLiveActivities.deviceId, input.deviceId), |
| 422 | ), |
| 423 | ); |
| 424 | return; |
| 425 | } |
| 330 | 426 | |
| 331 | | if (input.kind === "live_activity_start") { |
| 332 | | yield* db |
| 333 | | .update(relayMobileDevices) |
| 334 | | .set({ |
| 335 | | pushToStartToken: null, |
| 336 | | updatedAt: input.invalidatedAt, |
| 337 | | }) |
| 338 | | .where( |
| 339 | | and( |
| 340 | | eq(relayMobileDevices.userId, input.userId), |
| 341 | | eq(relayMobileDevices.deviceId, input.deviceId), |
| 342 | | ), |
| 343 | | ); |
| 344 | 427 | yield* db |
| 345 | 428 | .update(relayLiveActivities) |
| 346 | 429 | .set({ |
| 430 | activityPushToken: null, |
| 347 | 431 | remoteStartQueuedAt: null, |
| 432 | remoteStartedAt: null, |
| 433 | endedAt: input.invalidatedAt, |
| 348 | 434 | updatedAt: input.invalidatedAt, |
| 349 | 435 | }) |
| 350 | 436 | .where( |
| ... | ... | @@ -353,26 +439,19 @@ export const make = Effect.gen(function* () { |
| 353 | 439 | eq(relayLiveActivities.deviceId, input.deviceId), |
| 354 | 440 | ), |
| 355 | 441 | ); |
| 356 | | return; |
| 357 | | } |
| 358 | | |
| 359 | | yield* db |
| 360 | | .update(relayLiveActivities) |
| 361 | | .set({ |
| 362 | | activityPushToken: null, |
| 363 | | remoteStartQueuedAt: null, |
| 364 | | remoteStartedAt: null, |
| 365 | | endedAt: input.invalidatedAt, |
| 366 | | updatedAt: input.invalidatedAt, |
| 367 | | }) |
| 368 | | .where( |
| 369 | | and( |
| 370 | | eq(relayLiveActivities.userId, input.userId), |
| 371 | | eq(relayLiveActivities.deviceId, input.deviceId), |
| 372 | | ), |
| 373 | | ); |
| 442 | }).pipe( |
| 443 | Effect.mapError( |
| 444 | (cause) => |
| 445 | new LiveActivityDeliveryMarkPersistenceError({ |
| 446 | operation: "invalidate-delivery-token", |
| 447 | userId: input.userId, |
| 448 | deviceId: input.deviceId, |
| 449 | kind: input.kind, |
| 450 | cause, |
| 451 | }), |
| 452 | ), |
| 453 | ); |
| 374 | 454 | }, |
| 375 | | Effect.mapError((cause) => new LiveActivityDeliveryMarkPersistenceError({ cause })), |
| 376 | 455 | ), |
| 377 | 456 | }); |
| 378 | 457 | }); |