peertube-plugin-livechat/bots/lib/bot/room.ts

131 lines
3.5 KiB
TypeScript
Raw Normal View History

import type { BotComponent } from './component'
import type { BotHandler } from './handlers/base'
import type { XMPPStanza, XMPPUser } from './types'
import EventEmitter from 'events'
import { JID } from '@xmpp/jid'
2021-12-07 17:57:08 +00:00
import { logger } from '../logger'
export class BotRoom extends EventEmitter {
protected state: 'offline' | 'online' = 'offline'
protected userJID: JID | undefined
protected readonly roster: Map<string, XMPPUser> = new Map()
2021-12-07 17:57:08 +00:00
protected readonly handlers: BotHandler[] = []
2021-12-07 17:57:08 +00:00
constructor (
protected readonly component: BotComponent,
protected readonly roomJID: JID
2021-12-07 17:57:08 +00:00
) {
super()
this.on('reset', () => {
this.state = 'offline'
this.roster.clear()
})
this.on('stanza', (stanza: XMPPStanza, resource?: string) => {
this.receiveStanza(stanza, resource)
})
2021-12-07 17:57:08 +00:00
}
public isOnline (): boolean {
return this.state === 'online'
2021-12-07 17:57:08 +00:00
}
public async join (nick: string): Promise<void> {
this.userJID = new JID(this.roomJID.getLocal(), this.roomJID.getDomain(), nick)
logger.debug(`Emitting a presence for room ${this.roomJID.toString()}...`)
await this.component.sendStanza('presence',
{
to: this.userJID.toString()
},
this.component.xml('x', {
xmlns: 'http://jabber.org/protocol/muc'
})
)
// FIXME: should wait for a presence stanza from the server.
// FIXME: should handle used nick errors.
2021-12-07 17:57:08 +00:00
}
public async part (): Promise<void> {
if (!this.userJID) { return }
logger.debug(`Emitting a presence=unavailable for room ${this.roomJID.toString()}...`)
await this.component.sendStanza('presence', {
to: this.userJID.toString(),
type: 'unavailable'
})
// FIXME: should wait for a presence stanza from the server.
}
public async sendGroupchat (msg: string): Promise<void> {
if (!this.userJID) { return }
logger.debug(`Emitting a groupchat message for room ${this.roomJID.toString()}...`)
await this.component.sendStanza(
2021-12-07 17:57:08 +00:00
'message',
{
type: 'groupchat',
to: this.roomJID.toString()
2021-12-07 17:57:08 +00:00
},
this.component.xml('body', {}, msg)
2021-12-07 17:57:08 +00:00
)
}
public receiveStanza (stanza: XMPPStanza, fromResource?: string): void {
if (stanza.name === 'presence') {
this.receivePresenceStanza(stanza, fromResource)
2021-12-07 17:57:08 +00:00
}
}
public receivePresenceStanza (stanza: XMPPStanza, fromResource?: string): void {
if (!fromResource) {
2021-12-07 17:57:08 +00:00
return
}
const isPresent = stanza.attrs.type !== 'unavailable'
const statusElems = stanza.getChild('x')?.getChildren('status')
const statusCodes = []
if (statusElems) {
for (const s of statusElems) {
statusCodes.push(parseInt(s.attrs.code))
}
2021-12-07 17:57:08 +00:00
}
const isMe = statusCodes.includes(110) // status 110 means that is concern the current user.
let user = this.roster.get(fromResource)
const previousState = user?.state
2021-12-07 17:57:08 +00:00
if (!isPresent) {
if (!user) {
return
}
user.state = 'offline'
if (isMe) {
this.state = 'offline'
}
if (previousState === 'online') {
this.emit('room_part', user)
}
} else {
if (!user) {
user = {
state: 'online',
nick: fromResource,
isMe: isMe
}
this.roster.set(fromResource, user)
} else {
user.state = 'online'
}
if (isMe) {
this.state = 'online'
}
if (previousState !== 'online') {
this.emit('room_join', user)
2021-12-07 17:57:08 +00:00
}
}
}
public attachHandler (handler: BotHandler): void {
this.handlers.push(handler)
}
2021-12-07 17:57:08 +00:00
}