Skip to content

Commit

Permalink
Add /rooms API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
VityaSchel committed Aug 13, 2024
1 parent f9f0b64 commit 0bfa6d2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/router/get-rooms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { getRooms } from '@/room'
import type { SogsRequest, SogsResponse } from '@/router'
import { getRoomDetails } from '@/router/get-room'
import { testPermission } from '@/utils'

export async function getRoomsRoute(req: SogsRequest): Promise<SogsResponse> {
const rooms = getRooms()
const accessibleRooms: any[] = []

for (const room of rooms.values()) {
if (req.user !== null) {
const permissions = await room.getUserPermissions(req.user)
if (!testPermission(permissions, ['accessible'])) {
continue
}
} else if (!room.defaultAccessible) {
continue
}

accessibleRooms.push(await getRoomDetails(room, null))
}

return {
status: 200,
response: accessibleRooms,
contentType: 'application/json'
}
}
2 changes: 2 additions & 0 deletions src/router/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { User } from '@/user'
import { getCapabilities } from '@/router/get-capabilities'
import { getRoomsRoute } from '@/router/get-rooms'
import { getRoom } from '@/router/get-room'
import { getRoomUpdates } from '@/router/get-room-updates'
import { getRoomRecentMessages } from '@/router/get-room-recent-messages'
Expand Down Expand Up @@ -48,6 +49,7 @@ type Route = { method: string, route: string, handler: (req: SogsRequest) => Sog
const router: Route[] = []

router.push({ method: 'GET', route: '/capabilities', handler: getCapabilities })
router.push({ method: 'GET', route: '/rooms', handler: getRoomsRoute })
router.push({ method: 'GET', route: '/room/:token', handler: getRoom })
router.push({ method: 'GET', route: '/room/:token/pollInfo/:info_updates', handler: getRoomUpdates })
router.push({ method: 'DELETE', route: '/room/:token/all/:session_id', handler: deleteAllFromUser })
Expand Down

0 comments on commit 0bfa6d2

Please sign in to comment.