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.
|
|
|
|
*
|
2022-09-17 14:23:02 +00:00
|
|
|
* Change Date: 2026-09-17
|
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([
|
|
|
|
'Bastelstu.be/PromiseWrap/Ajax',
|
|
|
|
'./DataStructure/LRU',
|
|
|
|
'./User',
|
|
|
|
'WoltLabSuite/Core/User',
|
|
|
|
], function (Ajax, LRU, User, CoreUser) {
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const DEPENDENCIES = []
|
2018-08-16 22:30:59 +00:00
|
|
|
/**
|
|
|
|
* ProfileStore stores information about users.
|
|
|
|
*/
|
|
|
|
class ProfileStore {
|
|
|
|
constructor() {
|
|
|
|
this.users = new Map()
|
|
|
|
this.processing = new Map()
|
|
|
|
|
|
|
|
this.lastActivity = new LRU()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensures that information about the given userIDs are available
|
|
|
|
* in the store. The returned promise resolves once all the requests
|
|
|
|
* to fetch the data finished successfully.
|
|
|
|
*
|
|
|
|
* @param {number[]} userIDs
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
async ensureUsersByIDs(userIDs) {
|
|
|
|
// Dedup
|
2020-11-01 16:41:19 +00:00
|
|
|
userIDs = userIDs
|
|
|
|
.filter((value, index, self) => self.indexOf(value) === index)
|
|
|
|
.map((userID) => parseInt(userID, 10))
|
|
|
|
|
|
|
|
const missing = []
|
|
|
|
const promises = []
|
|
|
|
userIDs.forEach(
|
|
|
|
function (userID) {
|
|
|
|
if (this.isRecent(userID)) return
|
|
|
|
if (this.processing.has(userID)) {
|
|
|
|
promises.push(this.processing.get(userID))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
missing.push(userID)
|
|
|
|
}.bind(this)
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
if (missing.length > 0) {
|
2020-11-01 16:41:19 +00:00
|
|
|
const payload = {
|
|
|
|
actionName: 'getUsersByID',
|
|
|
|
parameters: { userIDs: missing },
|
|
|
|
}
|
|
|
|
const request = (async (_) => {
|
2018-08-16 22:30:59 +00:00
|
|
|
try {
|
|
|
|
const response = await Ajax.api(this, payload)
|
2020-11-01 16:41:19 +00:00
|
|
|
return Object.entries(response.returnValues).forEach(
|
|
|
|
([userID, user]) => {
|
|
|
|
userID = parseInt(userID, 10)
|
|
|
|
const data = { user: new User(user), date: Date.now() }
|
|
|
|
this.users.set(userID, data)
|
|
|
|
this.processing.delete(userID)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
} catch (err) {
|
|
|
|
missing.forEach((userID) => this.processing.delete(userID))
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
})()
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
missing.forEach((userID) => this.processing.set(userID, request))
|
2018-08-16 22:30:59 +00:00
|
|
|
promises.push(request)
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all(promises)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns information about the given userIDs.
|
|
|
|
*
|
|
|
|
* @param {number[]} userIDs
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
async getUsersByIDs(userIDs) {
|
|
|
|
await this.ensureUsersByIDs(userIDs)
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
return new Map(userIDs.map((userID) => [userID, this.get(userID)]))
|
2018-08-16 22:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns information about the currently logged in user.
|
|
|
|
*
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
getSelf() {
|
|
|
|
const self = this.get(CoreUser.userId)
|
|
|
|
if (self == null) {
|
|
|
|
throw new Error('Unreachable')
|
|
|
|
}
|
|
|
|
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns information about the given userID.
|
|
|
|
*
|
|
|
|
* @param {number} userID
|
|
|
|
* @returns {?User} null if no information are known
|
|
|
|
*/
|
|
|
|
get(userID) {
|
|
|
|
const user = this.users.get(userID)
|
|
|
|
|
|
|
|
if (user != null) {
|
|
|
|
return user.user
|
|
|
|
}
|
|
|
|
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether information about the given userID are known.
|
|
|
|
*
|
|
|
|
* @param {number} userID
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
has(userID) {
|
|
|
|
return this.users.has(userID)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Forces an update of the information about the given userID.
|
|
|
|
*
|
|
|
|
* @param {number} userID
|
|
|
|
*/
|
|
|
|
expire(userID) {
|
|
|
|
if (!this.users.has(userID)) return
|
|
|
|
|
|
|
|
this.users.get(userID).date = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the information about the given userID are recent.
|
|
|
|
*
|
|
|
|
* @param {number} userID
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
isRecent(userID) {
|
|
|
|
const user = this.users.get(userID)
|
|
|
|
|
|
|
|
if (user != null) {
|
2020-11-01 16:41:19 +00:00
|
|
|
return user.date > Date.now() - 5 * 60e3
|
2018-08-16 22:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the stored information.
|
|
|
|
*
|
|
|
|
* @returns {User[]}
|
|
|
|
*/
|
|
|
|
values() {
|
2020-11-01 16:41:19 +00:00
|
|
|
return Array.from(this.users.values()).map((item) => item.user)
|
2018-08-16 22:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pushLastActivity(userID) {
|
|
|
|
if (!userID) return
|
|
|
|
|
|
|
|
this.lastActivity.add(userID)
|
|
|
|
}
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
*getLastActivity() {
|
|
|
|
yield* this.lastActivity
|
2018-08-16 22:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_ajaxSetup() {
|
2020-11-01 16:41:19 +00:00
|
|
|
return {
|
|
|
|
silent: true,
|
|
|
|
ignoreError: true,
|
|
|
|
data: { className: 'chat\\data\\user\\UserAction' },
|
|
|
|
}
|
2018-08-16 22:30:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ProfileStore.DEPENDENCIES = DEPENDENCIES
|
|
|
|
|
|
|
|
return ProfileStore
|
2020-11-01 16:41:19 +00:00
|
|
|
})
|