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.

✨ feat(channel): let each person pin a conversation atop their own list (#23161)

Arvin Xu59b44f24e274
diff --git a/apps/server/src/routers/lambda/channel.ts b/apps/server/src/routers/lambda/channel.ts
index 6932e47a47d..387cee0a91c 100644
--- a/apps/server/src/routers/lambda/channel.ts
+++ b/apps/server/src/routers/lambda/channel.ts
@@ -282,6 +282,13 @@ export const channelRouter = router({
data: await ctx.channelService.repository.markRead(input.channelId, input.sequence),
success: true,
})),
+ /** Pins or unpins the conversation atop the caller's own list. */
+ setPinned: channelProcedure
+ .input(z.object({ channelId, pinned: z.boolean() }))
+ .mutation(async ({ ctx, input }) => ({
+ data: await ctx.channelService.repository.setPinned(input.channelId, input.pinned),
+ success: true,
+ })),
remove: channelProcedure.input(z.object({ channelId })).mutation(async ({ ctx, input }) => {
await ctx.channelService.remove(input.channelId);
return { success: true };
diff --git a/packages/database/src/repositories/channel/__tests__/pin.test.ts b/packages/database/src/repositories/channel/__tests__/pin.test.ts
new file mode 100644
index 00000000000..acdf32e90e9
--- /dev/null
+++ b/packages/database/src/repositories/channel/__tests__/pin.test.ts
@@ -0,0 +1,39 @@
+// @vitest-environment node
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+
+import { getTestDB } from '../../../core/getTestDB';
+import { createChannelFixture } from './testUtils';
+
+const db = await getTestDB();
+const fixture = createChannelFixture(db, 'channel-pin');
+beforeEach(fixture.setup);
+afterEach(fixture.cleanup);
+
+describe('Channel pin', () => {
+ it('pins a conversation for the caller only and keeps the first pin time', async () => {
+ const { asMember, asOwner, channel } = await fixture.createTeamChannel();
+
+ const pinnedAt = await asOwner.setPinned(channel.id, true);
+ expect(pinnedAt).toBeInstanceOf(Date);
+ // Pinning again keeps its place in the pinned order.
+ await expect(asOwner.setPinned(channel.id, true)).resolves.toEqual(pinnedAt);
+
+ expect(await asOwner.list()).toEqual([expect.objectContaining({ id: channel.id, pinnedAt })]);
+ // A workspace member's own list is untouched by someone else's pin.
+ expect(await asMember.list()).toEqual([
+ expect.objectContaining({ id: channel.id, pinnedAt: null }),
+ ]);
+
+ await expect(asOwner.setPinned(channel.id, false)).resolves.toBeNull();
+ expect(await asOwner.list()).toEqual([
+ expect.objectContaining({ id: channel.id, pinnedAt: null }),
+ ]);
+ });
+
+ it('pins an archived conversation, which stays readable', async () => {
+ const { asOwner, channel } = await fixture.createTeamChannel();
+ await asOwner.archive(channel.id);
+
+ await expect(asOwner.setPinned(channel.id, true)).resolves.toBeInstanceOf(Date);
+ });
+});
diff --git a/packages/database/src/repositories/channel/index.ts b/packages/database/src/repositories/channel/index.ts
index fbf3e25354b..7df683f9859 100644
--- a/packages/database/src/repositories/channel/index.ts
+++ b/packages/database/src/repositories/channel/index.ts
@@ -526,6 +526,18 @@ export class ChannelRepository {
{ allowArchived: true, allowTrashed: true },
);
+ /** Pins the conversation atop the caller's own list, or unpins it; nobody else sees it. */
+ setPinned = async (channelId: string, pinned: boolean) =>
+ this.write(
+ channelId,
+ async (tx, { member }) => {
+ const pinnedAt = pinned ? (member.pinnedAt ?? new Date()) : null;
+ await tx.update(channelMembers).set({ pinnedAt }).where(eq(channelMembers.id, member.id));
+ return pinnedAt;
+ },
+ { allowArchived: true, allowTrashed: true },
+ );
+
/**
* Channels the caller belongs to in the current scope, newest first. Archived ones are
* included with their `status`, so they stay readable outside the active list. Active open
@@ -542,6 +554,7 @@ export class ChannelRepository {
memberAvatars: ChannelAvatarMember[];
memberRole: ChannelMemberRole | null;
mentionCount: number;
+ pinnedAt: Date | null;
replyJobs: ChannelReplyJob[];
sharedAgentHandle: string | null;
unreadCount: number;
@@ -556,6 +569,7 @@ export class ChannelRepository {
channel: channels,
lastReadSequence: channelMembers.lastReadSequence,
memberId: channelMembers.id,
+ pinnedAt: channelMembers.pinnedAt,
role: this.callerRole(),
shareId: agentShares.id,
shareSlug: sql<string | null>`${agentShares.shareConfig} ->> 'slug'`,
@@ -662,25 +676,28 @@ export class ChannelRepository {
agentIdsOf.set(channelId, ids);
}
- const joined = rows.map(({ channel, lastReadSequence, role, shareId, shareSlug, ...agent }) => {
- const sharedAgent = isSharedAgentChannel(channel, agent, this.userId);
- return {
- ...channel,
- agentIds: agentIdsOf.get(channel.id) ?? [],
- sharedAgent,
- // Its visitor page (`/a/<handle>`) is where the caller can see this Agent; it has no other.
- sharedAgentHandle: sharedAgent ? (shareSlug ?? shareId) : null,
- canDelete: hasRole(role, 'admin'),
- joined: true,
- lastMessage: lastMessageOf.get(channel.id) ?? null,
- memberAvatars: avatarsOf.get(channel.id) ?? [],
- memberRole: role,
- mentionCount: mentionCountOf.get(channel.id) ?? 0,
- peerUserId: peerOf(channel, this.userId),
- replyJobs: replyJobsOf.get(channel.id) ?? [],
- unreadCount: Math.max(0, channel.sequence - (lastReadSequence ?? 0)),
- };
- });
+ const joined = rows.map(
+ ({ channel, lastReadSequence, pinnedAt, role, shareId, shareSlug, ...agent }) => {
+ const sharedAgent = isSharedAgentChannel(channel, agent, this.userId);
+ return {
+ ...channel,
+ agentIds: agentIdsOf.get(channel.id) ?? [],
+ sharedAgent,
+ // Its visitor page (`/a/<handle>`) is where the caller can see this Agent; it has no other.
+ sharedAgentHandle: sharedAgent ? (shareSlug ?? shareId) : null,
+ canDelete: hasRole(role, 'admin'),
+ joined: true,
+ lastMessage: lastMessageOf.get(channel.id) ?? null,
+ memberAvatars: avatarsOf.get(channel.id) ?? [],
+ memberRole: role,
+ mentionCount: mentionCountOf.get(channel.id) ?? 0,
+ peerUserId: peerOf(channel, this.userId),
+ pinnedAt,
+ replyJobs: replyJobsOf.get(channel.id) ?? [],
+ unreadCount: Math.max(0, channel.sequence - (lastReadSequence ?? 0)),
+ };
+ },
+ );
return [
...joined,
// Not joined: nothing is unread or pending for the caller yet.
@@ -696,6 +713,7 @@ export class ChannelRepository {
memberRole: null,
mentionCount: 0,
peerUserId: peerOf(channel, this.userId),
+ pinnedAt: null,
replyJobs: [],
unreadCount: 0,
})),
diff --git a/packages/types/src/channel.ts b/packages/types/src/channel.ts
index 5945303e5f8..6b48a7cf945 100644
--- a/packages/types/src/channel.ts
+++ b/packages/types/src/channel.ts
@@ -559,6 +559,11 @@ export interface ChannelListItem {
* the only entry of `memberAvatars`; null otherwise.
*/
peerUserId?: string | null;
+ /**
+ * When the caller pinned the conversation atop their own list, null when not pinned; set by the
+ * Channel list. Personal: other members never see it.
+ */
+ pinnedAt?: Date | null;
/** The project the channel is grouped under; null for a standalone channel. */
projectId: string | null;
/**