Skip to content
Merged
72 changes: 72 additions & 0 deletions app/lib/methods/getUsersPresence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { InteractionManager } from 'react-native';
import semver from 'semver';

import reduxStore from '../createStore';
import { setActiveUsers } from '../../actions/activeUsers';

export function subscribeUsersPresence() {
const serverVersion = reduxStore.getState().server.version;

// if server is lower than 1.1.0
if (serverVersion && semver.lt(semver.coerce(serverVersion), '1.1.0')) {
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');
}
}

let ids = [];

export default async function getUsersPresence() {
const serverVersion = reduxStore.getState().server.version;

// if server is greather than or equal 1.1.0
if (serverVersion && !semver.lt(semver.coerce(serverVersion), '1.1.0')) {
let params = {};

// if server is greather than or equal 3.0.0
if (serverVersion && !semver.lt(semver.coerce(serverVersion), '3.0.0')) {
// if not have any id
if (!ids.length) {
return;
}
// Request userPresence on demand
params = { ids: ids.join(',') };
ids = [];
}

// RC 1.1.0
const result = await this.sdk.get('users.presence', params);
if (result.success) {
const activeUsers = result.users.reduce((ret, item) => {
ret[item._id] = item.status;
return ret;
}, {});
InteractionManager.runAfterInteractions(() => {
reduxStore.dispatch(setActiveUsers(activeUsers));
});
}
}
}

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);
}

ids.push(uid);
}
40 changes: 4 additions & 36 deletions app/lib/rocketchat.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from '../actions/share';

import subscribeRooms from './methods/subscriptions/rooms';
import getUsersPresence, { getUserPresence, subscribeUsersPresence } from './methods/getUsersPresence';

import protectedFunction from './methods/helpers/protectedFunction';
import readMessages from './methods/readMessages';
Expand Down Expand Up @@ -1065,42 +1066,9 @@ const RocketChat = {
this.activeUsers[ddpMessage.id] = ddpMessage.fields.status;
}
},
getUserPresence() {
return new Promise(async(resolve) => {
const serverVersion = reduxStore.getState().server.version;

// if server is lower than 1.1.0
if (serverVersion && semver.lt(semver.coerce(serverVersion), '1.1.0')) {
if (this.activeUsersSubTimeout) {
clearTimeout(this.activeUsersSubTimeout);
this.activeUsersSubTimeout = false;
}
this.activeUsersSubTimeout = setTimeout(() => {
this.sdk.subscribe('activeUsers');
}, 5000);
return resolve();
} else {
const params = {};
// if (this.lastUserPresenceFetch) {
// params.from = this.lastUserPresenceFetch.toISOString();
// }

// RC 1.1.0
const result = await this.sdk.get('users.presence', params);
if (result.success) {
const activeUsers = result.users.reduce((ret, item) => {
ret[item._id] = item.status;
return ret;
}, {});
InteractionManager.runAfterInteractions(() => {
reduxStore.dispatch(setActiveUsers(activeUsers));
});
this.sdk.subscribe('stream-notify-logged', 'user-status');
return resolve();
}
}
});
},
getUsersPresence,
getUserPresence,
subscribeUsersPresence,
getDirectory({
query, count, offset, sort
}) {
Expand Down
15 changes: 12 additions & 3 deletions app/presentation/RoomItem/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import { View, Text } from 'react-native';
import { connect } from 'react-redux';
Expand Down Expand Up @@ -40,8 +40,15 @@ const arePropsEqual = (oldProps, newProps) => {
};

const RoomItem = React.memo(({
onPress, width, favorite, toggleFav, isRead, rid, toggleRead, hideChannel, testID, unread, userMentions, name, _updatedAt, alert, type, avatarSize, baseUrl, userId, username, token, id, prid, showLastMessage, hideUnreadStatus, lastMessage, status, avatar, useRealName, theme
onPress, width, favorite, toggleFav, isRead, rid, toggleRead, hideChannel, testID, unread, userMentions, name, _updatedAt, alert, type, avatarSize, baseUrl, userId, username, token, id, prid, showLastMessage, hideUnreadStatus, lastMessage, status, avatar, useRealName, getUserPresence, theme
}) => {
useEffect(() => {
if (type === 'd' && rid) {
const uid = rid.replace(userId, '');
getUserPresence(uid);
}
}, []);

const date = formatDate(_updatedAt);

let accessibilityLabel = name;
Expand Down Expand Up @@ -190,12 +197,14 @@ RoomItem.propTypes = {
avatar: PropTypes.bool,
hideUnreadStatus: PropTypes.bool,
useRealName: PropTypes.bool,
getUserPresence: PropTypes.func,
theme: PropTypes.string
};

RoomItem.defaultProps = {
avatarSize: 48,
status: 'offline'
status: 'offline',
getUserPresence: () => {}
};

const mapStateToProps = (state, ownProps) => ({
Expand Down
7 changes: 4 additions & 3 deletions app/sagas/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ const registerPushToken = function* registerPushToken() {
yield RocketChat.registerPushToken();
};

const fetchUserPresence = function* fetchUserPresence() {
yield RocketChat.getUserPresence();
const fetchUsersPresence = function* fetchUserPresence() {
yield RocketChat.getUsersPresence();
yield RocketChat.subscribeUsersPresence();
};

const handleLoginSuccess = function* handleLoginSuccess({ user }) {
Expand All @@ -87,7 +88,7 @@ const handleLoginSuccess = function* handleLoginSuccess({ user }) {
yield fork(fetchRoles);
yield fork(fetchSlashCommands);
yield fork(registerPushToken);
yield fork(fetchUserPresence);
yield fork(fetchUsersPresence);

I18n.locale = user.language;
moment.locale(toMomentLocale(user.language));
Expand Down
3 changes: 3 additions & 0 deletions app/views/RoomsListView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@ class RoomsListView extends React.Component {

getRoomAvatar = item => RocketChat.getRoomAvatar(item)

getUserPresence = uid => RocketChat.getUserPresence(uid)

goRoom = (item) => {
const { navigation } = this.props;
this.cancelSearch();
Expand Down Expand Up @@ -794,6 +796,7 @@ class RoomsListView extends React.Component {
toggleRead={this.toggleRead}
hideChannel={this.hideChannel}
useRealName={useRealName}
getUserPresence={this.getUserPresence}
/>
);
};
Expand Down