95 lines
2.7 KiB
TypeScript
95 lines
2.7 KiB
TypeScript
import { apiFetch } from "@/app/lib/api-client";
|
|
|
|
export interface ChatPeer {
|
|
id: string;
|
|
name?: string;
|
|
photo?: string;
|
|
location?: string;
|
|
citySlug?: string;
|
|
}
|
|
|
|
export interface ConversationItem {
|
|
id: string;
|
|
type: string;
|
|
intent?: string;
|
|
pairKey?: string;
|
|
matchConnectionId?: string;
|
|
lastMessageAt?: string;
|
|
lastMessagePreview?: string;
|
|
unreadCount: number;
|
|
peer?: ChatPeer | null;
|
|
created?: string;
|
|
updated?: string;
|
|
}
|
|
|
|
export interface ChatMessage {
|
|
id: string;
|
|
conversationId?: string;
|
|
senderId: string;
|
|
body: string;
|
|
created?: string;
|
|
mine: boolean;
|
|
}
|
|
|
|
export async function fetchConversations(): Promise<{
|
|
items: ConversationItem[];
|
|
unread: number;
|
|
}> {
|
|
const data = await apiFetch<{ items: ConversationItem[]; unread: number }>("/api/conversations");
|
|
return {
|
|
items: data.items || [],
|
|
unread: Number(data.unread || 0),
|
|
};
|
|
}
|
|
|
|
export async function fetchConversation(conversationId: string): Promise<ConversationItem> {
|
|
const data = await apiFetch<{ conversation: ConversationItem }>(`/api/conversations/${conversationId}`);
|
|
return data.conversation;
|
|
}
|
|
|
|
export async function fetchConversationByConnection(connectionId: string): Promise<ConversationItem> {
|
|
const data = await apiFetch<{ conversation: ConversationItem }>(
|
|
`/api/conversations/by-connection/${connectionId}`
|
|
);
|
|
return data.conversation;
|
|
}
|
|
|
|
export async function fetchMessages(
|
|
conversationId: string,
|
|
options?: { limit?: number; before?: string }
|
|
): Promise<ChatMessage[]> {
|
|
const params = new URLSearchParams();
|
|
if (options?.limit) params.set("limit", String(options.limit));
|
|
if (options?.before) params.set("before", options.before);
|
|
const query = params.toString();
|
|
const data = await apiFetch<{ items: ChatMessage[] }>(
|
|
`/api/conversations/${conversationId}/messages${query ? `?${query}` : ""}`
|
|
);
|
|
return data.items || [];
|
|
}
|
|
|
|
export async function sendChatMessage(conversationId: string, body: string): Promise<ChatMessage> {
|
|
const data = await apiFetch<{ message: ChatMessage }>(`/api/conversations/${conversationId}/messages`, {
|
|
method: "POST",
|
|
body: JSON.stringify({ body }),
|
|
});
|
|
return data.message;
|
|
}
|
|
|
|
export async function markChatRead(conversationId: string): Promise<void> {
|
|
await apiFetch(`/api/conversations/${conversationId}/read`, { method: "POST", body: JSON.stringify({}) });
|
|
}
|
|
|
|
export async function startConversation(options: {
|
|
profileId?: string;
|
|
targetUserId?: string;
|
|
intent?: string;
|
|
opener?: string;
|
|
}): Promise<ConversationItem> {
|
|
const data = await apiFetch<{ conversation: ConversationItem }>("/api/conversations/start", {
|
|
method: "POST",
|
|
body: JSON.stringify(options),
|
|
});
|
|
return data.conversation;
|
|
}
|