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([
|
|
|
|
|
'./Chat/console',
|
|
|
|
|
'Bastelstu.be/bottle',
|
|
|
|
|
'Bastelstu.be/_Push',
|
|
|
|
|
'WoltLabSuite/Core/Core',
|
|
|
|
|
'WoltLabSuite/Core/Language',
|
|
|
|
|
'WoltLabSuite/Core/Timer/Repeating',
|
|
|
|
|
'WoltLabSuite/Core/User',
|
|
|
|
|
'./Chat/Autocompleter',
|
|
|
|
|
'./Chat/CommandHandler',
|
|
|
|
|
'./Chat/DataStructure/Throttle',
|
|
|
|
|
'./Chat/Message',
|
|
|
|
|
'./Chat/Messenger',
|
|
|
|
|
'./Chat/ParseError',
|
|
|
|
|
'./Chat/ProfileStore',
|
|
|
|
|
'./Chat/Room',
|
|
|
|
|
'./Chat/Template',
|
|
|
|
|
'./Chat/Ui/Attachment/Upload',
|
|
|
|
|
'./Chat/Ui/AutoAway',
|
|
|
|
|
'./Chat/Ui/Chat',
|
|
|
|
|
'./Chat/Ui/ConnectionWarning',
|
|
|
|
|
'./Chat/Ui/ErrorDialog',
|
|
|
|
|
'./Chat/Ui/Input',
|
|
|
|
|
'./Chat/Ui/Input/Autocompleter',
|
|
|
|
|
'./Chat/Ui/MessageStream',
|
|
|
|
|
'./Chat/Ui/MessageActions/Delete',
|
|
|
|
|
'./Chat/Ui/Mobile',
|
|
|
|
|
'./Chat/Ui/Notification',
|
|
|
|
|
'./Chat/Ui/ReadMarker',
|
|
|
|
|
'./Chat/Ui/Settings',
|
|
|
|
|
'./Chat/Ui/Topic',
|
|
|
|
|
'./Chat/Ui/UserActionDropdownHandler',
|
|
|
|
|
'./Chat/Ui/UserList',
|
|
|
|
|
], function (
|
|
|
|
|
console,
|
|
|
|
|
Bottle,
|
|
|
|
|
Push,
|
|
|
|
|
Core,
|
|
|
|
|
Language,
|
|
|
|
|
RepeatingTimer,
|
|
|
|
|
CoreUser,
|
|
|
|
|
Autocompleter,
|
|
|
|
|
CommandHandler,
|
|
|
|
|
Throttle,
|
|
|
|
|
Message,
|
|
|
|
|
Messenger,
|
|
|
|
|
ParseError,
|
|
|
|
|
ProfileStore,
|
|
|
|
|
Room,
|
|
|
|
|
Template,
|
|
|
|
|
UiAttachmentUpload,
|
|
|
|
|
UiAutoAway,
|
|
|
|
|
Ui,
|
|
|
|
|
UiConnectionWarning,
|
|
|
|
|
ErrorDialog,
|
|
|
|
|
UiInput,
|
|
|
|
|
UiInputAutocompleter,
|
|
|
|
|
UiMessageStream,
|
|
|
|
|
UiMessageActionDelete,
|
|
|
|
|
UiMobile,
|
|
|
|
|
UiNotification,
|
|
|
|
|
UiReadMarker,
|
|
|
|
|
UiSettings,
|
|
|
|
|
UiTopic,
|
|
|
|
|
UiUserActionDropdownHandler,
|
|
|
|
|
UiUserList
|
|
|
|
|
) {
|
|
|
|
|
'use strict'
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
class Chat {
|
|
|
|
|
constructor(roomID, config) {
|
|
|
|
|
console.debug('Chat.constructor', 'Constructing …')
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
this.config = config
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
this.sessionID = Core.getUuid()
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
// Setup Bottle containers
|
2020-11-01 16:41:19 +00:00
|
|
|
|
this.bottle = new Bottle()
|
2018-08-16 22:30:59 +00:00
|
|
|
|
this.bottle.value('bottle', this.bottle)
|
|
|
|
|
this.bottle.value('config', config)
|
|
|
|
|
this.bottle.constant('sessionID', this.sessionID)
|
|
|
|
|
this.bottle.constant('roomID', roomID)
|
|
|
|
|
|
|
|
|
|
// Register chat components
|
|
|
|
|
this.service('Autocompleter', Autocompleter)
|
|
|
|
|
this.service('CommandHandler', CommandHandler)
|
|
|
|
|
this.service('Messenger', Messenger)
|
|
|
|
|
this.service('ProfileStore', ProfileStore)
|
|
|
|
|
this.service('Room', Room)
|
|
|
|
|
|
|
|
|
|
// Register UI components
|
|
|
|
|
this.service('Ui', Ui)
|
|
|
|
|
this.service('UiAutoAway', UiAutoAway)
|
|
|
|
|
this.service('UiConnectionWarning', UiConnectionWarning)
|
|
|
|
|
this.service('UiInput', UiInput)
|
|
|
|
|
this.service('UiInputAutocompleter', UiInputAutocompleter)
|
|
|
|
|
this.service('UiMessageActionDelete', UiMessageActionDelete)
|
|
|
|
|
this.service('UiMessageStream', UiMessageStream)
|
|
|
|
|
this.service('UiMobile', UiMobile)
|
|
|
|
|
this.service('UiNotification', UiNotification)
|
|
|
|
|
this.service('UiReadMarker', UiReadMarker)
|
|
|
|
|
this.service('UiSettings', UiSettings)
|
|
|
|
|
this.service('UiTopic', UiTopic)
|
|
|
|
|
this.service('UiUserActionDropdownHandler', UiUserActionDropdownHandler)
|
|
|
|
|
this.service('UiUserList', UiUserList)
|
2020-10-31 23:42:28 +00:00
|
|
|
|
this.service('UiAttachmentUpload', UiAttachmentUpload)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
// Register Models
|
|
|
|
|
this.bottle.instanceFactory('Message', (container, m) => {
|
|
|
|
|
return new Message(container.MessageType, m)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Register Templates
|
2020-11-01 16:41:19 +00:00
|
|
|
|
const selector = [
|
|
|
|
|
'[type="x-text/template"]',
|
|
|
|
|
'[data-application="be.bastelstu.chat"]',
|
|
|
|
|
'[data-template-name]',
|
|
|
|
|
].join('')
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
const templates = elBySelAll(selector)
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
Array.prototype.forEach.call(
|
|
|
|
|
templates,
|
|
|
|
|
function (template) {
|
|
|
|
|
this.bottle.factory(
|
|
|
|
|
`Template.${template.dataset.templateName}`,
|
|
|
|
|
function (container) {
|
|
|
|
|
const includeNames = (template.dataset.templateIncludes || '')
|
|
|
|
|
.split(/ /)
|
|
|
|
|
.filter((item) => item !== '')
|
|
|
|
|
const includes = {}
|
|
|
|
|
includeNames.forEach((item) => (includes[item] = container[item]))
|
|
|
|
|
|
|
|
|
|
return new Template(template.textContent, includes)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}.bind(this)
|
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
// Register MessageTypes
|
2020-11-01 16:41:19 +00:00
|
|
|
|
Object.entries(this.config.messageTypes).forEach(
|
|
|
|
|
([objectType, messageType]) => {
|
|
|
|
|
const MessageType = require(messageType.module)
|
|
|
|
|
|
|
|
|
|
this.bottle.factory(
|
|
|
|
|
`MessageType.${objectType.replace(/\./g, '-')}`,
|
|
|
|
|
(_) => {
|
|
|
|
|
const deps = this.bottle.digest(MessageType.DEPENDENCIES || [])
|
|
|
|
|
|
|
|
|
|
return new MessageType(...deps, objectType)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
// Register Commands
|
2020-11-01 16:41:19 +00:00
|
|
|
|
Object.values(this.config.commands).forEach((command) => {
|
2018-08-16 22:30:59 +00:00
|
|
|
|
const Command = require(command.module)
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
this.bottle.factory(
|
|
|
|
|
`Command.${command.package.replace(/\./g, '-')}:${
|
|
|
|
|
command.identifier
|
|
|
|
|
}`,
|
|
|
|
|
(_) => {
|
|
|
|
|
const deps = this.bottle.digest(Command.DEPENDENCIES || [])
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
return new Command(...deps, command)
|
|
|
|
|
}
|
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
})
|
2020-11-01 16:41:19 +00:00
|
|
|
|
this.bottle.constant(
|
|
|
|
|
'Trigger',
|
|
|
|
|
new Map(
|
|
|
|
|
Object.entries(this.config.triggers).map(([trigger, commandID]) => {
|
|
|
|
|
const command = this.config.commands[commandID]
|
|
|
|
|
const key = [command.package, command.identifier]
|
|
|
|
|
return [trigger, key]
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
// Register Settings
|
2020-11-01 16:41:19 +00:00
|
|
|
|
Array.from(
|
|
|
|
|
elBySelAll('#chatQuickSettingsNavigation .button[data-module]')
|
|
|
|
|
).forEach((item) => {
|
2018-08-16 22:30:59 +00:00
|
|
|
|
const Button = require(item.dataset.module)
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
this.bottle.instanceFactory(
|
|
|
|
|
`UiSettingsButton.${item.dataset.module.replace(/\./g, '-')}`,
|
|
|
|
|
(_, element) => {
|
|
|
|
|
const deps = this.bottle.digest(Button.DEPENDENCIES || [])
|
|
|
|
|
return new Button(element, ...deps)
|
|
|
|
|
}
|
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
})
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
this.knows = { from: undefined, to: undefined }
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
this.processMessagesThrottled = Throttle(this.processMessages.bind(this))
|
2020-11-01 16:41:19 +00:00
|
|
|
|
this.queuedMessages = []
|
2018-08-16 22:30:59 +00:00
|
|
|
|
this.messageSinks = new Set()
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
this.pullTimer = undefined
|
2018-08-16 22:30:59 +00:00
|
|
|
|
this.pullUserListTimer = undefined
|
2020-11-01 16:41:19 +00:00
|
|
|
|
this.pushConnected = false
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
this.firstFailure = null
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
service(name, _constructor, args = []) {
|
|
|
|
|
this.bottle.factory(name, (_) => {
|
|
|
|
|
const deps = this.bottle.digest(_constructor.DEPENDENCIES || [])
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
return new _constructor(...deps, ...args)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async bootstrap() {
|
|
|
|
|
console.debug('Chat.bootstrap', 'Initializing …')
|
|
|
|
|
|
|
|
|
|
this.ui = this.bottle.container.Ui
|
|
|
|
|
this.ui.bootstrap()
|
|
|
|
|
|
2020-11-01 11:03:04 +00:00
|
|
|
|
this.ui.input.on('submit', this.onSubmit.bind(this))
|
|
|
|
|
this.ui.input.on('autocomplete', this.onAutocomplete.bind(this))
|
|
|
|
|
this.ui.attachmentUpload.on('send', (event) => {
|
|
|
|
|
event.detail.promise = this.onSendAttachment(event)
|
|
|
|
|
})
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
await this.bottle.container.Room.join()
|
|
|
|
|
|
|
|
|
|
// Bind unload event to leave the Chat
|
2020-11-01 16:41:19 +00:00
|
|
|
|
window.addEventListener(
|
|
|
|
|
'unload',
|
|
|
|
|
this.bottle.container.Room.leave.bind(this.bottle.container.Room, true)
|
|
|
|
|
)
|
|
|
|
|
document.addEventListener('visibilitychange', (_) => {
|
2018-08-16 22:30:59 +00:00
|
|
|
|
this.processMessagesThrottled.setDelay(document.hidden ? 10000 : 125)
|
|
|
|
|
})
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
this.pullTimer = new RepeatingTimer(
|
|
|
|
|
Throttle(this.pullMessages.bind(this)),
|
|
|
|
|
this.config.reloadTime * 1e3
|
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
Push.onConnect((_) => {
|
2018-08-16 22:30:59 +00:00
|
|
|
|
console.debug('Chat.bootstrap', 'Push connected')
|
|
|
|
|
this.pushConnected = true
|
|
|
|
|
this.pullTimer.setDelta(30e3)
|
2020-11-01 16:41:19 +00:00
|
|
|
|
}).catch((error) => {
|
|
|
|
|
console.debug(error)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
})
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
Push.onDisconnect((_) => {
|
2018-08-16 22:30:59 +00:00
|
|
|
|
console.debug('Chat.bootstrap', 'Push disconnected')
|
|
|
|
|
this.pushConnected = false
|
|
|
|
|
this.pullTimer.setDelta(this.config.reloadTime * 1e3)
|
2020-11-01 16:41:19 +00:00
|
|
|
|
}).catch((error) => {
|
|
|
|
|
console.debug(error)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
})
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
Push.onMessage(
|
|
|
|
|
'be.bastelstu.chat.message',
|
|
|
|
|
this.pullMessages.bind(this)
|
|
|
|
|
).catch((error) => {
|
|
|
|
|
console.debug(error)
|
|
|
|
|
})
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
// Fetch user list every 60 seconds
|
|
|
|
|
// This acts as a safety net: It should be kept current by messages whenever possible.
|
2020-11-01 16:41:19 +00:00
|
|
|
|
this.pullUserListTimer = new RepeatingTimer(
|
|
|
|
|
this.updateUsers.bind(this),
|
|
|
|
|
60e3
|
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
this.registerMessageSink(this.bottle.container.UiMessageStream)
|
|
|
|
|
this.registerMessageSink(this.bottle.container.UiNotification)
|
|
|
|
|
this.registerMessageSink(this.bottle.container.UiAutoAway)
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
await Promise.all([
|
|
|
|
|
this.pullMessages(),
|
|
|
|
|
this.updateUsers(),
|
|
|
|
|
this.bottle.container.ProfileStore.ensureUsersByIDs([CoreUser.userId]),
|
|
|
|
|
])
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
return this
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerMessageSink(sink) {
|
|
|
|
|
if (typeof sink.ingest !== 'function') {
|
|
|
|
|
throw new Error('The given sink does not provide a .ingest function.')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.messageSinks.add(sink)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unregisterMessageSink(sink) {
|
|
|
|
|
this.messageSinks.delete(sink)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hcf(err = undefined) {
|
2020-11-01 16:41:19 +00:00
|
|
|
|
console.debug(
|
|
|
|
|
'Chat.hcf',
|
|
|
|
|
'Gotcha! FIRE was caught! FIRE’s data was newly added to the POKéDEX.',
|
|
|
|
|
err
|
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
this.pullTimer.stop()
|
|
|
|
|
this.pullUserListTimer.stop()
|
|
|
|
|
|
|
|
|
|
new ErrorDialog(Language.get('chat.error.hcf', { err }))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async onSubmit(event) {
|
|
|
|
|
const input = event.target
|
|
|
|
|
const value = input.getText()
|
|
|
|
|
|
|
|
|
|
console.debug('Chat.onSubmit', `Pushing message: ${value}`)
|
|
|
|
|
|
|
|
|
|
// Clear message input
|
|
|
|
|
input.insertText('', { append: false })
|
|
|
|
|
|
|
|
|
|
this.markAsBack()
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
let [
|
|
|
|
|
trigger,
|
|
|
|
|
parameterString,
|
|
|
|
|
] = this.bottle.container.CommandHandler.splitCommand(value)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
let command = null
|
|
|
|
|
if (trigger === null) {
|
2020-11-01 16:41:19 +00:00
|
|
|
|
command = this.bottle.container.CommandHandler.getCommandByIdentifier(
|
|
|
|
|
'be.bastelstu.chat',
|
|
|
|
|
'plain'
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
command = this.bottle.container.CommandHandler.getCommandByTrigger(
|
|
|
|
|
trigger
|
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (command === null) {
|
2020-11-01 16:41:19 +00:00
|
|
|
|
this.ui.input.inputError(
|
|
|
|
|
Language.get('chat.error.triggerNotFound', { trigger })
|
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
let parameters
|
|
|
|
|
try {
|
2020-11-01 16:41:19 +00:00
|
|
|
|
parameters = this.bottle.container.CommandHandler.applyCommand(
|
|
|
|
|
command,
|
|
|
|
|
parameterString
|
|
|
|
|
)
|
|
|
|
|
} catch (e) {
|
2018-08-16 22:30:59 +00:00
|
|
|
|
if (e instanceof ParseError) {
|
2020-11-01 16:41:19 +00:00
|
|
|
|
e = new Error(
|
|
|
|
|
Language.get('chat.error.invalidParameters', { data: e.data })
|
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
}
|
|
|
|
|
throw e
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
const payload = { commandID: command.id, parameters }
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await this.bottle.container.Messenger.push(payload)
|
|
|
|
|
this.ui.input.hideInputError()
|
2020-11-01 16:41:19 +00:00
|
|
|
|
} catch (error) {
|
2018-08-16 22:30:59 +00:00
|
|
|
|
let seriousError = true
|
2020-11-01 16:41:19 +00:00
|
|
|
|
if (
|
|
|
|
|
error.returnValues &&
|
|
|
|
|
error.returnValues.fieldName === 'message' &&
|
|
|
|
|
(error.returnValues.realErrorMessage ||
|
|
|
|
|
error.returnValues.errorType)
|
|
|
|
|
) {
|
|
|
|
|
this.ui.input.inputError(
|
|
|
|
|
error.returnValues.realErrorMessage ||
|
|
|
|
|
error.returnValues.errorType
|
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
seriousError = false
|
2020-11-01 16:41:19 +00:00
|
|
|
|
} else {
|
2018-08-16 22:30:59 +00:00
|
|
|
|
this.ui.input.inputError(error.message)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (seriousError) {
|
|
|
|
|
this.handleError(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We assume that a running push server will push us our own message
|
|
|
|
|
if (!this.pushConnected) {
|
|
|
|
|
this.pullMessages()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.debug('Chat.onSubmit', `Done`)
|
2020-11-01 16:41:19 +00:00
|
|
|
|
} catch (e) {
|
2018-08-16 22:30:59 +00:00
|
|
|
|
this.ui.input.inputError(e.message)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async markAsBack() {
|
|
|
|
|
try {
|
|
|
|
|
if (this.bottle.container.ProfileStore.getSelf().away == null) return
|
|
|
|
|
console.debug('Chat.markAsBack', `Marking as back`)
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
const command = this.bottle.container.CommandHandler.getCommandByIdentifier(
|
|
|
|
|
'be.bastelstu.chat',
|
|
|
|
|
'back'
|
|
|
|
|
)
|
|
|
|
|
return this.bottle.container.Messenger.push({
|
|
|
|
|
commandID: command.id,
|
|
|
|
|
parameters: {},
|
|
|
|
|
})
|
|
|
|
|
} catch (err) {
|
2018-08-16 22:30:59 +00:00
|
|
|
|
console.error('Chat.markAsBack', err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-01 11:03:04 +00:00
|
|
|
|
async onSendAttachment(event) {
|
2021-02-04 21:44:45 +00:00
|
|
|
|
await this.bottle.container.Messenger.pushAttachment(event.detail.tmpHash)
|
2020-11-07 19:00:43 +00:00
|
|
|
|
|
|
|
|
|
this.markAsBack()
|
2020-11-01 11:03:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-16 22:30:59 +00:00
|
|
|
|
onAutocomplete(event) {
|
|
|
|
|
const input = event.target
|
|
|
|
|
const value = input.getText(true)
|
|
|
|
|
|
|
|
|
|
console.debug('Chat.onAutocomplete', `Autocompleting message: ${value}`)
|
|
|
|
|
|
|
|
|
|
const result = this.bottle.container.Autocompleter.autocomplete(value)
|
2020-10-31 16:05:30 +00:00
|
|
|
|
const completions = []
|
2018-08-16 22:30:59 +00:00
|
|
|
|
for (const item of result) {
|
2020-10-31 16:05:30 +00:00
|
|
|
|
completions.push(item)
|
|
|
|
|
if (completions.length == 5) break
|
2018-08-16 22:30:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-31 16:05:30 +00:00
|
|
|
|
this.ui.autocompleter.sendCompletions(completions)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async pullMessages() {
|
2020-11-01 16:41:19 +00:00
|
|
|
|
console.debug(
|
|
|
|
|
'Chat.pullMessages',
|
|
|
|
|
`Pulling new messages, starting at ${
|
|
|
|
|
this.knows.to ? this.knows.to + 1 : ''
|
|
|
|
|
}`
|
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
let payload
|
|
|
|
|
try {
|
|
|
|
|
if (this.knows.to === undefined) {
|
|
|
|
|
payload = await this.bottle.container.Messenger.pull()
|
2020-11-01 16:41:19 +00:00
|
|
|
|
} else {
|
|
|
|
|
payload = await this.bottle.container.Messenger.pull(
|
|
|
|
|
this.knows.to + 1
|
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
}
|
2020-11-01 16:41:19 +00:00
|
|
|
|
} catch (e) {
|
2018-08-16 22:30:59 +00:00
|
|
|
|
this.handleError(e)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.debug('Chat.pullMessages', `Handling result: `, payload)
|
|
|
|
|
const start = (performance ? performance : Date).now()
|
|
|
|
|
this.ui.connectionWarning.hide()
|
|
|
|
|
this.firstFailure = null
|
|
|
|
|
|
|
|
|
|
// Null range: No messages satisfy the constraints
|
|
|
|
|
if (payload.from > payload.to) {
|
|
|
|
|
const end = (performance ? performance : Date).now()
|
|
|
|
|
console.debug('Chat.pullMessages', `took ${(end - start) / 1000}s`)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let messages = payload.messages
|
|
|
|
|
|
|
|
|
|
if (this.knows.from !== undefined && this.knows.to !== undefined) {
|
|
|
|
|
messages = messages.filter((message) => {
|
2020-11-01 16:41:19 +00:00
|
|
|
|
return !(
|
|
|
|
|
this.knows.from <= message.messageID &&
|
|
|
|
|
message.messageID <= this.knows.to
|
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
if (this.knows.from === undefined || payload.from < this.knows.from)
|
|
|
|
|
this.knows.from = payload.from
|
|
|
|
|
if (this.knows.to === undefined || payload.to > this.knows.to)
|
|
|
|
|
this.knows.to = payload.to
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
this.queuedMessages.push(messages)
|
|
|
|
|
const end = (performance ? performance : Date).now()
|
|
|
|
|
console.debug('Chat.pullMessages', `took ${(end - start) / 1000}s`)
|
|
|
|
|
|
|
|
|
|
this.processMessagesThrottled()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleError(error) {
|
|
|
|
|
if (this.firstFailure === null) {
|
2020-11-01 16:41:19 +00:00
|
|
|
|
console.error(
|
|
|
|
|
'Chat.handleError',
|
|
|
|
|
`Request failed, 30 seconds until shutdown`
|
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
this.firstFailure = Date.now()
|
|
|
|
|
this.ui.connectionWarning.show()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.debugException(error)
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
if (Date.now() - this.firstFailure >= 30e3) {
|
2018-08-16 22:30:59 +00:00
|
|
|
|
console.error('Chat.handleError', ' Failures for 30 seconds, aborting')
|
|
|
|
|
|
|
|
|
|
this.hcf(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async processMessages() {
|
|
|
|
|
console.debug('Chat.processMessages', 'Processing messages')
|
|
|
|
|
const start = (performance ? performance : Date).now()
|
2020-11-01 16:41:19 +00:00
|
|
|
|
const messages = [].concat(...this.queuedMessages)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
this.queuedMessages = []
|
|
|
|
|
|
|
|
|
|
if (messages.length === 0) return
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
await Promise.all(
|
|
|
|
|
messages.map(async (message) => {
|
|
|
|
|
this.bottle.container.ProfileStore.pushLastActivity(message.userID)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
return message.getMessageType().preProcess(message)
|
|
|
|
|
})
|
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
const updateUserList = messages.some((message) => {
|
|
|
|
|
return message.getMessageType().shouldUpdateUserList(message)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (updateUserList) {
|
|
|
|
|
this.updateUsers()
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
await this.bottle.container.ProfileStore.ensureUsersByIDs(
|
|
|
|
|
[].concat(
|
|
|
|
|
...messages.map((message) =>
|
|
|
|
|
message.getMessageType().getReferencedUsers(message)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
messages.forEach((message) => {
|
|
|
|
|
message.getMessageType().preRender(message)
|
|
|
|
|
})
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
this.messageSinks.forEach((sink) => sink.ingest(messages))
|
2018-08-16 22:30:59 +00:00
|
|
|
|
const end = (performance ? performance : Date).now()
|
|
|
|
|
console.debug('Chat.processMessages', `took ${(end - start) / 1000}s`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async updateUsers() {
|
|
|
|
|
console.debug('Chat.updateUsers')
|
|
|
|
|
|
|
|
|
|
const users = await this.bottle.container.Room.getUsers()
|
2020-11-01 16:41:19 +00:00
|
|
|
|
await this.bottle.container.ProfileStore.ensureUsersByIDs(
|
|
|
|
|
users.map((user) => user.userID)
|
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
this.ui.userList.render(users)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Chat
|
2020-11-01 16:41:19 +00:00
|
|
|
|
})
|