Add room handler
This commit is contained in:
parent
98fe90264f
commit
ae3353c6cb
77
src/rooms.test.ts
Normal file
77
src/rooms.test.ts
Normal file
@ -0,0 +1,77 @@
|
||||
import { MatrixRoomPresets, RcRoomTypes, mapRoom } from './rooms'
|
||||
|
||||
const roomCreator = {
|
||||
_id: 'roomcreatorid',
|
||||
name: 'RoomCreator',
|
||||
username: 'RoomCreator',
|
||||
roles: [],
|
||||
__rooms: [],
|
||||
}
|
||||
|
||||
test('mapping direct chats', () => {
|
||||
expect(
|
||||
mapRoom({
|
||||
_id: 'aliceidbobid',
|
||||
t: RcRoomTypes.direct,
|
||||
usernames: ['Alice', 'Bob'],
|
||||
uids: ['aliceid', 'bobid'],
|
||||
})
|
||||
).toEqual({
|
||||
is_direct: true,
|
||||
preset: MatrixRoomPresets.trusted,
|
||||
creation_content: {
|
||||
'm.federate': false,
|
||||
},
|
||||
_creatorId: 'aliceid',
|
||||
})
|
||||
})
|
||||
|
||||
test('mapping public rooms', () => {
|
||||
expect(
|
||||
mapRoom({
|
||||
_id: 'randomRoomId',
|
||||
fname: 'public',
|
||||
description: 'Public chat room',
|
||||
name: 'public',
|
||||
t: RcRoomTypes.chat,
|
||||
u: roomCreator,
|
||||
})
|
||||
).toEqual({
|
||||
preset: MatrixRoomPresets.public,
|
||||
room_alias_name: 'public',
|
||||
name: 'public',
|
||||
topic: 'Public chat room',
|
||||
creation_content: {
|
||||
'm.federate': false,
|
||||
},
|
||||
_creatorId: roomCreator._id,
|
||||
})
|
||||
})
|
||||
|
||||
test('mapping private rooms', () => {
|
||||
expect(
|
||||
mapRoom({
|
||||
_id: 'privateRoomId',
|
||||
name: 'private',
|
||||
fname: 'private',
|
||||
description: 'Private chat room',
|
||||
t: RcRoomTypes.private,
|
||||
u: roomCreator,
|
||||
})
|
||||
).toEqual({
|
||||
preset: MatrixRoomPresets.private,
|
||||
room_alias_name: 'private',
|
||||
name: 'private',
|
||||
topic: 'Private chat room',
|
||||
creation_content: {
|
||||
'm.federate': false,
|
||||
},
|
||||
_creatorId: roomCreator._id,
|
||||
})
|
||||
})
|
||||
|
||||
test('mapping live chats', () => {
|
||||
expect(() =>
|
||||
mapRoom({ _id: 'liveChatId', t: RcRoomTypes.live })
|
||||
).toThrowError('Room type l is unknown')
|
||||
})
|
||||
88
src/rooms.ts
Normal file
88
src/rooms.ts
Normal file
@ -0,0 +1,88 @@
|
||||
import log from './helpers/logger'
|
||||
import { getAccessToken } from './helpers/storage'
|
||||
import { axios, getUserSessionOptions } from './helpers/synapse'
|
||||
import { RcUser } from './users'
|
||||
|
||||
export const enum RcRoomTypes {
|
||||
direct = 'd',
|
||||
chat = 'c',
|
||||
private = 'p',
|
||||
live = 'l',
|
||||
}
|
||||
|
||||
export type RcRoom = {
|
||||
_id: string
|
||||
t: RcRoomTypes
|
||||
uids?: string[]
|
||||
usernames?: string[]
|
||||
name?: string
|
||||
u?: RcUser
|
||||
topic?: string
|
||||
fname?: string
|
||||
description?: string
|
||||
}
|
||||
|
||||
export const enum MatrixRoomPresets {
|
||||
private = 'private_chat',
|
||||
public = 'public_chat',
|
||||
trusted = 'trusted_private_chat',
|
||||
}
|
||||
|
||||
export type MatrixRoom = {
|
||||
room_id?: string
|
||||
name?: string
|
||||
creation_content?: object
|
||||
room_alias_name?: string
|
||||
topic?: string
|
||||
is_direct?: boolean
|
||||
preset?: MatrixRoomPresets
|
||||
_creatorId?: string
|
||||
}
|
||||
|
||||
export function mapRoom(rcRoom: RcRoom): MatrixRoom {
|
||||
const room: MatrixRoom = {
|
||||
creation_content: {
|
||||
'm.federate': false,
|
||||
},
|
||||
}
|
||||
rcRoom.name && (room.name = rcRoom.name)
|
||||
rcRoom.name && (room.room_alias_name = rcRoom.name)
|
||||
rcRoom.description && (room.topic = rcRoom.description)
|
||||
|
||||
switch (rcRoom.t) {
|
||||
case 'd':
|
||||
room.is_direct = true
|
||||
room.preset = MatrixRoomPresets.trusted
|
||||
room._creatorId = rcRoom.uids?.[0]
|
||||
break
|
||||
|
||||
case 'c':
|
||||
room.preset = MatrixRoomPresets.public
|
||||
room._creatorId = rcRoom.u?._id
|
||||
break
|
||||
|
||||
case 'p':
|
||||
room.preset = MatrixRoomPresets.private
|
||||
room._creatorId = rcRoom.u?._id
|
||||
break
|
||||
|
||||
default:
|
||||
const message = `Room type ${rcRoom.t} is unknown`
|
||||
log.error(message)
|
||||
throw new Error(message)
|
||||
}
|
||||
return room
|
||||
}
|
||||
|
||||
export async function createRoom(rcRoom: RcRoom): Promise<MatrixRoom> {
|
||||
const room: MatrixRoom = mapRoom(rcRoom)
|
||||
room.room_id = (
|
||||
await axios.post(
|
||||
'/_matrix/client/v3/createRoom',
|
||||
room,
|
||||
await getUserSessionOptions(room._creatorId!)
|
||||
)
|
||||
).data.room_id
|
||||
|
||||
return room
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user