2020-03-03 21:10:39 +00:00
|
|
|
import { InteractionManager } from 'react-native';
|
2020-10-30 13:12:02 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2020-03-03 21:10:39 +00:00
|
|
|
|
2022-04-04 19:15:29 +00:00
|
|
|
import { IActiveUsers } from '../../../reducers/activeUsers';
|
|
|
|
import { compareServerVersion } from '../../utils';
|
|
|
|
import { store as reduxStore } from '../../auxStore';
|
|
|
|
import { setActiveUsers } from '../../../actions/activeUsers';
|
|
|
|
import { setUser } from '../../../actions/login';
|
|
|
|
import database from '../../database';
|
|
|
|
import { IRocketChat, IUser } from '../../../definitions';
|
|
|
|
import sdk from '../services/sdk';
|
2020-03-03 21:10:39 +00:00
|
|
|
|
2022-02-19 02:31:38 +00:00
|
|
|
export function subscribeUsersPresence(this: IRocketChat) {
|
|
|
|
const serverVersion = reduxStore.getState().server.version as string;
|
2020-03-03 21:10:39 +00:00
|
|
|
|
|
|
|
// if server is lower than 1.1.0
|
2022-02-07 18:44:04 +00:00
|
|
|
if (compareServerVersion(serverVersion, 'lowerThan', '1.1.0')) {
|
2020-03-03 21:10:39 +00:00
|
|
|
if (this.activeUsersSubTimeout) {
|
|
|
|
clearTimeout(this.activeUsersSubTimeout);
|
|
|
|
this.activeUsersSubTimeout = false;
|
|
|
|
}
|
|
|
|
this.activeUsersSubTimeout = setTimeout(() => {
|
2022-02-19 02:31:38 +00:00
|
|
|
sdk.subscribe('activeUsers');
|
2020-03-03 21:10:39 +00:00
|
|
|
}, 5000);
|
2022-02-09 20:44:53 +00:00
|
|
|
} else if (compareServerVersion(serverVersion, 'lowerThan', '4.1.0')) {
|
2022-02-19 02:31:38 +00:00
|
|
|
sdk.subscribe('stream-notify-logged', 'user-status');
|
2020-03-03 21:10:39 +00:00
|
|
|
}
|
2020-10-30 13:12:02 +00:00
|
|
|
|
|
|
|
// RC 0.49.1
|
2022-02-19 02:31:38 +00:00
|
|
|
sdk.subscribe('stream-notify-logged', 'updateAvatar');
|
2020-10-30 13:12:02 +00:00
|
|
|
// RC 0.58.0
|
2022-02-19 02:31:38 +00:00
|
|
|
sdk.subscribe('stream-notify-logged', 'Users:NameChanged');
|
2020-03-03 21:10:39 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 21:27:53 +00:00
|
|
|
let usersBatch: string[] = [];
|
2020-03-03 21:10:39 +00:00
|
|
|
|
2022-03-30 21:27:53 +00:00
|
|
|
export default async function getUsersPresence(usersParams: string[]) {
|
2022-02-19 02:31:38 +00:00
|
|
|
const serverVersion = reduxStore.getState().server.version as string;
|
2020-04-03 18:03:53 +00:00
|
|
|
const { user: loggedUser } = reduxStore.getState().login;
|
2020-03-03 21:10:39 +00:00
|
|
|
|
|
|
|
// if server is greather than or equal 1.1.0
|
2022-02-07 18:44:04 +00:00
|
|
|
if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '1.1.0')) {
|
2020-03-03 21:10:39 +00:00
|
|
|
let params = {};
|
|
|
|
|
|
|
|
// if server is greather than or equal 3.0.0
|
2022-02-07 18:44:04 +00:00
|
|
|
if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '3.0.0')) {
|
2020-03-03 21:10:39 +00:00
|
|
|
// if not have any id
|
2022-03-30 21:27:53 +00:00
|
|
|
if (!usersParams.length) {
|
2020-03-03 21:10:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Request userPresence on demand
|
2022-03-30 21:27:53 +00:00
|
|
|
params = { ids: usersParams.join(',') };
|
2020-03-03 21:10:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 18:03:53 +00:00
|
|
|
try {
|
|
|
|
// RC 1.1.0
|
2022-02-19 02:31:38 +00:00
|
|
|
const result = (await sdk.get('users.presence' as any, params as any)) as any;
|
2022-02-09 20:44:53 +00:00
|
|
|
|
|
|
|
if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '4.1.0')) {
|
2022-03-30 21:27:53 +00:00
|
|
|
sdk.subscribeRaw('stream-user-presence', ['', { added: usersParams }]);
|
2022-02-09 20:44:53 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 18:03:53 +00:00
|
|
|
if (result.success) {
|
2020-10-30 13:12:02 +00:00
|
|
|
const { users } = result;
|
|
|
|
|
2022-03-30 21:27:53 +00:00
|
|
|
const activeUsers = usersParams.reduce((ret: IActiveUsers, id) => {
|
2022-02-19 02:31:38 +00:00
|
|
|
const user = users.find((u: IUser) => u._id === id) ?? { _id: id, status: 'offline' };
|
2021-03-31 17:47:17 +00:00
|
|
|
const { _id, status, statusText } = user;
|
2020-04-03 18:03:53 +00:00
|
|
|
|
|
|
|
if (loggedUser && loggedUser.id === _id) {
|
|
|
|
reduxStore.dispatch(setUser({ status, statusText }));
|
|
|
|
}
|
|
|
|
|
|
|
|
ret[_id] = { status, statusText };
|
|
|
|
return ret;
|
|
|
|
}, {});
|
|
|
|
InteractionManager.runAfterInteractions(() => {
|
|
|
|
reduxStore.dispatch(setActiveUsers(activeUsers));
|
|
|
|
});
|
2020-10-30 13:12:02 +00:00
|
|
|
|
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const userCollection = db.get('users');
|
2022-02-19 02:31:38 +00:00
|
|
|
users.forEach(async (user: IUser) => {
|
2020-10-30 13:12:02 +00:00
|
|
|
try {
|
|
|
|
const userRecord = await userCollection.find(user._id);
|
2022-02-19 02:31:38 +00:00
|
|
|
await db.write(async () => {
|
2021-09-13 20:41:05 +00:00
|
|
|
await userRecord.update(u => {
|
2020-10-30 13:12:02 +00:00
|
|
|
Object.assign(u, user);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
// User not found
|
2022-02-19 02:31:38 +00:00
|
|
|
await db.write(async () => {
|
2021-09-13 20:41:05 +00:00
|
|
|
await userCollection.create(u => {
|
2020-10-30 13:12:02 +00:00
|
|
|
u._raw = sanitizedRaw({ id: user._id }, userCollection.schema);
|
|
|
|
Object.assign(u, user);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2020-04-03 18:03:53 +00:00
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
// do nothing
|
2020-03-03 21:10:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 18:59:39 +00:00
|
|
|
let usersTimer: ReturnType<typeof setTimeout> | null = null;
|
2022-02-19 02:31:38 +00:00
|
|
|
export function getUserPresence(uid: string) {
|
2020-03-03 21:10:39 +00:00
|
|
|
if (!usersTimer) {
|
|
|
|
usersTimer = setTimeout(() => {
|
2022-03-30 21:27:53 +00:00
|
|
|
getUsersPresence(usersBatch);
|
|
|
|
usersBatch = [];
|
2020-03-03 21:10:39 +00:00
|
|
|
usersTimer = null;
|
|
|
|
}, 2000);
|
|
|
|
}
|
|
|
|
|
2020-10-30 13:12:02 +00:00
|
|
|
if (uid) {
|
2022-03-30 21:27:53 +00:00
|
|
|
usersBatch.push(uid);
|
2020-10-30 13:12:02 +00:00
|
|
|
}
|
2020-03-03 21:10:39 +00:00
|
|
|
}
|