Files
ds-chat/frontend/src/api/rooms.ts
T
ksmithandClaude Sonnet 5 278f8bb995 Also email room subscribers on the first unread message, not just mentions (#67)
The previous pass only emailed on a mention. Corrected scope: the
room's first unread message triggers one debounced email (same shape
as #66), and every mention additionally emails regardless of that
debounce, since a mention shouldn't get silently absorbed by an
earlier plain message's already-sent notification.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-28 19:51:02 -06:00

196 lines
5.7 KiB
TypeScript

import { apiFetch, ApiError, NetworkError } from './client'
import type {
Message,
MessageFileInfo,
MyRoomItem,
Room,
RoomAttachment,
RoomListItem,
RoomMember,
RoomRole,
} from '../types'
export function listRooms(): Promise<RoomListItem[]> {
return apiFetch<RoomListItem[]>('/api/rooms')
}
export function listMyRooms(): Promise<MyRoomItem[]> {
return apiFetch<MyRoomItem[]>('/api/rooms/mine')
}
export function createRoom(
name: string,
description?: string,
isPrivate = false,
): Promise<Room> {
return apiFetch<Room>('/api/rooms', {
method: 'POST',
body: JSON.stringify({ name, description: description || null, is_private: isPrivate }),
})
}
// #52: find-or-create -- returns the existing DM with this person if one
// already exists, rather than always creating a new room.
export function startDm(otherUserId: string): Promise<Room> {
return apiFetch<Room>('/api/rooms/dm', {
method: 'POST',
body: JSON.stringify({ other_user_id: otherUserId }),
})
}
export function updateRoom(
roomId: string,
data: { name?: string; description?: string; is_private?: boolean },
): Promise<Room> {
return apiFetch<Room>(`/api/rooms/${roomId}`, {
method: 'PATCH',
body: JSON.stringify(data),
})
}
export function deleteRoom(roomId: string): Promise<void> {
return apiFetch<void>(`/api/rooms/${roomId}`, { method: 'DELETE' })
}
export function joinRoom(roomId: string): Promise<Room> {
return apiFetch<Room>(`/api/rooms/${roomId}/join`, { method: 'POST' })
}
export function leaveRoom(roomId: string): Promise<void> {
return apiFetch<void>(`/api/rooms/${roomId}/leave`, { method: 'POST' })
}
// #52 follow-up: only for DMs -- hides it from this user's own sidebar
// without touching the other participant's copy. Reversible: messaging
// again (startDm, above) or a new message from the other person un-hides
// it automatically.
export function hideDm(roomId: string): Promise<void> {
return apiFetch<void>(`/api/rooms/${roomId}/hide`, { method: 'POST' })
}
// #67: per-viewer opt-in for email on this room's first unread message
// and every mention. 400s for a DM (backend rejects it -- see
// set_room_email_notifications), so callers should only expose the
// toggle for a non-DM room.
export function updateRoomNotifications(roomId: string, emailNotifications: boolean): Promise<void> {
return apiFetch<void>(`/api/rooms/${roomId}/notifications`, {
method: 'PATCH',
body: JSON.stringify({ email_notifications: emailNotifications }),
})
}
export function listRoomMembers(roomId: string): Promise<RoomMember[]> {
return apiFetch<RoomMember[]>(`/api/rooms/${roomId}/members`)
}
export function listRoomAttachments(roomId: string): Promise<RoomAttachment[]> {
return apiFetch<RoomAttachment[]>(`/api/rooms/${roomId}/attachments`)
}
export function markRoomRead(roomId: string): Promise<void> {
return apiFetch<void>(`/api/rooms/${roomId}/read`, { method: 'POST' })
}
export function addRoomMember(roomId: string, userId: string): Promise<RoomMember> {
return apiFetch<RoomMember>(`/api/rooms/${roomId}/members`, {
method: 'POST',
body: JSON.stringify({ user_id: userId }),
})
}
export function removeMember(roomId: string, userId: string): Promise<void> {
return apiFetch<void>(`/api/rooms/${roomId}/members/${userId}`, { method: 'DELETE' })
}
export function changeMemberRole(
roomId: string,
userId: string,
role: RoomRole,
): Promise<RoomMember> {
return apiFetch<RoomMember>(`/api/rooms/${roomId}/members/${userId}`, {
method: 'PATCH',
body: JSON.stringify({ role }),
})
}
export function transferOwnership(roomId: string, newOwnerUserId: string): Promise<Room> {
return apiFetch<Room>(`/api/rooms/${roomId}/transfer-ownership`, {
method: 'POST',
body: JSON.stringify({ new_owner_user_id: newOwnerUserId }),
})
}
export function getRoomMessages(roomId: string): Promise<Message[]> {
return apiFetch<Message[]>(`/api/rooms/${roomId}/messages`)
}
// Not apiFetch: that wrapper always sets Content-Type: application/json,
// which would stomp the multipart boundary the browser needs to set itself
// for a file upload.
export async function uploadRoomImage(roomId: string, file: File): Promise<{ id: string }> {
const formData = new FormData()
formData.append('file', file)
let response: Response
try {
response = await fetch(`/api/rooms/${roomId}/images`, {
method: 'POST',
credentials: 'include',
body: formData,
})
} catch {
throw new NetworkError()
}
if (!response.ok) {
let detail = response.statusText
try {
const body = await response.json()
detail = body.detail ?? detail
} catch {
// response had no JSON body
}
throw new ApiError(response.status, detail)
}
return (await response.json()) as { id: string }
}
export function getRoomImageUrl(roomId: string, imageId: string): string {
return `/api/rooms/${roomId}/images/${imageId}`
}
// Not apiFetch, same multipart-boundary reason as uploadRoomImage.
export async function uploadRoomFile(roomId: string, file: File): Promise<MessageFileInfo> {
const formData = new FormData()
formData.append('file', file)
let response: Response
try {
response = await fetch(`/api/rooms/${roomId}/files`, {
method: 'POST',
credentials: 'include',
body: formData,
})
} catch {
throw new NetworkError()
}
if (!response.ok) {
let detail = response.statusText
try {
const body = await response.json()
detail = body.detail ?? detail
} catch {
// response had no JSON body
}
throw new ApiError(response.status, detail)
}
return (await response.json()) as MessageFileInfo
}
export function getRoomFileUrl(roomId: string, fileId: string): string {
return `/api/rooms/${roomId}/files/${fileId}`
}