feat(web): add spaces, grouped channels and file chat
This commit is contained in:
1 parent
c7d8ef15bc
commit
f6cfb12716
111 files changed
+4371
-506
No files matched your search
@@ -0,0 +1,34 @@
|
||||
import { openDB, type DBSchema } from 'idb';
|
||||
import { apiPath } from './http';
|
||||
|
||||
export interface RememberedAccount {
|
||||
username: string;
|
||||
token: string;
|
||||
key: CryptoKey;
|
||||
vault: string;
|
||||
revision: number;
|
||||
}
|
||||
interface LocalIdentity extends DBSchema {
|
||||
sessions: { key: string; value: RememberedAccount };
|
||||
device: { key: string; value: { id: string; name: string } };
|
||||
}
|
||||
const database = () => openDB<LocalIdentity>('webdrop-account-local-2', 1, { upgrade(db) {
|
||||
db.createObjectStore('sessions'); db.createObjectStore('device');
|
||||
} });
|
||||
const endpoint = () => apiPath('/');
|
||||
export async function rememberedAccount(): Promise<RememberedAccount | undefined> { return (await database()).get('sessions', endpoint()); }
|
||||
export async function rememberAccount(value: RememberedAccount): Promise<void> { await (await database()).put('sessions', value, endpoint()); }
|
||||
export async function forgetAccount(): Promise<void> { await (await database()).delete('sessions', endpoint()); }
|
||||
export async function localAccountDevice(): Promise<{ id: string; name: string }> {
|
||||
const db = await database();
|
||||
const existing = await db.get('device', endpoint());
|
||||
if (existing) return existing;
|
||||
const name = /Android/i.test(navigator.userAgent) ? 'Android 浏览器' : /iPhone|iPad/i.test(navigator.userAgent) ? 'Apple 移动浏览器'
|
||||
: /Windows/i.test(navigator.userAgent) ? 'Windows 浏览器' : /Mac/i.test(navigator.userAgent) ? 'Mac 浏览器' : '浏览器设备';
|
||||
const value = { id: crypto.randomUUID(), name };
|
||||
await db.put('device', value, endpoint());
|
||||
return value;
|
||||
}
|
||||
export async function saveDeviceName(name: string): Promise<void> {
|
||||
const device = await localAccountDevice(); await (await database()).put('device', { ...device, name }, endpoint());
|
||||
}
|
||||
Reference in new issue
Block a user