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:
+22
-4
@@ -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)
|
||||
|
||||
@@ -69,7 +69,7 @@ export class GvSyncEngine extends EventEmitter {
|
||||
private burstUntil = 0
|
||||
private running = false
|
||||
private backfilling = false
|
||||
private polling?: Promise<void>
|
||||
private polling?: Promise<boolean>
|
||||
private status: GvStatus
|
||||
|
||||
constructor(
|
||||
@@ -158,11 +158,12 @@ export class GvSyncEngine extends EventEmitter {
|
||||
}, d)
|
||||
}
|
||||
|
||||
async pollNow(): Promise<void> {
|
||||
await this.poll()
|
||||
// Resolves false when the listing couldn't be fetched.
|
||||
pollNow(): Promise<boolean> {
|
||||
return this.poll()
|
||||
}
|
||||
|
||||
private poll(): Promise<void> {
|
||||
private poll(): Promise<boolean> {
|
||||
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<void> {
|
||||
private async fetchRecent(): Promise<boolean> {
|
||||
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
|
||||
|
||||
+16
-5
@@ -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<MessageDb, 'getMessage' | 'confirmPending' | 'markFailed'>
|
||||
// One attempt; resolves with the id voip.ms assigned.
|
||||
attempt: () => Promise<number>
|
||||
// Fetch recent messages, which replaces the local copy of anything that
|
||||
// turns out to have been sent (MessageDb.reconcileSent).
|
||||
checkSent: () => Promise<void>
|
||||
// turns out to have been sent (MessageDb.reconcileSent). false = couldn't.
|
||||
checkSent: () => Promise<boolean | void>
|
||||
sleep?: (ms: number) => Promise<void>
|
||||
// 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) {
|
||||
|
||||
+8
-5
@@ -62,7 +62,7 @@ export class SyncEngine extends EventEmitter {
|
||||
private burstUntil = 0
|
||||
private running = false
|
||||
private backfilling = false
|
||||
private polling?: Promise<void>
|
||||
private polling?: Promise<boolean>
|
||||
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<void> {
|
||||
await this.poll()
|
||||
// Resolves false when the listing couldn't be fetched.
|
||||
pollNow(): Promise<boolean> {
|
||||
return this.poll()
|
||||
}
|
||||
|
||||
private poll(): Promise<void> {
|
||||
private poll(): Promise<boolean> {
|
||||
this.polling ??= this.fetchRecent().finally(() => (this.polling = undefined))
|
||||
return this.polling
|
||||
}
|
||||
|
||||
private async fetchRecent(): Promise<void> {
|
||||
private async fetchRecent(): Promise<boolean> {
|
||||
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<void> {
|
||||
|
||||
Reference in New Issue
Block a user