1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-11-01 14:20:07 +00:00
Tims-Chat/files_wcf/js/Bastelstu.be/Chat/Ui/AutoAway.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

124 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-08-16 22:30:59 +00:00
/*
* Copyright (c) 2010-2018 Tim Düsterhus.
*
* Use of this software is governed by the Business Source License
* included in the LICENSE file.
*
2020-11-08 11:30:49 +00:00
* Change Date: 2024-11-08
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.
*/
define([
'../console',
'../CommandHandler',
'../LocalStorage',
'../Messenger',
'../ProfileStore',
'WoltLabSuite/Core/Language',
'WoltLabSuite/Core/Timer/Repeating',
], function (
console,
CommandHandler,
LocalStorage,
Messenger,
ProfileStore,
Language,
RepeatingTimer
) {
'use strict'
const DEPENDENCIES = [
'config',
'CommandHandler',
'Messenger',
'ProfileStore',
'UiInput',
]
class AutoAway {
constructor(config, commandHandler, messenger, profileStore, input) {
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')
this.storage = new LocalStorage('AutoAway.')
this.awayCommand = commandHandler.getCommandByIdentifier(
'be.bastelstu.chat',
'away'
)
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
}
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()
})
2020-11-01 16:41:19 +00:00
)
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) {
if (messages.some((message) => message.isOwnMessage())) this.reset()
}
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`)
if (this.storage.get('setAway') >= Date.now() - 10e3) {
console.debug(
'AutoAway.setAway',
`setAway called within the last 10 seconds in another Tab`
)
return
}
this.storage.set('setAway', Date.now())
if (this.profileStore.getSelf().away) {
console.debug('AutoAway.setAway', `User is already away`)
return
}
this.messenger.push({
commandID: this.awayCommand.id,
parameters: { reason: Language.get('chat.user.autoAway') },
})
}
}
AutoAway.DEPENDENCIES = DEPENDENCIES
return AutoAway
})