-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
266 lines (236 loc) · 7.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
'use strict'
const {EventEmitter} = require('@xmpp/events')
const xml = require('@xmpp/xml')
const NS_PUBSUB = 'http://jabber.org/protocol/pubsub'
const NS_PUBSUB_EVENT = `${NS_PUBSUB}#event`
const NS_PUBSUB_OWNER = `${NS_PUBSUB}#owner`
const NS_PUBSUB_NODE_CONFIG = `${NS_PUBSUB}#node_config`
const NS_RSM = 'http://jabber.org/protocol/rsm'
const NS_X_DATA = 'jabber:x:data'
function isPubSubEventNotification({stanza}) {
const child = stanza.getChild('event')
return stanza.is('message') && child && child.attrs.xmlns === NS_PUBSUB_EVENT
}
class PubSubPlugin extends EventEmitter {
constructor(client) {
super()
this.client = client
}
handleNotification({stanza}) {
const service = stanza.attrs.from
const items = stanza.getChild('event').getChild('items')
const {node} = items.attrs
const item = items.getChild('item')
const retract = items.getChild('retract')
if (item) {
const {id} = item.attrs
const entry = item.getChild('entry')
const delay = stanza.getChild('delay')
if (delay) {
const {stamp} = delay.attrs
this.emit(`last-item-published:${service}`, {node, id, entry, stamp})
this.emit(`last-item-published:${service}:${node}`, {
id,
entry,
stamp,
})
} else {
this.emit(`item-published:${service}`, {node, id, entry})
this.emit(`item-published:${service}:${node}`, {id, entry})
}
}
if (retract) {
const {id} = retract.attrs
this.emit(`item-deleted:${service}`, {node, id})
this.emit(`item-deleted:${service}:${node}`, {id})
}
}
/** @typedef {{[key: string]: string}} Options */
/**
* Create a new pubsub node.
*
* @param {string} service Service id
* @param {string} node Pubsub node id
* @param {Options|undefined} options PubSub node configuration
* @returns {Promise<string>} Name of the created node
*/
createNode(service, node, options) {
const {iqCaller} = this.client
const stanza = xml('pubsub', {xmlns: NS_PUBSUB}, xml('create', {node}))
if (options) {
const config = xml('configure')
const x = config.cnode(
xml(
'x',
{xmlns: NS_X_DATA, type: 'submit'},
xml(
'field',
{var: 'FORM_TYPE', type: 'hidden'},
xml('value', {}, NS_PUBSUB_NODE_CONFIG)
)
)
)
for (const key of Object.keys(options)) {
const option = xml(
'field',
{var: key},
xml('value', {}, options[key].toString())
)
x.cnode(option)
}
stanza.cnode(config)
}
return iqCaller
.request(xml('iq', {type: 'set', to: service}, stanza))
.then(result => result.getChild('pubsub').getChild('create').attrs.node)
}
/**
* Delete an existing pubsub node.
*
* @param {string} service Service id
* @param {string} node Pubsub node id
* @returns {Promise<void>} Result promise
*/
deleteNode(service, node) {
const {iqCaller} = this.client
return iqCaller
.request(
xml(
'iq',
{type: 'set', to: service},
xml('pubsub', {xmlns: NS_PUBSUB_OWNER}, xml('delete', {node}))
)
)
.then(() => undefined)
}
/**
* Publish an item to a pubsub node
*
* @param {string} service Service id
* @param {string} node Pubsub node id
* @param {XML} item XML item node with entry children
* @return {Promise<string>} id of the created pubsub item
*/
publish(service, node, item) {
const {iqCaller} = this.client
const stanza = xml('pubsub', {xmlns: NS_PUBSUB}, xml('publish', {node}))
if (item) {
stanza.getChild('publish').cnode(item)
}
return iqCaller.request(xml('iq', {type: 'set', to: service}, stanza)).then(
result =>
result
.getChild('pubsub')
.getChild('publish')
.getChild('item').attrs.id
)
}
/**
* Delete an item from a pubsub node
*
* @param {string} service Service id
* @param {string} node Pubsub node id
* @param {string} id Id of pubsub item to delete
* @param {boolean} notify Notify all subscribers of deletion
* @returns {Promise<void>} Ready promise
*/
retract(service, node, id, notify = true) {
const {iqCaller} = this.client
const stanza = xml(
'pubsub',
{xmlns: NS_PUBSUB},
xml('retract', {node, notify}, xml('item', {id}))
)
return iqCaller.request(xml('iq', {type: 'set', to: service}, stanza))
}
/**
* Fetch a single item from a pubsub node.
*
* @param {string} service Service id
* @param {string} node Pubsub node id
* @param {string} id Pubsub item id for the item to retrieve
* @returns {Promise<any>} The retrieved item, or `null` if the item does not exist
*/
get(service, node, id) {
const {iqCaller} = this.client
const stanza = xml(
'pubsub',
{xmlns: NS_PUBSUB},
xml('items', {node}, xml('item', {id}))
)
return iqCaller
.request(xml('iq', {type: 'get', to: service}, stanza))
.then(result => {
const pubSubResult = result.getChild('pubsub')
const items = pubSubResult.getChild('items').children
return items.length > 0 ? items[0] : null
})
}
/**
* @typedef {{[key: string]: string|number}} RSM
*/
/**
* @typedef {{items: any[], rsm?: object}} ItemResult
*/
/**
* Retrieve items from the pub-sub node.
*
* @param {string} service Service id
* @param {string} node Pubsub node id
* @param {RSM|undefined} rsm Result set management info
* @returns {Promise<ItemResult>} List of items and RSM information
*/
items(service, node, rsm) {
const {iqCaller} = this.client
const stanza = xml('pubsub', {xmlns: NS_PUBSUB}, xml('items', {node}))
if (rsm) {
const rsmEl = xml('set', {xmlns: NS_RSM})
for (const key of Object.keys(rsm)) {
const e = rsmEl.c(key)
if (rsm[key] !== null) {
e.t(rsm[key].toString())
}
}
stanza.up().cnode(rsmEl)
}
return iqCaller
.request(xml('iq', {type: 'get', to: service}, stanza))
.then(result => {
const pubSubResult = result.getChild('pubsub')
const rsmEl = pubSubResult.getChild('set')
const items = pubSubResult.getChild('items').children
if (rsmEl) {
return {
items,
rsm: rsmEl.children.reduce((obj, el) => {
if (el.name === 'max' || el.name === 'count') {
obj[el.name] = parseInt(el.text(), 10)
} else {
obj[el.name] = el.text()
}
return obj
}, {}),
}
}
return {items}
})
}
}
/**
* Register a vcard plugin.
*
* @param {Client} client XMPP client instance
* @returns {PubSubPlugin} Plugin instance
*/
function setupPubSub(client) {
const {middleware} = client
const plugin = new PubSubPlugin(client)
middleware.use((context, next) => {
if (isPubSubEventNotification(context)) {
return plugin.handleNotification(context)
}
return next()
})
return plugin
}
module.exports = setupPubSub