diff --git a/packages/homeserver/src/procedures/autorization/ensureAuthorizationRules.ts b/packages/homeserver/src/procedures/autorization/ensureAuthorizationRules.ts index 4ce12cd..31ffc15 100644 --- a/packages/homeserver/src/procedures/autorization/ensureAuthorizationRules.ts +++ b/packages/homeserver/src/procedures/autorization/ensureAuthorizationRules.ts @@ -31,6 +31,29 @@ export const ensureAuthorizationRulesBatch = function* ( } }; +export const ensureAuthorizationRulesAndStoreBatch = async ( + events: { + insertMany: (events: EventStore[]) => Promise; + }, + authChain: EventBase[], + roomId: string, + size = 100, +) => { + for await (const eventsToBeStored of ensureAuthorizationRulesBatch( + authChain, + roomId, + size, + )) { + await events.insertMany( + [...eventsToBeStored.entries()].map(([key, event]) => ({ + _id: key, + event, + outlier: true, + })), + ); + } +}; + export const ensureAuthorizationRules = async ( events: EventBase[], roomId: string, @@ -71,11 +94,11 @@ export const ensureAuthorizationRules = async ( } } - const eventsToBeStored = new Set(); + const eventsToBeStored = new Map(); for await (const event of sortedAuthEvents) { try { - if (await prep(event, authMap)) { - eventsToBeStored.add(generateId(event)); + if (await checkEventAuthorization(event, authMap)) { + eventsToBeStored.set(generateId(event), event); } } catch (e) { console.log("error", e); @@ -85,7 +108,10 @@ export const ensureAuthorizationRules = async ( return eventsToBeStored; }; -async function prep(event: EventBase, authMap: Map) { +export async function checkEventAuthorization( + event: EventBase, + authMap: Map, +) { const authEvents = new Map(); for (const authEventId of event.auth_events) { const ae = authMap.get(authEventId);