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.
|
|
|
|
|
*
|
2023-02-22 16:45:50 +00:00
|
|
|
|
* Change Date: 2027-02-22
|
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/bottle',
|
|
|
|
|
'WoltLabSuite/Core/Core',
|
|
|
|
|
'./Message',
|
|
|
|
|
'./Messenger',
|
|
|
|
|
'./ProfileStore',
|
|
|
|
|
'./Room',
|
|
|
|
|
'./Template',
|
|
|
|
|
'./Ui/Log',
|
|
|
|
|
'./Ui/MessageStream',
|
|
|
|
|
'./Ui/MessageActions/Delete',
|
|
|
|
|
], function (
|
|
|
|
|
console,
|
|
|
|
|
Bottle,
|
|
|
|
|
Core,
|
|
|
|
|
Message,
|
|
|
|
|
Messenger,
|
|
|
|
|
ProfileStore,
|
|
|
|
|
Room,
|
|
|
|
|
Template,
|
|
|
|
|
Ui,
|
|
|
|
|
UiMessageStream,
|
|
|
|
|
UiMessageActionDelete
|
|
|
|
|
) {
|
|
|
|
|
'use strict'
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
const loader = Symbol('loader')
|
|
|
|
|
|
|
|
|
|
class Log {
|
|
|
|
|
constructor(params, config) {
|
|
|
|
|
console.debug('ChatLog.constructor', 'Constructing …')
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
this.config = config
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
this.sessionID = Core.getUuid()
|
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', params.roomID)
|
|
|
|
|
|
|
|
|
|
// Register chat components
|
|
|
|
|
this.service('Messenger', Messenger)
|
|
|
|
|
this.service('ProfileStore', ProfileStore)
|
|
|
|
|
this.service('Room', Room)
|
|
|
|
|
|
|
|
|
|
// Register UI components
|
|
|
|
|
this.service('Ui', Ui)
|
|
|
|
|
this.service('UiMessageActionDelete', UiMessageActionDelete)
|
|
|
|
|
this.service('UiMessageStream', UiMessageStream)
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
templates.forEach(
|
|
|
|
|
function (template) {
|
|
|
|
|
this.bottle.factory(
|
|
|
|
|
`Template.${elData(template, 'template-name')}`,
|
|
|
|
|
function (container) {
|
|
|
|
|
const includeNames = (elData(template, 'template-includes') || '')
|
|
|
|
|
.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
|
|
|
|
|
const messageTypes = Object.entries(this.config.messageTypes)
|
2020-11-01 16:41:19 +00:00
|
|
|
|
messageTypes.forEach(([objectType, messageType]) => {
|
2018-08-16 22:30:59 +00:00
|
|
|
|
const MessageType = require(messageType.module)
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
this.bottle.factory(
|
|
|
|
|
`MessageType.${objectType.replace(/\./g, '-')}`,
|
|
|
|
|
(_) => {
|
|
|
|
|
const deps = this.bottle.digest(MessageType.DEPENDENCIES || [])
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
return new MessageType(...deps, objectType)
|
|
|
|
|
}
|
|
|
|
|
)
|
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.messageSinks = new Set()
|
|
|
|
|
|
|
|
|
|
this.params = params
|
|
|
|
|
|
|
|
|
|
this.pulling = false
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
service(name, _constructor, args = []) {
|
2018-08-16 22:30:59 +00:00
|
|
|
|
this.bottle.factory(name, function (container) {
|
2020-11-01 16:41:19 +00:00
|
|
|
|
const deps = (_constructor.DEPENDENCIES || []).map(
|
|
|
|
|
(dep) => container[dep]
|
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
return new _constructor(...deps, ...args)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async bootstrap() {
|
|
|
|
|
console.debug('ChatLog.bootstrap', 'Initializing …')
|
|
|
|
|
|
|
|
|
|
this.ui = this.bottle.container.Ui
|
|
|
|
|
this.ui.bootstrap()
|
|
|
|
|
|
|
|
|
|
this.registerMessageSink(this.bottle.container.UiMessageStream)
|
|
|
|
|
|
|
|
|
|
if (this.params.messageID > 0) {
|
2020-11-01 16:41:19 +00:00
|
|
|
|
await Promise.all([
|
|
|
|
|
this.pull(undefined, this.params.messageID),
|
|
|
|
|
this.pull(this.params.messageID + 1),
|
|
|
|
|
])
|
|
|
|
|
} else {
|
2018-08-16 22:30:59 +00:00
|
|
|
|
await this.pull()
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
this.bottle.container.UiMessageStream.on(
|
|
|
|
|
'nearTop',
|
|
|
|
|
this.pullOlder.bind(this)
|
|
|
|
|
)
|
|
|
|
|
this.bottle.container.UiMessageStream.on(
|
|
|
|
|
'reachedTop',
|
|
|
|
|
this.pullOlder.bind(this)
|
|
|
|
|
)
|
|
|
|
|
this.bottle.container.UiMessageStream.on(
|
|
|
|
|
'nearBottom',
|
|
|
|
|
this.pullNewer.bind(this)
|
|
|
|
|
)
|
|
|
|
|
this.bottle.container.UiMessageStream.on(
|
|
|
|
|
'reachedBottom',
|
|
|
|
|
this.pullNewer.bind(this)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const element = document.querySelector(
|
|
|
|
|
`#message-${this.params.messageID}`
|
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
// Force changing the hash to trigger a new lookup of the element.
|
|
|
|
|
// At least Chrome won’t target an element if it is not in the DOM
|
|
|
|
|
// on the initial page load with an hash set.
|
|
|
|
|
window.location.hash = ''
|
|
|
|
|
window.location.hash = `message-${this.params.messageID}`
|
|
|
|
|
|
|
|
|
|
if (element && element.scrollIntoView) element.scrollIntoView()
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async pull(from, to) {
|
|
|
|
|
try {
|
|
|
|
|
await this.handlePull(await this.performPull(from, to))
|
2020-11-01 16:41:19 +00:00
|
|
|
|
} catch (e) {
|
2018-08-16 22:30:59 +00:00
|
|
|
|
this.handleError(e)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async pullOlder() {
|
|
|
|
|
if (this.pulling) return
|
|
|
|
|
if (this.knows.from <= 1) return
|
|
|
|
|
|
|
|
|
|
this.pulling = true
|
|
|
|
|
|
|
|
|
|
await this.pull(undefined, this.knows.from - 1)
|
|
|
|
|
|
|
|
|
|
this.pulling = false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async pullNewer() {
|
|
|
|
|
if (this.pulling) return
|
|
|
|
|
|
|
|
|
|
this.pulling = true
|
|
|
|
|
|
|
|
|
|
await this.pull(this.knows.to + 1)
|
|
|
|
|
|
|
|
|
|
this.pulling = false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async performPull(from = undefined, to = undefined) {
|
2020-11-01 16:41:19 +00:00
|
|
|
|
console.debug(
|
|
|
|
|
'ChatLog.performPull',
|
|
|
|
|
`Pulling new messages; from: ${
|
|
|
|
|
from !== undefined ? from : 'undefined'
|
|
|
|
|
}, to: ${to !== undefined ? to : 'undefined'}`
|
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
|
|
return this.bottle.container.Messenger.pull(from, to, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleError(error) {
|
|
|
|
|
console.debug('ChatLog.handleError', `Request failed`)
|
|
|
|
|
console.debugException(error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async handlePull(payload) {
|
|
|
|
|
console.debug('ChatLog.handlePull', payload)
|
|
|
|
|
|
|
|
|
|
// Null range: No messages satisfy the constraints
|
|
|
|
|
if (payload.from > payload.to) return
|
|
|
|
|
|
|
|
|
|
let messages = payload.messages
|
|
|
|
|
|
|
|
|
|
if (this.knows.from !== undefined && this.knows.to !== undefined) {
|
2020-11-01 16:41:19 +00:00
|
|
|
|
messages = messages.filter(
|
|
|
|
|
function (message) {
|
|
|
|
|
return !(
|
|
|
|
|
this.knows.from <= message.messageID &&
|
|
|
|
|
message.messageID <= this.knows.to
|
|
|
|
|
)
|
|
|
|
|
}.bind(this)
|
|
|
|
|
)
|
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
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
await Promise.all(
|
|
|
|
|
messages.map((message) => {
|
|
|
|
|
return message.getMessageType().preProcess(message)
|
|
|
|
|
})
|
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
const userIDs = messages
|
|
|
|
|
.map((message) => message.userID)
|
|
|
|
|
.filter((userID) => userID !== null)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
await this.bottle.container.ProfileStore.ensureUsersByIDs(userIDs)
|
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
|
this.messageSinks.forEach((sink) => sink.ingest(messages))
|
2018-08-16 22:30:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Log
|
2020-11-01 16:41:19 +00:00
|
|
|
|
})
|