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): count a sent message as read for its sender (#23263)

Arvin Xu4406d5c052fc
diff --git a/packages/database/src/repositories/channel/__tests__/agentPair.test.ts b/packages/database/src/repositories/channel/__tests__/agentPair.test.ts
index a4b08d4e19e..af869ee98c2 100644
--- a/packages/database/src/repositories/channel/__tests__/agentPair.test.ts
+++ b/packages/database/src/repositories/channel/__tests__/agentPair.test.ts
@@ -82,6 +82,17 @@ const setup = async () => {
};
describe('A conversation between Agents', () => {
+ // An Agent's request is not the person reading: their unseen answers stay unread.
+ it('leaves the person’s unread count alone when an Agent asks', async () => {
+ const { answer, ask, pair, repository } = await setup();
+ const first = await ask('First?');
+ await answer(first.id, 'First answer');
+ await ask('Second?');
+
+ const listed = (await repository.list()).find((item) => item.id === pair.channel.id);
+ expect(listed?.unreadCount).toBe(3);
+ });
+
it('is one per pair, whichever Agent opens it', async () => {
const { answerer, asker, open, pair } = await setup();
diff --git a/packages/database/src/repositories/channel/__tests__/identity.test.ts b/packages/database/src/repositories/channel/__tests__/identity.test.ts
index 4e0887590b2..6a524fccf69 100644
--- a/packages/database/src/repositories/channel/__tests__/identity.test.ts
+++ b/packages/database/src/repositories/channel/__tests__/identity.test.ts
@@ -68,7 +68,8 @@ describe('direct Channel identity', () => {
await repository.send(channel.id, { ...sendInput, content: 'Second', requestKey: 'second' });
expect((await repository.list()).find((item) => item.id === channel.id)).toMatchObject({
lastMessage: { content: 'Second', thread: null },
- unreadCount: 2,
+ // The caller's own messages are never unread for them.
+ unreadCount: 0,
});
});
diff --git a/packages/database/src/repositories/channel/__tests__/projectAccess.test.ts b/packages/database/src/repositories/channel/__tests__/projectAccess.test.ts
index 5397eecfaa0..932f4b220d2 100644
--- a/packages/database/src/repositories/channel/__tests__/projectAccess.test.ts
+++ b/packages/database/src/repositories/channel/__tests__/projectAccess.test.ts
@@ -101,10 +101,11 @@ describe('open project Channels', () => {
await teammate.send(channel.id, { content: 'Hi', requestKey: 'hi', routing: 'fallback' });
expect(await membershipOf(channel.id)).toEqual([
- expect.objectContaining({ lastReadSequence: 1, role: 'member', status: 'active' }),
+ // Sending reads up to the teammate's own message.
+ expect.objectContaining({ lastReadSequence: 2, role: 'member', status: 'active' }),
]);
expect(await teammate.list()).toEqual([
- expect.objectContaining({ id: channel.id, joined: true, unreadCount: 1 }),
+ expect.objectContaining({ id: channel.id, joined: true, unreadCount: 0 }),
]);
expect((await teammate.detail(channel.id)).joined).toBe(true);
expect((await owner.detail(channel.id)).messages.map((message) => message.content)).toEqual([
diff --git a/packages/database/src/repositories/channel/__tests__/send.test.ts b/packages/database/src/repositories/channel/__tests__/send.test.ts
index 6f7d6ded76a..ea64c8956c6 100644
--- a/packages/database/src/repositories/channel/__tests__/send.test.ts
+++ b/packages/database/src/repositories/channel/__tests__/send.test.ts
@@ -30,6 +30,40 @@ describe('ChannelRepository.send', () => {
).toEqual([]);
});
+ it('counts a message as read for its sender, unread for everyone else', async () => {
+ const { asMember, asOwner, channel } = await fixture.createTeamChannel();
+ await asOwner.send(channel.id, {
+ content: 'Earlier',
+ requestKey: 'earlier',
+ routing: 'fallback',
+ });
+ await asMember.send(channel.id, { content: 'Hi', requestKey: 'hi', routing: 'fallback' });
+
+ const [forMember] = await asMember.list();
+ expect(forMember.unreadCount).toBe(0);
+ const [forOwner] = await asOwner.list();
+ expect(forOwner.unreadCount).toBe(1);
+ });
+
+ // A relay (or its receipt) is posted for the person while they may be away.
+ it('leaves the sender’s unread alone for a request an Agent relayed for them', async () => {
+ const { asMember, asOwner, channel } = await fixture.createTeamChannel();
+ await asOwner.send(channel.id, {
+ content: 'Earlier',
+ requestKey: 'earlier',
+ routing: 'fallback',
+ });
+ await asMember.send(channel.id, {
+ content: 'Relayed',
+ metadata: { via: { agentId: 'agent-relay', name: 'Relay' } },
+ requestKey: 'relayed',
+ routing: 'fallback',
+ });
+
+ const [forMember] = await asMember.list();
+ expect(forMember.unreadCount).toBe(2);
+ });
+
it('queues a job per mentioned Agent, on behalf of the sender', async () => {
const { asMember, channel, member } = await fixture.createTeamChannel();
const coder = await fixture.addAgent(channel.id, 'Coder');
diff --git a/packages/database/src/repositories/channel/send.ts b/packages/database/src/repositories/channel/send.ts
index be2a9e9894b..b1a617a37fb 100644
--- a/packages/database/src/repositories/channel/send.ts
+++ b/packages/database/src/repositories/channel/send.ts
@@ -1,6 +1,6 @@
import type { ChannelMessageMetadata, ChannelRoutingMode } from '@lobechat/types';
import { CHANNEL_LIMITS } from '@lobechat/types';
-import { and, asc, eq, inArray } from 'drizzle-orm';
+import { and, asc, eq, inArray, sql } from 'drizzle-orm';
import type { ChannelMessageItem } from '../../schemas';
import { agents, channelMembers, channelMessages, channelThreads, files } from '../../schemas';
@@ -309,6 +309,17 @@ export const sendMessage = async (
threadId,
});
await attachFiles(tx, { fileIds, messageId: message.id });
+ // Like any chat app, sending reads the conversation up to the sender's own message, so it
+ // never counts as unread (or notifies) on the sender's other devices. An Agent's request, or a
+ // relay (`via`) posted for the person, is not the person reading: their unseen messages stay
+ // unread.
+ if (!author && !input.metadata?.via)
+ await tx
+ .update(channelMembers)
+ .set({
+ lastReadSequence: sql`greatest(coalesce(${channelMembers.lastReadSequence}, 0), ${message.sequence})`,
+ })
+ .where(eq(channelMembers.id, member.id));
const delivered = await deliver(tx, message, recipients, member.id);
if (delivered.length) {
await tx