v0.2.0: bubble for Android 0.2.0 — chat search, swipe for unread, faster chat opening
Desktop: - Search lists chats whose own name matches first, so a named group isn't pushed out of the top 8 by chats that merely include a member. Android (Rabbit R1) app, 0.2.0: - Pull the chat list past its top for a search box (name, person, number, then group members; archived chats too; Back clears it). - Swipe right to left marks a chat unread / read over a magenta band; left to right still archives (SwipeActions replaces SwipeArchive). - Google Voice chats don't offer voice notes in the paperclip bubble. - Chats open with their newest 60 messages, then the full history; marking read (and the Google Voice network call) waits until they're on screen. Cached pictures show at once, and one slow download no longer blocks the other images. Fixed a contacts reload race. - Sandbox data gains filler chats, a named group and a 4,000-message chat. Also: the Android app, Google Voice support, media, export, emoji and the other work since the first commit, and a CLAUDE.md carrying the privacy rule.
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
// MMS attachments cached on disk in <dataDir>/media, next to the database, so
|
||||
// backups include them and they outlive voip.ms's copies. Files are named by
|
||||
// content hash; messages reference them via MediaRef.file.
|
||||
import { nativeImage, net } from 'electron'
|
||||
import crypto from 'node:crypto'
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import { extFor, guessMime } from '@shared/mime'
|
||||
import type { MediaRef } from '@shared/types'
|
||||
import type { MessageDb, NewMessage } from './db'
|
||||
|
||||
export class MediaStore {
|
||||
private inflight = new Map<string, Promise<MediaRef | undefined>>()
|
||||
|
||||
constructor(
|
||||
readonly dir: string,
|
||||
private db: MessageDb,
|
||||
// How to fetch a MediaRef.url; Google Voice's gv:<id> refs need its session.
|
||||
private fetchUrl: (url: string) => Promise<Response> = (url) => net.fetch(url)
|
||||
) {}
|
||||
|
||||
filePath(file: string): string {
|
||||
return path.join(this.dir, path.basename(file))
|
||||
}
|
||||
|
||||
save(data: Uint8Array, mime: string): string {
|
||||
const file = `${crypto.createHash('sha1').update(data).digest('hex')}.${extFor(mime)}`
|
||||
fs.mkdirSync(this.dir, { recursive: true })
|
||||
const dest = this.filePath(file)
|
||||
if (!fs.existsSync(dest)) fs.writeFileSync(dest, data)
|
||||
return file
|
||||
}
|
||||
|
||||
// The local copy of attachment `index` of message `id`, downloading it first
|
||||
// if we only have the provider's URL. Undefined when it can't be had.
|
||||
async resolve(id: number, index: number): Promise<MediaRef | undefined> {
|
||||
const ref = this.db.getMessage(id)?.media?.[index]
|
||||
if (!ref) return undefined
|
||||
if (ref.file && fs.existsSync(this.filePath(ref.file))) return ref
|
||||
if (!ref.url) return undefined
|
||||
const key = `${id}/${index}`
|
||||
let p = this.inflight.get(key)
|
||||
if (!p) {
|
||||
p = this.download(id, index, ref.url).finally(() => this.inflight.delete(key))
|
||||
this.inflight.set(key, p)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
private async download(id: number, index: number, url: string): Promise<MediaRef | undefined> {
|
||||
try {
|
||||
const res = await this.fetchUrl(url)
|
||||
const header = res.headers.get('content-type')?.split(';')[0].trim()
|
||||
// voip.ms answers expired/unknown media with a 200 HTML page.
|
||||
if (!res.ok || header === 'text/html') return undefined
|
||||
const mime = header && header !== 'application/octet-stream' ? header : (guessMime(url) ?? 'application/octet-stream')
|
||||
const data = new Uint8Array(await res.arrayBuffer())
|
||||
const file = this.save(data, mime)
|
||||
// Remember the size so the chat can reserve space before the image loads.
|
||||
const patch: MediaRef = { file, mime }
|
||||
if (mime.startsWith('image/')) {
|
||||
const size = nativeImage.createFromBuffer(Buffer.from(data)).getSize()
|
||||
if (size.width) Object.assign(patch, size)
|
||||
}
|
||||
this.db.setMediaFile(id, index, patch)
|
||||
return { url, ...patch }
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
// Cache attachments of freshly synced messages right away, one at a time.
|
||||
prefetch(msgs: NewMessage[]): void {
|
||||
const todo = msgs.flatMap((m) => (m.media ?? []).map((_, i) => [m.id, i] as const))
|
||||
void (async () => {
|
||||
for (const [id, i] of todo) await this.resolve(id, i)
|
||||
})()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user