Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(community): keep dm replies free of automatic mentions (#988)

Gener3e7ad16007e0
diff --git a/src/web/src/components/community/channels/dm-view.tsx b/src/web/src/components/community/channels/dm-view.tsx
index 350dadb5d..bd9ea5c39 100644
--- a/src/web/src/components/community/channels/dm-view.tsx
+++ b/src/web/src/components/community/channels/dm-view.tsx
@@ -534,6 +534,7 @@ export function DmView({ dmId }: { dmId: string }) {
<Composer
sendContract="accepted"
channel={dm.name}
+ channelType={metadata.data?.type}
context="dm"
// DM context short-circuits `rankMentionItems` to `[]` — no popup,
// no candidate pool needed. Passing [] keeps the Member[] typing
diff --git a/src/web/src/components/community/messages/composer-types.ts b/src/web/src/components/community/messages/composer-types.ts
index 700f7ca48..d80813af5 100644
--- a/src/web/src/components/community/messages/composer-types.ts
+++ b/src/web/src/components/community/messages/composer-types.ts
@@ -1,4 +1,4 @@
-import type { MentionType } from "@alook/shared"
+import type { CommunityChannelResource, MentionType } from "@alook/shared"
import type { ChannelRefCandidate } from "@/lib/community/channel-ref-extension"
import type { MentionContext } from "@/lib/community/mention-extension"
import type { SendAttachment } from "@/lib/community/models/message"
@@ -51,6 +51,7 @@ export type ComposerHandle = {
type ComposerBaseProps = {
channel: string
+ channelType?: CommunityChannelResource["type"]
context: MentionContext
members: Member[]
mentionCandidates?: MentionCandidateSource
diff --git a/src/web/src/components/community/messages/reply-visible-mention.dom.test.ts b/src/web/src/components/community/messages/reply-visible-mention.dom.test.ts
index c0830686e..639209b70 100644
--- a/src/web/src/components/community/messages/reply-visible-mention.dom.test.ts
+++ b/src/web/src/components/community/messages/reply-visible-mention.dom.test.ts
@@ -127,10 +127,75 @@ const Harness = forwardRef<ComposerHandle, ComposerProps>(function Harness(props
})
const reply = { authorId: "author", authorName: "Same", text: "original" }
function props(onAcceptSend = vi.fn(() => true)): ComposerProps {
- return { channel: "dm", context: "dm", members: [], sendContract: "accepted", onAcceptSend, draftKey: "dm-reply" }
+ return { channel: "general", channelType: "text", context: "channel", members: [], sendContract: "accepted", onAcceptSend, draftKey: "dm-reply" }
}
describe("real common composer reply lifecycle", () => {
+ it.each(["text", "forum", "thread", undefined] as const)("keeps automatic mentions for actual type %s even with DM mention context", async (channelType) => {
+ await act(async () => { render(createElement(Harness, { ...props(), channelType, context: "dm", replyingTo: reply })) })
+ expect(mentionIds(view.editor!)).toEqual(["author"])
+ expect(serializeComposerDocument(view.editor!)).toBe("@Same#1234 ")
+ })
+
+ it.each(["dm", "channel"] as const)("keeps an actual DM reply empty with %s mention context and a late profile", async (context) => {
+ profiles.value = new Map()
+ const base = { ...props(), channelType: "dm" as const, context }
+ let renderer!: ReturnType<typeof render>
+ await act(async () => { renderer = render(createElement(Harness, { ...base, replyingTo: reply })) })
+ const before = view.editor!.getJSON()
+ expect(view.editor!.isEmpty).toBe(true)
+ expect(mentionIds(view.editor!)).toEqual([])
+ expect(view.replyingTo).toBe(reply)
+ profiles.value = new Map([["author", { id: "author", name: "Same", discriminator: "1234" }]])
+ await act(async () => { renderer.rerender(createElement(Harness, { ...base, replyingTo: reply })) })
+ expect(view.editor!.getJSON()).toEqual(before)
+ expect(readComposerDraft("viewer:dm-reply")).toBeNull()
+ expect(view.replyingTo).toBe(reply)
+ })
+
+ it.each(["author", "member-author"])("keeps the restored manual %s atom in place across DM reply changes", async (id) => {
+ const saved = makeEditor(doc(
+ paragraph(text("before "), mentionNode(id), text(" after @Same#1234")),
+ { type: "orderedList", attrs: { start: 3 }, content: [{ type: "listItem", content: [paragraph(text("item"))] }] },
+ )).getJSON()
+ writeComposerDraft("viewer:dm-reply", saved)
+ const base = { ...props(), channelType: "dm" as const, context: "dm" as const }
+ let renderer!: ReturnType<typeof render>
+ await act(async () => { renderer = render(createElement(Harness, { ...base, replyingTo: reply })) })
+ expect(view.editor!.getJSON()).toEqual(saved)
+ await act(async () => { renderer.rerender(createElement(Harness, base)) })
+ expect(view.editor!.getJSON()).toEqual(saved)
+ await act(async () => { renderer.rerender(createElement(Harness, { ...base, replyingTo: reply })) })
+ expect(view.editor!.getJSON()).toEqual(saved)
+ const otherReply = { authorId: "other", authorName: "Other", text: "another target" }
+ profiles.value = new Map([...profiles.value, ["other", { id: "other", name: "Other", discriminator: "5678" }]])
+ await act(async () => { renderer.rerender(createElement(Harness, { ...base, replyingTo: otherReply })) })
+ expect(view.replyingTo).toBe(otherReply)
+ expect(view.editor!.getJSON()).toEqual(saved)
+ expect(mentionIds(view.editor!)).toEqual([id])
+ expect(readComposerDraft("viewer:dm-reply")).toEqual(saved)
+ })
+
+ it.each([false, true])("preserves the exact manual DM body through rejected send and retry, atom=%s", async (atom) => {
+ const saved = makeEditor(doc(paragraph(text("before "), atom ? mentionNode() : text("@Same#1234"), text(" after")))).getJSON()
+ writeComposerDraft("viewer:dm-reply", saved)
+ const accept = vi.fn(() => false)
+ const base = { ...props(accept), channelType: "dm" as const, context: "dm" as const }
+ const handle = createRef<ComposerHandle>()
+ await act(async () => { render(createElement(Harness, { ...base, replyingTo: reply, ref: handle })) })
+ expect(view.editor!.getJSON()).toEqual(saved)
+ await act(async () => { handle.current!.submitNow() })
+ expect(accept).toHaveBeenCalledWith("before @Same#1234 after", undefined, undefined)
+ expect(view.replyingTo).toBe(reply)
+ expect(view.editor!.getJSON()).toEqual(saved)
+ expect(readComposerDraft("viewer:dm-reply")).toEqual(saved)
+ accept.mockReturnValue(true)
+ await act(async () => { handle.current!.submitNow() })
+ expect(accept.mock.calls[1]).toEqual(accept.mock.calls[0])
+ expect(view.editor!.isEmpty).toBe(true)
+ expect(readComposerDraft("viewer:dm-reply")).toBeNull()
+ })
+
it.each(["canonical user", "membership"])("does not reuse a restored conflicting %s identity", async (identity) => {
profiles.value.set("other", { id: "other", name: "Other", discriminator: "5678" })
const id = identity === "canonical user" ? "other" : "member-other"
diff --git a/src/web/src/components/community/messages/use-composer-controller.ts b/src/web/src/components/community/messages/use-composer-controller.ts
index 967917f7b..af2f5aa77 100644
--- a/src/web/src/components/community/messages/use-composer-controller.ts
+++ b/src/web/src/components/community/messages/use-composer-controller.ts
@@ -40,6 +40,7 @@ import { useComposerReplyMention } from "./use-composer-reply-mention"
export function useComposerController(
{
channel,
+ channelType,
context,
members,
mentionCandidates,
@@ -348,7 +349,7 @@ export function useComposerController(
suppressSuggestions(() => editor.commands.focus("end"))
}, [autoFocus, editor, channel, isForumThreadBody, suppressSuggestions])
- useComposerReplyMention({ editor, isForumThreadBody, replyingTo, members, suppressSuggestions })
+ useComposerReplyMention({ editor, channelType, isForumThreadBody, replyingTo, members, suppressSuggestions })
const handleDrop = (event: DragEvent<HTMLDivElement>) => {
handleDropRaw(event)
diff --git a/src/web/src/components/community/messages/use-composer-reply-mention.ts b/src/web/src/components/community/messages/use-composer-reply-mention.ts
index f3be81a03..5b97da446 100644
--- a/src/web/src/components/community/messages/use-composer-reply-mention.ts
+++ b/src/web/src/components/community/messages/use-composer-reply-mention.ts
@@ -3,13 +3,14 @@ import type { Editor } from "@tiptap/react"
import { formatHandle } from "@alook/shared"
import { useCanonicalProfilesByUserId } from "@/lib/community-db/projections"
import type { Member } from "@/lib/community/models/people"
-import type { ComposerReplyTarget } from "./composer-types"
+import type { ComposerProps, ComposerReplyTarget } from "./composer-types"
import { prependReplyMention } from "./prepend-reply-mention"
export function useComposerReplyMention({
- editor, isForumThreadBody, replyingTo, members, suppressSuggestions,
+ editor, channelType, isForumThreadBody, replyingTo, members, suppressSuggestions,
}: {
editor: Editor | null
+ channelType?: ComposerProps["channelType"]
isForumThreadBody: boolean
replyingTo?: ComposerReplyTarget
members: Member[]
@@ -23,20 +24,22 @@ export function useComposerReplyMention({
return
}
if (previousReplyingToRef.current === replyingTo || !editor || isForumThreadBody) return
- const authorId = replyingTo.authorId
- const profile = authorId ? replyProfiles.get(authorId) : undefined
- if (authorId && (!profile?.name || !profile.discriminator)) {
- suppressSuggestions(() => editor.commands.focus("end"))
- return
- }
- if (authorId && profile?.name && profile.discriminator) {
- prependReplyMention(editor, {
- id: authorId,
- label: formatHandle(profile.name, profile.discriminator),
- }, [authorId, ...members.filter((member) => member.userId === authorId).map((member) => member.id)],
- (id) => replyProfiles.has(id) ? id : members.find((member) => member.id === id)?.userId)
+ if (channelType !== "dm") {
+ const authorId = replyingTo.authorId
+ const profile = authorId ? replyProfiles.get(authorId) : undefined
+ if (authorId && (!profile?.name || !profile.discriminator)) {
+ suppressSuggestions(() => editor.commands.focus("end"))
+ return
+ }
+ if (authorId && profile?.name && profile.discriminator) {
+ prependReplyMention(editor, {
+ id: authorId,
+ label: formatHandle(profile.name, profile.discriminator),
+ }, [authorId, ...members.filter((member) => member.userId === authorId).map((member) => member.id)],
+ (id) => replyProfiles.has(id) ? id : members.find((member) => member.id === id)?.userId)
+ }
}
previousReplyingToRef.current = replyingTo
suppressSuggestions(() => editor.commands.focus("end"))
- }, [replyingTo, replyProfiles, members, editor, isForumThreadBody, suppressSuggestions])
+ }, [replyingTo, replyProfiles, members, editor, channelType, isForumThreadBody, suppressSuggestions])
}
diff --git a/src/web/src/test/e2e-ui/39-mobile-message-interactions.spec.ts b/src/web/src/test/e2e-ui/39-mobile-message-interactions.spec.ts
index 6e134c014..5be0067f1 100644
--- a/src/web/src/test/e2e-ui/39-mobile-message-interactions.spec.ts
+++ b/src/web/src/test/e2e-ui/39-mobile-message-interactions.spec.ts
@@ -852,17 +852,31 @@ test("mobile reply, avatar mention, and typing rail keep exact backend and WS id
}
}
await expect(dmReplyTarget).toBeVisible()
+ const dmEditable = composerEditable(alice.page)
+ const manualDmDraft = `before @${bobInfo.name}#${bobInfo.discriminator} after`
+ await dmEditable.fill(manualDmDraft)
await dispatchSwipe(alice.page, dmWsReadyId, { x: 72, y: 1 })
await expect(replyPreview).toHaveText(`Replying to ${bobInfo.name} · dm reply target ${stamp}`)
- await expect(composerEditable(alice.page).locator(`[data-type="mention"][data-id="${userId("bob")}"]`)).toHaveCount(1)
- await expect(composerEditable(alice.page)).toBeFocused()
+ await expect(dmEditable).toHaveText(manualDmDraft)
+ await expect(dmEditable.locator('[data-type="mention"]')).toHaveCount(0)
+ await alice.page.getByRole("button", { name: "Cancel reply", exact: true }).click()
+ await expect(replyPreview).toHaveCount(0)
+ await expect(dmEditable).toHaveText(manualDmDraft)
+ await dmEditable.fill("")
+ await dispatchSwipe(alice.page, dmWsReadyId, { x: 72, y: 1 })
+ await expect(replyPreview).toHaveText(`Replying to ${bobInfo.name} · dm reply target ${stamp}`)
+ await expect(dmEditable).toHaveText("")
+ await expect(dmEditable.locator('[data-type="mention"]')).toHaveCount(0)
+ await expect(dmEditable).toBeFocused()
const dmReplyBody = `dm swipe reply ${stamp}`
- const dmReplyContent = `${replyAuthorMention} ${dmReplyBody}`
+ const dmReplyContent = dmReplyBody
const dmReplyResponsePromise = alice.page.waitForResponse((response) => (
response.request().method() === "POST"
&& new URL(response.url()).pathname === `/api/community/channels/${dmId}/messages`
))
- await typeReplyAfterAuthorMention(alice.page, userId("bob"), bobInfo, dmReplyBody)
+ await dmEditable.pressSequentially(dmReplyBody)
+ await expect(dmEditable).toHaveText(dmReplyBody)
+ await expect(dmEditable.locator('[data-type="mention"]')).toHaveCount(0)
await alice.page.getByTestId(tid.composerSend).click()
const dmReplyResponse = await dmReplyResponsePromise
expect(dmReplyResponse.status()).toBe(201)
@@ -882,7 +896,6 @@ test("mobile reply, avatar mention, and typing rail keep exact backend and WS id
)), { timeout: 20_000 }).toBe(true)
const receivedDmReply = bob.page.getByTestId(tid.message(dmReplyPayload.message.id))
await expect(receivedDmReply).toContainText(dmReplyBody)
- await expect(receivedDmReply).toContainText(`@${bobInfo.name}`)
const beforeDmTypingSeq = await latestSeq(alice.page, dmId)
const dmScrollerBeforeTyping = await settledScrollerGeometry(alice.page)
const bobDmEditable = composerEditable(bob.page)