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',
|
|
|
|
'../CommandHandler',
|
|
|
|
'../LocalStorage',
|
|
|
|
'../Messenger',
|
|
|
|
'../ProfileStore',
|
|
|
|
'WoltLabSuite/Core/Language',
|
|
|
|
'WoltLabSuite/Core/Timer/Repeating',
|
|
|
|
], function (
|
|
|
|
console,
|
|
|
|
CommandHandler,
|
|
|
|
LocalStorage,
|
|
|
|
Messenger,
|
|
|
|
ProfileStore,
|
|
|
|
Language,
|
|
|
|
RepeatingTimer
|
|
|
|
) {
|
|
|
|
'use strict'
|
2018-08-16 22:30:59 +00:00
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
const DEPENDENCIES = [
|
|
|
|
'config',
|
|
|
|
'CommandHandler',
|
|
|
|
'Messenger',
|
|
|
|
'ProfileStore',
|
|
|
|
'UiInput',
|
|
|
|
]
|
2018-08-16 22:30:59 +00:00
|
|
|
class AutoAway {
|
|
|
|
constructor(config, commandHandler, messenger, profileStore, input) {
|
2020-11-01 16:41:19 +00:00
|
|
|
if (!(commandHandler instanceof CommandHandler))
|
|
|
|
throw new TypeError('You must pass a CommandHandler to the AutoAway')
|
|
|
|
if (!(messenger instanceof Messenger))
|
|
|
|
throw new TypeError('You must pass a Messenger to the AutoAway')
|
|
|
|
if (!(profileStore instanceof ProfileStore))
|
|
|
|
throw new TypeError('You must pass a ProfileStore to the AutoAway')
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
this.storage = new LocalStorage('AutoAway.')
|
2020-11-01 16:41:19 +00:00
|
|
|
this.awayCommand = commandHandler.getCommandByIdentifier(
|
|
|
|
'be.bastelstu.chat',
|
|
|
|
'away'
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
if (this.awayCommand == null) {
|
|
|
|
throw new Error('Unreachable')
|
|
|
|
}
|
|
|
|
this.config = config
|
|
|
|
this.messenger = messenger
|
|
|
|
this.input = input
|
|
|
|
this.profileStore = profileStore
|
|
|
|
}
|
|
|
|
|
|
|
|
bootstrap() {
|
|
|
|
if (this.config.autoAwayTime === 0) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!this.awayCommand.isAvailable) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
this.timer = new RepeatingTimer(
|
|
|
|
this.setAway.bind(this),
|
|
|
|
this.config.autoAwayTime * 60e3
|
|
|
|
)
|
|
|
|
this.input.on(
|
|
|
|
'input',
|
|
|
|
(this.inputListener = (event) => {
|
|
|
|
this.storage.set('channel', Date.now())
|
|
|
|
this.reset()
|
|
|
|
})
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
this.storage.observe('channel', this.reset.bind(this))
|
|
|
|
}
|
2020-11-01 16:41:19 +00:00
|
|
|
|
2018-08-16 22:30:59 +00:00
|
|
|
ingest(messages) {
|
2020-11-01 16:41:19 +00:00
|
|
|
if (messages.some((message) => message.isOwnMessage())) this.reset()
|
2018-08-16 22:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
console.debug('AutoAway.reset', `Resetting timer`)
|
|
|
|
|
|
|
|
if (!this.timer) return
|
|
|
|
|
|
|
|
this.timer.setDelta(this.config.autoAwayTime * 60e3)
|
|
|
|
}
|
|
|
|
|
|
|
|
async setAway() {
|
|
|
|
console.debug('AutoAway.setAway', `Attempting to set as away`)
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
if (this.storage.get('setAway') >= Date.now() - 10e3) {
|
|
|
|
console.debug(
|
|
|
|
'AutoAway.setAway',
|
|
|
|
`setAway called within the last 10 seconds in another Tab`
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
this.storage.set('setAway', Date.now())
|
|
|
|
|
|
|
|
if (this.profileStore.getSelf().away) {
|
|
|
|
console.debug('AutoAway.setAway', `User is already away`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
this.messenger.push({
|
|
|
|
commandID: this.awayCommand.id,
|
|
|
|
parameters: { reason: Language.get('chat.user.autoAway') },
|
|
|
|
})
|
2018-08-16 22:30:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
AutoAway.DEPENDENCIES = DEPENDENCIES
|
|
|
|
|
|
|
|
return AutoAway
|
2020-11-01 16:41:19 +00:00
|
|
|
})
|