Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

3.5 KB
Raw Blame
import { match } from 'ts-pattern';
import { ChannelAccessRequest } from '../../entities/channels/access-request';
import { Channel } from '../../entities/channels/channel';
import { User } from '../../entities/users/user';
import type { MacroClient } from '../../utils/client';
import type { MacroEvent } from '../types';

export type ChannelEvent = Extract<
  MacroEvent,
  { event_type: `channel.${string}` }
>;

/** Wire prefix on a user principal; bots instead arrive as `bot|<uuid>`. */
const USER_PREFIX = 'macro|';

/**
 * Resolve a channel principal to a user handle, or undefined when it names a
 * bot. Lower-cased to match how the backend stores ids. Note `|` is legal in an
 * email, so `macro|bot|[email protected]` is a user.
 */
export function userFromPrincipal(client: MacroClient, principalId: string) {
  return principalId.startsWith(USER_PREFIX) &&
    principalId.length > USER_PREFIX.length
    ? User.byId(client, principalId.toLowerCase())
    : undefined;
}

/** Attach SDK entity handles to a channel webhook event. */
export function hydrateChannelEvent(client: MacroClient, event: ChannelEvent) {
  return match(event)
    .with({ event_type: 'channel.created' }, ({ metadata }) => ({
      event_type: 'channel.created' as const,
      metadata,
      channel: Channel.byId(client, metadata.channel_id),
      actor: userFromPrincipal(client, metadata.actor),
      participants: metadata.participant_user_ids.map((userId) =>
        User.byId(client, userId),
      ),
    }))
    .with({ event_type: 'channel.updated' }, ({ metadata }) => ({
      event_type: 'channel.updated' as const,
      metadata,
      channel: Channel.byId(client, metadata.channel_id),
      actor: userFromPrincipal(client, metadata.actor),
    }))
    .with({ event_type: 'channel.deleted' }, ({ metadata }) => ({
      event_type: 'channel.deleted' as const,
      metadata,
      channel: Channel.byId(client, metadata.channel_id),
      actor: userFromPrincipal(client, metadata.actor),
    }))
    .with({ event_type: 'channel.picture_changed' }, ({ metadata }) => ({
      event_type: 'channel.picture_changed' as const,
      metadata,
      channel: Channel.byId(client, metadata.channel_id),
      actor: userFromPrincipal(client, metadata.actor),
    }))
    .with({ event_type: 'channel.participant_added' }, ({ metadata }) => ({
      event_type: 'channel.participant_added' as const,
      metadata,
      channel: Channel.byId(client, metadata.channel_id),
      addedBy: userFromPrincipal(client, metadata.added_by),
      addedUsers: metadata.added_user_ids.map((userId) =>
        User.byId(client, userId),
      ),
    }))
    .with({ event_type: 'channel.participant_removed' }, ({ metadata }) => ({
      event_type: 'channel.participant_removed' as const,
      metadata,
      channel: Channel.byId(client, metadata.channel_id),
      removedBy: User.byId(client, metadata.removed_by),
      removedUsers: metadata.removed_user_ids.map((userId) =>
        User.byId(client, userId),
      ),
    }))
    .with({ event_type: 'channel.access_requested' }, ({ metadata }) => {
      const channel = Channel.byId(client, metadata.channel_id);
      return {
        event_type: 'channel.access_requested' as const,
        metadata,
        channel,
        request: ChannelAccessRequest.byId(
          client,
          channel,
          metadata.request_id,
        ),
        requester: User.byId(client, metadata.requester_id),
      };
    })
    .exhaustive((unknown) => {
      console.warn('macro sdk: unknown channel event', unknown);
      return undefined;
    });
}