diff --git a/android/app/src/main/java/me/teafry/voipmessages/Sender.java b/android/app/src/main/java/me/teafry/voipmessages/Sender.java index c750c64..ffacaa3 100644 --- a/android/app/src/main/java/me/teafry/voipmessages/Sender.java +++ b/android/app/src/main/java/me/teafry/voipmessages/Sender.java @@ -26,7 +26,12 @@ final class Sender { private static final int MAX_ATTEMPTS = 3; private static final long[] RETRY_DELAYS = {3000, 10_000}; - private static final long[] CHECK_DELAYS = {5000, 15_000}; + // After a send that may have gone out (no reply), look for it in the listing + // before sending again. Two looks that got through are enough; the later + // ones are for a network that's still coming back. + private static final long[] CHECK_DELAYS = {5000, 15_000, 30_000, 60_000, 120_000}; + private static final int CHECKS_NEEDED = 2; + static final String UNCHECKED = "This may have gone out, but there was no connection to check. Retry if it didn't arrive."; /** A photo already saved into files/media. */ static final class Photo { @@ -158,10 +163,18 @@ final class Sender { if (store.getMessage(tempId) == null) return null; // a check found it already sent String kind = classify(e, gv); if (kind.equals("unknown")) { + // Never send again without having seen that it isn't in the listing. + int looked = 0; for (long ms : CHECK_DELAYS) { sleep(ms); Sync.poll(c); + if ((gv ? Sync.gvError : Sync.voipError) == null) looked++; if (store.getMessage(tempId) == null) return null; + if (looked >= CHECKS_NEEDED) break; + } + if (looked == 0) { + store.markFailed(tempId, UNCHECKED); + return UNCHECKED; } } if (kind.equals("fatal") || n >= MAX_ATTEMPTS) { diff --git a/android/app/src/main/java/me/teafry/voipmessages/Store.java b/android/app/src/main/java/me/teafry/voipmessages/Store.java index 73a3da2..7dd6cf3 100644 --- a/android/app/src/main/java/me/teafry/voipmessages/Store.java +++ b/android/app/src/main/java/me/teafry/voipmessages/Store.java @@ -217,14 +217,35 @@ final class Store { sorted.sort(Comparator.comparingLong(a -> a.ts)); for (Msg m : sorted) { if (m.in() || m.id < 0) continue; - try (Cursor c = db.rawQuery("SELECT id FROM messages WHERE id < 0 AND status IN ('pending','failed') AND direction = 'out'" - + " AND did = ? AND contact = ? AND body = ? AND ts BETWEEN ? AND ? ORDER BY ts, id DESC LIMIT 1", - new String[] {m.did, m.contact, m.body, String.valueOf(m.ts - MATCH_AFTER), String.valueOf(m.ts + MATCH_BEFORE)})) { - if (c.moveToFirst()) confirmPending(c.getLong(0), m.id); + // voip.ms's listing drops emoji, so an exact match first, then one without them. + long exact = 0, loose = 0; + String key = withoutEmoji(m.body); + try (Cursor c = db.rawQuery("SELECT id, body FROM messages WHERE id < 0 AND status IN ('pending','failed') AND direction = 'out'" + + " AND did = ? AND contact = ? AND ts BETWEEN ? AND ? ORDER BY ts, id DESC", + new String[] {m.did, m.contact, String.valueOf(m.ts - MATCH_AFTER), String.valueOf(m.ts + MATCH_BEFORE)})) { + while (c.moveToNext() && exact == 0) { + String body = c.getString(1) == null ? "" : c.getString(1); + if (body.equals(m.body)) exact = c.getLong(0); + else if (loose == 0 && withoutEmoji(body).equals(key)) loose = c.getLong(0); + } } + long id = exact != 0 ? exact : loose; + if (id != 0) confirmPending(id, m.id); } } + /** A text with its emoji (and their joiners and variation selectors) taken out, trimmed. */ + static String withoutEmoji(String s) { + if (s == null) return ""; + StringBuilder out = new StringBuilder(); + s.codePoints().forEach(c -> { + boolean emoji = c >= 0x1F000 || (c >= 0x2600 && c <= 0x27BF) || (c >= 0x2300 && c <= 0x23FF) || (c >= 0x2B00 && c <= 0x2BFF) + || (c >= 0xFE00 && c <= 0xFE0F) || c == 0x200D || c == 0x20E3; + if (!emoji) out.appendCodePoint(c); + }); + return out.toString().trim(); + } + void deleteMessage(long id) { db.execSQL("DELETE FROM messages WHERE id = ?", new Object[] {id}); } diff --git a/android/build.py b/android/build.py index 45f3b89..ea6d989 100644 --- a/android/build.py +++ b/android/build.py @@ -26,8 +26,8 @@ import sys import zipfile from pathlib import Path -VERSION_NAME = "0.5.0" -VERSION_CODE = 5 +VERSION_NAME = "0.5.1" +VERSION_CODE = 6 PACKAGE = "me.teafry.voipmessages" DEV_PACKAGE = PACKAGE + ".dev" diff --git a/package.json b/package.json index 182345b..9170a69 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "voipms-desktop", "productName": "bubble", - "version": "0.5.0", + "version": "0.5.1", "description": "Signal-style desktop SMS client for voip.ms", "author": "trav ", "private": true, diff --git a/resources/android/VoipMessages.apk b/resources/android/VoipMessages.apk index f577780..6013788 100644 Binary files a/resources/android/VoipMessages.apk and b/resources/android/VoipMessages.apk differ diff --git a/resources/android/VoipMessages.json b/resources/android/VoipMessages.json index 957e105..9cf70cb 100644 --- a/resources/android/VoipMessages.json +++ b/resources/android/VoipMessages.json @@ -1 +1 @@ -{"version_name": "0.5.0", "version_code": 5, "package": "me.teafry.voipmessages"} +{"version_name": "0.5.1", "version_code": 6, "package": "me.teafry.voipmessages"} diff --git a/src/main/db.ts b/src/main/db.ts index 08a1d16..bb2393b 100644 --- a/src/main/db.ts +++ b/src/main/db.ts @@ -47,6 +47,20 @@ const MESSAGE_COLS = 'id, did, contact, direction, body, ts, kind, status, media const MATCH_BEFORE = 2 * 60_000 const MATCH_AFTER = 15 * 60_000 +// A text with its emoji (and their joiners and variation selectors) taken +// out, trimmed: what's left of it in voip.ms's listing. +export function withoutEmoji(s: string): string { + let out = '' + for (const ch of s) { + const c = ch.codePointAt(0)! + const emoji = + c >= 0x1f000 || (c >= 0x2600 && c <= 0x27bf) || (c >= 0x2300 && c <= 0x23ff) || (c >= 0x2b00 && c <= 0x2bff) || + (c >= 0xfe00 && c <= 0xfe0f) || c === 0x200d || c === 0x20e3 + if (!emoji) out += ch + } + return out.trim() +} + function fromRow({ media_json, error, sender, ...m }: MessageRow): Message { return { ...m, @@ -250,16 +264,20 @@ export class MessageDb { // Returns the local ids that were replaced. reconcileSent(added: NewMessage[]): number[] { const find = this.db.prepare( - `SELECT id FROM messages + `SELECT id, body FROM messages WHERE id < 0 AND status IN ('pending', 'failed') AND direction = 'out' - AND did = ? AND contact = ? AND body = ? AND ts BETWEEN ? AND ? - ORDER BY ts, id DESC LIMIT 1` + AND did = ? AND contact = ? AND ts BETWEEN ? AND ? + ORDER BY ts, id DESC` ) const claimed: number[] = [] this.db.transaction(() => { for (const m of [...added].sort((a, b) => a.ts - b.ts || a.id - b.id)) { if (m.direction !== 'out' || m.id < 0) continue - const row = find.get(m.did, m.contact, m.body, m.ts - MATCH_AFTER, m.ts + MATCH_BEFORE) as { id: number } | undefined + const rows = find.all(m.did, m.contact, m.ts - MATCH_AFTER, m.ts + MATCH_BEFORE) as { id: number; body: string }[] + // voip.ms's listing drops emoji ("…everything ❤️" comes back as + // "…everything "), so an exact match first, then one without them. + const key = withoutEmoji(m.body) + const row = rows.find((r) => r.body === m.body) ?? rows.find((r) => withoutEmoji(r.body) === key) if (!row) continue this.confirmPending(row.id, m.id) claimed.push(row.id) diff --git a/src/main/gvoice/sync.ts b/src/main/gvoice/sync.ts index a7f3d1c..cb32ba8 100644 --- a/src/main/gvoice/sync.ts +++ b/src/main/gvoice/sync.ts @@ -69,7 +69,7 @@ export class GvSyncEngine extends EventEmitter { private burstUntil = 0 private running = false private backfilling = false - private polling?: Promise + private polling?: Promise private status: GvStatus constructor( @@ -158,11 +158,12 @@ export class GvSyncEngine extends EventEmitter { }, d) } - async pollNow(): Promise { - await this.poll() + // Resolves false when the listing couldn't be fetched. + pollNow(): Promise { + return this.poll() } - private poll(): Promise { + private poll(): Promise { this.polling ??= this.fetchRecent().finally(() => (this.polling = undefined)) return this.polling } @@ -170,7 +171,7 @@ export class GvSyncEngine extends EventEmitter { // The thread list carries each thread's latest few messages. If none of // them were known yet and the thread already has history here, more may // have arrived than the list shows, so fetch the thread itself. - private async fetchRecent(): Promise { + private async fetchRecent(): Promise { try { const { threads } = await this.gv.listThreads() for (const t of threads) { @@ -185,7 +186,9 @@ export class GvSyncEngine extends EventEmitter { await this.syncReadStateOnce() } catch (e) { this.fail(e) + return false } + return true } // Walk every text thread (resumable page by page), and each thread's diff --git a/src/main/send.ts b/src/main/send.ts index b10194d..745c9e5 100644 --- a/src/main/send.ts +++ b/src/main/send.ts @@ -6,16 +6,20 @@ const MAX_ATTEMPTS = 3 // Pause before sending again after an attempt that certainly didn't go out. const RETRY_DELAYS = [3000, 10_000] // After an attempt that may have gone out (no reply), look for it in the -// listing at these points before sending again. -const CHECK_DELAYS = [5000, 15_000] +// listing at these points before sending again. Two looks that got through +// are enough; the later ones are for when the network is still coming back +// (e.g. the laptop slept mid-send). +const CHECK_DELAYS = [5000, 15_000, 30_000, 60_000, 120_000] +const CHECKS_NEEDED = 2 +export const UNCHECKED = "This may have gone out, but there was no connection to check. Retry if it didn't arrive." export interface SendDeps { db: Pick // One attempt; resolves with the id voip.ms assigned. attempt: () => Promise // Fetch recent messages, which replaces the local copy of anything that - // turns out to have been sent (MessageDb.reconcileSent). - checkSent: () => Promise + // turns out to have been sent (MessageDb.reconcileSent). false = couldn't. + checkSent: () => Promise sleep?: (ms: number) => Promise // How to read the provider's errors (default: voip.ms's). classify?: (e: unknown) => SendFailure @@ -40,10 +44,17 @@ export async function sendWithRetry( if (sent()) return const kind = classify(e) if (kind === 'unknown') { + // Never send again without having seen that it isn't in the listing. + let looked = 0 for (const ms of CHECK_DELAYS) { await sleep(ms) - await checkSent().catch(() => undefined) + if ((await checkSent().catch(() => false)) !== false) looked++ if (sent()) return + if (looked >= CHECKS_NEEDED) break + } + if (!looked) { + db.markFailed(tempId, UNCHECKED) + return } } if (kind === 'fatal' || n >= MAX_ATTEMPTS) { diff --git a/src/main/sync.ts b/src/main/sync.ts index 81bd952..d165289 100644 --- a/src/main/sync.ts +++ b/src/main/sync.ts @@ -62,7 +62,7 @@ export class SyncEngine extends EventEmitter { private burstUntil = 0 private running = false private backfilling = false - private polling?: Promise + private polling?: Promise private status: SyncStatus constructor( @@ -126,16 +126,17 @@ export class SyncEngine extends EventEmitter { // Manual refresh. Joins a check that's already under way rather than // returning early, so the caller knows when fresh results are in. - async pollNow(): Promise { - await this.poll() + // Resolves false when the listing couldn't be fetched. + pollNow(): Promise { + return this.poll() } - private poll(): Promise { + private poll(): Promise { this.polling ??= this.fetchRecent().finally(() => (this.polling = undefined)) return this.polling } - private async fetchRecent(): Promise { + private async fetchRecent(): Promise { try { const now = Date.now() const rows = await this.api.getMessages({ from: ymd(now - DAY), to: ymd(now + DAY), limit: PAGE_LIMIT }) @@ -143,7 +144,9 @@ export class SyncEngine extends EventEmitter { this.setStatus({ lastPoll: Date.now(), error: undefined, phase: this.backfilling ? 'backfill' : 'polling' }) } catch (e) { this.setStatus({ phase: 'error', error: (e as Error).message }) + return false } + return true } private async backfill(): Promise { diff --git a/test/db.test.ts b/test/db.test.ts index f188e2b..7f6b48c 100644 --- a/test/db.test.ts +++ b/test/db.test.ts @@ -239,6 +239,17 @@ describe('MessageDb', () => { expect(db.getMessage(t)?.status).toBe('failed') }) + it("matches a copy that came back without its emoji, but prefers an exact one", () => { + const t = pending('see you tonight ❤️', 10 * MIN) + db.markFailed(t, 'interrupted') + expect(poll(msg(700, 'a', 'see you tonight ', 10 * MIN + 17_000, 'out'))).toEqual([t]) + const t1 = pending('ok 👍🏽', 20 * MIN) + const t2 = pending('ok', 20 * MIN + 1) + expect(poll(msg(701, 'a', 'ok', 20 * MIN + 2000, 'out'))).toEqual([t2]) + expect(db.getMessage(t1)?.status).toBe('pending') + expect(poll(msg(702, 'a', 'not ok', 20 * MIN + 3000, 'out'))).toEqual([]) + }) + it('matches identical texts one to one, oldest first', () => { const t1 = pending('ok', 10 * MIN) const t2 = pending('ok', 10 * MIN + 1) diff --git a/test/send.test.ts b/test/send.test.ts index 22d07fd..d2991a8 100644 --- a/test/send.test.ts +++ b/test/send.test.ts @@ -3,7 +3,7 @@ import os from 'node:os' import path from 'node:path' import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { MessageDb } from '../src/main/db' -import { sendWithRetry } from '../src/main/send' +import { sendWithRetry, UNCHECKED } from '../src/main/send' import { VoipMsError } from '../src/main/voipms' let dir: string @@ -59,6 +59,29 @@ describe('sendWithRetry', () => { expect(db.getConversationMessages('5550001111', 'a').map((m) => m.id)).toEqual([99]) }) + it('does not resend a text the laptop slept on, though the listing dropped its emoji', async () => { + const t = db.insertPending({ did: '5550001111', contact: 'b', direction: 'out', body: 'see you tonight ❤️', ts: 1000 }) + const cut = Object.assign(new TypeError('fetch failed'), { cause: { code: 'ECONNRESET' } }) + const attempt = attempts(cut, 99) + // Just woken: the first look can't get through, the next one can. + const looks = [false, true] + const checkSent = async (): Promise => { + const ok = looks.shift()! + if (ok) db.reconcileSent(db.upsertMessages([{ id: 88, did: '5550001111', contact: 'b', direction: 'out', body: 'see you tonight ', ts: 18_000 }])) + return ok + } + await sendWithRetry(t, { db, attempt, checkSent, sleep }) + expect(attempt.calls).toBe(1) + expect(db.getConversationMessages('5550001111', 'b').map((m) => m.id)).toEqual([88]) + }) + + it("marks it failed instead of resending when the listing can't be checked", async () => { + const attempt = attempts(timeout(), 99) + await sendWithRetry(tempId, { db, attempt, checkSent: async () => false, sleep }) + expect(attempt.calls).toBe(1) + expect(db.getMessage(tempId)).toMatchObject({ status: 'failed', error: UNCHECKED }) + }) + it('resends right away when the request never left', async () => { const attempt = attempts(offline(), 99) await sendWithRetry(tempId, { db, attempt, checkSent: async () => undefined, sleep })