debian-mirror-gitlab/app/assets/javascripts/lib/utils/users_cache.js

56 lines
1.5 KiB
JavaScript
Raw Normal View History

2021-03-08 18:12:59 +05:30
import { getUsers, getUser, getUserStatus } from '~/rest_api';
2017-09-10 17:25:29 +05:30
import Cache from './cache';
class UsersCache extends Cache {
retrieve(username) {
if (this.hasData(username)) {
return Promise.resolve(this.get(username));
}
2021-03-08 18:12:59 +05:30
return getUsers('', { username }).then(({ data }) => {
2018-12-13 13:39:08 +05:30
if (!data.length) {
throw new Error(`User "${username}" could not be found!`);
}
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
if (data.length > 1) {
throw new Error(`Expected username "${username}" to be unique!`);
}
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
const user = data[0];
this.internalStorage[username] = user;
return user;
});
// missing catch is intentional, error handling depends on use case
2017-09-10 17:25:29 +05:30
}
2019-02-15 15:39:39 +05:30
retrieveById(userId) {
if (this.hasData(userId) && this.get(userId).username) {
return Promise.resolve(this.get(userId));
}
2021-03-08 18:12:59 +05:30
return getUser(userId).then(({ data }) => {
2019-02-15 15:39:39 +05:30
this.internalStorage[userId] = data;
return data;
});
// missing catch is intentional, error handling depends on use case
}
retrieveStatusById(userId) {
if (this.hasData(userId) && this.get(userId).status) {
return Promise.resolve(this.get(userId).status);
}
2021-03-08 18:12:59 +05:30
return getUserStatus(userId).then(({ data }) => {
2019-02-15 15:39:39 +05:30
if (!this.hasData(userId)) {
this.internalStorage[userId] = {};
}
this.internalStorage[userId].status = data;
return data;
});
// missing catch is intentional, error handling depends on use case
}
2017-09-10 17:25:29 +05:30
}
export default new UsersCache();