11import type { Table } from "dexie" ;
22import type Dexie from "dexie" ;
33import { Mutex } from "async-mutex" ;
4- import type { Client , ConsentState } from "@xmtp/xmtp-js" ;
4+ import type { Client , ConsentState , ConsentListEntryType } from "@xmtp/xmtp-js" ;
55import { ConsentListEntry } from "@xmtp/xmtp-js" ;
66
77export type CachedConsentEntry = {
8- peerAddress : string ;
8+ type : ConsentListEntryType ;
9+ value : string ;
910 state : ConsentState ;
1011 walletAddress : string ;
1112} ;
1213
1314export type CachedConsentTable = Table < CachedConsentEntry , string > ;
1415
1516export type CachedConsentEntryMap = {
16- [ peerAddress : string ] : ConsentListEntry ;
17+ [ value : string ] : ConsentListEntry ;
1718} ;
1819
1920/**
20- * Retrieve a cached consent entry by wallet and peer address
21+ * Retrieve a cached consent entry by wallet address, type, and value
2122 *
2223 * @returns The cached consent entry if found, otherwise `undefined`
2324 */
2425export const getCachedConsentEntry = async (
2526 walletAddress : string ,
26- peerAddress : string ,
27+ type : ConsentListEntryType ,
28+ value : string ,
2729 db : Dexie ,
2830) => {
2931 const consentTable = db . table ( "consent" ) as CachedConsentTable ;
3032 return consentTable
3133 . where ( {
3234 walletAddress,
33- peerAddress,
35+ type,
36+ value,
3437 } )
3538 . first ( ) ;
3639} ;
3740
3841/**
39- * Retrieve all cached consent entries
42+ * Retrieve all cached consent entries for a given wallet address
4043 *
4144 * @returns An array of ConsentListEntry instances
4245 */
@@ -47,7 +50,7 @@ export const getCachedConsentEntries = async (
4750 const consentTable = db . table ( "consent" ) as CachedConsentTable ;
4851 const entries = await consentTable . where ( { walletAddress } ) . toArray ( ) ;
4952 return entries . map ( ( entry ) =>
50- ConsentListEntry . fromAddress ( entry . peerAddress , entry . state ) ,
53+ ConsentListEntry . fromAddress ( entry . value , entry . state ) ,
5154 ) ;
5255} ;
5356
@@ -72,16 +75,17 @@ export const getCachedConsentEntriesMap = async (
7275} ;
7376
7477/**
75- * Retrieve a cached consent state by wallet and peer address
78+ * Retrieve a cached consent state by wallet address, type, and value
7679 *
7780 * @returns The cached consent state if found, otherwise `undefined`
7881 */
7982export const getCachedConsentState = async (
8083 walletAddress : string ,
81- peerAddress : string ,
84+ type : ConsentListEntryType ,
85+ value : string ,
8286 db : Dexie ,
8387) => {
84- const entry = await getCachedConsentEntry ( walletAddress , peerAddress , db ) ;
88+ const entry = await getCachedConsentEntry ( walletAddress , type , value , db ) ;
8589 return entry ?. state ?? "unknown" ;
8690} ;
8791
@@ -97,21 +101,22 @@ export const loadConsentListFromCache = async (client: Client, db: Dexie) => {
97101const putConsentStateMutex = new Mutex ( ) ;
98102
99103/**
100- * Add or update a peer address's consent state
104+ * Add or update a consent state
101105 */
102106export const putConsentState = async (
103107 walletAddress : string ,
104- peerAddress : string ,
108+ type : ConsentListEntryType ,
109+ value : string ,
105110 state : ConsentState ,
106111 db : Dexie ,
107112) =>
108113 putConsentStateMutex . runExclusive ( async ( ) => {
109114 const consentTable = db . table ( "consent" ) as CachedConsentTable ;
110- await consentTable . put ( { peerAddress , state, walletAddress } ) ;
115+ await consentTable . put ( { type , value , state, walletAddress } ) ;
111116 } ) ;
112117
113118/**
114- * Add or update multiple peer addresses' consent state
119+ * Add or update multiple consent states
115120 */
116121export const bulkPutConsentState = async (
117122 entries : CachedConsentEntry [ ] ,
0 commit comments