v0.5.1: bubble for Android 0.5.1 — no double sends when a text with an emoji is cut off mid-send

voip.ms's listing drops emoji, so a send whose reply was lost (here the
laptop slept 8 s after Send) never matched its own copy in the listing and
went out a second time. Match with the emoji taken out (exact match first),
and after an uncertain send only send again once a listing check actually
got through; if none can, mark it failed with a note instead.
This commit is contained in:
2026-11-07 14:05:23 -08:00
parent 96c368dfd1
commit 2e2086e178
12 changed files with 132 additions and 29 deletions
@@ -26,7 +26,12 @@ final class Sender {
private static final int MAX_ATTEMPTS = 3; private static final int MAX_ATTEMPTS = 3;
private static final long[] RETRY_DELAYS = {3000, 10_000}; 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. */ /** A photo already saved into files/media. */
static final class Photo { static final class Photo {
@@ -158,10 +163,18 @@ final class Sender {
if (store.getMessage(tempId) == null) return null; // a check found it already sent if (store.getMessage(tempId) == null) return null; // a check found it already sent
String kind = classify(e, gv); String kind = classify(e, gv);
if (kind.equals("unknown")) { 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) { for (long ms : CHECK_DELAYS) {
sleep(ms); sleep(ms);
Sync.poll(c); Sync.poll(c);
if ((gv ? Sync.gvError : Sync.voipError) == null) looked++;
if (store.getMessage(tempId) == null) return null; 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) { if (kind.equals("fatal") || n >= MAX_ATTEMPTS) {
@@ -217,14 +217,35 @@ final class Store {
sorted.sort(Comparator.comparingLong(a -> a.ts)); sorted.sort(Comparator.comparingLong(a -> a.ts));
for (Msg m : sorted) { for (Msg m : sorted) {
if (m.in() || m.id < 0) continue; 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'" // voip.ms's listing drops emoji, so an exact match first, then one without them.
+ " AND did = ? AND contact = ? AND body = ? AND ts BETWEEN ? AND ? ORDER BY ts, id DESC LIMIT 1", long exact = 0, loose = 0;
new String[] {m.did, m.contact, m.body, String.valueOf(m.ts - MATCH_AFTER), String.valueOf(m.ts + MATCH_BEFORE)})) { String key = withoutEmoji(m.body);
if (c.moveToFirst()) confirmPending(c.getLong(0), m.id); 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) { void deleteMessage(long id) {
db.execSQL("DELETE FROM messages WHERE id = ?", new Object[] {id}); db.execSQL("DELETE FROM messages WHERE id = ?", new Object[] {id});
} }
+2 -2
View File
@@ -26,8 +26,8 @@ import sys
import zipfile import zipfile
from pathlib import Path from pathlib import Path
VERSION_NAME = "0.5.0" VERSION_NAME = "0.5.1"
VERSION_CODE = 5 VERSION_CODE = 6
PACKAGE = "me.teafry.voipmessages" PACKAGE = "me.teafry.voipmessages"
DEV_PACKAGE = PACKAGE + ".dev" DEV_PACKAGE = PACKAGE + ".dev"
+1 -1
View File
@@ -1,7 +1,7 @@
{ {
"name": "voipms-desktop", "name": "voipms-desktop",
"productName": "bubble", "productName": "bubble",
"version": "0.5.0", "version": "0.5.1",
"description": "Signal-style desktop SMS client for voip.ms", "description": "Signal-style desktop SMS client for voip.ms",
"author": "trav <trav@teafry.me>", "author": "trav <trav@teafry.me>",
"private": true, "private": true,
Binary file not shown.
+1 -1
View File
@@ -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"}
+22 -4
View File
@@ -47,6 +47,20 @@ const MESSAGE_COLS = 'id, did, contact, direction, body, ts, kind, status, media
const MATCH_BEFORE = 2 * 60_000 const MATCH_BEFORE = 2 * 60_000
const MATCH_AFTER = 15 * 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 { function fromRow({ media_json, error, sender, ...m }: MessageRow): Message {
return { return {
...m, ...m,
@@ -250,16 +264,20 @@ export class MessageDb {
// Returns the local ids that were replaced. // Returns the local ids that were replaced.
reconcileSent(added: NewMessage[]): number[] { reconcileSent(added: NewMessage[]): number[] {
const find = this.db.prepare( 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' WHERE id < 0 AND status IN ('pending', 'failed') AND direction = 'out'
AND did = ? AND contact = ? AND body = ? AND ts BETWEEN ? AND ? AND did = ? AND contact = ? AND ts BETWEEN ? AND ?
ORDER BY ts, id DESC LIMIT 1` ORDER BY ts, id DESC`
) )
const claimed: number[] = [] const claimed: number[] = []
this.db.transaction(() => { this.db.transaction(() => {
for (const m of [...added].sort((a, b) => a.ts - b.ts || a.id - b.id)) { for (const m of [...added].sort((a, b) => a.ts - b.ts || a.id - b.id)) {
if (m.direction !== 'out' || m.id < 0) continue 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 if (!row) continue
this.confirmPending(row.id, m.id) this.confirmPending(row.id, m.id)
claimed.push(row.id) claimed.push(row.id)
+8 -5
View File
@@ -69,7 +69,7 @@ export class GvSyncEngine extends EventEmitter {
private burstUntil = 0 private burstUntil = 0
private running = false private running = false
private backfilling = false private backfilling = false
private polling?: Promise<void> private polling?: Promise<boolean>
private status: GvStatus private status: GvStatus
constructor( constructor(
@@ -158,11 +158,12 @@ export class GvSyncEngine extends EventEmitter {
}, d) }, d)
} }
async pollNow(): Promise<void> { // Resolves false when the listing couldn't be fetched.
await this.poll() pollNow(): Promise<boolean> {
return this.poll()
} }
private poll(): Promise<void> { private poll(): Promise<boolean> {
this.polling ??= this.fetchRecent().finally(() => (this.polling = undefined)) this.polling ??= this.fetchRecent().finally(() => (this.polling = undefined))
return this.polling 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 // 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 // them were known yet and the thread already has history here, more may
// have arrived than the list shows, so fetch the thread itself. // have arrived than the list shows, so fetch the thread itself.
private async fetchRecent(): Promise<void> { private async fetchRecent(): Promise<boolean> {
try { try {
const { threads } = await this.gv.listThreads() const { threads } = await this.gv.listThreads()
for (const t of threads) { for (const t of threads) {
@@ -185,7 +186,9 @@ export class GvSyncEngine extends EventEmitter {
await this.syncReadStateOnce() await this.syncReadStateOnce()
} catch (e) { } catch (e) {
this.fail(e) this.fail(e)
return false
} }
return true
} }
// Walk every text thread (resumable page by page), and each thread's // Walk every text thread (resumable page by page), and each thread's
+16 -5
View File
@@ -6,16 +6,20 @@ const MAX_ATTEMPTS = 3
// Pause before sending again after an attempt that certainly didn't go out. // Pause before sending again after an attempt that certainly didn't go out.
const RETRY_DELAYS = [3000, 10_000] const RETRY_DELAYS = [3000, 10_000]
// After an attempt that may have gone out (no reply), look for it in the // After an attempt that may have gone out (no reply), look for it in the
// listing at these points before sending again. // listing at these points before sending again. Two looks that got through
const CHECK_DELAYS = [5000, 15_000] // 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 { export interface SendDeps {
db: Pick<MessageDb, 'getMessage' | 'confirmPending' | 'markFailed'> db: Pick<MessageDb, 'getMessage' | 'confirmPending' | 'markFailed'>
// One attempt; resolves with the id voip.ms assigned. // One attempt; resolves with the id voip.ms assigned.
attempt: () => Promise<number> attempt: () => Promise<number>
// Fetch recent messages, which replaces the local copy of anything that // Fetch recent messages, which replaces the local copy of anything that
// turns out to have been sent (MessageDb.reconcileSent). // turns out to have been sent (MessageDb.reconcileSent). false = couldn't.
checkSent: () => Promise<void> checkSent: () => Promise<boolean | void>
sleep?: (ms: number) => Promise<void> sleep?: (ms: number) => Promise<void>
// How to read the provider's errors (default: voip.ms's). // How to read the provider's errors (default: voip.ms's).
classify?: (e: unknown) => SendFailure classify?: (e: unknown) => SendFailure
@@ -40,10 +44,17 @@ export async function sendWithRetry(
if (sent()) return if (sent()) return
const kind = classify(e) const kind = classify(e)
if (kind === 'unknown') { 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) { for (const ms of CHECK_DELAYS) {
await sleep(ms) await sleep(ms)
await checkSent().catch(() => undefined) if ((await checkSent().catch(() => false)) !== false) looked++
if (sent()) return if (sent()) return
if (looked >= CHECKS_NEEDED) break
}
if (!looked) {
db.markFailed(tempId, UNCHECKED)
return
} }
} }
if (kind === 'fatal' || n >= MAX_ATTEMPTS) { if (kind === 'fatal' || n >= MAX_ATTEMPTS) {
+8 -5
View File
@@ -62,7 +62,7 @@ export class SyncEngine extends EventEmitter {
private burstUntil = 0 private burstUntil = 0
private running = false private running = false
private backfilling = false private backfilling = false
private polling?: Promise<void> private polling?: Promise<boolean>
private status: SyncStatus private status: SyncStatus
constructor( constructor(
@@ -126,16 +126,17 @@ export class SyncEngine extends EventEmitter {
// Manual refresh. Joins a check that's already under way rather than // Manual refresh. Joins a check that's already under way rather than
// returning early, so the caller knows when fresh results are in. // returning early, so the caller knows when fresh results are in.
async pollNow(): Promise<void> { // Resolves false when the listing couldn't be fetched.
await this.poll() pollNow(): Promise<boolean> {
return this.poll()
} }
private poll(): Promise<void> { private poll(): Promise<boolean> {
this.polling ??= this.fetchRecent().finally(() => (this.polling = undefined)) this.polling ??= this.fetchRecent().finally(() => (this.polling = undefined))
return this.polling return this.polling
} }
private async fetchRecent(): Promise<void> { private async fetchRecent(): Promise<boolean> {
try { try {
const now = Date.now() const now = Date.now()
const rows = await this.api.getMessages({ from: ymd(now - DAY), to: ymd(now + DAY), limit: PAGE_LIMIT }) 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' }) this.setStatus({ lastPoll: Date.now(), error: undefined, phase: this.backfilling ? 'backfill' : 'polling' })
} catch (e) { } catch (e) {
this.setStatus({ phase: 'error', error: (e as Error).message }) this.setStatus({ phase: 'error', error: (e as Error).message })
return false
} }
return true
} }
private async backfill(): Promise<void> { private async backfill(): Promise<void> {
+11
View File
@@ -239,6 +239,17 @@ describe('MessageDb', () => {
expect(db.getMessage(t)?.status).toBe('failed') 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', () => { it('matches identical texts one to one, oldest first', () => {
const t1 = pending('ok', 10 * MIN) const t1 = pending('ok', 10 * MIN)
const t2 = pending('ok', 10 * MIN + 1) const t2 = pending('ok', 10 * MIN + 1)
+24 -1
View File
@@ -3,7 +3,7 @@ import os from 'node:os'
import path from 'node:path' import path from 'node:path'
import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { MessageDb } from '../src/main/db' 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' import { VoipMsError } from '../src/main/voipms'
let dir: string let dir: string
@@ -59,6 +59,29 @@ describe('sendWithRetry', () => {
expect(db.getConversationMessages('5550001111', 'a').map((m) => m.id)).toEqual([99]) 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<boolean> => {
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 () => { it('resends right away when the request never left', async () => {
const attempt = attempts(offline(), 99) const attempt = attempts(offline(), 99)
await sendWithRetry(tempId, { db, attempt, checkSent: async () => undefined, sleep }) await sendWithRetry(tempId, { db, attempt, checkSent: async () => undefined, sleep })