Skip to main content

server-core/conversation

packages/server/src/conversation

Purpose

Conversation-domain service barrel.

Public surface

conversationCreate

Variable
export const conversationCreate: ServerHandler<typeof ConversationCreate> = (
  params,
)

conversationList

Variable
export const conversationList: ServerHandler<typeof ConversationList> = (
  params,
)

ConversationService

Class
export class ConversationService {
  /** In-memory cache for last message previews — avoids decrypting on every list() call */
  private previewCache = new Map<ConversationId, string>();

  constructor(
    private db: Db,
    private connections: ConnectionManager,
    private resolveContactPolicy: ContactPolicyResolver = () => null,
  ) {}

  /** Writes the plaintext preview before message-part encryption. */
  updatePreviewCache(
    conversationId: ConversationId,
    firstPartText: string,
  ): void {
    this.previewCache.delete(conversationId);
    this.previewCache.set(
      conversationId,
      firstPartText.slice(0, PREVIEW_CACHE_TEXT_CHARS),
    );
    if (this.previewCache.size > PREVIEW_CACHE_MAX) {
      const oldest = this.previewCache.keys().next().value!;
      this.previewCache.delete(oldest);
    }
  }

ConversationServiceLive

Variable
export const ConversationServiceLive = Layer.effect(
  ConversationServiceTag,
  Effect.gen(function* () {
    const db = yield* DbTag;
    const connections = yield* ConnectionManagerTag;
    const appEndpointRegistry = yield* AppEndpointRegistryTag;
    return new ConversationService(db, connections, () => {
      const contacts = appEndpointRegistry.getContactService();
      if (!contacts) return null;
      return (a, b) => contacts.areInContact(a, b);
    });
  }).pipe(Effect.withSpan("ConversationServiceLive")),
)

ConversationServiceTag

Class
export class ConversationServiceTag extends Context.Tag(
  "moltzap/ConversationService",
)<ConversationServiceTag, ConversationService>() {}

conversationUpdate

Variable
export const conversationUpdate: ServerHandler<typeof ConversationUpdate> = (
  params,
)

Files

  • conversation.service.ts
  • handlers.ts
  • layer.ts