2018-08-16 22:30:59 +00:00
|
|
|
/*
|
2021-02-04 22:04:35 +00:00
|
|
|
* Copyright (c) 2010-2021 Tim Düsterhus.
|
2018-08-16 22:30:59 +00:00
|
|
|
*
|
|
|
|
* Use of this software is governed by the Business Source License
|
|
|
|
* included in the LICENSE file.
|
|
|
|
*
|
2021-03-05 16:19:27 +00:00
|
|
|
* Change Date: 2025-03-05
|
2018-08-16 22:30:59 +00:00
|
|
|
*
|
|
|
|
* On the date above, in accordance with the Business Source
|
|
|
|
* License, use of this software will be governed by version 2
|
|
|
|
* or later of the General Public License.
|
|
|
|
*/
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
define(['./console', 'Bastelstu.be/PromiseWrap/Ajax', './Room'], function (
|
|
|
|
console,
|
|
|
|
Ajax,
|
|
|
|
Room
|
|
|
|
) {
|
|
|
|
'use strict'
|
2018-08-16 22:30:59 +00:00
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
const DEPENDENCIES = ['sessionID', 'Room', 'Message']
|
2018-08-16 22:30:59 +00:00
|
|
|
class Messenger {
|
|
|
|
constructor(sessionID, room, Message) {
|
2020-11-01 16:41:19 +00:00
|
|
|
if (!(room instanceof Room))
|
|
|
|
throw new TypeError('You must pass a Room to the Messenger')
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
this.sessionID = sessionID
|
|
|
|
this.room = room
|
|
|
|
this.Message = Message
|
|
|
|
}
|
|
|
|
|
|
|
|
async pull(from = 0, to = 0, inLog = false) {
|
|
|
|
console.debug(`Messenger.pull`, 'from', from, 'to', to, 'inLog', inLog)
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
const payload = { actionName: 'pull', parameters: { inLog } }
|
2018-08-16 22:30:59 +00:00
|
|
|
if (from !== 0 && to !== 0) {
|
|
|
|
throw new Error('You must not set both from and to')
|
|
|
|
}
|
|
|
|
if (from !== 0) payload.parameters.from = from
|
|
|
|
if (to !== 0) payload.parameters.to = to
|
|
|
|
|
|
|
|
const data = await Ajax.api(this, payload)
|
2020-11-01 16:41:19 +00:00
|
|
|
const messages = Object.values(data.returnValues.messages).map((item) =>
|
|
|
|
this.Message.instance(item)
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
const { from: newFrom, to: newTo } = data.returnValues
|
|
|
|
|
|
|
|
return { messages, from: newFrom, to: newTo }
|
|
|
|
}
|
|
|
|
|
|
|
|
async push({ commandID, parameters }) {
|
2020-11-01 16:41:19 +00:00
|
|
|
const payload = {
|
|
|
|
actionName: 'push',
|
|
|
|
parameters: { commandID, parameters: JSON.stringify(parameters) },
|
|
|
|
}
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
return Ajax.api(this, payload)
|
|
|
|
}
|
|
|
|
|
2020-11-01 11:15:06 +00:00
|
|
|
async pushAttachment(tmpHash) {
|
2020-11-01 16:41:19 +00:00
|
|
|
const payload = { actionName: 'pushAttachment', parameters: { tmpHash } }
|
2020-11-01 11:03:04 +00:00
|
|
|
|
|
|
|
return Ajax.api(this, payload)
|
|
|
|
}
|
|
|
|
|
2018-08-16 22:30:59 +00:00
|
|
|
_ajaxSetup() {
|
2020-11-01 16:41:19 +00:00
|
|
|
return {
|
|
|
|
silent: true,
|
|
|
|
ignoreError: true,
|
|
|
|
data: {
|
|
|
|
className: 'chat\\data\\message\\MessageAction',
|
|
|
|
parameters: { roomID: this.room.roomID, sessionID: this.sessionID },
|
|
|
|
},
|
|
|
|
}
|
2018-08-16 22:30:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Messenger.DEPENDENCIES = DEPENDENCIES
|
|
|
|
|
|
|
|
return Messenger
|
2020-11-01 16:41:19 +00:00
|
|
|
})
|