Task lists WIP:
* pubsub manager * some refactoring * various fixes
This commit is contained in:
248
conversejs/custom/shared/lib/pubsub-manager.js
Normal file
248
conversejs/custom/shared/lib/pubsub-manager.js
Normal file
@ -0,0 +1,248 @@
|
||||
import { converse, _converse, api } from '../../../src/headless/core.js'
|
||||
const { Strophe, $iq, sizzle } = converse.env
|
||||
|
||||
/**
|
||||
* This class helps to manage some objects that are stored on pubsub nodes.
|
||||
* This if for example used for livechat-taskslists and livechat-tasks,
|
||||
* but could be used for other object types that would be added one day.
|
||||
*/
|
||||
export class PubSubManager {
|
||||
roomJID
|
||||
node
|
||||
types
|
||||
stanzaHandler
|
||||
|
||||
/**
|
||||
* Created a new pubsub manager.
|
||||
* @param {string} roomJID the room JID
|
||||
* @param {string} node the node name to which subscribe
|
||||
* @param {Array} types an array with object describing the object type, and how to handle them. Here is its format:
|
||||
* {
|
||||
* itemTag: 'task',
|
||||
* xmlns: XMLNS_TASK,
|
||||
* collection: mucModel.tasks,
|
||||
* fields: { // these are item child nodes (tag names)
|
||||
* name: String,
|
||||
* },
|
||||
* attributes: { // these are attribute on the node
|
||||
* order: Number
|
||||
* done: Boolean,
|
||||
* list: String,
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
constructor (roomJID, node, types) {
|
||||
this.roomJID = roomJID
|
||||
this.node = node
|
||||
this.types = types
|
||||
|
||||
this.stanzaHandler = undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the manager.
|
||||
* This will subscribe to the node, and retrieve all existing objects.
|
||||
*/
|
||||
async start () {
|
||||
// FIXME: handle errors. Find a way to display to user that this failed.
|
||||
|
||||
this.stanzaHandler = _converse.connection.addHandler(
|
||||
(message) => {
|
||||
try {
|
||||
this._handleMessage(message)
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
return true // if returning anything else, the handler will not be called again!
|
||||
},
|
||||
null, // The namespace to match.
|
||||
'message', // The stanza name to match.
|
||||
'headline', // The stanza type attribute to match.
|
||||
null, // The stanza id attribute to match.
|
||||
this.roomJID, // The stanza from attribute to match.
|
||||
{
|
||||
matchBareFromJid: true
|
||||
} // The handler options
|
||||
)
|
||||
await this._subscribe()
|
||||
await this._retrieveAllItems()
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the manager
|
||||
*/
|
||||
async stop () {
|
||||
// await this._unsubscribe() TODO
|
||||
|
||||
if (this.stanzaHandler) {
|
||||
_converse.connection.deleteHandler(this.stanzaHandler)
|
||||
this.stanzaHandler = undefined
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribed to the pubsub node.
|
||||
*/
|
||||
async _subscribe () {
|
||||
const stanza = $iq({
|
||||
type: 'set',
|
||||
from: _converse.jid,
|
||||
to: this.roomJID
|
||||
}).c('pubsub', { xmlns: Strophe.NS.PUBSUB })
|
||||
.c('subscribe', { node: this.node, jid: _converse.bare_jid })
|
||||
|
||||
try {
|
||||
const iq = await api.sendIQ(stanza)
|
||||
|
||||
console.debug('subscribtion ok: ', iq)
|
||||
} catch (iq) {
|
||||
console.error('Failed to subscribe to ' + this.node, iq)
|
||||
throw iq
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all items
|
||||
*
|
||||
* TODO: handle pagination if results are not all sent.
|
||||
* See https://xmpp.org/extensions/xep-0060.html#subscriber-retrieve-returnsome
|
||||
*/
|
||||
async _retrieveAllItems () {
|
||||
// Requesting all items.
|
||||
const stanza = $iq({
|
||||
type: 'get',
|
||||
from: _converse.jid,
|
||||
to: this.roomJID
|
||||
}).c('pubsub', { xmlns: Strophe.NS.PUBSUB })
|
||||
.c('items', { node: this.node })
|
||||
|
||||
try {
|
||||
const iq = await api.sendIQ(stanza)
|
||||
this._handleIQ(iq)
|
||||
} catch (iq) {
|
||||
if (iq === null || !iq?.querySelector) {
|
||||
console.error('Failed to retrieve objects from ' + this.node, iq)
|
||||
throw iq
|
||||
}
|
||||
if (!iq.querySelector('error[type="cancel"] item-not-found')) {
|
||||
console.error('Failed to retrieve objects from ' + this.node + ':', iq)
|
||||
throw iq
|
||||
}
|
||||
// This is totally normal when you open an empty node.
|
||||
console.log('Not ' + this.node + ' node for now')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an incomming message contains data to dispatch.
|
||||
* @param message The incoming stanza
|
||||
*/
|
||||
_handleMessage (message) {
|
||||
const itemsNodes = sizzle(`event[xmlns="${Strophe.NS.PUBSUB}#event"] items[node="${this.node}"]`, message)
|
||||
if (!itemsNodes.length) {
|
||||
return
|
||||
}
|
||||
for (const itemsNode of itemsNodes) {
|
||||
this._dispatchStanza(itemsNode)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* As _handleMessage, but for IQ.
|
||||
* @param iq stanza
|
||||
*/
|
||||
_handleIQ (iq) {
|
||||
const itemsNodes = sizzle(`pubsub items[node="${this.node}"]`, iq)
|
||||
if (!itemsNodes.length) { return }
|
||||
for (const itemsNode of itemsNodes) {
|
||||
this._dispatchStanza(itemsNode)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse items in a stanza response, and dispatch them to collections.
|
||||
* @param {stanza} itemsNode the 'items' part of the incomming stanza.
|
||||
*/
|
||||
_dispatchStanza (itemsNode) {
|
||||
console.debug('Dispatching items for node ' + this.node + ' from stanza items: ', itemsNode)
|
||||
|
||||
for (const key in this.types) {
|
||||
const type = this.types[key]
|
||||
|
||||
const selector = `item ${type.itemTag}[xmlns="${type.xmlns}"]`
|
||||
const items = sizzle(selector, itemsNode)
|
||||
if (!items.length) { continue }
|
||||
|
||||
console.log('Found ' + items.length + ' ' + type.itemTag + ' in stanza, dispatching...')
|
||||
for (const item of items) {
|
||||
// Note: we consider that there is only one object in an item.
|
||||
const id = item.parentNode?.getAttribute('id')
|
||||
if (!id) {
|
||||
console.error('Missing id for this item', item)
|
||||
continue
|
||||
}
|
||||
|
||||
const data = this._parseItem(item, type)
|
||||
if (data === null) { continue }
|
||||
|
||||
const existing = type.collection.get(id)
|
||||
if (existing) {
|
||||
existing.save(data)
|
||||
} else {
|
||||
type.collection.create(Object.assign(data, { id }))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this._handleRetractations(itemsNode)
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse retractations in IQ or Message stanza, and process them.
|
||||
* @param stanza IQ response
|
||||
*/
|
||||
_handleRetractations (stanza) {
|
||||
// Note: here we don't know the object type. We must try on each collection.
|
||||
const ids = sizzle('', stanza).map(i => i.getAttribute('id'))
|
||||
for (const id of ids) {
|
||||
for (const key in this.types) {
|
||||
const type = this.types[key]
|
||||
const item = type.collection.get(id)
|
||||
if (!item) { continue }
|
||||
console.log('Removing Item ' + id + ' that was found in collection ' + key)
|
||||
type.collection.remove(item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_parseItem (itemNode, type) {
|
||||
const data = {}
|
||||
if (type.fields) {
|
||||
for (const child of itemNode.children ?? []) {
|
||||
const fieldName = child.tagName
|
||||
if (!(fieldName in type.fields)) {
|
||||
continue
|
||||
}
|
||||
data[fieldName] = this._readValue(child.textContent, type.fields[fieldName])
|
||||
}
|
||||
}
|
||||
if (type.attributes) {
|
||||
for (const attr in type.attributes) {
|
||||
const value = itemNode.getAttribute(attr)
|
||||
if (attr !== undefined) {
|
||||
data[attr] = this._readValue(value, type.attributes[attr])
|
||||
}
|
||||
}
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
_readValue (v, t) {
|
||||
switch (t) {
|
||||
case String: return (v ?? '').toString()
|
||||
case Number: return parseInt(v) // only integers?
|
||||
case Boolean: return !!v
|
||||
default: return v // dont know what to do
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user