Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

refactor(community): post/thread member panel reads unified /members

Simonee307466fbf7d
diff --git a/src/web/src/app/api/community/channels/[id]/participants/route.test.ts b/src/web/src/app/api/community/channels/[id]/participants/route.test.ts
index 42e85c026..a365f0362 100644
--- a/src/web/src/app/api/community/channels/[id]/participants/route.test.ts
+++ b/src/web/src/app/api/community/channels/[id]/participants/route.test.ts
@@ -51,7 +51,7 @@ vi.mock("@/lib/middleware/helpers", () => {
}
})
-import { GET, POST } from "./route"
+import { POST } from "./route"
const ctx = { params: { id: "t1" } } as any
function postReq(body: unknown) {
@@ -71,47 +71,8 @@ function threadCtx(over: Record<string, unknown> = {}) {
}
}
-describe("GET /channels/[id]/participants", () => {
- beforeEach(() => {
- vi.clearAllMocks()
- mockResolveChannelAccessContext.mockResolvedValue(threadCtx())
- mockListThreadParticipants.mockResolvedValue([
- { userId: "u1", userName: "Ann", userImage: null, discriminator: "0001", source: "spoke" },
- { userId: "u2", userName: "Bob", userImage: null, discriminator: "0002", source: "mention" },
- ])
- })
-
- it("lists participants with source", async () => {
- const res = await GET(new NextRequest("http://localhost/api/community/channels/t1/participants"), ctx)
- expect(res.status).toBe(200)
- const body = await res.json()
- expect(body.participants).toHaveLength(2)
- expect(body.participants[1]).toMatchObject({ userId: "u2", source: "mention" })
- expect(body.participants[1]).not.toHaveProperty("muted")
- })
-
- it("also serves a forum_post (its panel is the participant set)", async () => {
- mockResolveChannelAccessContext.mockResolvedValue(threadCtx({
- channel: { id: "p1", serverId: "s1", type: "forum_post", parentChannelId: "f1", parentMessageId: null, creatorId: "u1" },
- }))
- const res = await GET(new NextRequest("http://localhost/api/community/channels/p1/participants"), { params: { id: "p1" } } as any)
- expect(res.status).toBe(200)
- const body = await res.json()
- expect(body.participants).toHaveLength(2)
- })
-
- it("400 when the channel is not a thread or forum post", async () => {
- mockResolveChannelAccessContext.mockResolvedValue(threadCtx({ channel: { id: "c1", serverId: "s1", type: "text", parentChannelId: null, parentMessageId: null, creatorId: "u1" } }))
- const res = await GET(new NextRequest("http://localhost/api/community/channels/c1/participants"), ctx)
- expect(res.status).toBe(400)
- })
-
- it("403 for a caller who can't see the thread", async () => {
- mockResolveChannelAccessContext.mockResolvedValue(null)
- const res = await GET(new NextRequest("http://localhost/api/community/channels/t1/participants"), ctx)
- expect(res.status).toBe(403)
- })
-})
+// GET (participant-list read) was retired — the unified `/members` endpoint now
+// serves the notify set. Only the POST write remains on this route.
describe("POST /channels/[id]/participants", () => {
beforeEach(() => {
diff --git a/src/web/src/app/api/community/channels/[id]/participants/route.ts b/src/web/src/app/api/community/channels/[id]/participants/route.ts
index e866a169a..d7686aece 100644
--- a/src/web/src/app/api/community/channels/[id]/participants/route.ts
+++ b/src/web/src/app/api/community/channels/[id]/participants/route.ts
@@ -5,36 +5,11 @@ import { getDb } from "@/lib/db"
import { queries, WS_EVENTS, isThread, isForumPost } from "@alook/shared"
import { broadcastToUserSafe } from "@/lib/community/fanout"
import { requireChannelAccess } from "@/lib/community/permissions"
-import { avatarInitial } from "@/lib/community/avatar"
-/**
- * List a thread/forum-post's participants — the NOTIFY set. Both thread and
- * forum_post are the notification dimension (their panel == their notify set),
- * so both use this endpoint. Any member with access (who therefore passes the
- * access gate) may read the list.
- */
-export const GET = withAuth(async (_req: NextRequest, ctx) => {
- const channelId = ctx.params?.id
- if (!channelId) return writeError("missing channel id", 400)
-
- const db = getDb(ctx.env.DB)
- const access = await requireChannelAccess(db, channelId, ctx.userId)
- if (!access.ok) return writeError(access.error, access.status)
- const type = access.value.channel.type
- if (!isThread(type) && !isForumPost(type)) {
- return writeError("not a thread or forum post", 400)
- }
-
- const rows = await queries.communityThread.listThreadParticipants(db, channelId)
- const participants = rows.map((r) => ({
- userId: r.userId,
- name: r.userName ?? null,
- discriminator: r.discriminator ?? null,
- avatar: r.userImage ?? avatarInitial(r.userName ?? ""),
- source: r.source,
- }))
- return writeJSON({ participants })
-})
+// GET (the participant-list READ) was retired: the unified `/members` endpoint
+// now serves the notify set in the canonical `MappedMember` shape, and the
+// post/thread panel reads that. Only the participant WRITE endpoints remain
+// here (POST add + DELETE in `[userId]/route.ts`).
/**
* Add a participant to a thread/forum-post — the "add from channel" flow. ANY
diff --git a/src/web/src/app/c/channels/[serverId]/[channelId]/page.tsx b/src/web/src/app/c/channels/[serverId]/[channelId]/page.tsx
index f05eb102c..3871f7187 100644
--- a/src/web/src/app/c/channels/[serverId]/[channelId]/page.tsx
+++ b/src/web/src/app/c/channels/[serverId]/[channelId]/page.tsx
@@ -20,7 +20,7 @@ import { canManageServer } from "@/components/community/_types"
import type { MentionType } from "@alook/shared"
import { isForum as isForumType, deriveThreadName } from "@alook/shared"
import { resolveRowPresence } from "@/lib/community/presence"
-import { makeUserNameResolver, displayName } from "@/lib/community/display-name"
+import { makeUserNameResolver } from "@/lib/community/display-name"
import { avatarInitial } from "@/lib/community/avatar"
import {
useCommunityStore,
@@ -34,7 +34,7 @@ import { useCurrentUser } from "@/contexts/community/current-user"
import { useServer } from "@/hooks/community/use-servers"
import { useServerMembers } from "@/hooks/community/use-server-members"
import { useChannelMembers, useAddableMembers, useAddChannelMember, useRemoveChannelMember } from "@/hooks/community/use-channel-members"
-import { useThreadParticipants, useAddThreadParticipant, useRemoveThreadParticipant } from "@/hooks/community/use-thread-participants"
+import { useAddThreadParticipant, useRemoveThreadParticipant } from "@/hooks/community/use-thread-participants"
import { useMessages } from "@/hooks/community/use-messages"
import { useChannelReadStateSnapshot } from "@/hooks/community/use-channel-read-state"
import { useChannelBootstrap } from "@/hooks/community/use-channel-bootstrap"
@@ -160,11 +160,13 @@ function ChannelView() {
const cat = cats.find((c) => c.channels.some((ch) => ch.id === anchorId))
return !!cat?.private
}, [currentServer, isChildChannel, currentChannelMeta, channelId])
- // A notify unit (thread/post) has no roster of its own — its own
- // `useChannelMembers` stays disabled; its panel is the participant set.
- const channelMembersHook = useChannelMembers(channelId, currentChannelPrivate && !isNotifyUnit)
- // Thread/post drawer shows the notify PARTICIPANT set, not the channel audience.
- const threadParticipantsHook = useThreadParticipants(channelId, isNotifyUnit)
+ // Members drawer read: `/members` for BOTH a private channel and a notify
+ // unit (thread/post). For a notify unit `/members` resolves the NOTIFY
+ // (participant) set — same people the old `/participants` read returned — but
+ // in the unified `MappedMember` shape (status/role/isCreator), so a post's
+ // panel matches a channel's. A public top-level channel still uses the server
+ // roster (`members`), so the hook stays gated to private-or-notify-unit.
+ const channelMembersHook = useChannelMembers(channelId, isNotifyUnit || (currentChannelPrivate && !isNotifyUnit))
const removeThreadParticipantMut = useRemoveThreadParticipant(channelId)
const addThreadParticipantMut = useAddThreadParticipant(channelId)
// Top-level channel/forum add-picker source + mutations (add: any member).
@@ -217,18 +219,15 @@ function ChannelView() {
!q || name.toLowerCase().includes(q) || (disc ?? "").toLowerCase().includes(q)
if (isNotifyUnit) {
- const unitCreatorId = currentChannelMeta?.creatorId
- return threadParticipantsHook.participants
- .filter((p) => matches(p.name ?? "", p.discriminator))
- .map((p) => withPresence({
- userId: p.userId,
- name: displayName(p),
- discriminator: p.discriminator ?? "0000",
- avatar: p.avatar,
- // Rows are all real participants (removable); the unit creator's
- // (thread starter / post author) row is locked.
- isCreator: p.userId === unitCreatorId,
- }))
+ // Notify unit → the participant set, now read from `/members` in the
+ // unified shape (`isCreator` computed server-side against the unit's
+ // author). Participants are ALL real, creator-removable rows, so drop
+ // `source` to `undefined` — the notify set has no explicit/inherited/admin
+ // distinction, and MemberList treats `source === undefined` as removable
+ // (the same behavior the old `/participants` read gave).
+ return channelMembersHook.members
+ .filter((m) => matches(m.name, m.discriminator))
+ .map((m) => withPresence({ ...m, source: undefined }))
}
if (!currentChannelPrivate) return members
return channelMembersHook.members
@@ -236,8 +235,6 @@ function ChannelView() {
.map((m) => withPresence(m))
}, [
isNotifyUnit,
- threadParticipantsHook.participants,
- currentChannelMeta,
currentChannelPrivate,
members,
channelMembersHook.members,
@@ -811,7 +808,7 @@ function ChannelView() {
onOpenThread: enterThread,
members: panelMembers,
membersLoading: isNotifyUnit
- ? threadParticipantsHook.isLoading
+ ? channelMembersHook.isLoading
: currentChannelPrivate ? channelMembersHook.isLoading : membersHook.loading,
membersLoadingMore: scopedDrawer ? false : membersHook.loadingMore,
membersHasMore: scopedDrawer ? false : membersHook.hasMore,
@@ -855,7 +852,7 @@ function ChannelView() {
const manageMembersDialog = (() => {
if (!manageMembersOpen) return null
if (isNotifyUnit) {
- const participantIds = new Set(threadParticipantsHook.participants.map((p) => p.userId))
+ const participantIds = new Set(channelMembersHook.members.map((m) => m.userId))
const candidates = parentChannelMembersHook.members
.filter((m) => !participantIds.has(m.userId) && m.userId !== currentUser.id)
.map((m) => ({ userId: m.userId, name: m.name ?? null, avatar: m.avatar }))
diff --git a/src/web/src/hooks/community/use-thread-participants.ts b/src/web/src/hooks/community/use-thread-participants.ts
index d47a85ce1..f180b499f 100644
--- a/src/web/src/hooks/community/use-thread-participants.ts
+++ b/src/web/src/hooks/community/use-thread-participants.ts
@@ -1,36 +1,16 @@
"use client"
-import { useMutation, useQuery, useQueryClient, type UseQueryResult } from "@tanstack/react-query"
+import { useMutation, useQueryClient } from "@tanstack/react-query"
import { apiFetch } from "@/lib/api/client"
import { communityKeys } from "@/lib/query-keys"
-export type ThreadParticipant = {
- userId: string
- name: string | null
- discriminator: string | null
- avatar: string
- source: "mention" | "spoke" | "added"
-}
-
-const EMPTY: readonly ThreadParticipant[] = Object.freeze([])
-
-/** A thread's notify participants. */
-export function useThreadParticipants(
- channelId: string,
- enabled = true,
-): UseQueryResult<{ participants: ThreadParticipant[] }> & { participants: ThreadParticipant[] } {
- const query = useQuery({
- queryKey: communityKeys.threadParticipants(channelId),
- queryFn: () =>
- apiFetch<{ participants: ThreadParticipant[] }>(
- `/api/community/channels/${encodeURIComponent(channelId)}/participants`,
- ),
- enabled: enabled && !!channelId,
- })
- return { ...query, participants: query.data?.participants ?? (EMPTY as ThreadParticipant[]) }
-}
+// The participant READ hook was retired: the post/thread member panel now reads
+// the unified `/members` endpoint (via `useChannelMembers`), which returns the
+// canonical `MappedMember` shape for the notify set. Only the add/remove WRITE
+// hooks remain here — they mutate `/participants` and invalidate the
+// channelMembers cache the panel now reads (NOT the old threadParticipants key).
-/** Any participant adds a parent-channel member to the thread. */
+/** Any participant adds a parent-channel member to the thread/post. */
export function useAddThreadParticipant(channelId: string) {
const qc = useQueryClient()
return useMutation({
@@ -40,12 +20,12 @@ export function useAddThreadParticipant(channelId: string) {
body: JSON.stringify({ userId }),
}),
onSuccess: () => {
- void qc.invalidateQueries({ queryKey: communityKeys.threadParticipants(channelId) })
+ void qc.invalidateQueries({ queryKey: communityKeys.channelMembers(channelId) })
},
})
}
-/** Leave a thread (remove your own participation, or the creator removes someone). */
+/** Leave a thread/post (remove your own participation, or the creator removes someone). */
export function useRemoveThreadParticipant(channelId: string) {
const qc = useQueryClient()
return useMutation({
@@ -55,7 +35,7 @@ export function useRemoveThreadParticipant(channelId: string) {
{ method: "DELETE" },
),
onSuccess: () => {
- void qc.invalidateQueries({ queryKey: communityKeys.threadParticipants(channelId) })
+ void qc.invalidateQueries({ queryKey: communityKeys.channelMembers(channelId) })
},
})
}