import { http } from "./http"; import type { Chat, ChatType, Message, MessageType } from "../chat/types"; import axios from "axios"; export async function getChats(): Promise { const { data } = await http.get("/chats"); return data; } export async function createPrivateChat(memberId: number): Promise { return createChat("private", null, [memberId]); } export async function createChat(type: ChatType, title: string | null, memberIds: number[] = []): Promise { const { data } = await http.post("/chats", { type, title, member_ids: memberIds }); return data; } export async function getMessages(chatId: number, beforeId?: number): Promise { const { data } = await http.get(`/messages/${chatId}`, { params: { limit: 50, before_id: beforeId } }); return data; } export async function sendMessage(chatId: number, text: string, type: MessageType = "text"): Promise { const { data } = await http.post("/messages", { chat_id: chatId, text, type }); return data; } export interface UploadUrlResponse { upload_url: string; file_url: string; object_key: string; expires_in: number; required_headers: Record; } export async function requestUploadUrl(file: File): Promise { const { data } = await http.post("/media/upload-url", { file_name: file.name, file_type: file.type || "application/octet-stream", file_size: file.size }); return data; } export async function uploadToPresignedUrl( uploadUrl: string, requiredHeaders: Record, file: File, onProgress?: (percent: number) => void ): Promise { await axios.put(uploadUrl, file, { headers: requiredHeaders, onUploadProgress: (progressEvent) => { if (!onProgress || !progressEvent.total) { return; } onProgress(Math.round((progressEvent.loaded * 100) / progressEvent.total)); } }); } export async function attachFile(messageId: number, fileUrl: string, fileType: string, fileSize: number): Promise { await http.post("/media/attachments", { message_id: messageId, file_url: fileUrl, file_type: fileType, file_size: fileSize }); }