Skip to content

Commit dc9d4e6

Browse files
fix(relay): preserve legacy jobs with current APNs routing
1 parent 4743626 commit dc9d4e6

2 files changed

Lines changed: 22 additions & 13 deletions

File tree

‎infra/relay/src/agentActivity/ApnsDeliveries.test.ts‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,17 +1978,18 @@ describe("fast completion delivery", () => {
19781978

19791979
describe("signed APNs registration metadata", () => {
19801980
for (const kind of ["live_activity_update", "push_notification"] as const) {
1981-
for (const changed of ["bundle", "environment"] as const) {
1982-
it.effect(`skips ${kind} when the ${changed} changes without token rotation`, () => {
1981+
for (const changed of ["bundle", "environment", "legacy"] as const) {
1982+
it.effect(`routes ${kind} using current registration with ${changed} job metadata`, () => {
19831983
const attempts: DeliveryAttempts.DeliveryAttemptInput[] = [];
1984-
let sent = 0;
1984+
const requests: HttpClientRequest.HttpClientRequest[] = [];
19851985
const payload = makeApnsDeliveryJobPayload({
19861986
kind,
19871987
userId: target.user_id,
19881988
deviceId: target.device_id,
19891989
token: "unchanged-token",
1990-
bundleId: "com.t3tools.t3code.dev",
1991-
apsEnvironment: "sandbox",
1990+
...(changed === "legacy"
1991+
? {}
1992+
: { bundleId: "com.t3tools.t3code.dev", apsEnvironment: "sandbox" as const }),
19921993
aggregate: kind === "live_activity_update" ? aggregate : null,
19931994
...(kind === "push_notification"
19941995
? {
@@ -2012,8 +2013,14 @@ describe("signed APNs registration metadata", () => {
20122013
return Effect.gen(function* () {
20132014
const deliveries = yield* ApnsDeliveries.ApnsDeliveries;
20142015
const result = yield* deliveries.processSignedJob(signed);
2015-
expect(sent).toBe(0);
2016-
expect(result.apnsReason).toBe("Stale APNs delivery job skipped.");
2016+
expect(result.ok).toBe(true);
2017+
expect(requests).toHaveLength(1);
2018+
expect(requests[0]?.url).toBe(
2019+
`${changed === "environment" ? "https://api.push.apple.com" : "https://api.sandbox.push.apple.com"}/3/device/unchanged-token`,
2020+
);
2021+
expect(requests[0]?.headers["apns-topic"]).toBe(
2022+
`${changed === "bundle" ? "com.t3tools.t3code.preview" : "com.t3tools.t3code.dev"}${kind === "live_activity_update" ? ".push-type.liveactivity" : ""}`,
2023+
);
20172024
}).pipe(
20182025
Effect.provide(
20192026
makeLayer({
@@ -2031,7 +2038,7 @@ describe("signed APNs registration metadata", () => {
20312038
],
20322039
execute: (request) =>
20332040
Effect.sync(() => {
2034-
sent++;
2041+
requests.push(request);
20352042
return HttpClientResponse.fromWeb(request, new Response("", { status: 200 }));
20362043
}),
20372044
}),

‎infra/relay/src/agentActivity/ApnsDeliveries.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -666,9 +666,7 @@ export const make = Effect.gen(function* () {
666666
Effect.map((targets) => {
667667
const currentTarget = targets.find((row) => row.device_id === input.target.device_id);
668668
return currentTarget &&
669-
expectedCurrentToken({ target: currentTarget, kind: input.kind }) === input.token &&
670-
currentTarget.bundle_id === input.target.bundle_id &&
671-
currentTarget.aps_environment === input.target.aps_environment
669+
expectedCurrentToken({ target: currentTarget, kind: input.kind }) === input.token
672670
? currentTarget
673671
: null;
674672
}),
@@ -683,6 +681,7 @@ export const make = Effect.gen(function* () {
683681
"relay.delivery.kind": input.kind,
684682
...(input.sourceJobId ? { "relay.delivery.job_id": input.sourceJobId } : {}),
685683
});
684+
let deliveryTarget = input.target;
686685
const now = yield* DateTime.now;
687686
const aggregate =
688687
input.aggregate === null ? null : sanitizeAgentActivityAggregateState(input.aggregate);
@@ -724,6 +723,7 @@ export const make = Effect.gen(function* () {
724723
});
725724
return staleJobResult({ deviceId: input.target.device_id, kind: input.kind });
726725
}
726+
deliveryTarget = currentTarget;
727727
if (alert) {
728728
const preferences = parsePreferences(currentTarget.preferences_json);
729729
const previousAggregate = parseAggregate(currentTarget.last_aggregate_json);
@@ -781,7 +781,7 @@ export const make = Effect.gen(function* () {
781781
);
782782
const result = yield* apns
783783
.sendLiveActivityRequest({
784-
credentials: credentialsForTarget(config.apns, input.target),
784+
credentials: credentialsForTarget(config.apns, deliveryTarget),
785785
request,
786786
issuedAtUnixSeconds: epochSeconds,
787787
})
@@ -847,6 +847,7 @@ export const make = Effect.gen(function* () {
847847
"relay.delivery.kind": "push_notification",
848848
...(input.sourceJobId ? { "relay.delivery.job_id": input.sourceJobId } : {}),
849849
});
850+
let deliveryTarget = input.target;
850851
const now = yield* DateTime.now;
851852
const epochSeconds = Math.floor(now.epochMilliseconds / 1_000);
852853
const notification = sanitizeApnsNotificationPayload(input.notification);
@@ -916,6 +917,7 @@ export const make = Effect.gen(function* () {
916917
kind: "push_notification",
917918
});
918919
}
920+
deliveryTarget = currentTarget;
919921
const preferences = parsePreferences(currentTarget.preferences_json);
920922
const alertAllowed =
921923
notification.phase !== undefined && notification.updatedAt !== undefined
@@ -937,7 +939,7 @@ export const make = Effect.gen(function* () {
937939
}
938940
const result = yield* apns
939941
.sendPushNotificationRequest({
940-
credentials: credentialsForTarget(config.apns, input.target),
942+
credentials: credentialsForTarget(config.apns, deliveryTarget),
941943
request,
942944
issuedAtUnixSeconds: epochSeconds,
943945
})

0 commit comments

Comments
 (0)