Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Not properly fetching users presence on some startup situations #3967

Merged
merged 1 commit into from
Mar 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions app/lib/methods/getUsersPresence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export function subscribeUsersPresence(this: IRocketChat) {
sdk.subscribe('stream-notify-logged', 'Users:NameChanged');
}

let ids: string[] = [];
let usersBatch: string[] = [];

export default async function getUsersPresence() {
export default async function getUsersPresence(usersParams: string[]) {
const serverVersion = reduxStore.getState().server.version as string;
const { user: loggedUser } = reduxStore.getState().login;

Expand All @@ -45,25 +45,25 @@ export default async function getUsersPresence() {
// if server is greather than or equal 3.0.0
if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '3.0.0')) {
// if not have any id
if (!ids.length) {
if (!usersParams.length) {
return;
}
// Request userPresence on demand
params = { ids: ids.join(',') };
params = { ids: usersParams.join(',') };
}

try {
// RC 1.1.0
const result = (await sdk.get('users.presence' as any, params as any)) as any;

if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '4.1.0')) {
sdk.subscribeRaw('stream-user-presence', ['', { added: ids }]);
sdk.subscribeRaw('stream-user-presence', ['', { added: usersParams }]);
}

if (result.success) {
const { users } = result;

const activeUsers = ids.reduce((ret: IActiveUsers, id) => {
const activeUsers = usersParams.reduce((ret: IActiveUsers, id) => {
const user = users.find((u: IUser) => u._id === id) ?? { _id: id, status: 'offline' };
const { _id, status, statusText } = user;

Expand All @@ -77,7 +77,6 @@ export default async function getUsersPresence() {
InteractionManager.runAfterInteractions(() => {
reduxStore.dispatch(setActiveUsers(activeUsers));
});
ids = [];

const db = database.active;
const userCollection = db.get('users');
Expand Down Expand Up @@ -110,12 +109,13 @@ let usersTimer: ReturnType<typeof setTimeout> | null = null;
export function getUserPresence(uid: string) {
if (!usersTimer) {
usersTimer = setTimeout(() => {
getUsersPresence();
getUsersPresence(usersBatch);
usersBatch = [];
usersTimer = null;
}, 2000);
}

if (uid) {
ids.push(uid);
usersBatch.push(uid);
}
}