Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
import { getChannelParams } from '@channel/Channel/link';
import { buildSimpleEntityUrl } from '@core/util/url';
import {
buildReplyTargetMarkdown,
isReplyTargetData,
markdownToPlainText,
type ReplyTargetParent,
stripLeadingReplyTargetMarkdown,
} from '@macro-inc/lexical-core';
import type { Message } from '@service-storage/messages';
import type { MessageData } from '../../Message';
export const DEFAULT_REACTION_EMOJI = '👍';
const EMPTY_REPLY_PARAGRAPH = ' ';
const BOT_SENDER_PREFIX = 'bot|';
export type ActionableMessage = Pick<
MessageData,
'id' | 'sender_id' | 'deleted_at' | 'reactions' | 'content'
> & {
kind?: Message['kind'];
thread_id?: string | null;
};
export function isOwnMessage(
message: Pick<ActionableMessage, 'sender_id'>,
currentUserId: string | undefined
): boolean {
if (!currentUserId) return false;
return message.sender_id === currentUserId;
}
export function isBotMessage(
message: Pick<ActionableMessage, 'sender_id'>
): boolean {
return message.sender_id.startsWith(BOT_SENDER_PREFIX);
}
export function canEditMessage(
message: Pick<ActionableMessage, 'sender_id' | 'deleted_at' | 'kind'>,
currentUserId: string | undefined
): boolean {
if (message.kind === 'call_root') return false;
return isOwnMessage(message, currentUserId) && !message.deleted_at;
}
export function canDeleteMessage(
message: Pick<ActionableMessage, 'sender_id' | 'deleted_at' | 'kind'>,
currentUserId: string | undefined,
/**
* Document owners may delete comments they did not write. Channel members
* leave this false; bot posts stay deletable without it.
*/
canModerate = false
): boolean {
if (message.deleted_at || message.kind === 'call_root') return false;
return (
canModerate || isOwnMessage(message, currentUserId) || isBotMessage(message)
);
}
export function canReplyToMessage(
message: Pick<ActionableMessage, 'thread_id' | 'deleted_at'>
): boolean {
return !message.deleted_at;
}
function oneLinePreview(text: string): string {
return text.trim().replace(/\s+/g, ' ');
}
function stripMagicChipMarkdown(markdown: string): string {
return markdown.replace(/<m-magic-chip>.*?<\/m-magic-chip>/gs, '');
}
export function buildReplyTargetValue(
input: {
message: Pick<MessageData, 'id' | 'content' | 'sender_id' | 'thread_id'>;
selectedText?: string;
renderedText?: string;
existingValue?: string;
} & (
| { channelId: string; parent?: never }
| { parent: ReplyTargetParent; channelId?: never }
)
): string {
const parent: ReplyTargetParent = input.parent ?? {
type: 'channel',
id: input.channelId,
};
if (parent.type === 'channel' && !input.message.thread_id) {
return input.existingValue ?? '';
}
for (const match of (input.existingValue ?? '').matchAll(
/<m-reply-target>(.*?)<\/m-reply-target>/gs
)) {
try {
const target: unknown = JSON.parse(match[1]);
if (
isReplyTargetData(target) &&
target.targetMessageId === input.message.id
) {
return input.existingValue ?? '';
}
} catch {
// Malformed references should not prevent adding a valid reply target.
}
}
const messageText = markdownToPlainText(
stripMagicChipMarkdown(
stripLeadingReplyTargetMarkdown(input.message.content)
)
);
const displayText = oneLinePreview(
input.selectedText || input.renderedText || messageText
);
const replyTarget = buildReplyTargetMarkdown({
parent,
targetMessageId: input.message.id,
targetThreadId: input.message.thread_id ?? input.message.id,
displayText,
senderId: input.message.sender_id,
});
const existingValue = input.existingValue?.trimStart() ?? '';
return existingValue
? `${replyTarget}\n\n${existingValue}`
: `${replyTarget}\n\n${EMPTY_REPLY_PARAGRAPH}`;
}
export function hasReactionFromUser(
message: Pick<ActionableMessage, 'reactions'>,
emoji: string,
userId: string | undefined
): boolean {
if (!userId) return false;
return message.reactions.some(
(reaction) => reaction.emoji === emoji && reaction.users.includes(userId)
);
}
export function buildMessageLink(
channelId: string,
messageId: string,
threadId?: string | null
): string {
const params = getChannelParams(messageId, threadId);
return buildSimpleEntityUrl({ type: 'channel', id: channelId }, params);
}