migrate auth v2 code
This commit is contained in:
@@ -7,8 +7,18 @@ interface ChatInit {
|
||||
actorType: 'admin' | 'member';
|
||||
actorName: string;
|
||||
actorColor: string;
|
||||
deviceToken?: string;
|
||||
memberId?: string;
|
||||
pbToken?: string;
|
||||
|
||||
}
|
||||
|
||||
// Client id for optimistic chat messages. `crypto.randomUUID()` requires a
|
||||
// secure context (HTTPS/localhost) — over plain HTTP on a LAN it's undefined,
|
||||
// so fall back to a time+random string that's still unique enough per session.
|
||||
function genClientId(): string {
|
||||
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
|
||||
return crypto.randomUUID();
|
||||
}
|
||||
return Date.now().toString(36) + Math.random().toString(36).slice(2);
|
||||
}
|
||||
|
||||
class ChatStore {
|
||||
@@ -23,8 +33,7 @@ class ChatStore {
|
||||
actorType = $state<'admin' | 'member'>('member');
|
||||
actorName = $state('');
|
||||
actorColor = $state('');
|
||||
deviceToken = $state('');
|
||||
memberId = $state('');
|
||||
pbToken = $state('');
|
||||
|
||||
private unsubs: (() => void)[] = [];
|
||||
private destroyed = false;
|
||||
@@ -46,8 +55,7 @@ class ChatStore {
|
||||
this.actorType = opts.actorType;
|
||||
this.actorName = opts.actorName;
|
||||
this.actorColor = opts.actorColor;
|
||||
this.deviceToken = opts.deviceToken || '';
|
||||
this.memberId = opts.memberId || '';
|
||||
this.pbToken = opts.pbToken || '';
|
||||
|
||||
if (this.initialized && this.famId === opts.famId) return;
|
||||
if (this.initPromise) {
|
||||
@@ -185,7 +193,7 @@ class ChatStore {
|
||||
async send(content: string) {
|
||||
const text = content.trim();
|
||||
if (!text || !this.famId) return;
|
||||
const clientId = crypto.randomUUID();
|
||||
const clientId = genClientId();
|
||||
const temp: ChatMessage = {
|
||||
id: 'temp-' + clientId,
|
||||
famId: this.famId,
|
||||
|
||||
@@ -13,7 +13,7 @@ import type {
|
||||
} from '$lib/types';
|
||||
|
||||
type CollectionName =
|
||||
| 'members'
|
||||
| 'users'
|
||||
| 'chore_templates'
|
||||
| 'assigned_chores'
|
||||
| 'completions'
|
||||
@@ -96,9 +96,9 @@ class FamStore {
|
||||
rewardsRes,
|
||||
seasonsRes
|
||||
] = await Promise.all([
|
||||
pb.collection('members').getFullList({ filter: `famId = '${famId}'` }) as Promise<
|
||||
Member[]
|
||||
>,
|
||||
pb.collection('users').getFullList({
|
||||
filter: `famId = '${famId}' && role = 'child'`
|
||||
}) as Promise<Member[]>,
|
||||
pb.collection('chore_templates').getFullList({ filter: `famId = '${famId}'` }) as Promise<
|
||||
ChoreTemplate[]
|
||||
>,
|
||||
@@ -144,19 +144,18 @@ class FamStore {
|
||||
}
|
||||
|
||||
private async subscribe() {
|
||||
const subs: { collection: CollectionName; filter?: string }[] = [
|
||||
{ collection: 'members', filter: this.famId },
|
||||
{ collection: 'chore_templates', filter: this.famId },
|
||||
{ collection: 'assigned_chores', filter: this.famId },
|
||||
{ collection: 'completions', filter: this.famId },
|
||||
{ collection: 'bonus_configs', filter: this.famId },
|
||||
{ collection: 'bonus_templates', filter: this.famId },
|
||||
{ collection: 'rewards', filter: this.famId },
|
||||
{ collection: 'seasons', filter: this.famId }
|
||||
const subs: { collection: CollectionName; filter: string }[] = [
|
||||
{ collection: 'users', filter: `famId = '${this.famId}' && role = 'child'` },
|
||||
{ collection: 'chore_templates', filter: `famId = '${this.famId}'` },
|
||||
{ collection: 'assigned_chores', filter: `famId = '${this.famId}'` },
|
||||
{ collection: 'completions', filter: `famId = '${this.famId}'` },
|
||||
{ collection: 'bonus_configs', filter: `famId = '${this.famId}'` },
|
||||
{ collection: 'bonus_templates', filter: `famId = '${this.famId}'` },
|
||||
{ collection: 'rewards', filter: `famId = '${this.famId}'` },
|
||||
{ collection: 'seasons', filter: `famId = '${this.famId}'` }
|
||||
];
|
||||
|
||||
const promises = subs.map(({ collection, filter }) => {
|
||||
const filterStr = filter ? `famId = '${filter}'` : '';
|
||||
return pb
|
||||
.collection(collection)
|
||||
.subscribe(
|
||||
@@ -165,7 +164,7 @@ class FamStore {
|
||||
if (this.destroyed) return;
|
||||
this.handleRealtime(collection, data.action, data.record);
|
||||
},
|
||||
{ filter: filterStr || undefined }
|
||||
{ filter: filter || undefined }
|
||||
)
|
||||
.then((unsub) => {
|
||||
if (this.destroyed) {
|
||||
@@ -194,7 +193,7 @@ class FamStore {
|
||||
};
|
||||
|
||||
switch (collection) {
|
||||
case 'members':
|
||||
case 'users':
|
||||
this.members = apply(this.members);
|
||||
break;
|
||||
case 'chore_templates':
|
||||
|
||||
Reference in New Issue
Block a user