2017-03-30 20:04:01 +02:00
|
|
|
// This module just manages data. See activity.js for
|
|
|
|
// the UI of our buddy list.
|
|
|
|
|
|
|
|
exports.presence_info = {};
|
|
|
|
|
|
|
|
|
|
|
|
/* Mark users as offline after 140 seconds since their last checkin,
|
|
|
|
* Keep in sync with zerver/tornado/event_queue.py:receiver_is_idle
|
|
|
|
*/
|
|
|
|
var OFFLINE_THRESHOLD_SECS = 140;
|
|
|
|
|
2017-03-31 00:18:04 +02:00
|
|
|
var BIG_REALM_COUNT = 250;
|
|
|
|
|
2017-03-30 20:04:01 +02:00
|
|
|
var MOBILE_DEVICES = ["Android", "ZulipiOS", "ios"];
|
|
|
|
|
|
|
|
function is_mobile(device) {
|
|
|
|
return MOBILE_DEVICES.indexOf(device) !== -1;
|
|
|
|
}
|
|
|
|
|
2018-04-24 18:12:09 +02:00
|
|
|
exports.is_active = function (user_id) {
|
2018-08-04 08:14:33 +02:00
|
|
|
if (exports.presence_info[user_id]) {
|
|
|
|
var status = exports.presence_info[user_id].status;
|
2018-06-06 18:50:09 +02:00
|
|
|
if (status && status === "active") {
|
2017-03-30 20:04:01 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_status = function (user_id) {
|
2019-01-03 16:44:06 +01:00
|
|
|
if (people.is_my_user_id(user_id)) {
|
2017-04-13 04:28:15 +02:00
|
|
|
return "active";
|
|
|
|
}
|
2017-06-19 19:03:39 +02:00
|
|
|
if (user_id in exports.presence_info) {
|
|
|
|
return exports.presence_info[user_id].status;
|
|
|
|
}
|
2017-10-12 18:08:42 +02:00
|
|
|
return "offline";
|
2017-03-30 20:04:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_user_ids = function () {
|
|
|
|
var user_ids = Object.keys(exports.presence_info);
|
|
|
|
return user_ids;
|
|
|
|
};
|
|
|
|
|
|
|
|
function status_from_timestamp(baseline_time, info) {
|
|
|
|
var status = 'offline';
|
|
|
|
var last_active = 0;
|
|
|
|
var mobileAvailable = false;
|
|
|
|
var nonmobileAvailable = false;
|
|
|
|
_.each(info, function (device_presence, device) {
|
|
|
|
var age = baseline_time - device_presence.timestamp;
|
|
|
|
if (last_active < device_presence.timestamp) {
|
|
|
|
last_active = device_presence.timestamp;
|
|
|
|
}
|
|
|
|
if (is_mobile(device)) {
|
|
|
|
mobileAvailable = device_presence.pushable || mobileAvailable;
|
|
|
|
}
|
|
|
|
if (age < OFFLINE_THRESHOLD_SECS) {
|
|
|
|
switch (device_presence.status) {
|
2018-05-07 01:28:37 +02:00
|
|
|
case 'active':
|
|
|
|
if (is_mobile(device)) {
|
|
|
|
mobileAvailable = true;
|
|
|
|
} else {
|
|
|
|
nonmobileAvailable = true;
|
|
|
|
}
|
|
|
|
status = device_presence.status;
|
|
|
|
break;
|
|
|
|
case 'idle':
|
|
|
|
if (status !== 'active') {
|
2017-03-30 20:04:01 +02:00
|
|
|
status = device_presence.status;
|
2018-05-07 01:28:37 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'offline':
|
|
|
|
if (status !== 'active' && status !== 'idle') {
|
|
|
|
status = device_presence.status;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
blueslip.error('Unexpected status', {presence_object: device_presence, device: device}, undefined);
|
2017-03-30 20:04:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return {status: status,
|
|
|
|
mobile: !nonmobileAvailable && mobileAvailable,
|
|
|
|
last_active: last_active };
|
|
|
|
}
|
|
|
|
|
|
|
|
// For testing
|
|
|
|
exports._status_from_timestamp = status_from_timestamp;
|
|
|
|
|
2018-12-18 18:30:40 +01:00
|
|
|
exports.set_info_for_user = function (user_id, info, server_time) {
|
2017-03-30 20:04:01 +02:00
|
|
|
var status = status_from_timestamp(server_time, info);
|
|
|
|
exports.presence_info[user_id] = status;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.set_info = function (presences, server_timestamp) {
|
|
|
|
exports.presence_info = {};
|
|
|
|
_.each(presences, function (info, this_email) {
|
2018-09-08 14:25:06 +02:00
|
|
|
var person = people.get_by_email(this_email);
|
|
|
|
|
|
|
|
if (person === undefined) {
|
|
|
|
if (!(server_events.suspect_offline || reload_state.is_in_progress())) {
|
|
|
|
// If we're online, and we get a user who we don't
|
|
|
|
// know about in the presence data, throw an error.
|
|
|
|
blueslip.error('Unknown email in presence data: ' + this_email);
|
2017-03-30 20:04:01 +02:00
|
|
|
}
|
2018-09-08 14:25:06 +02:00
|
|
|
// Either way, we deal by skipping this user and
|
|
|
|
// rendering everyone else, to avoid disruption.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var user_id = person.user_id;
|
|
|
|
|
|
|
|
if (user_id) {
|
|
|
|
var status = status_from_timestamp(server_timestamp,
|
|
|
|
info);
|
|
|
|
exports.presence_info[user_id] = status;
|
2017-03-30 20:04:01 +02:00
|
|
|
}
|
|
|
|
});
|
2017-03-31 00:18:04 +02:00
|
|
|
exports.update_info_for_small_realm();
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.update_info_for_small_realm = function () {
|
|
|
|
if (people.get_realm_count() >= BIG_REALM_COUNT) {
|
|
|
|
// For big realms, we don't want to bloat our buddy
|
|
|
|
// lists with lots of long-time-inactive users.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For small realms, we create presence info for users
|
|
|
|
// that the server didn't include in its presence update.
|
|
|
|
var persons = people.get_realm_persons();
|
|
|
|
|
|
|
|
_.each(persons, function (person) {
|
|
|
|
var user_id = person.user_id;
|
2018-09-08 14:41:41 +02:00
|
|
|
var status = "offline";
|
2017-03-31 00:18:04 +02:00
|
|
|
|
2018-08-04 08:14:33 +02:00
|
|
|
if (exports.presence_info[user_id]) {
|
2017-03-31 00:18:04 +02:00
|
|
|
// this is normal, we have data for active
|
|
|
|
// users that we don't want to clobber.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-07 18:36:26 +02:00
|
|
|
if (person.is_bot) {
|
|
|
|
// we don't show presence for bots
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-08 14:41:41 +02:00
|
|
|
if (people.is_my_user_id(user_id)) {
|
|
|
|
status = "active";
|
|
|
|
}
|
|
|
|
|
2018-08-04 08:14:33 +02:00
|
|
|
exports.presence_info[user_id] = {
|
2018-09-08 14:41:41 +02:00
|
|
|
status: status,
|
2017-03-31 00:18:04 +02:00
|
|
|
mobile: false,
|
|
|
|
last_active: undefined,
|
|
|
|
};
|
|
|
|
});
|
2017-03-30 20:04:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.last_active_date = function (user_id) {
|
|
|
|
var info = exports.presence_info[user_id];
|
|
|
|
|
|
|
|
if (!info || !info.last_active) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var date = new XDate(info.last_active * 1000);
|
|
|
|
return date;
|
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.presence = exports;
|