31 lines
1009 B
TypeScript
31 lines
1009 B
TypeScript
import { ChatRoom, createChatRoom, fetchChatRooms } from '@/lib/api';
|
|
|
|
export async function findOrCreateMemberChat(
|
|
groupId: string,
|
|
memberUserId: string,
|
|
memberName: string,
|
|
currentUserId: string,
|
|
token: string
|
|
): Promise<ChatRoom> {
|
|
const response = await fetchChatRooms(groupId, token);
|
|
const rooms = response.rooms ?? [];
|
|
const existing = rooms.find(
|
|
(room) =>
|
|
room.type === 'GROUP' &&
|
|
room.members.length === 2 &&
|
|
room.members.some((member) => member.userId === memberUserId) &&
|
|
room.members.some((member) => member.userId === currentUserId)
|
|
);
|
|
if (existing) return existing;
|
|
return createChatRoom(groupId, memberName, [memberUserId], token);
|
|
}
|
|
|
|
export function roomDisplayLabel(room: ChatRoom, currentUserId: string) {
|
|
if (room.type === 'GENERAL') return room.name;
|
|
if (room.members.length === 2) {
|
|
const other = room.members.find((member) => member.userId !== currentUserId);
|
|
if (other) return other.displayName;
|
|
}
|
|
return room.name;
|
|
}
|