2020-03-03 21:10:39 +00:00
|
|
|
import { InteractionManager } from 'react-native';
|
2021-02-01 17:18:55 +00:00
|
|
|
import lt from 'semver/functions/lt';
|
|
|
|
import gte from 'semver/functions/gte';
|
2020-10-30 13:12:02 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2020-03-03 21:10:39 +00:00
|
|
|
|
|
|
|
import reduxStore from '../createStore';
|
|
|
|
import { setActiveUsers } from '../../actions/activeUsers';
|
2020-04-03 18:03:53 +00:00
|
|
|
import { setUser } from '../../actions/login';
|
2020-10-30 13:12:02 +00:00
|
|
|
import database from '../database';
|
2020-03-03 21:10:39 +00:00
|
|
|
|
|
|
|
export function subscribeUsersPresence() {
|
|
|
|
const serverVersion = reduxStore.getState().server.version;
|
|
|
|
|
|
|
|
// if server is lower than 1.1.0
|
2021-02-01 17:18:55 +00:00
|
|
|
if (serverVersion && lt(serverVersion, '1.1.0')) {
|
2020-03-03 21:10:39 +00:00
|
|
|
if (this.activeUsersSubTimeout) {
|
|
|
|
clearTimeout(this.activeUsersSubTimeout);
|
|
|
|
this.activeUsersSubTimeout = false;
|
|
|
|
}
|
|
|
|
this.activeUsersSubTimeout = setTimeout(() => {
|
|
|
|
this.sdk.subscribe('activeUsers');
|
|
|
|
}, 5000);
|
|
|
|
} else {
|
|
|
|
this.sdk.subscribe('stream-notify-logged', 'user-status');
|
|
|
|
}
|
2020-10-30 13:12:02 +00:00
|
|
|
|
|
|
|
// RC 0.49.1
|
|
|
|
this.sdk.subscribe('stream-notify-logged', 'updateAvatar');
|
|
|
|
// RC 0.58.0
|
|
|
|
this.sdk.subscribe('stream-notify-logged', 'Users:NameChanged');
|
2020-03-03 21:10:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let ids = [];
|
|
|
|
|
|
|
|
export default async function getUsersPresence() {
|
|
|
|
const serverVersion = reduxStore.getState().server.version;
|
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
|
2021-02-01 17:18:55 +00:00
|
|
|
if (serverVersion && gte(serverVersion, '1.1.0')) {
|
2020-03-03 21:10:39 +00:00
|
|
|
let params = {};
|
|
|
|
|
|
|
|
// if server is greather than or equal 3.0.0
|
2021-02-01 17:18:55 +00:00
|
|
|
if (serverVersion && gte(serverVersion, '3.0.0')) {
|
2020-03-03 21:10:39 +00:00
|
|
|
// if not have any id
|
|
|
|
if (!ids.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Request userPresence on demand
|
|
|
|
params = { ids: ids.join(',') };
|
|
|
|
}
|
|
|
|
|
2020-04-03 18:03:53 +00:00
|
|
|
try {
|
|
|
|
// RC 1.1.0
|
|
|
|
const result = await this.sdk.get('users.presence', params);
|
|
|
|
if (result.success) {
|
2020-10-30 13:12:02 +00:00
|
|
|
const { users } = result;
|
|
|
|
|
|
|
|
const activeUsers = users.reduce((ret, item) => {
|
2020-04-03 18:03:53 +00:00
|
|
|
const { _id, status, statusText } = item;
|
|
|
|
|
|
|
|
if (loggedUser && loggedUser.id === _id) {
|
|
|
|
reduxStore.dispatch(setUser({ status, statusText }));
|
|
|
|
}
|
|
|
|
|
|
|
|
ret[_id] = { status, statusText };
|
|
|
|
return ret;
|
|
|
|
}, {});
|
|
|
|
InteractionManager.runAfterInteractions(() => {
|
|
|
|
reduxStore.dispatch(setActiveUsers(activeUsers));
|
|
|
|
});
|
|
|
|
ids = [];
|
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');
|
2020-10-30 13:12:02 +00:00
|
|
|
users.forEach(async(user) => {
|
|
|
|
try {
|
|
|
|
const userRecord = await userCollection.find(user._id);
|
|
|
|
await db.action(async() => {
|
|
|
|
await userRecord.update((u) => {
|
|
|
|
Object.assign(u, user);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
// User not found
|
|
|
|
await db.action(async() => {
|
|
|
|
await userCollection.create((u) => {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let usersTimer = null;
|
|
|
|
export function getUserPresence(uid) {
|
|
|
|
const auth = reduxStore.getState().login.isAuthenticated;
|
|
|
|
|
|
|
|
if (!usersTimer) {
|
|
|
|
usersTimer = setTimeout(() => {
|
|
|
|
if (auth && ids.length) {
|
|
|
|
getUsersPresence.call(this);
|
|
|
|
}
|
|
|
|
usersTimer = null;
|
|
|
|
}, 2000);
|
|
|
|
}
|
|
|
|
|
2020-10-30 13:12:02 +00:00
|
|
|
if (uid) {
|
|
|
|
ids.push(uid);
|
|
|
|
}
|
2020-03-03 21:10:39 +00:00
|
|
|
}
|