2022-04-04 19:15:29 +00:00
|
|
|
import { InteractionManager } from 'react-native';
|
|
|
|
|
2022-04-07 16:53:07 +00:00
|
|
|
import { setActiveUsers } from '../../actions/activeUsers';
|
|
|
|
import { setUser } from '../../actions/login';
|
2022-05-13 14:52:37 +00:00
|
|
|
import { IUser } from '../../definitions';
|
2022-04-07 16:53:07 +00:00
|
|
|
import { store as reduxStore } from '../store/auxStore';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { compareServerVersion } from './helpers';
|
2022-04-04 19:15:29 +00:00
|
|
|
|
2022-05-13 14:52:37 +00:00
|
|
|
export interface IActiveUsers {
|
|
|
|
[key: string]: { status: string; statusText?: string } | string | boolean;
|
|
|
|
msg: string;
|
|
|
|
collection: string;
|
|
|
|
id: string;
|
|
|
|
cleared: boolean;
|
|
|
|
fields: {
|
|
|
|
emails: {
|
|
|
|
address: string;
|
|
|
|
verified: boolean;
|
|
|
|
}[];
|
|
|
|
username: string;
|
|
|
|
status: string;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const _activeUsers = { activeUsers: {} as IActiveUsers };
|
|
|
|
export const _setUserTimer: { setUserTimer: null | ReturnType<typeof setTimeout> } = { setUserTimer: null };
|
|
|
|
|
|
|
|
export function _setUser(ddpMessage: IActiveUsers): void {
|
|
|
|
_activeUsers.activeUsers = _activeUsers.activeUsers || {};
|
2022-04-04 19:15:29 +00:00
|
|
|
const { user } = reduxStore.getState().login;
|
|
|
|
|
|
|
|
if (ddpMessage.fields && user && user.id === ddpMessage.id) {
|
2022-05-13 14:52:37 +00:00
|
|
|
reduxStore.dispatch(setUser(ddpMessage.fields as Partial<IUser>));
|
2022-04-04 19:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ddpMessage.cleared && user && user.id === ddpMessage.id) {
|
|
|
|
reduxStore.dispatch(setUser({ status: 'offline' }));
|
|
|
|
}
|
|
|
|
|
|
|
|
const serverVersion = reduxStore.getState().server.version;
|
|
|
|
if (compareServerVersion(serverVersion, 'lowerThan', '4.1.0')) {
|
2022-05-13 14:52:37 +00:00
|
|
|
if (!_setUserTimer.setUserTimer) {
|
|
|
|
_setUserTimer.setUserTimer = setTimeout(() => {
|
|
|
|
const activeUsersBatch = _activeUsers.activeUsers;
|
2022-04-04 19:15:29 +00:00
|
|
|
InteractionManager.runAfterInteractions(() => {
|
2022-05-13 14:52:37 +00:00
|
|
|
// @ts-ignore
|
2022-04-04 19:15:29 +00:00
|
|
|
reduxStore.dispatch(setActiveUsers(activeUsersBatch));
|
|
|
|
});
|
2022-05-13 14:52:37 +00:00
|
|
|
_setUserTimer.setUserTimer = null;
|
|
|
|
_activeUsers.activeUsers = {} as IActiveUsers;
|
2022-04-04 19:15:29 +00:00
|
|
|
}, 10000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ddpMessage.fields) {
|
2022-05-13 14:52:37 +00:00
|
|
|
_activeUsers.activeUsers[ddpMessage.id] = { status: 'offline' };
|
2022-04-04 19:15:29 +00:00
|
|
|
} else if (ddpMessage.fields.status) {
|
2022-05-13 14:52:37 +00:00
|
|
|
_activeUsers.activeUsers[ddpMessage.id] = { status: ddpMessage.fields.status };
|
2022-04-04 19:15:29 +00:00
|
|
|
}
|
|
|
|
}
|