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:
2026-11-06 02:19:56 -08:00
parent a2b8f764db
commit 8c84feaa91
114 changed files with 12987 additions and 379 deletions
+265
View File
@@ -0,0 +1,265 @@
// Google Voice's private web API (what voice.google.com itself calls), as
// worked out by mautrix-gvoice. Unofficial: Google can change it at any time,
// so everything here fails soft and only ever takes Google Voice down.
//
// Requests go through an Electron session that holds the Google sign-in
// cookies (see ./session.ts); this class only needs a fetch that sends them
// and a way to read the SAPISID cookie for the Authorization header.
import crypto from 'node:crypto'
import { decode, fromPblite, toPblite, type Decoded, type Schema } from './proto'
import * as S from './schema'
export const ORIGIN = 'https://voice.google.com'
const API_BASE = 'https://clients6.google.com/voice/v1/voiceclient'
const WAA_BASE = 'https://waa-pa.clients6.google.com/$rpc/google.internal.waa.v1.Waa'
// Constants from the web client (pkg/libgv/constants.go).
const API_KEY = 'AIzaSyDTYc1N4xiODyrQYK0Kl6g_y279LjYkrBg'
const WAA_API_KEY = 'AIzaSyBGb5fGAyC-pRcRU6MUHb__b_vKha71HRE'
export const WAA_REQUEST_KEY = '/JR8jsAkqotcKsEKhXic'
const CLIENT_VERSION = '665865172'
export const USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
const CLIENT_DETAILS = new URLSearchParams({ appVersion: '5.0 (X11)', platform: 'Linux x86_64', userAgent: USER_AGENT }).toString()
const PBLITE = 'application/json+protobuf'
const PROTOBUF = 'application/x-protobuf'
export interface Transport {
fetch: (url: string, init: RequestInit) => Promise<Response>
cookie: (name: string) => Promise<string | undefined>
}
export class GVoiceError extends Error {
constructor(
message: string,
readonly status?: number, // HTTP status, when Google answered
readonly code?: string // Chromium network error, when it didn't
) {
super(message)
}
get signedOut(): boolean {
return this.status === 401 || this.status === 403
}
}
export function sapisidHash(sapisid: string, now = Date.now(), origin = ORIGIN): string {
const ts = Math.floor(now / 1000)
const hash = crypto.createHash('sha1').update(`${ts} ${sapisid} ${origin}`).digest('hex')
return `SAPISIDHASH ${ts}_${hash}`
}
// A random id below 10^14, like the web client's.
export function transactionId(): number {
return Math.floor(Math.random() * 1e14)
}
// GV wants E.164; we store bare 10-digit NANP numbers.
export function toE164(n: string): string {
const d = n.replace(/\D/g, '')
if (d.length === 10) return `+1${d}`
return `+${d}`
}
// Text responses wrap the payload as "<length>\n<data>", the length counted in
// UTF-16 code units (JS string units), and protobuf inside them is base64.
export async function readBody(res: Response): Promise<{ mime: string; data: Uint8Array | string }> {
const plain = (res.headers.get('content-type') ?? '').split(';')[0].trim()
const safety = (res.headers.get('x-goog-safety-content-type') ?? '').split(';')[0].trim()
const mime = safety || plain
if (plain === 'text/plain') {
const text = await res.text()
const nl = text.indexOf('\n')
const len = Number(text.slice(0, nl))
const chunk = nl > 0 && Number.isFinite(len) ? text.slice(nl + 1, nl + 1 + len) : text
return { mime, data: mime === PROTOBUF ? new Uint8Array(Buffer.from(chunk, 'base64')) : chunk }
}
if (mime === PROTOBUF) return { mime, data: new Uint8Array(await res.arrayBuffer()) }
return { mime, data: await res.text() }
}
export function decodeBody(schema: Schema, { mime, data }: { mime: string; data: Uint8Array | string }): Decoded {
if (data instanceof Uint8Array) return decode(schema, data)
if (mime === PBLITE || mime === 'application/json' || mime === 'text/plain') return fromPblite(schema, JSON.parse(data) as unknown[])
throw new GVoiceError(`Google Voice sent an unexpected response (${mime || 'no content type'}).`)
}
export interface SendSmsRequest {
threadId: string // '' to start a new thread with `recipients`
recipients: string[] // E.164
text: string
media?: { mime: string; data: Uint8Array }
transactionId: number
trackingData?: string
}
export class GVoice {
constructor(private t: Transport) {}
private async headers(url: URL, contentType?: string): Promise<Record<string, string>> {
// Origin is added by the session (see session.ts): Chromium refuses it here.
const h: Record<string, string> = {
'X-Goog-AuthUser': '0',
Referer: ORIGIN + '/',
Accept: '*/*'
}
if (contentType) h['Content-Type'] = contentType
if (url.host === 'clients6.google.com') {
h['X-Client-Version'] = CLIENT_VERSION
h['X-ClientDetails'] = CLIENT_DETAILS
h['X-JavaScript-User-Agent'] = 'google-api-javascript-client/1.1.0'
h['X-Requested-With'] = 'XMLHttpRequest'
h['X-Goog-Encode-Response-If-Executable'] = 'base64'
} else if (url.host.startsWith('waa-pa.')) {
h['X-Goog-Api-Key'] = WAA_API_KEY
h['X-User-Agent'] = 'grpc-web-javascript/0.1'
}
const sapisid = await this.t.cookie('SAPISID')
if (!sapisid) throw new GVoiceError('Not signed in to Google Voice.', 401)
h['Authorization'] = sapisidHash(sapisid)
return h
}
private async request(url: string, init: { method?: string; body?: string; contentType?: string } = {}): Promise<Response> {
const u = new URL(url)
if (u.host === 'clients6.google.com') {
u.searchParams.set('key', API_KEY)
u.searchParams.set('alt', 'proto')
}
let res: Response
try {
res = await this.t.fetch(u.toString(), {
method: init.method ?? 'POST',
headers: await this.headers(u, init.contentType),
body: init.body,
credentials: 'include'
})
} catch (e) {
if (e instanceof GVoiceError) throw e
const msg = (e as Error).message ?? String(e)
throw new GVoiceError(msg, undefined, /net::(ERR_\w+)/.exec(msg)?.[1] ?? (e as Error).name)
}
if (!res.ok) {
const detail = await res.text().catch(() => '')
console.warn(`[gvoice] ${u.pathname} → ${res.status}`, detail.slice(0, 300))
throw new GVoiceError(httpMessage(res.status), res.status)
}
return res
}
private async call(endpoint: string, reqSchema: Schema, req: Decoded, respSchema: Schema): Promise<Decoded> {
const res = await this.request(endpoint, { body: JSON.stringify(toPblite(reqSchema, req)), contentType: PBLITE })
return decodeBody(respSchema, await readBody(res))
}
// Our Google Voice number, E.164.
async getAccount(): Promise<string> {
const r = await this.call(`${API_BASE}/account/get`, S.ReqGetAccount, { unknownInt2: 1 }, S.RespGetAccount)
const acc = r.account as { primaryDestinationId: string; phones: Array<{ accountPhone?: { phoneNumber?: { e164: string } } }> }
const number = acc.phones.map((p) => p.accountPhone?.phoneNumber?.e164).find(Boolean) ?? acc.primaryDestinationId
if (!number) throw new GVoiceError("This Google account doesn't have a Google Voice number.")
return number
}
async listThreads(paginationToken = ''): Promise<{ threads: S.GvThread[]; paginationToken: string }> {
const r = await this.call(
`${API_BASE}/api2thread/list`,
S.ReqListThreads,
{ folder: S.ThreadFolder.TEXT_THREADS, unknownInt2: 20, unknownInt3: 15, paginationToken, unknownWrapper: { unknownInt2: 1, unknownInt3: 1 } },
S.RespListThreads
)
return { threads: r.threads as S.GvThread[], paginationToken: r.paginationToken as string }
}
async getThread(threadId: string, count = 100, paginationToken = ''): Promise<S.GvThread> {
const r = await this.call(
`${API_BASE}/api2thread/get`,
S.ReqGetThread,
{ threadId, maybeMessageCount: count, paginationToken, unknownWrapper: { unknownInt2: 1, unknownInt3: 1 } },
S.RespGetThread
)
return r.thread as S.GvThread
}
async sendSms(req: SendSmsRequest): Promise<{ threadId: string; threadItemId: string; timestampMs: number }> {
const body: Decoded = {
text: req.text,
threadId: req.threadId,
recipients: req.threadId ? [] : req.recipients,
transactionId: { id: req.transactionId },
trackingData: { data: req.trackingData || '!' }
}
if (req.media) {
const type = S.SendMediaType[req.media.mime]
if (!type) throw new GVoiceError(`Google Voice can't send ${req.media.mime} attachments, only photos.`, 400)
body.media = { type, data: req.media.data }
}
const r = await this.call(`${API_BASE}/api2thread/sendsms`, S.ReqSendSMS, body, S.RespSendSMS)
return r as { threadId: string; threadItemId: string; timestampMs: number }
}
async markRead(threadId: string): Promise<void> {
await this.call(
`${API_BASE}/thread/updateattributes`,
S.ReqUpdateAttributes,
{ attributes: { threadId, read: true }, otherAttributes: { read: true }, unknownInt: 1 },
S.RespEmpty
)
}
async downloadMedia(id: string): Promise<Response> {
return this.request(`${ORIGIN}/u/0/a/i/${encodeURIComponent(id)}?s=${S.SizeSpec.ORIGINAL}`, { method: 'GET' })
}
// ---- WAA (BotGuard) signing, see ./waa.ts ----
async createWaa(): Promise<{ interpreterUrl: string; interpreterHash: string; program: string; globalName: string }> {
const r = await this.call(`${WAA_BASE}/Create`, S.ReqCreateWaa, { requestKey: WAA_REQUEST_KEY }, S.RespCreateWaa)
const w = r.waa as { interpreterUrl: { url: string }; interpreterHash: string; program: string; globalName: string }
return { interpreterUrl: w.interpreterUrl.url, interpreterHash: w.interpreterHash, program: w.program, globalName: w.globalName }
}
async pingWaa(payload: string): Promise<void> {
await this.call(
`${WAA_BASE}/Ping`,
S.ReqPingWaa,
{ requestKey: WAA_REQUEST_KEY, payload, i1: 72, i2: Math.floor(Math.random() * 2e9) },
S.RespEmpty
)
}
}
function httpMessage(status: number): string {
if (status === 401 || status === 403) return 'Google Voice signed out. Reconnect it in Settings.'
if (status === 429) return 'Google Voice is rate-limiting messages. Try again in a bit.'
if (status === 404) return 'Google Voice could not find that conversation.'
if (status >= 500) return `Google Voice had a server error (${status}).`
return `Google Voice rejected the request (${status}).`
}
// Network errors where the request certainly never reached Google.
const NOT_SENT = new Set([
'ERR_INTERNET_DISCONNECTED',
'ERR_NAME_NOT_RESOLVED',
'ERR_NAME_RESOLUTION_FAILED',
'ERR_CONNECTION_REFUSED',
'ERR_ADDRESS_UNREACHABLE',
'ERR_PROXY_CONNECTION_FAILED'
])
// For sendWithRetry: 'safe' = certainly not sent, retry; 'unknown' = may have
// gone out, look before resending; 'fatal' = give up.
export function classifyGvError(e: unknown): 'safe' | 'unknown' | 'fatal' {
if (!(e instanceof GVoiceError)) return 'unknown'
if (e.status === undefined) return e.code && NOT_SENT.has(e.code) ? 'safe' : 'unknown'
if (e.status === 429) return 'safe'
if (e.status >= 500) return 'unknown'
return 'fatal'
}
export function gvErrorMessage(e: unknown): string {
const err = e as Error
if (err instanceof GVoiceError) {
if (err.status === undefined && err.code && NOT_SENT.has(err.code)) return "Couldn't reach Google Voice. Check your internet connection."
return err.message
}
return err?.message || String(e)
}