room delete endpoint :(

This commit is contained in:
Devin Zuczek
2026-07-20 11:37:59 -04:00
parent bbd702faf1
commit 3319a5d91a
5 changed files with 112 additions and 0 deletions
+4
View File
@@ -16,6 +16,10 @@ export type Env = SharedHonoEnv & {
// SignalR notifications hub (DO owned by the `notify` worker). Bound here to
// push RoomUpdate notifications when a room is mutated.
RECFLARE_NOTIFICATIONS_HUB: DurableObjectNamespace<NotificationsHub>
// Shared `recflare-cdn` R2 bucket (the `cdn`/`storage` workers own it). Room
// images/files live here under the `room/` key prefix; bound so deleting a room
// can remove its image object.
CDN_ASSETS: R2Bucket
}
/** Variables can be extended */
+40
View File
@@ -5,6 +5,7 @@ import {
canManageRoom,
cloneRoom,
cloneSubRoom,
deleteRoom,
findSubRoom,
getBaseRooms,
getFavoritedRooms,
@@ -582,6 +583,45 @@ const app = new Hono<App>()
return roomResult(c, { Success: true })
})
// Delete a room. Auth-gated (401) and owner-only (the room's CreatorAccountId).
// Removes the room record (and per-player interactions with it) and the room's
// image object from the shared CDN bucket. Images players *took* in the room are
// left alone — they live in the api/img world and outlast the room.
.delete('/rooms/:roomId{[0-9]+}', async (c) => {
const accountId = await authedAccountId(c)
if (accountId === null) return unauthorized(c)
const roomId = Number.parseInt(c.req.param('roomId'), 10)
const room = await getRoomById(c.env.DB, roomId)
if (!room) {
return roomResult(c, {
Success: false,
ErrorId: 'Rooms.DoesntExist',
Error: 'This room does not exist!',
})
}
if (room.CreatorAccountId !== accountId) {
return roomResult(c, {
Success: false,
ErrorId: 'Rooms.NotOwner',
Error: 'You are not the owner of this room!',
})
}
await deleteRoom(c.env.DB, roomId)
// Remove the room image from the CDN bucket. The stored ImageName is the
// un-prefixed key the `cdn` worker serves back under `room/` (see storage
// upload + the `GET /room/:dataBlob` route), so the object key is `room/<name>`.
// R2 deletes are idempotent, so a canonical/static or already-gone image is fine.
const imageName = typeof room.ImageName === 'string' ? room.ImageName : ''
if (imageName !== '') {
await c.env.CDN_ASSETS.delete(`room/${imageName}`)
}
return roomResult(c, { Success: true })
})
// Set a member's role in a room (`Roles[].Role`). Auth-gated (401) and gated to
// the room creator or a co-owner (403 otherwise) — the same owner/co-owner check
// the other room-admin actions use. Body is the `role` form field (an integer role
@@ -582,6 +582,51 @@ describe('rooms endpoints', () => {
expect(room.ImageName).toBe(imageName)
})
it('DELETE /rooms/:id is auth-gated, owner-only, and removes the room + its CDN image', async () => {
// Throwaway room owned by account 1, with its image object in the CDN bucket and
// a player interaction row.
const ImageName = 'test/2026-07-17/delete-me.jpg'
await env.DB.prepare('INSERT INTO room (data) VALUES (?1)')
.bind(
JSON.stringify({
RoomId: 9500,
Name: 'DeleteMe',
CreatorAccountId: 1,
IsDorm: false,
Accessibility: 1,
ImageName,
SubRooms: [],
})
)
.run()
await env.CDN_ASSETS.put(`room/${ImageName}`, new Uint8Array([1, 2, 3]))
await env.DB.prepare(
'INSERT INTO interaction (player_id, room_id, cheered, favorited) VALUES (7, 9500, 1, 1)'
).run()
const del = async (sub?: string) =>
SELF.fetch(`${ORIGIN}/rooms/9500`, {
method: 'DELETE',
headers: sub ? await bearer(sub) : {},
})
const roomExists = async () =>
(await env.DB.prepare('SELECT 1 FROM room WHERE room_id = 9500').first()) !== null
// No token → 401. A non-owner → Success:false (room untouched).
expect((await del()).status).toBe(401)
expect(await bodyOf(await del('2'))).toMatchObject({ Success: false, ErrorId: 'Rooms.NotOwner' })
expect(await roomExists()).toBe(true)
// Owner → Success:true; the room, its interactions, and the CDN image are gone.
expect(await bodyOf(await del('1'))).toMatchObject({ Success: true })
expect(await roomExists()).toBe(false)
expect(await env.CDN_ASSETS.get(`room/${ImageName}`)).toBeNull()
const interactions = await env.DB.prepare(
'SELECT COUNT(*) AS n FROM interaction WHERE room_id = 9500'
).first<{ n: number }>()
expect(interactions!.n).toBe(0)
})
it('PUT /rooms/:id/roles/:accountId is auth-gated, owner/co-owner-only, and persists', async () => {
const rolesOf = async (): Promise<Array<{ AccountId: number; Role: number }>> => {
const room = (await (await SELF.fetch(`${ORIGIN}/rooms/2`)).json()) as {