2013-02-07 19:57:45 +01:00
|
|
|
var activity = (function () {
|
2013-02-12 20:40:28 +01:00
|
|
|
var exports = {};
|
2013-02-07 19:57:45 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Helpers for detecting user activity and managing user idle states
|
|
|
|
*/
|
|
|
|
|
2013-03-05 17:11:55 +01:00
|
|
|
/* After this amount of no activity, mark you idle regardless of your focus */
|
|
|
|
var DEFAULT_IDLE_TIMEOUT_MS = 30 * 60 * 1000;
|
|
|
|
/* Time between keep-alive pings */
|
2013-02-12 20:40:28 +01:00
|
|
|
var ACTIVE_PING_INTERVAL_MS = 60 * 1000;
|
|
|
|
|
|
|
|
/* Timeouts for away and idle state */
|
2013-03-05 17:11:55 +01:00
|
|
|
var AWAY_THRESHOLD_SECS = 10 * 60;
|
2013-02-12 20:40:28 +01:00
|
|
|
var IDLE_THRESHOLD_SECS = DEFAULT_IDLE_TIMEOUT_MS / 1000;
|
2013-02-07 19:57:45 +01:00
|
|
|
|
|
|
|
/* Keep in sync with views.py:json_update_active_status() */
|
|
|
|
var ACTIVE = "active";
|
|
|
|
var IDLE = "idle";
|
|
|
|
|
|
|
|
var has_focus = true;
|
|
|
|
var ping_timer;
|
|
|
|
|
2013-04-03 22:00:02 +02:00
|
|
|
var user_info = {};
|
|
|
|
|
2013-02-07 19:57:45 +01:00
|
|
|
function sort_users(users, user_info) {
|
|
|
|
// TODO sort by unread count first, once we support that
|
|
|
|
users.sort(function (a, b) {
|
2013-02-17 19:04:34 +01:00
|
|
|
if (user_info[a] === 'active' && user_info[b] !== 'active') {
|
2013-02-12 20:40:28 +01:00
|
|
|
return -1;
|
2013-02-17 19:04:34 +01:00
|
|
|
} else if (user_info[b] === 'active' && user_info[a] !== 'active') {
|
2013-02-12 20:40:28 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-02-17 19:04:34 +01:00
|
|
|
if (user_info[a] === 'away' && user_info[b] !== 'away') {
|
2013-02-07 19:57:45 +01:00
|
|
|
return -1;
|
2013-02-17 19:04:34 +01:00
|
|
|
} else if (user_info[b] === 'away' && user_info[a] !== 'away') {
|
2013-02-07 19:57:45 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort equivalent PM names alphabetically
|
2013-02-15 21:01:49 +01:00
|
|
|
var full_name_a = a;
|
|
|
|
var full_name_b = b;
|
|
|
|
if (people_dict[a] !== undefined) {
|
|
|
|
full_name_a = people_dict[a].full_name;
|
|
|
|
}
|
|
|
|
if (people_dict[b] !== undefined) {
|
|
|
|
full_name_b = people_dict[b].full_name;
|
|
|
|
}
|
|
|
|
return full_name_a.localeCompare(full_name_b);
|
2013-02-07 19:57:45 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return users;
|
|
|
|
}
|
|
|
|
|
|
|
|
function focus_lost() {
|
|
|
|
if (!has_focus) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
has_focus = false;
|
|
|
|
|
|
|
|
clearInterval(ping_timer);
|
|
|
|
ping_timer = undefined;
|
|
|
|
|
|
|
|
$.post('/json/update_active_status', {status: IDLE});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-03 22:00:02 +02:00
|
|
|
function update_users() {
|
|
|
|
var users = sort_users(Object.keys(user_info), user_info);
|
|
|
|
ui.set_presence_list(users, user_info);
|
|
|
|
}
|
|
|
|
|
2013-05-06 17:14:59 +02:00
|
|
|
function status_from_timestamp(baseline_time, presence) {
|
2013-04-05 00:13:03 +02:00
|
|
|
if (presence.website === undefined) {
|
|
|
|
return 'idle';
|
|
|
|
}
|
|
|
|
|
2013-05-06 17:14:59 +02:00
|
|
|
var age = baseline_time - presence.website.timestamp;
|
2013-04-05 00:13:03 +02:00
|
|
|
|
|
|
|
var status = 'idle';
|
|
|
|
if (presence.website.status === ACTIVE && age >= 0) {
|
|
|
|
if (age < AWAY_THRESHOLD_SECS) {
|
|
|
|
status = 'active';
|
|
|
|
} else if (age < IDLE_THRESHOLD_SECS) {
|
|
|
|
status = 'away';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2013-02-07 19:57:45 +01:00
|
|
|
function focus_ping() {
|
|
|
|
if (!has_focus) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$.post('/json/update_active_status', {status: ACTIVE}, function (data) {
|
2013-04-03 22:00:02 +02:00
|
|
|
user_info = {};
|
2013-02-07 19:57:45 +01:00
|
|
|
|
2013-03-13 20:02:57 +01:00
|
|
|
// Update Zephyr mirror activity warning
|
|
|
|
if (data.zephyr_mirror_active === false) {
|
|
|
|
$('#zephyr-mirror-error').show();
|
|
|
|
} else {
|
|
|
|
$('#zephyr-mirror-error').hide();
|
|
|
|
}
|
|
|
|
|
2013-02-07 19:57:45 +01:00
|
|
|
// Ping returns the active peer list
|
|
|
|
$.each(data.presences, function (this_email, presence) {
|
2013-03-25 23:26:14 +01:00
|
|
|
if (page_params.email !== this_email) {
|
2013-05-06 17:14:59 +02:00
|
|
|
user_info[this_email] = status_from_timestamp(data.server_timestamp, presence);
|
2013-02-07 19:57:45 +01:00
|
|
|
}
|
|
|
|
});
|
2013-04-03 22:00:02 +02:00
|
|
|
update_users();
|
2013-02-07 19:57:45 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function focus_gained() {
|
|
|
|
if (!has_focus) {
|
|
|
|
has_focus = true;
|
2013-02-12 20:40:28 +01:00
|
|
|
ping_timer = setInterval(focus_ping, ACTIVE_PING_INTERVAL_MS);
|
2013-02-07 19:57:45 +01:00
|
|
|
|
|
|
|
focus_ping();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.initialize = function () {
|
|
|
|
$(window).focus(focus_gained);
|
2013-02-12 20:40:28 +01:00
|
|
|
$(window).idle({idle: DEFAULT_IDLE_TIMEOUT_MS,
|
2013-02-07 19:57:45 +01:00
|
|
|
onIdle: focus_lost,
|
|
|
|
onActive: focus_gained,
|
|
|
|
keepTracking: true});
|
|
|
|
|
2013-02-12 20:40:28 +01:00
|
|
|
ping_timer = setInterval(focus_ping, ACTIVE_PING_INTERVAL_MS);
|
2013-04-05 00:13:03 +02:00
|
|
|
|
|
|
|
focus_ping();
|
2013-04-03 22:00:02 +02:00
|
|
|
};
|
|
|
|
|
2013-05-06 17:14:59 +02:00
|
|
|
exports.set_user_status = function (user_email, presence, server_time) {
|
2013-04-03 22:00:02 +02:00
|
|
|
if (user_email === page_params.email) {
|
|
|
|
return;
|
|
|
|
}
|
2013-05-06 17:14:59 +02:00
|
|
|
user_info[user_email] = status_from_timestamp(server_time, presence);
|
2013-02-07 19:57:45 +01:00
|
|
|
|
2013-04-03 22:00:02 +02:00
|
|
|
update_users();
|
2013-02-07 19:57:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|