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-01 16:09:27 +00:00
|
|
|
* Change Date: 2024-11-01
|
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 3
|
|
|
|
* or later of the General Public License.
|
|
|
|
*/
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
define(['../Command', '../Parser'], function (Command, Parser) {
|
|
|
|
'use strict'
|
2018-08-16 22:30:59 +00:00
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
const DEPENDENCIES = ['ProfileStore']
|
2018-08-16 22:30:59 +00:00
|
|
|
class Suspension extends Command {
|
|
|
|
constructor(profileStore, id) {
|
|
|
|
super(id)
|
|
|
|
this.profileStore = profileStore
|
|
|
|
}
|
|
|
|
|
|
|
|
getParameterParser() {
|
2020-11-01 16:41:19 +00:00
|
|
|
const Globally = Parser.C.string('global').thenLeft(
|
|
|
|
Parser.C.string('ly').opt()
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
const Forever = Parser.C.string('forever').thenReturns(null)
|
2020-11-01 16:41:19 +00:00
|
|
|
const Timespan = Parser.N.digits
|
|
|
|
.then(Parser.C.charIn('dhm'))
|
|
|
|
.map(function ([span, unit]) {
|
|
|
|
switch (unit) {
|
|
|
|
case 'd':
|
|
|
|
return span * 86400
|
|
|
|
case 'h':
|
|
|
|
return span * 3600
|
|
|
|
case 'm':
|
|
|
|
return span * 60
|
|
|
|
}
|
|
|
|
throw new Error('Unreachable')
|
|
|
|
})
|
|
|
|
.rep()
|
|
|
|
.map((parts) => parts.array().reduce((carry, item) => carry + item, 0))
|
|
|
|
.map((offset) => Math.floor(Date.now() / 1000) + offset)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
const Duration = Forever.or(Timespan).or(
|
|
|
|
Parser.ISODate.map((item) => Math.floor(item.valueOf() / 1000))
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
return Parser.Username.thenLeft(Parser.Whitespace.rep())
|
2020-11-01 16:41:19 +00:00
|
|
|
.then(
|
|
|
|
Globally.thenLeft(Parser.Whitespace.rep())
|
|
|
|
.thenReturns(true)
|
|
|
|
.or(Parser.F.returns(false))
|
|
|
|
)
|
|
|
|
.then(Duration)
|
|
|
|
.then(
|
|
|
|
Parser.Whitespace.rep()
|
|
|
|
.thenRight(Parser.Rest1)
|
|
|
|
.or(Parser.F.eos.thenReturns(null))
|
|
|
|
)
|
|
|
|
.map(([username, globally, duration, reason]) => {
|
|
|
|
return { username, globally, duration, reason }
|
|
|
|
})
|
2018-08-16 22:30:59 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
*autocomplete(parameterString) {
|
|
|
|
const usernameDone = Parser.Username.thenLeft(
|
|
|
|
Parser.Whitespace.rep()
|
|
|
|
).map((username) => `"${username.replace(/"/g, '""')}"`)
|
|
|
|
const globallyDone = usernameDone
|
|
|
|
.then(Parser.C.string('global').thenLeft(Parser.C.string('ly').opt()))
|
|
|
|
.thenLeft(Parser.Whitespace.rep())
|
2018-08-16 22:30:59 +00:00
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
const usernameCheck = usernameDone.parse(
|
|
|
|
Parser.Streams.ofString(parameterString)
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
if (usernameCheck.isAccepted()) {
|
2020-11-01 16:41:19 +00:00
|
|
|
const globallyCheck = globallyDone.parse(
|
|
|
|
Parser.Streams.ofString(parameterString)
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
let prefix, rest
|
|
|
|
if (globallyCheck.isAccepted()) {
|
|
|
|
prefix = parameterString.substring(0, globallyCheck.offset)
|
|
|
|
rest = parameterString.substring(globallyCheck.offset)
|
2020-11-01 16:41:19 +00:00
|
|
|
} else {
|
2018-08-16 22:30:59 +00:00
|
|
|
prefix = parameterString.substring(0, usernameCheck.offset)
|
|
|
|
rest = parameterString.substring(usernameCheck.offset)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!globallyCheck.isAccepted() && 'globally'.startsWith(rest)) {
|
|
|
|
yield `${prefix}globally `
|
|
|
|
}
|
|
|
|
if (/^[0-9]+$/.test(rest)) {
|
|
|
|
yield `${prefix}${rest}h `
|
|
|
|
yield `${prefix}${rest}d `
|
|
|
|
yield `${prefix}${rest}m `
|
|
|
|
}
|
|
|
|
if (rest === '') {
|
|
|
|
yield `${prefix}1h `
|
|
|
|
yield `${prefix}1d `
|
|
|
|
yield `${prefix}5m `
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const userID of this.profileStore.getLastActivity()) {
|
|
|
|
const user = this.profileStore.get(userID)
|
|
|
|
if (!user.username.startsWith(parameterString)) continue
|
|
|
|
|
|
|
|
yield `"${user.username.replace(/"/g, '""')}" `
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Suspension.DEPENDENCIES = DEPENDENCIES
|
|
|
|
|
|
|
|
return Suspension
|
2020-11-01 16:41:19 +00:00
|
|
|
})
|