🤯 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(contact): open the one-on-one conversation once a contact request is accepted (#23411)
Arvin Xud2e6a9760fa9
diff --git a/apps/server/src/routers/lambda/__tests__/contact.notify.test.ts b/apps/server/src/routers/lambda/__tests__/contact.notify.test.tsindex 16e892a2b95..96c6d3e9d1b 100644--- a/apps/server/src/routers/lambda/__tests__/contact.notify.test.ts+++ b/apps/server/src/routers/lambda/__tests__/contact.notify.test.ts@@ -11,9 +11,10 @@ const model = vi.hoisted(() => ({sendRequest: vi.fn(),}));const { acceptRequest, sendRequest } = model;-const { contactsChanged, notify } = vi.hoisted(() => ({+const { contactsChanged, notify, openConversation } = vi.hoisted(() => ({contactsChanged: vi.fn(),notify: vi.fn(),+ openConversation: vi.fn(),}));vi.mock('@/database/core/db-adaptor', () => ({@@ -34,6 +35,10 @@ vi.mock('@/server/services/contact/notify', () => ({notifyContactsChangedAfterResponse: contactsChanged,}));+vi.mock('@/server/services/contact/conversation', () => ({+ openContactConversation: openConversation,+}));+const { contactRouter } = await import('../contact');const REQUEST_ID = '6f1c1d2e-4b0a-4c55-9a39-3b1d8f0e2a11';@@ -81,6 +86,36 @@ describe('contactRouter notifications', () => {});});+ describe('opens the conversation of the new contacts', () => {+ it('as the requester, in the request scope, on accepting', async () => {+ acceptRequest.mockResolvedValue({ contactUserId: 'them', workspaceId: 'ws' });+ await caller().acceptRequest({ requestId: REQUEST_ID });+ expect(openConversation).toHaveBeenCalledWith({+ addresseeId: 'me',+ db: {},+ requesterId: 'them',+ workspaceId: 'ws',+ });+ });++ it('when sending answers their pending request', async () => {+ sendRequest.mockResolvedValue({ requestId: REQUEST_ID, status: 'accepted' });+ await caller().sendRequest({ userId: 'them' });+ expect(openConversation).toHaveBeenCalledWith({+ addresseeId: 'me',+ db: {},+ requesterId: 'them',+ workspaceId: null,+ });+ });++ it('not for a request still pending', async () => {+ sendRequest.mockResolvedValue({ requestId: REQUEST_ID, status: 'pending' });+ await caller().sendRequest({ userId: 'them' });+ expect(openConversation).not.toHaveBeenCalled();+ });+ });+it('notifies no one when the request fails', async () => {sendRequest.mockRejectedValue(new Error('Already a contact'));diff --git a/apps/server/src/routers/lambda/contact.ts b/apps/server/src/routers/lambda/contact.tsindex 107005c8b52..2d35837fb77 100644--- a/apps/server/src/routers/lambda/contact.ts+++ b/apps/server/src/routers/lambda/contact.ts@@ -11,6 +11,7 @@ import { ContactModel } from '@/database/models/contact';import { router } from '@/libs/trpc/lambda';import { serverDatabase } from '@/libs/trpc/lambda/middleware';import { isChannelEnabled } from '@/server/services/channel/featureGate';+import { openContactConversation } from '@/server/services/contact/conversation';import {notifyContactRequestAfterResponse,notifyContactsChangedAfterResponse,@@ -76,6 +77,14 @@ export const contactRouter = router({).mutation(async ({ ctx, input }) => {const result = await ctx.contactModel.sendRequest(input.userId, input.message);+ // Their pending request is the one answered: they asked, in the active scope.+ if (result.status === 'accepted')+ await openContactConversation({+ addresseeId: ctx.userId,+ db: ctx.serverDB,+ requesterId: input.userId,+ workspaceId: ctx.workspaceId ?? null,+ });// Sending while their request is pending accepts theirs, so they hear it was accepted.notifyContactRequestAfterResponse({actorUserId: ctx.userId,@@ -91,6 +100,14 @@ export const contactRouter = router({.input(z.object({ requestId })).mutation(async ({ ctx, input }) => {const result = await ctx.contactModel.acceptRequest(input.requestId);+ // Opened by the requester in the request's scope: in a workspace that is the member who+ // invited, while the caller answers from the personal space.+ await openContactConversation({+ addresseeId: ctx.userId,+ db: ctx.serverDB,+ requesterId: result.contactUserId,+ workspaceId: result.workspaceId,+ });notifyContactRequestAfterResponse({actorUserId: ctx.userId,kind: 'accepted',diff --git a/apps/server/src/services/contact/__tests__/conversation.test.ts b/apps/server/src/services/contact/__tests__/conversation.test.tsnew file mode 100644index 00000000000..170efc1a919--- /dev/null+++ b/apps/server/src/services/contact/__tests__/conversation.test.ts@@ -0,0 +1,42 @@+// @vitest-environment node+import { beforeEach, describe, expect, it, vi } from 'vitest';++const { ChannelService, openPeer } = vi.hoisted(() => {+ const openPeer = vi.fn();+ return {+ ChannelService: vi.fn(function () {+ return { openPeer };+ }),+ openPeer,+ };+});++vi.mock('@/server/services/channel/ChannelService', () => ({ ChannelService }));++const { openContactConversation } = await import('../conversation');++const db = {} as any;++describe('openContactConversation', () => {+ beforeEach(() => vi.clearAllMocks());++ it('opens it as the requester in the request scope', async () => {+ await openContactConversation({ addresseeId: 'b', db, requesterId: 'a', workspaceId: 'ws' });+ expect(ChannelService).toHaveBeenCalledWith(db, 'a', 'ws');+ expect(openPeer).toHaveBeenCalledWith('b');+ });++ it('opens it in the personal space without a workspace', async () => {+ await openContactConversation({ addresseeId: 'b', db, requesterId: 'a', workspaceId: null });+ expect(ChannelService).toHaveBeenCalledWith(db, 'a', undefined);+ });++ it('never fails the acceptance', async () => {+ const error = vi.spyOn(console, 'error').mockImplementation(() => {});+ openPeer.mockRejectedValue(new Error('You are not contacts'));+ await expect(+ openContactConversation({ addresseeId: 'b', db, requesterId: 'a', workspaceId: null }),+ ).resolves.toBeUndefined();+ expect(error).toHaveBeenCalled();+ });+});diff --git a/apps/server/src/services/contact/conversation.ts b/apps/server/src/services/contact/conversation.tsnew file mode 100644index 00000000000..0b306e72e79--- /dev/null+++ b/apps/server/src/services/contact/conversation.ts@@ -0,0 +1,36 @@+import type { LobeChatDatabase } from '@lobechat/database';++import { ChannelService } from '@/server/services/channel/ChannelService';++export interface OpenContactConversationParams {+ /** Who was asked; the other side of the conversation. */+ addresseeId: string;+ db: LobeChatDatabase;+ /** Who sent the request; always allowed to open it in the request's scope. */+ requesterId: string;+ /** The request's scope: `null` is the personal space. */+ workspaceId: string | null;+}++/**+ * Open the one-on-one conversation of two people who just became contacts, so it lands in both+ * lists without either of them having to start it. Opening is idempotent, and it never fails the+ * acceptance: either person can still open it later from the contact.+ */+export const openContactConversation = async ({+ addresseeId,+ db,+ requesterId,+ workspaceId,+}: OpenContactConversationParams) => {+ try {+ await new ChannelService(db, requesterId, workspaceId ?? undefined).openPeer(addresseeId);+ } catch (error) {+ console.error('[contact] Failed to open the conversation of new contacts', {+ addresseeId,+ error,+ requesterId,+ workspaceId,+ });+ }+};