inbox-list.ts (3717B)
1 import { canonicalPublicKey } from '../contracts/public-key.ts'; 2 import { boundedUtf8 } from '../contracts/food-availability-v1/text.ts'; 3 import { LOCAL_PERSISTENCE_BUDGETS } from '../config/budgets.ts'; 4 import { localUnreadRumors } from './unread.ts'; 5 import type { ConversationData } from './admit-conversation.ts'; 6 export type InboxListRow = Readonly<{ 7 peer: string; 8 count: number; 9 unread: number; 10 latest: ConversationData; 11 }>; 12 export type InboxListSnapshot = Readonly<{ 13 status: 'ready' | 'locked' | 'unavailable'; 14 requests: readonly InboxListRow[]; 15 conversations: readonly InboxListRow[]; 16 }>; 17 const unavailable = (): InboxListSnapshot => ({ 18 status: 'unavailable', 19 requests: [], 20 conversations: [] 21 }); 22 function arrayValue(value: unknown): boolean { 23 return Array.isArray(value); 24 } 25 // Pure detached presentation. These values never authenticate a sender or grant 26 // storage, navigation, decryption, query or reply permission. 27 export function groupInboxMessages( 28 owner: unknown, 29 messages: readonly ConversationData[], 30 read: readonly string[] 31 ): InboxListSnapshot { 32 if ( 33 !canonicalPublicKey(owner) || 34 !arrayValue(messages) || 35 !arrayValue(read) || 36 messages.length > LOCAL_PERSISTENCE_BUDGETS.receivedEnvelopes 37 ) 38 return unavailable(); 39 const rows = new Map<string, ConversationData>(), 40 wires = new Map<string, string>(); 41 for (const row of messages) { 42 if ( 43 !row || 44 row.owner !== owner || 45 !canonicalPublicKey(row.peer) || 46 row.peer === owner || 47 !['inbound', 'self_archive'].includes(row.role) || 48 row.sender !== (row.role === 'inbound' ? row.peer : owner) || 49 row.recipient !== (row.role === 'inbound' ? owner : row.peer) || 50 typeof row.rumorId !== 'string' || 51 !/^[0-9a-f]{64}$/.test(row.rumorId) || 52 !Number.isSafeInteger(row.createdAt) || 53 row.createdAt < 0 || 54 typeof row.content !== 'string' || 55 !boundedUtf8(row.content, 4096) || 56 (row.subject !== null && typeof row.subject !== 'string') || 57 (row.replyTo !== null && 58 (typeof row.replyTo !== 'string' || 59 !/^[0-9a-f]{64}$/.test(row.replyTo))) 60 ) 61 return unavailable(); 62 const wire = JSON.stringify([ 63 row.owner, 64 row.peer, 65 row.sender, 66 row.recipient, 67 row.role, 68 row.rumorId, 69 row.createdAt, 70 row.content, 71 row.subject, 72 row.replyTo 73 ]), 74 old = wires.get(row.rumorId); 75 if (old !== undefined && old !== wire) return unavailable(); 76 wires.set(row.rumorId, wire); 77 rows.set(row.rumorId, { ...row }); 78 } 79 const grouped = new Map<string, ConversationData[]>(); 80 for (const row of rows.values()) 81 grouped.set(row.peer, (grouped.get(row.peer) ?? []).concat(row)); 82 let requests = Array.from<InboxListRow>([]), 83 conversations = Array.from<InboxListRow>([]); 84 for (const [peer, group] of grouped) { 85 const ordered = group 86 .slice() 87 .sort( 88 (a, b) => 89 b.createdAt - a.createdAt || b.rumorId.localeCompare(a.rumorId) 90 ), 91 row = { 92 peer, 93 count: group.length, 94 unread: localUnreadRumors(group, owner as string, read).length, 95 latest: { ...ordered[0] } 96 }; 97 if (group.some((value) => value.role === 'self_archive')) 98 conversations = conversations.concat(row); 99 else requests = requests.concat(row); 100 } 101 const order = (a: InboxListRow, b: InboxListRow) => 102 b.latest.createdAt - a.latest.createdAt || 103 b.latest.rumorId.localeCompare(a.latest.rumorId) || 104 a.peer.localeCompare(b.peer); 105 return { 106 status: 'ready', 107 requests: requests.sort(order), 108 conversations: conversations.sort(order) 109 }; 110 }