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.

feat(new-channels): limit @ user mentions to participants, add back @here support (#2116)

teo457ebcdb5c1b
diff --git a/js/app/packages/channel/Channel/Channel.tsx b/js/app/packages/channel/Channel/Channel.tsx
index 86efc47a3e..6eb466cdae 100644
--- a/js/app/packages/channel/Channel/Channel.tsx
+++ b/js/app/packages/channel/Channel/Channel.tsx
@@ -67,6 +67,7 @@ import {
useRemoveReactionMutation,
} from '@queries/channel/reaction';
import { resetKeyboardModality } from './util';
+import { useChannelParticipants } from '@channel/use-channel-participants';
type ChannelProps = {
channelId: string;
@@ -114,6 +115,8 @@ export function Channel(props: ChannelProps) {
const messages = createMemo(() => messageIndex().items);
const messageById = createMemo(() => messageIndex().byId);
+ const participants = useChannelParticipants(() => props.channelId);
+
const activity = useChannelActivity(props.channelId);
const updateActivityMutation = useUpdateChannelsActivityMutation({
@@ -216,7 +219,10 @@ export function Channel(props: ChannelProps) {
channelID: props.channelId,
senderId,
optimisticId: crypto.randomUUID(),
- message: buildPostMessageRequest(snapshot),
+ message: buildPostMessageRequest({
+ snapshot,
+ participantIds: participants.ids(),
+ }),
});
};
@@ -334,6 +340,7 @@ export function Channel(props: ChannelProps) {
isDraggingOverChannel: dragState.isDraggingOverChannel(),
isValidChannelDrag: dragState.isValidChannelDrag(),
}}
+ participants={participants.users}
attachmentTracker={attachmentTracker}
persistenceKey={makeInputValuePersistenceKey({
channelId: props.channelId,
diff --git a/js/app/packages/channel/Input/ChannelInput.tsx b/js/app/packages/channel/Input/ChannelInput.tsx
index 2b78714ef5..2b9cef9ee1 100644
--- a/js/app/packages/channel/Input/ChannelInput.tsx
+++ b/js/app/packages/channel/Input/ChannelInput.tsx
@@ -20,14 +20,16 @@ import type {
InputPersistenceKey,
} from './types';
import { applyInlineFormat, applyNodeFormat } from './utils/formatting';
-import { Match, Show, Switch, type JSX } from 'solid-js';
+import { Match, Show, Switch, type Accessor, type JSX } from 'solid-js';
import { isReplyInput } from './types';
+import type { IUser } from '@core/user/types';
export type ChannelInputProps = InputCallbacks & {
input: InputData;
markdownNamespace?: string;
persistenceKey?: InputPersistenceKey;
attachmentTracker?: InputAttachmentTracker;
+ participants?: Accessor<IUser[]>;
onReady?: (handle: InputHandle) => void;
children?: JSX.Element;
};
@@ -88,6 +90,7 @@ export function ChannelInput(props: ChannelInputProps) {
const markdownEditor = createConfiguredChannelMarkdownEditor({
namespace: props.markdownNamespace ?? 'channel-input-markdown',
enableMentions: true,
+ users: props.participants,
onMentionCreate: (mention) => {
mentionsTracker.onMentionCreate(mention);
},
diff --git a/js/app/packages/channel/Input/configured-markdown-editor.ts b/js/app/packages/channel/Input/configured-markdown-editor.ts
index 6694b927d4..de3a9d818e 100644
--- a/js/app/packages/channel/Input/configured-markdown-editor.ts
+++ b/js/app/packages/channel/Input/configured-markdown-editor.ts
@@ -6,12 +6,14 @@ import {
} from '@core/component/LexicalMarkdown/plugins';
import { tableCellResizerPlugin } from '@core/component/LexicalMarkdown/plugins/tables/tableCellResizerPlugin';
import { tablePlugin } from '@core/component/LexicalMarkdown/plugins/tables/tablePlugin';
+import type { IUser } from '@core/user/types';
type CreateConfiguredChannelMarkdownEditorOptions = {
namespace: string;
enableMentions?: boolean;
onMentionCreate?: (mention: ItemMention) => void;
onMentionRemove?: (mention: ItemMention) => void;
+ users?: () => IUser[];
onChange?: (markdown: string) => void;
onEnter?: (event: KeyboardEvent, markdown: string) => boolean;
onPasteFilesAndDirs?: (
@@ -29,6 +31,8 @@ export function createConfiguredChannelMarkdownEditor(
editor.withMentions({
onCreate: options.onMentionCreate,
onRemove: options.onMentionRemove,
+ users: options.users,
+ block: 'channel',
});
}
diff --git a/js/app/packages/channel/Input/message-payload.ts b/js/app/packages/channel/Input/message-payload.ts
index fc18496de9..237e1e0e64 100644
--- a/js/app/packages/channel/Input/message-payload.ts
+++ b/js/app/packages/channel/Input/message-payload.ts
@@ -1,6 +1,9 @@
import { STATIC_IMAGE, STATIC_VIDEO } from '@core/store/cacheChannelInput';
+import type { ItemMention } from '@core/component/LexicalMarkdown/plugins';
import type { PostMessageRequest } from '@service-comms/generated/models';
+import type { SimpleMention } from '@service-comms/generated/models/simpleMention';
import type { InputAttachmentData, InputSnapshot } from './types';
+import { match } from 'ts-pattern';
function attachmentEntityType(
attachment: Pick<InputAttachmentData, 'kind'>
@@ -15,17 +18,72 @@ function attachmentEntityType(
}
}
+function expandGroupMention(
+ mention: ItemMention,
+ participantIds: string[],
+ seenUserIds: Set<string>
+): SimpleMention[] {
+ return match(mention.groupAlias)
+ .with('here', () => {
+ const result: SimpleMention[] = [];
+ for (const userId of participantIds) {
+ if (!seenUserIds.has(userId)) {
+ seenUserIds.add(userId);
+ result.push({ entity_type: 'user', entity_id: userId });
+ }
+ }
+ return result;
+ })
+ .otherwise(() => []);
+}
+
+/**
+ * Expands raw editor mentions into the flat list the API expects.
+ *
+ * - `group` mentions (e.g. @here) are fanned out to one user mention per
+ * participant, de-duplicated against explicitly mentioned users.
+ * - Regular user mentions are de-duplicated so the same user isn't sent twice.
+ */
+function expandMentions(
+ mentions: ItemMention[],
+ participantIds: string[]
+): SimpleMention[] {
+ const result: SimpleMention[] = [];
+ const seenUserIds = new Set<string>();
+
+ for (const mention of mentions) {
+ if (mention.itemType === 'group') {
+ result.push(...expandGroupMention(mention, participantIds, seenUserIds));
+ } else {
+ if (mention.itemType === 'user') {
+ if (seenUserIds.has(mention.itemId)) continue;
+ seenUserIds.add(mention.itemId);
+ }
+ result.push({
+ entity_type: mention.itemType,
+ entity_id: mention.itemId,
+ });
+ }
+ }
+
+ return result;
+}
+
+type BuildPostMessageRequestOptions = {
+ snapshot: InputSnapshot;
+ threadId?: string;
+ participantIds?: string[];
+};
+
export function buildPostMessageRequest(
- snapshot: InputSnapshot,
- threadId?: string
+ options: BuildPostMessageRequestOptions
): PostMessageRequest {
+ const { snapshot, threadId, participantIds } = options;
+
return {
content: snapshot.value,
thread_id: threadId,
- mentions: snapshot.mentions.map((mention) => ({
- entity_id: mention.itemId,
- entity_type: mention.itemType,
- })),
+ mentions: expandMentions(snapshot.mentions, participantIds ?? []),
attachments: snapshot.attachments.map((attachment) => ({
entity_id: attachment.id,
entity_type: attachmentEntityType(attachment),
diff --git a/js/app/packages/channel/Input/tests/message-payload.test.ts b/js/app/packages/channel/Input/tests/message-payload.test.ts
new file mode 100644
index 0000000000..d159783a23
--- /dev/null
+++ b/js/app/packages/channel/Input/tests/message-payload.test.ts
@@ -0,0 +1,71 @@
+import { describe, expect, it } from 'vitest';
+import { buildPostMessageRequest } from '../message-payload';
+import type { ItemMention } from '@core/component/LexicalMarkdown/plugins';
+
+const snap = (mentions: ItemMention[]) => ({
+ value: '',
+ mentions,
+ attachments: [],
+});
+
+describe('buildPostMessageRequest — @here expansion', () => {
+ it('deduplicates @here expansion against an earlier explicit user mention', () => {
+ const result = buildPostMessageRequest({
+ snapshot: snap([
+ { itemType: 'user', itemId: 'user-a' },
+ { itemType: 'group', itemId: 'here', groupAlias: 'here' },
+ ]),
+ participantIds: ['user-a', 'user-b'],
+ });
+
+ // user-a should appear once (from the explicit mention), user-b from @here
+ expect(result.mentions).toEqual([
+ { entity_type: 'user', entity_id: 'user-a' },
+ { entity_type: 'user', entity_id: 'user-b' },
+ ]);
+ });
+
+ it('preserves non-user mentions when mixed with @here', () => {
+ const result = buildPostMessageRequest({
+ snapshot: snap([
+ { itemType: 'document', itemId: 'doc-1' },
+ { itemType: 'group', itemId: 'here', groupAlias: 'here' },
+ { itemType: 'document', itemId: 'doc-1' },
+ ]),
+ participantIds: ['user-a'],
+ });
+
+ // Documents are not deduped (no user-id semantics), @here expands to user-a
+ expect(result.mentions).toEqual([
+ { entity_type: 'document', entity_id: 'doc-1' },
+ { entity_type: 'user', entity_id: 'user-a' },
+ { entity_type: 'document', entity_id: 'doc-1' },
+ ]);
+ });
+
+ it('deduplicates explicit user mention against earlier @here expansion', () => {
+ const result = buildPostMessageRequest({
+ snapshot: snap([
+ { itemType: 'group', itemId: 'here', groupAlias: 'here' },
+ { itemType: 'user', itemId: 'user-a' },
+ ]),
+ participantIds: ['user-a', 'user-b'],
+ });
+
+ // user-a and user-b from @here; trailing explicit user-a skipped
+ expect(result.mentions).toEqual([
+ { entity_type: 'user', entity_id: 'user-a' },
+ { entity_type: 'user', entity_id: 'user-b' },
+ ]);
+ });
+
+ it('produces no user mentions when @here is used with empty participants', () => {
+ const result = buildPostMessageRequest({
+ snapshot: snap([
+ { itemType: 'group', itemId: 'here', groupAlias: 'here' },
+ ]),
+ });
+
+ expect(result.mentions).toEqual([]);
+ });
+});
diff --git a/js/app/packages/channel/Thread/ThreadReplyInput.tsx b/js/app/packages/channel/Thread/ThreadReplyInput.tsx
index 20725b6bbb..ab5716ae41 100644
--- a/js/app/packages/channel/Thread/ThreadReplyInput.tsx
+++ b/js/app/packages/channel/Thread/ThreadReplyInput.tsx
@@ -11,6 +11,7 @@ import {
makeAttachmentTrackerPersistenceKey,
makeInputValuePersistenceKey,
} from '@channel/Input/utils/persistence';
+import { useChannelParticipants } from '@channel/use-channel-participants';
type ThreadReplyInputProps = {
channelId: string;
@@ -23,6 +24,9 @@ type ThreadReplyInputProps = {
export function ThreadReplyInput(props: ThreadReplyInputProps) {
const userId = useUserId();
const sendMessageMutation = useSendMessageMutation();
+
+ const participants = useChannelParticipants(() => props.channelId);
+
const tracker = createInputAttachmentTracker({
persistenceKey: makeAttachmentTrackerPersistenceKey({
channelId: props.channelId,
@@ -53,6 +57,7 @@ export function ThreadReplyInput(props: ThreadReplyInputProps) {
isDraggingOverChannel: entityDropZone.isDraggingOver(),
mode: 'reply',
}}
+ participants={participants.users}
attachmentTracker={tracker}
persistenceKey={makeInputValuePersistenceKey({
channelId: props.channelId,
@@ -72,7 +77,11 @@ export function ThreadReplyInput(props: ThreadReplyInputProps) {
channelID: props.channelId,
senderId,
optimisticId: crypto.randomUUID(),
- message: buildPostMessageRequest(snapshot, props.messageId),
+ message: buildPostMessageRequest({
+ snapshot,
+ threadId: props.messageId,
+ participantIds: participants.ids(),
+ }),
});
props.setReplyInputState(undefined);
diff --git a/js/app/packages/channel/use-channel-participants.ts b/js/app/packages/channel/use-channel-participants.ts
new file mode 100644
index 0000000000..fa4a3ad1d2
--- /dev/null
+++ b/js/app/packages/channel/use-channel-participants.ts
@@ -0,0 +1,26 @@
+import { createMemo, type Accessor } from 'solid-js';
+import { useChannelParticipantsQuery } from '@queries/channel/channel-participants';
+import { channelParticipantInfo } from '@core/user/util';
+import type { IUser } from '@core/user/types';
+
+export type ChannelParticipantsData = {
+ users: Accessor<IUser[]>;
+ ids: Accessor<string[]>;
+};
+export function useChannelParticipants(
+ channelId: Accessor<string>
+): ChannelParticipantsData {
+ const query = useChannelParticipantsQuery(channelId);
+
+ const users = createMemo(() => {
+ if (query.isLoading) return [];
+ return (query.data ?? []).map(channelParticipantInfo);
+ });
+
+ const ids = createMemo(() => {
+ if (query.isLoading) return [];
+ return (query.data ?? []).map((p) => p.user_id);
+ });
+
+ return { users, ids };
+}