2017-10-19 12:28:59 +03:00
|
|
|
/*
|
|
|
|
Copyright 2017 New Vector Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2018-03-01 17:40:03 +03:00
|
|
|
import EventEmitter from 'events';
|
2017-10-19 12:28:59 +03:00
|
|
|
|
|
|
|
const BULK_REQUEST_DEBOUNCE_MS = 200;
|
|
|
|
|
|
|
|
// Does the server support groups? Assume yes until we receive M_UNRECOGNIZED.
|
|
|
|
// If true, flair can function and we should keep sending requests for groups and avatars.
|
|
|
|
let groupSupport = true;
|
|
|
|
|
|
|
|
const USER_GROUPS_CACHE_BUST_MS = 1800000; // 30 mins
|
|
|
|
const GROUP_PROFILES_CACHE_BUST_MS = 1800000; // 30 mins
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores data used by <Flair/>
|
|
|
|
*/
|
2018-03-01 17:40:03 +03:00
|
|
|
class FlairStore extends EventEmitter {
|
2017-10-19 12:28:59 +03:00
|
|
|
constructor(matrixClient) {
|
2018-03-01 17:40:03 +03:00
|
|
|
super();
|
2017-10-19 12:28:59 +03:00
|
|
|
this._matrixClient = matrixClient;
|
|
|
|
this._userGroups = {
|
|
|
|
// $userId: ['+group1:domain', '+group2:domain', ...]
|
|
|
|
};
|
|
|
|
this._groupProfiles = {
|
|
|
|
// $groupId: {
|
|
|
|
// avatar_url: 'mxc://...'
|
|
|
|
// }
|
|
|
|
};
|
2017-12-15 19:28:50 +03:00
|
|
|
this._groupProfilesPromise = {
|
|
|
|
// $groupId: Promise
|
|
|
|
};
|
2017-10-19 12:28:59 +03:00
|
|
|
this._usersPending = {
|
|
|
|
// $userId: {
|
|
|
|
// prom: Promise
|
|
|
|
// resolve: () => {}
|
|
|
|
// reject: () => {}
|
|
|
|
// }
|
|
|
|
};
|
2017-11-03 21:19:10 +03:00
|
|
|
this._usersInFlight = {
|
2017-11-03 21:48:15 +03:00
|
|
|
// This has the same schema as _usersPending
|
2017-11-03 21:19:10 +03:00
|
|
|
};
|
2017-10-19 12:28:59 +03:00
|
|
|
|
|
|
|
this._debounceTimeoutID = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
groupSupport() {
|
|
|
|
return groupSupport;
|
|
|
|
}
|
|
|
|
|
2017-10-23 17:28:38 +03:00
|
|
|
invalidatePublicisedGroups(userId) {
|
|
|
|
delete this._userGroups[userId];
|
|
|
|
}
|
|
|
|
|
2017-10-19 12:28:59 +03:00
|
|
|
getPublicisedGroupsCached(matrixClient, userId) {
|
|
|
|
if (this._userGroups[userId]) {
|
|
|
|
return Promise.resolve(this._userGroups[userId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bulk lookup ongoing, return promise to resolve/reject
|
2017-11-08 14:31:04 +03:00
|
|
|
if (this._usersPending[userId]) {
|
2017-10-19 12:28:59 +03:00
|
|
|
return this._usersPending[userId].prom;
|
|
|
|
}
|
2017-11-08 14:31:04 +03:00
|
|
|
// User has been moved from pending to in-flight
|
|
|
|
if (this._usersInFlight[userId]) {
|
|
|
|
return this._usersInFlight[userId].prom;
|
|
|
|
}
|
2017-10-19 12:28:59 +03:00
|
|
|
|
|
|
|
this._usersPending[userId] = {};
|
|
|
|
this._usersPending[userId].prom = new Promise((resolve, reject) => {
|
|
|
|
this._usersPending[userId].resolve = resolve;
|
|
|
|
this._usersPending[userId].reject = reject;
|
|
|
|
}).then((groups) => {
|
|
|
|
this._userGroups[userId] = groups;
|
|
|
|
setTimeout(() => {
|
|
|
|
delete this._userGroups[userId];
|
|
|
|
}, USER_GROUPS_CACHE_BUST_MS);
|
|
|
|
return this._userGroups[userId];
|
|
|
|
}).catch((err) => {
|
|
|
|
// Indicate whether the homeserver supports groups
|
|
|
|
if (err.errcode === 'M_UNRECOGNIZED') {
|
|
|
|
console.warn('Cannot display flair, server does not support groups');
|
|
|
|
groupSupport = false;
|
|
|
|
// Return silently to avoid spamming for non-supporting servers
|
|
|
|
return;
|
|
|
|
}
|
2017-11-17 16:56:56 +03:00
|
|
|
console.error('Could not get groups for user', userId, err);
|
2017-10-19 12:28:59 +03:00
|
|
|
throw err;
|
|
|
|
}).finally(() => {
|
2017-11-02 18:59:26 +03:00
|
|
|
delete this._usersInFlight[userId];
|
2017-10-19 12:28:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// This debounce will allow consecutive requests for the public groups of users that
|
|
|
|
// are sent in intervals of < BULK_REQUEST_DEBOUNCE_MS to be batched and only requested
|
|
|
|
// when no more requests are received within the next BULK_REQUEST_DEBOUNCE_MS. The naive
|
|
|
|
// implementation would do a request that only requested the groups for `userId`, leading
|
|
|
|
// to a worst and best case of 1 user per request. This implementation's worst is still
|
|
|
|
// 1 user per request but only if the requests are > BULK_REQUEST_DEBOUNCE_MS apart and the
|
|
|
|
// best case is N users per request.
|
|
|
|
//
|
|
|
|
// This is to reduce the number of requests made whilst trading off latency when viewing
|
|
|
|
// a Flair component.
|
|
|
|
if (this._debounceTimeoutID) clearTimeout(this._debounceTimeoutID);
|
|
|
|
this._debounceTimeoutID = setTimeout(() => {
|
|
|
|
this._batchedGetPublicGroups(matrixClient);
|
|
|
|
}, BULK_REQUEST_DEBOUNCE_MS);
|
|
|
|
|
|
|
|
return this._usersPending[userId].prom;
|
|
|
|
}
|
|
|
|
|
|
|
|
async _batchedGetPublicGroups(matrixClient) {
|
2017-11-02 18:59:26 +03:00
|
|
|
// Move users pending to users in flight
|
|
|
|
this._usersInFlight = this._usersPending;
|
|
|
|
this._usersPending = {};
|
|
|
|
|
2017-10-19 12:28:59 +03:00
|
|
|
let resp = {
|
|
|
|
users: [],
|
|
|
|
};
|
|
|
|
try {
|
2017-11-02 18:59:26 +03:00
|
|
|
resp = await matrixClient.getPublicisedGroups(Object.keys(this._usersInFlight));
|
2017-10-19 12:28:59 +03:00
|
|
|
} catch (err) {
|
|
|
|
// Propagate the same error to all usersInFlight
|
2017-11-02 18:59:26 +03:00
|
|
|
Object.keys(this._usersInFlight).forEach((userId) => {
|
2017-11-03 21:43:43 +03:00
|
|
|
// The promise should always exist for userId, but do a null-check anyway
|
|
|
|
if (!this._usersInFlight[userId]) return;
|
2017-11-02 18:59:26 +03:00
|
|
|
this._usersInFlight[userId].reject(err);
|
2017-10-19 12:28:59 +03:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const updatedUserGroups = resp.users;
|
2017-11-02 18:59:26 +03:00
|
|
|
Object.keys(this._usersInFlight).forEach((userId) => {
|
2017-11-03 21:43:43 +03:00
|
|
|
// The promise should always exist for userId, but do a null-check anyway
|
|
|
|
if (!this._usersInFlight[userId]) return;
|
2017-11-02 18:59:26 +03:00
|
|
|
this._usersInFlight[userId].resolve(updatedUserGroups[userId] || []);
|
2017-10-19 12:28:59 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-28 23:56:59 +03:00
|
|
|
/**
|
|
|
|
* Gets the profile for the given group if known, otherwise returns null.
|
|
|
|
* This triggers `getGroupProfileCached` if needed, though the result of the
|
|
|
|
* call will not be returned by this function.
|
2020-08-31 19:59:37 +03:00
|
|
|
* @param matrixClient {MatrixClient} The matrix client to use to fetch the profile, if needed.
|
|
|
|
* @param groupId {string} The group ID to get the profile for.
|
|
|
|
* @returns {*} The profile if known, otherwise null.
|
2020-08-28 23:56:59 +03:00
|
|
|
*/
|
|
|
|
getGroupProfileCachedFast(matrixClient, groupId) {
|
2020-08-29 05:13:26 +03:00
|
|
|
if (!matrixClient || !groupId) return null;
|
2020-08-28 23:56:59 +03:00
|
|
|
if (this._groupProfiles[groupId]) {
|
|
|
|
return this._groupProfiles[groupId];
|
|
|
|
}
|
|
|
|
this.getGroupProfileCached(matrixClient, groupId);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-10-19 12:28:59 +03:00
|
|
|
async getGroupProfileCached(matrixClient, groupId) {
|
|
|
|
if (this._groupProfiles[groupId]) {
|
|
|
|
return this._groupProfiles[groupId];
|
|
|
|
}
|
|
|
|
|
2018-03-06 14:33:56 +03:00
|
|
|
// A request is ongoing, wait for it to complete and return the group profile.
|
|
|
|
if (this._groupProfilesPromise[groupId]) {
|
|
|
|
try {
|
|
|
|
await this._groupProfilesPromise[groupId];
|
|
|
|
} catch (e) {
|
|
|
|
// Don't log the error; this is done below
|
2018-03-06 15:14:41 +03:00
|
|
|
return null;
|
2018-03-06 14:33:56 +03:00
|
|
|
}
|
|
|
|
return this._groupProfiles[groupId];
|
2017-12-15 19:28:50 +03:00
|
|
|
}
|
|
|
|
|
2018-03-06 14:33:56 +03:00
|
|
|
// No request yet, start one
|
|
|
|
console.log('FlairStore: Request group profile of ' + groupId);
|
2018-03-06 15:13:46 +03:00
|
|
|
this._groupProfilesPromise[groupId] = matrixClient.getGroupProfile(groupId);
|
2018-03-06 14:33:56 +03:00
|
|
|
|
2018-01-02 13:22:34 +03:00
|
|
|
let profile;
|
|
|
|
try {
|
|
|
|
profile = await this._groupProfilesPromise[groupId];
|
|
|
|
} catch (e) {
|
2018-03-06 14:33:56 +03:00
|
|
|
console.log('FlairStore: Failed to get group profile for ' + groupId, e);
|
2018-01-02 13:22:34 +03:00
|
|
|
// Don't retry, but allow a retry when the profile is next requested
|
|
|
|
delete this._groupProfilesPromise[groupId];
|
2018-03-06 15:14:41 +03:00
|
|
|
return null;
|
2018-01-02 13:22:34 +03:00
|
|
|
}
|
|
|
|
|
2017-10-19 12:28:59 +03:00
|
|
|
this._groupProfiles[groupId] = {
|
|
|
|
groupId,
|
|
|
|
avatarUrl: profile.avatar_url,
|
2017-10-19 18:18:01 +03:00
|
|
|
name: profile.name,
|
|
|
|
shortDescription: profile.short_description,
|
2017-10-19 12:28:59 +03:00
|
|
|
};
|
2018-01-02 13:22:34 +03:00
|
|
|
delete this._groupProfilesPromise[groupId];
|
2017-12-15 19:28:50 +03:00
|
|
|
|
2018-03-01 17:40:03 +03:00
|
|
|
/// XXX: This is verging on recreating a third "Flux"-looking Store. We really
|
|
|
|
/// should replace FlairStore with a Flux store and some async actions.
|
2018-03-06 14:33:56 +03:00
|
|
|
console.log('FlairStore: Emit updateGroupProfile for ' + groupId);
|
2018-03-01 17:40:03 +03:00
|
|
|
this.emit('updateGroupProfile');
|
|
|
|
|
2017-10-19 12:28:59 +03:00
|
|
|
setTimeout(() => {
|
2018-03-01 18:06:05 +03:00
|
|
|
this.refreshGroupProfile(matrixClient, groupId);
|
2017-10-19 12:28:59 +03:00
|
|
|
}, GROUP_PROFILES_CACHE_BUST_MS);
|
|
|
|
|
|
|
|
return this._groupProfiles[groupId];
|
|
|
|
}
|
2018-03-01 17:40:03 +03:00
|
|
|
|
|
|
|
refreshGroupProfile(matrixClient, groupId) {
|
|
|
|
// Invalidate the cache
|
|
|
|
delete this._groupProfiles[groupId];
|
|
|
|
// Fetch new profile data, and cache it
|
|
|
|
return this.getGroupProfileCached(matrixClient, groupId);
|
|
|
|
}
|
2017-10-19 12:28:59 +03:00
|
|
|
}
|
|
|
|
|
2017-10-19 14:00:03 +03:00
|
|
|
if (global.singletonFlairStore === undefined) {
|
|
|
|
global.singletonFlairStore = new FlairStore();
|
2017-10-19 12:28:59 +03:00
|
|
|
}
|
2017-10-19 14:00:03 +03:00
|
|
|
export default global.singletonFlairStore;
|