[IMPROVEMENT] Update user presence endpoint (#924)

* [IMPROVEMENT] Update user presence endpoint

* Use `from` parameter in case of reconnection
This commit is contained in:
Diego Mello 2019-05-28 13:52:26 -03:00 committed by GitHub
parent a148e6d15d
commit b1eb18351f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 62 additions and 8 deletions

View File

@ -44,6 +44,8 @@ export const MARKDOWN_KEY = 'RC_MARKDOWN_KEY';
const returnAnArray = obj => obj || []; const returnAnArray = obj => obj || [];
const MIN_ROCKETCHAT_VERSION = '0.70.0'; const MIN_ROCKETCHAT_VERSION = '0.70.0';
const STATUSES = ['offline', 'online', 'away', 'busy'];
const RocketChat = { const RocketChat = {
TOKEN_KEY, TOKEN_KEY,
subscribeRooms, subscribeRooms,
@ -169,14 +171,7 @@ const RocketChat = {
this.getCustomEmoji(); this.getCustomEmoji();
this.getRoles(); this.getRoles();
this.registerPushToken().catch(e => console.log(e)); this.registerPushToken().catch(e => console.log(e));
this.getUserPresence();
if (this.activeUsersSubTimeout) {
clearTimeout(this.activeUsersSubTimeout);
this.activeUsersSubTimeout = false;
}
this.activeUsersSubTimeout = setTimeout(() => {
this.sdk.subscribe('activeUsers');
}, 5000);
}, },
connect({ server, user }) { connect({ server, user }) {
database.setActiveDB(server); database.setActiveDB(server);
@ -214,6 +209,10 @@ const RocketChat = {
this.sdk.onStreamData('connected', () => { this.sdk.onStreamData('connected', () => {
reduxStore.dispatch(connectSuccess()); reduxStore.dispatch(connectSuccess());
const { isAuthenticated } = reduxStore.getState().login;
if (isAuthenticated) {
this.getUserPresence();
}
}); });
this.sdk.onStreamData('close', () => { this.sdk.onStreamData('close', () => {
@ -221,6 +220,25 @@ const RocketChat = {
}); });
this.sdk.onStreamData('users', protectedFunction(ddpMessage => RocketChat._setUser(ddpMessage))); this.sdk.onStreamData('users', protectedFunction(ddpMessage => RocketChat._setUser(ddpMessage)));
this.sdk.onStreamData('stream-notify-logged', protectedFunction((ddpMessage) => {
const { eventName } = ddpMessage.fields;
if (eventName === 'user-status') {
const userStatus = ddpMessage.fields.args[0];
const [id, username, status] = userStatus;
if (username) {
database.memoryDatabase.write(() => {
try {
database.memoryDatabase.create('activeUsers', {
id, username, status: STATUSES[status]
}, true);
} catch (error) {
console.log(error);
}
});
}
}
}));
}, },
register(credentials) { register(credentials) {
@ -782,6 +800,42 @@ const RocketChat = {
return this.sdk.get('chat.syncThreadsList', { return this.sdk.get('chat.syncThreadsList', {
rid, updatedSince rid, updatedSince
}); });
},
async getUserPresence() {
const serverVersion = reduxStore.getState().server.version;
// if server is lower than 1.1.0
if (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 {
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) {
this.lastUserPresenceFetch = new Date();
database.memoryDatabase.write(() => {
result.users.forEach((item) => {
try {
item.id = item._id;
database.memoryDatabase.create('activeUsers', item, true);
} catch (error) {
console.log(error);
}
});
});
this.sdk.subscribe('stream-notify-logged', 'user-status');
}
}
} }
}; };