Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(channel): pinning or filing an open project Channel joins it (#23527)

Lobehub Coder50522b782c63
diff --git a/apps/server/src/routers/lambda/channel.ts b/apps/server/src/routers/lambda/channel.ts
index 954494fa1ba..ada5bcc402b 100644
--- a/apps/server/src/routers/lambda/channel.ts
+++ b/apps/server/src/routers/lambda/channel.ts
@@ -340,14 +340,14 @@ export const channelRouter = router({
setPinned: externalChannelProcedure
.input(z.object({ channelId, pinned: z.boolean() }))
.mutation(async ({ ctx, input }) => ({
- data: await ctx.channelService.repository.setPinned(input.channelId, input.pinned),
+ data: await ctx.channelService.setPinned(input.channelId, input.pinned),
success: true,
})),
/** Files the conversation under one of the caller's own sections; null returns it to the built-in one. */
moveToSection: externalChannelProcedure
.input(z.object({ channelId, sectionId: sectionId.nullable() }))
.mutation(async ({ ctx, input }) => ({
- data: await ctx.channelService.repository.moveToSection(input.channelId, input.sectionId),
+ data: await ctx.channelService.moveToSection(input.channelId, input.sectionId),
success: true,
})),
/** Hides the conversation from the caller's own list until its next message, or shows it again. */
diff --git a/apps/server/src/services/channel/ChannelService.members.test.ts b/apps/server/src/services/channel/ChannelService.members.test.ts
index 4cfa668bb65..fb1fb8471e6 100644
--- a/apps/server/src/services/channel/ChannelService.members.test.ts
+++ b/apps/server/src/services/channel/ChannelService.members.test.ts
@@ -4,11 +4,20 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
import { ChannelService } from './ChannelService';
import { notifyChannel } from './notify';
-const { create, getAgent } = vi.hoisted(() => ({ create: vi.fn(), getAgent: vi.fn() }));
+const { create, findAccess, getAgent, moveToSection, setPinned } = vi.hoisted(() => ({
+ create: vi.fn(),
+ findAccess: vi.fn(),
+ getAgent: vi.fn(),
+ moveToSection: vi.fn(),
+ setPinned: vi.fn(),
+}));
vi.mock('@/database/repositories/channel', async (original) => ({
...(await original<object>()),
ChannelRepository: class {
create = create;
+ findAccess = findAccess;
+ moveToSection = moveToSection;
+ setPinned = setPinned;
},
}));
vi.mock('@/database/models/project', () => ({ ProjectModel: class {} }));
@@ -51,3 +60,44 @@ describe('creating a Channel with people', () => {
expect(notifyChannel).not.toHaveBeenCalled();
});
});
+
+describe('pinning or filing an open project Channel', () => {
+ beforeEach(() => {
+ vi.resetAllMocks();
+ });
+ const service = () => new ChannelService({} as never, 'mia', 'workspace');
+
+ it('tells its members about a teammate who joins it by pinning', async () => {
+ const pinnedAt = new Date();
+ setPinned.mockResolvedValue(pinnedAt);
+
+ expect(await service().setPinned('chn_1', true)).toBe(pinnedAt);
+ expect(setPinned).toHaveBeenCalledWith('chn_1', true);
+ expect(notifyChannel).toHaveBeenCalledWith(expect.anything(), 'chn_1');
+ });
+
+ it('tells its members about a teammate who joins it by filing it under a section', async () => {
+ moveToSection.mockResolvedValue('sec_1');
+
+ expect(await service().moveToSection('chn_1', 'sec_1')).toBe('sec_1');
+ expect(notifyChannel).toHaveBeenCalledWith(expect.anything(), 'chn_1');
+ });
+
+ it("keeps a member's own pin, section and unpin quiet", async () => {
+ findAccess.mockResolvedValue({ channel: { id: 'chn_1' }, member: { id: 'mem_1' } });
+
+ await service().setPinned('chn_1', true);
+ await service().moveToSection('chn_1', 'sec_1');
+ await service().setPinned('chn_1', false);
+ await service().moveToSection('chn_1', null);
+
+ expect(notifyChannel).not.toHaveBeenCalled();
+ });
+
+ it('notifies nobody when the write is refused', async () => {
+ setPinned.mockRejectedValue(new Error('NOT_FOUND'));
+
+ await expect(service().setPinned('chn_1', true)).rejects.toThrow('NOT_FOUND');
+ expect(notifyChannel).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/server/src/services/channel/ChannelService.ts b/apps/server/src/services/channel/ChannelService.ts
index e9b9fe28209..e6949d3ddd2 100644
--- a/apps/server/src/services/channel/ChannelService.ts
+++ b/apps/server/src/services/channel/ChannelService.ts
@@ -463,8 +463,33 @@ export class ChannelService {
return access;
};
+ /**
+ * Pins the conversation atop the caller's own list, or unpins it. Pinning an open project
+ * Channel joins it, so its members see the newcomer as with {@link join}.
+ */
+ setPinned = (channelId: string, pinned: boolean) =>
+ this.writeMayJoin(channelId, pinned, () => this.repository.setPinned(channelId, pinned));
+
+ /** Files the conversation under one of the caller's own sections, joining it like a pin. */
+ moveToSection = (channelId: string, sectionId: string | null) =>
+ this.writeMayJoin(channelId, sectionId !== null, () =>
+ this.repository.moveToSection(channelId, sectionId),
+ );
+
changed = (channelId: string) => notifyChannel(this.db, channelId);
+ /** A personal list write that joins an open Channel first; members hear only about a join. */
+ private writeMayJoin = async <T>(
+ channelId: string,
+ mayJoin: boolean,
+ write: () => Promise<T>,
+ ) => {
+ const joining = mayJoin && !(await this.repository.findAccess(channelId));
+ const result = await write();
+ if (joining) await this.changed(channelId);
+ return result;
+ };
+
/** Visitor conversations with the caller's shared Agent, when its share lets the caller read them. */
shareVisitors = (agentId: string) => listShareVisitorConversations(this.db, this.userId, agentId);
diff --git a/packages/database/src/repositories/channel/__tests__/projectAccess.test.ts b/packages/database/src/repositories/channel/__tests__/projectAccess.test.ts
index 932f4b220d2..baded9667ef 100644
--- a/packages/database/src/repositories/channel/__tests__/projectAccess.test.ts
+++ b/packages/database/src/repositories/channel/__tests__/projectAccess.test.ts
@@ -124,6 +124,51 @@ describe('open project Channels', () => {
expect(await membershipOf(channel.id)).toHaveLength(1);
});
+ it('joins a teammate who pins it or files it under a section, keeping it on their own list', async () => {
+ const { channel, owner, teammate } = await projectChannel();
+
+ const pinnedAt = await teammate.setPinned(channel.id, true);
+
+ expect(pinnedAt).toBeInstanceOf(Date);
+ expect(await membershipOf(channel.id)).toEqual([
+ expect.objectContaining({ pinnedAt, role: 'member', status: 'active' }),
+ ]);
+
+ const section = await teammate.sections.create('Following');
+ await teammate.moveToSection(channel.id, section.id);
+
+ expect(await teammate.list()).toEqual([
+ expect.objectContaining({ id: channel.id, joined: true, pinnedAt, sectionId: section.id }),
+ ]);
+ // Their pin and section stay theirs.
+ expect(await owner.list()).toEqual([
+ expect.objectContaining({ id: channel.id, pinnedAt: null, sectionId: null }),
+ ]);
+ });
+
+ it('joins a teammate who files it under a section before anything else', async () => {
+ const { channel, teammate } = await projectChannel();
+ const section = await teammate.sections.create('Following');
+
+ await teammate.moveToSection(channel.id, section.id);
+
+ expect(await membershipOf(channel.id)).toEqual([
+ expect.objectContaining({ sectionId: section.id, status: 'active' }),
+ ]);
+ });
+
+ it('does not join a teammate who only unpins or unfiles a Channel they never joined', async () => {
+ const { channel, teammate } = await projectChannel();
+
+ await expect(teammate.setPinned(channel.id, false)).rejects.toMatchObject({
+ code: 'NOT_FOUND',
+ });
+ await expect(teammate.moveToSection(channel.id, null)).rejects.toMatchObject({
+ code: 'NOT_FOUND',
+ });
+ expect(await membershipOf(channel.id)).toEqual([]);
+ });
+
it("keeps a private project's Channels with their members", async () => {
const { channel, teammate } = await projectChannel('private');
diff --git a/packages/database/src/repositories/channel/index.ts b/packages/database/src/repositories/channel/index.ts
index a0b7fbfd03c..c9458a11093 100644
--- a/packages/database/src/repositories/channel/index.ts
+++ b/packages/database/src/repositories/channel/index.ts
@@ -486,7 +486,7 @@ export class ChannelRepository {
{ allowArchived: true, allowTrashed: true },
);
- /** Pins the conversation atop the caller's own list, or unpins it; nobody else sees it. */
+ /** Pins the conversation atop the caller's own list (joining an open one), or unpins it. */
setPinned = async (channelId: string, pinned: boolean) =>
this.write(
channelId,
@@ -495,12 +495,12 @@ export class ChannelRepository {
await tx.update(channelMembers).set({ pinnedAt }).where(eq(channelMembers.id, member.id));
return pinnedAt;
},
- { allowArchived: true, allowTrashed: true },
+ { allowArchived: true, allowTrashed: true, joinOpen: pinned },
);
/**
- * Files the conversation under one of the caller's own sections, or back into its built-in
- * section with `null`; nobody else sees it.
+ * Files the conversation under one of the caller's own sections (joining an open one), or back
+ * into its built-in section with `null`; nobody else sees it.
*/
moveToSection = async (channelId: string, sectionId: string | null) =>
this.write(
@@ -510,7 +510,7 @@ export class ChannelRepository {
await tx.update(channelMembers).set({ sectionId }).where(eq(channelMembers.id, member.id));
return sectionId;
},
- { allowArchived: true, allowTrashed: true },
+ { allowArchived: true, allowTrashed: true, joinOpen: sectionId !== null },
);
/**