Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Drop a muted bot's waiting pushes, and re-check the mute before resending

AdityaUmaleb02bf13a53ba
diff --git a/server/phone-push.test.ts b/server/phone-push.test.ts
index c2fcc301c2..bc17cb8820 100644
--- a/server/phone-push.test.ts
+++ b/server/phone-push.test.ts
@@ -291,6 +291,44 @@ describe("offering a notify frame", () => {
expect(push.pending()).toBe(0);
});
+ it("drops a bot's waiting pushes when the phone mutes it, so unmuting before the retry does not send the old one", async () => {
+ const { push, relay } = harness();
+ await push.register(PHONE, { platform: "apns", token: APNS });
+ relay.answers.push("unreachable", "unreachable");
+ push.offer({ notification: notification({ threadId: "th_a" }), seq: 1 }, everyone);
+ push.offer({ notification: notification({ botId: "bot_2", botName: "Atlas", threadId: "th_b" }), seq: 2 }, everyone);
+ await push.flush();
+ expect(push.pending()).toBe(2);
+ await push.register(PHONE, { platform: "apns", token: APNS, mutedBots: ["bot_1"] });
+ expect(push.pending()).toBe(1);
+ await push.register(PHONE, { platform: "apns", token: APNS });
+ for (const wait of PUSH_RETRY_MS) await advance(push, wait);
+ expect(relay.sends().map((call) => (call.body.notification as { botId: string }).botId)).toEqual(["bot_1", "bot_2", "bot_2"]);
+ expect(push.pending()).toBe(0);
+ });
+
+ it("does not resend after an unknown-device recovery when the phone muted the bot while it registered again", async () => {
+ const relay = fakeRelay();
+ let muted = false;
+ const { push } = harness({
+ fetchImpl: (async (input: string | URL | Request, init?: RequestInit) => {
+ const answer = await relay.fetchImpl(input, init);
+ if (!muted && String(input).endsWith("/register") && relay.sends().length === 1) {
+ muted = true;
+ await push.register(PHONE, { platform: "apns", token: APNS, mutedBots: ["bot_1"] });
+ }
+ return answer;
+ }) as typeof fetch,
+ });
+ await push.register(PHONE, { platform: "apns", token: APNS });
+ relay.answers.push({ status: 404, error: PUSH_RELAY_ERRORS.unknownDevice });
+ push.offer({ notification: notification(), seq: 1 }, everyone);
+ await push.flush();
+ expect(muted).toBe(true);
+ expect(relay.calls.map((call) => call.path.split("/").at(-1))).toEqual(["register", "send", "register", "register"]);
+ expect(push.pending()).toBe(0);
+ });
+
it("tells the relay a bot's push may use the bot's own Android channel, only for bots the phone made one for", async () => {
const { push, relay } = harness();
expect((await push.register(OTHER, { platform: "fcm", token: FCM, botChannels: ["bot_1", "bot_1"] })).device?.botChannels).toEqual(["bot_1"]);
diff --git a/server/phone-push.ts b/server/phone-push.ts
index dd0edc5ad1..7d12c22a1e 100644
--- a/server/phone-push.ts
+++ b/server/phone-push.ts
@@ -324,6 +324,7 @@ export class PhonePush {
};
this.devices = [...others, next];
this.persist();
+ for (const [slot, job] of this.outbox) if (job.key === key && next.mutedBots?.includes(job.push.botId)) this.outbox.delete(slot);
if (previous && previous.token !== next.token) this.dropFromRelay(previous);
if (this.status() !== "on") return { ...this.answer(key), relay: "off" };
const answer = await this.relay(PHONE_PUSH_RELAY_PATHS.register, this.registerBody(next));
@@ -459,7 +460,13 @@ export class PhonePush {
let answer = await this.relay(PHONE_PUSH_RELAY_PATHS.send, body);
if (answer?.status === 404 && answer.code === PUSH_RELAY_ERRORS.unknownDevice) {
const registered = await this.relay(PHONE_PUSH_RELAY_PATHS.register, this.registerBody(device));
- if (registered && registered.status < 300) answer = await this.relay(PHONE_PUSH_RELAY_PATHS.send, body);
+ if (registered && registered.status < 300) {
+ // The phone may have gone, or muted this bot, while it registered again.
+ const current = this.devices.find((candidate) => candidate.key === job.key);
+ if (!current) return "gone";
+ if (current.mutedBots?.includes(job.push.botId)) return "dropped";
+ answer = await this.relay(PHONE_PUSH_RELAY_PATHS.send, body);
+ }
}
if (!answer) return "retry";
if (answer.status < 300) {