Chore: Migrate getUsersPresence to TS (#3717)
* migrate getUsersPresence to ts * use sdk and remove this context from getUsersPresence
This commit is contained in:
parent
8237b3e673
commit
5e39cc0ba5
|
@ -1,14 +1,17 @@
|
||||||
import { InteractionManager } from 'react-native';
|
import { InteractionManager } from 'react-native';
|
||||||
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
||||||
|
|
||||||
|
import { IActiveUsers } from '../../reducers/activeUsers';
|
||||||
import { compareServerVersion } from '../utils';
|
import { compareServerVersion } from '../utils';
|
||||||
import { store as reduxStore } from '../auxStore';
|
import { store as reduxStore } from '../auxStore';
|
||||||
import { setActiveUsers } from '../../actions/activeUsers';
|
import { setActiveUsers } from '../../actions/activeUsers';
|
||||||
import { setUser } from '../../actions/login';
|
import { setUser } from '../../actions/login';
|
||||||
import database from '../database';
|
import database from '../database';
|
||||||
|
import { IRocketChat, IUser } from '../../definitions';
|
||||||
|
import sdk from '../rocketchat/services/sdk';
|
||||||
|
|
||||||
export function subscribeUsersPresence() {
|
export function subscribeUsersPresence(this: IRocketChat) {
|
||||||
const serverVersion = reduxStore.getState().server.version;
|
const serverVersion = reduxStore.getState().server.version as string;
|
||||||
|
|
||||||
// if server is lower than 1.1.0
|
// if server is lower than 1.1.0
|
||||||
if (compareServerVersion(serverVersion, 'lowerThan', '1.1.0')) {
|
if (compareServerVersion(serverVersion, 'lowerThan', '1.1.0')) {
|
||||||
|
@ -17,22 +20,22 @@ export function subscribeUsersPresence() {
|
||||||
this.activeUsersSubTimeout = false;
|
this.activeUsersSubTimeout = false;
|
||||||
}
|
}
|
||||||
this.activeUsersSubTimeout = setTimeout(() => {
|
this.activeUsersSubTimeout = setTimeout(() => {
|
||||||
this.sdk.subscribe('activeUsers');
|
sdk.subscribe('activeUsers');
|
||||||
}, 5000);
|
}, 5000);
|
||||||
} else if (compareServerVersion(serverVersion, 'lowerThan', '4.1.0')) {
|
} else if (compareServerVersion(serverVersion, 'lowerThan', '4.1.0')) {
|
||||||
this.sdk.subscribe('stream-notify-logged', 'user-status');
|
sdk.subscribe('stream-notify-logged', 'user-status');
|
||||||
}
|
}
|
||||||
|
|
||||||
// RC 0.49.1
|
// RC 0.49.1
|
||||||
this.sdk.subscribe('stream-notify-logged', 'updateAvatar');
|
sdk.subscribe('stream-notify-logged', 'updateAvatar');
|
||||||
// RC 0.58.0
|
// RC 0.58.0
|
||||||
this.sdk.subscribe('stream-notify-logged', 'Users:NameChanged');
|
sdk.subscribe('stream-notify-logged', 'Users:NameChanged');
|
||||||
}
|
}
|
||||||
|
|
||||||
let ids = [];
|
let ids: string[] = [];
|
||||||
|
|
||||||
export default async function getUsersPresence() {
|
export default async function getUsersPresence() {
|
||||||
const serverVersion = reduxStore.getState().server.version;
|
const serverVersion = reduxStore.getState().server.version as string;
|
||||||
const { user: loggedUser } = reduxStore.getState().login;
|
const { user: loggedUser } = reduxStore.getState().login;
|
||||||
|
|
||||||
// if server is greather than or equal 1.1.0
|
// if server is greather than or equal 1.1.0
|
||||||
|
@ -51,17 +54,17 @@ export default async function getUsersPresence() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// RC 1.1.0
|
// RC 1.1.0
|
||||||
const result = await this.sdk.get('users.presence', params);
|
const result = (await sdk.get('users.presence' as any, params as any)) as any;
|
||||||
|
|
||||||
if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '4.1.0')) {
|
if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '4.1.0')) {
|
||||||
this.sdk.subscribeRaw('stream-user-presence', ['', { added: ids }]);
|
sdk.subscribeRaw('stream-user-presence', ['', { added: ids }]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
const { users } = result;
|
const { users } = result;
|
||||||
|
|
||||||
const activeUsers = ids.reduce((ret, id) => {
|
const activeUsers = ids.reduce((ret: IActiveUsers, id) => {
|
||||||
const user = users.find(u => u._id === id) ?? { _id: id, status: 'offline' };
|
const user = users.find((u: IUser) => u._id === id) ?? { _id: id, status: 'offline' };
|
||||||
const { _id, status, statusText } = user;
|
const { _id, status, statusText } = user;
|
||||||
|
|
||||||
if (loggedUser && loggedUser.id === _id) {
|
if (loggedUser && loggedUser.id === _id) {
|
||||||
|
@ -78,17 +81,17 @@ export default async function getUsersPresence() {
|
||||||
|
|
||||||
const db = database.active;
|
const db = database.active;
|
||||||
const userCollection = db.get('users');
|
const userCollection = db.get('users');
|
||||||
users.forEach(async user => {
|
users.forEach(async (user: IUser) => {
|
||||||
try {
|
try {
|
||||||
const userRecord = await userCollection.find(user._id);
|
const userRecord = await userCollection.find(user._id);
|
||||||
await db.action(async () => {
|
await db.write(async () => {
|
||||||
await userRecord.update(u => {
|
await userRecord.update(u => {
|
||||||
Object.assign(u, user);
|
Object.assign(u, user);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// User not found
|
// User not found
|
||||||
await db.action(async () => {
|
await db.write(async () => {
|
||||||
await userCollection.create(u => {
|
await userCollection.create(u => {
|
||||||
u._raw = sanitizedRaw({ id: user._id }, userCollection.schema);
|
u._raw = sanitizedRaw({ id: user._id }, userCollection.schema);
|
||||||
Object.assign(u, user);
|
Object.assign(u, user);
|
||||||
|
@ -103,11 +106,11 @@ export default async function getUsersPresence() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let usersTimer = null;
|
let usersTimer: number | null = null;
|
||||||
export function getUserPresence(uid) {
|
export function getUserPresence(uid: string) {
|
||||||
if (!usersTimer) {
|
if (!usersTimer) {
|
||||||
usersTimer = setTimeout(() => {
|
usersTimer = setTimeout(() => {
|
||||||
getUsersPresence.call(this);
|
getUsersPresence();
|
||||||
usersTimer = null;
|
usersTimer = null;
|
||||||
}, 2000);
|
}, 2000);
|
||||||
}
|
}
|
Loading…
Reference in New Issue