import type { ChannelMsgDirectoryRequest } from "../../channel.js" import type { Command } from "../../synchronizer-program.js" import { getPermissionContext } from "../permission-context.js" import type { EstablishedHandlerContext } from "../types.js" import { batchAsNeeded } from "../utils.js" export function handleDirectoryRequest( _message: ChannelMsgDirectoryRequest, { channel, model, fromChannelId, permissions, logger, }: EstablishedHandlerContext, ): Command | undefined { // Filter documents based on visibility permission // We use a Result type to track both successes and errors type Result = | { success: true; docId: string } | { success: false; error: Error } const docResults: Result[] = Array.from( model.documents.keys(), ).flatMap(docId => { // Get permission context for permission checking const context = getPermissionContext({ channel, docState: model.documents.get(docId), model, }) // If we can't get context (e.g., missing peer state), log error if (context instanceof Error) { logger.warn(`directory-request error: ${context.message}`) return [] } // Check visibility permission - can we tell this peer about this document? if (permissions.visibility(context.doc, context.peer)) { return [{ success: true, docId }] } else { // Permission denied - don't reveal this document return [] } }) // Separate successful docIds from errors const allowedDocIds = docResults.flatMap(result => result.success ? [result.docId] : [], ) // Send directory-response with filtered list const sendMessageCmd: Command = { type: "cmd/send-message", envelope: { toChannelIds: [fromChannelId], message: { type: "channel/directory-response", docIds: allowedDocIds, }, }, } return batchAsNeeded(sendMessageCmd) }