Track MUC occupants from presence instead of disco#items
XEP-0045 §6.5 lets a room keep its occupant list private (deployments are advised to disable it), so disco#items is unreliable for non-admin accounts. Occupants are instead tracked from the occupant presence broadcast the service sends after a join (XEP-0045 §7.1/§7.2.2). - getUsersInRoom() now returns nicks observed via occupant presence, excluding the bot itself - track occupants on available presence, including the initial roster which arrives before our own self-presence; untrack on unavailable - clear tracked occupants on leaveRoom()
This commit is contained in:
2 files changed
+83
-32
No files matched your search
+27
-16
@@ -4,7 +4,6 @@ import { client, xml, jid } from '@xmpp/client'
|
||||
const NS_MUC = 'http://jabber.org/protocol/muc'
|
||||
const NS_MUC_USER = 'http://jabber.org/protocol/muc#user'
|
||||
const NS_NICK = 'http://jabber.org/protocol/nick'
|
||||
const NS_DISCO_ITEMS = 'http://jabber.org/protocol/disco#items'
|
||||
const NS_XHTML_IM = 'http://jabber.org/protocol/xhtml-im'
|
||||
const NS_XHTML = 'http://www.w3.org/1999/xhtml'
|
||||
const NS_PING = 'urn:xmpp:ping'
|
||||
@@ -60,6 +59,11 @@ class XmppAdapter extends Adapter {
|
||||
// Key is the room JID, value is the private JID
|
||||
this.roomToPrivateJID = {}
|
||||
|
||||
// Store the occupants of each joined room, keyed by the room's bare JID.
|
||||
// Populated from occupant presence (see XEP-0045 §7.2.2); disco#items is
|
||||
// not used as deployments are advised to keep the occupant list private.
|
||||
this.occupants = new Map()
|
||||
|
||||
this.connected = false
|
||||
this.everConnected = false
|
||||
this.heardOwnPresence = false
|
||||
@@ -201,6 +205,8 @@ class XmppAdapter extends Adapter {
|
||||
const index = this.options.rooms.findIndex((joined) => joined.jid === room.jid)
|
||||
if (index > -1) this.options.rooms.splice(index, 1)
|
||||
|
||||
this.occupants.delete(room.jid)
|
||||
|
||||
this.robot.logger.debug(`Leaving ${room.jid}/${this.robot.name}`)
|
||||
|
||||
return this.client.send(xml('presence', {
|
||||
@@ -209,21 +215,13 @@ class XmppAdapter extends Adapter {
|
||||
}))
|
||||
}
|
||||
|
||||
// Send a query for users in the room and apply the callback against the
|
||||
// retrieved data. callback should be of the form `(usersInRoom) => ...`
|
||||
// where usersInRoom is an array of username strings.
|
||||
async getUsersInRoom(room, callback) {
|
||||
// http://xmpp.org/extensions/xep-0045.html#disco-roomitems
|
||||
this.robot.logger.debug(`Fetching users in the room ${room.jid}`)
|
||||
|
||||
let usersInRoom = []
|
||||
try {
|
||||
const query = await this.client.iqCaller.get(xml('query', { xmlns: NS_DISCO_ITEMS }), room.jid)
|
||||
// Note that this contains usernames and NOT the full user JID.
|
||||
usersInRoom = query.getChildren('item').map((item) => item.attrs.name)
|
||||
} catch (error) {
|
||||
this.robot.logger.error(`Unable to fetch users in room ${room.jid}: ${error}`)
|
||||
}
|
||||
// Return the nicks of the occupants currently known to be in the room,
|
||||
// excluding the bot itself. The list is derived from occupant presence
|
||||
// (XEP-0045 §7.2.2) and is therefore empty until the presence broadcast
|
||||
// following a join has been received.
|
||||
getUsersInRoom(room, callback) {
|
||||
const occupants = this.occupants.get(room.jid) ?? new Set()
|
||||
const usersInRoom = [...occupants].filter((nick) => nick !== this.robot.name)
|
||||
|
||||
this.robot.logger.debug(`[users in room] ${room.jid} has ${usersInRoom}`)
|
||||
if (typeof callback === 'function') callback(usersInRoom)
|
||||
@@ -358,6 +356,15 @@ class XmppAdapter extends Adapter {
|
||||
// ignored
|
||||
if (room === fromJID.toString()) { return }
|
||||
|
||||
// Track the occupant from presence (XEP-0045 §7.2.2). This runs
|
||||
// before our own self-presence is heard, so the initial roster sent
|
||||
// upon joining is captured as well.
|
||||
const nick = fromJID.resource
|
||||
if (nick) {
|
||||
if (!this.occupants.has(room)) this.occupants.set(room, new Set())
|
||||
this.occupants.get(room).add(nick)
|
||||
}
|
||||
|
||||
// Try to resolve the private JID
|
||||
const privateChatJID = this.resolvePrivateJID(stanza)
|
||||
|
||||
@@ -395,6 +402,10 @@ class XmppAdapter extends Adapter {
|
||||
// ignore our own messages in rooms
|
||||
if (user === this.options.username) { return }
|
||||
|
||||
// Stop tracking the occupant
|
||||
const occupants = this.occupants.get(room)
|
||||
if (occupants) occupants.delete(user)
|
||||
|
||||
this.robot.logger.debug(`Unavailable received from ${user} in room ${room}`)
|
||||
|
||||
user = this.robot.brain.userForId(user, { room })
|
||||
|
||||
+56
-16
@@ -7,7 +7,6 @@ import entry, { XmppAdapter } from '../index.mjs'
|
||||
|
||||
const NS_MUC = 'http://jabber.org/protocol/muc'
|
||||
const NS_MUC_USER = 'http://jabber.org/protocol/muc#user'
|
||||
const NS_DISCO_ITEMS = 'http://jabber.org/protocol/disco#items'
|
||||
const NS_XHTML_IM = 'http://jabber.org/protocol/xhtml-im'
|
||||
const NS_XHTML = 'http://www.w3.org/1999/xhtml'
|
||||
const NS_PING = 'urn:xmpp:ping'
|
||||
@@ -49,9 +48,6 @@ class MockClient extends EventEmitter {
|
||||
super()
|
||||
this.sent = []
|
||||
this.status = 'online'
|
||||
this.iqCaller = {
|
||||
get: async () => xml('query', { xmlns: NS_DISCO_ITEMS })
|
||||
}
|
||||
}
|
||||
|
||||
send(element) {
|
||||
@@ -131,6 +127,16 @@ describe('XmppAdapter', () => {
|
||||
assert.equal(presence.attrs.to, `${room.jid}/${adapter.robot.name}`)
|
||||
assert.equal(presence.attrs.type, 'unavailable')
|
||||
})
|
||||
|
||||
it('forgets the tracked occupants of the room', () => {
|
||||
const adapter = createAdapter()
|
||||
const room = { jid: 'test@example.com', password: false }
|
||||
adapter.occupants.set('test@example.com', new Set(['mark']))
|
||||
|
||||
adapter.leaveRoom(room)
|
||||
|
||||
assert.deepEqual(adapter.getUsersInRoom(room), [])
|
||||
})
|
||||
})
|
||||
|
||||
describe('#ping()', () => {
|
||||
@@ -157,25 +163,59 @@ describe('XmppAdapter', () => {
|
||||
})
|
||||
|
||||
describe('#getUsersInRoom()', () => {
|
||||
it('returns the users reported by the disco query', async () => {
|
||||
const room = { jid: 'test@example.com' }
|
||||
|
||||
function joinPresence(nick, jid) {
|
||||
return xml('presence', { type: 'available', to: 'bot@example.com', from: `test@example.com/${nick}` },
|
||||
xml('x', { xmlns: NS_MUC_USER }, xml('item', { jid, role: 'participant' })))
|
||||
}
|
||||
|
||||
function leavePresence(nick) {
|
||||
return xml('presence', { type: 'unavailable', to: 'bot@example.com', from: `test@example.com/${nick}` })
|
||||
}
|
||||
|
||||
it('is empty before any occupant presence is received', () => {
|
||||
const adapter = createAdapter()
|
||||
adapter.client.iqCaller.get = async () =>
|
||||
xml('query', { xmlns: NS_DISCO_ITEMS },
|
||||
xml('item', { name: 'mark' }),
|
||||
xml('item', { name: 'anup' }))
|
||||
|
||||
const users = await adapter.getUsersInRoom({ jid: 'test@example.com' })
|
||||
|
||||
assert.deepEqual(users, ['mark', 'anup'])
|
||||
assert.deepEqual(adapter.getUsersInRoom(room), [])
|
||||
})
|
||||
|
||||
it('invokes the callback with the users', async () => {
|
||||
it('tracks occupants from the initial roster broadcast', () => {
|
||||
const adapter = createAdapter()
|
||||
adapter.client.iqCaller.get = async () =>
|
||||
xml('query', { xmlns: NS_DISCO_ITEMS }, xml('item', { name: 'mark' }))
|
||||
// The initial roster arrives before our own self-presence
|
||||
adapter.heardOwnPresence = false
|
||||
|
||||
adapter.readPresence(joinPresence('mark', 'mark@example.com/mark'))
|
||||
adapter.readPresence(joinPresence('anup', 'anup@example.com/anup'))
|
||||
|
||||
assert.deepEqual(adapter.getUsersInRoom(room).sort(), ['anup', 'mark'])
|
||||
})
|
||||
|
||||
it('removes occupants when they leave', () => {
|
||||
const adapter = createAdapter()
|
||||
adapter.heardOwnPresence = true
|
||||
|
||||
adapter.readPresence(joinPresence('mark', 'mark@example.com/mark'))
|
||||
adapter.readPresence(joinPresence('anup', 'anup@example.com/anup'))
|
||||
adapter.readPresence(leavePresence('mark'))
|
||||
|
||||
assert.deepEqual(adapter.getUsersInRoom(room), ['anup'])
|
||||
})
|
||||
|
||||
it('excludes the bot itself', () => {
|
||||
const adapter = createAdapter()
|
||||
adapter.occupants.set('test@example.com', new Set(['bot', 'mark']))
|
||||
|
||||
assert.deepEqual(adapter.getUsersInRoom(room), ['mark'])
|
||||
})
|
||||
|
||||
it('invokes the callback with the users', () => {
|
||||
const adapter = createAdapter()
|
||||
adapter.heardOwnPresence = true
|
||||
adapter.readPresence(joinPresence('mark', 'mark@example.com/mark'))
|
||||
|
||||
let calledWith = null
|
||||
await adapter.getUsersInRoom({ jid: 'test@example.com' }, (users) => { calledWith = users })
|
||||
adapter.getUsersInRoom(room, (users) => { calledWith = users })
|
||||
|
||||
assert.deepEqual(calledWith, ['mark'])
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user