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-08-20 20:57:26 +02:00
|
|
|
/* Broadcast "idle" to server after 5 minutes of local inactivity */
|
|
|
|
var DEFAULT_IDLE_TIMEOUT_MS = 5 * 60 * 1000;
|
2013-03-05 17:11:55 +01:00
|
|
|
/* Time between keep-alive pings */
|
2013-08-21 23:40:12 +02:00
|
|
|
var ACTIVE_PING_INTERVAL_MS = 50 * 1000;
|
2013-02-12 20:40:28 +01:00
|
|
|
|
2013-10-23 19:02:18 +02:00
|
|
|
var presence_descriptions = {
|
|
|
|
active: 'is active',
|
2017-01-12 00:17:43 +01:00
|
|
|
idle: 'is not active',
|
2013-10-23 19:02:18 +02:00
|
|
|
};
|
|
|
|
|
2016-04-03 08:01:10 +02:00
|
|
|
/* Keep in sync with views.py:update_active_status_backend() */
|
2013-08-20 21:18:56 +02:00
|
|
|
exports.ACTIVE = "active";
|
|
|
|
exports.IDLE = "idle";
|
2013-02-07 19:57:45 +01:00
|
|
|
|
2017-10-03 03:16:37 +02:00
|
|
|
var meta = {};
|
|
|
|
|
2013-09-16 18:22:52 +02:00
|
|
|
// When you start Zulip, has_focus should be true, but it might not be the
|
|
|
|
// case after a server-initiated reload.
|
|
|
|
exports.has_focus = document.hasFocus && document.hasFocus();
|
2013-02-07 19:57:45 +01:00
|
|
|
|
2013-09-06 21:52:12 +02:00
|
|
|
// We initialize this to true, to count new page loads, but set it to
|
|
|
|
// false in the onload function in reload.js if this was a
|
|
|
|
// server-initiated-reload to avoid counting a server-initiated reload
|
|
|
|
// as user activity.
|
|
|
|
exports.new_user_input = true;
|
|
|
|
|
2013-10-23 20:44:31 +02:00
|
|
|
var huddle_timestamps = new Dict();
|
|
|
|
|
2017-09-27 20:41:19 +02:00
|
|
|
exports.update_scrollbar = (function () {
|
|
|
|
var $user_presences = $("#user_presences");
|
|
|
|
var $group_pms = $("#group-pms");
|
|
|
|
|
|
|
|
return {
|
|
|
|
users: function () {
|
2017-09-29 02:14:22 +02:00
|
|
|
if (!$user_presences.length) {
|
|
|
|
$user_presences = $("#user_presences");
|
|
|
|
}
|
2017-09-27 20:41:19 +02:00
|
|
|
ui.update_scrollbar($user_presences);
|
|
|
|
},
|
|
|
|
group_pms: function () {
|
2017-09-29 02:14:22 +02:00
|
|
|
if (!$group_pms.length) {
|
|
|
|
$group_pms = $("#group-pms");
|
|
|
|
}
|
2017-09-27 20:41:19 +02:00
|
|
|
ui.update_scrollbar($group_pms);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}());
|
|
|
|
|
2017-05-24 20:00:04 +02:00
|
|
|
function update_pm_count_in_dom(count_span, value_span, count) {
|
|
|
|
var li = count_span.parent();
|
|
|
|
|
2016-11-11 20:48:13 +01:00
|
|
|
if (count === 0) {
|
|
|
|
count_span.hide();
|
2017-05-24 20:00:04 +02:00
|
|
|
li.removeClass("user-with-count");
|
2016-11-11 20:48:13 +01:00
|
|
|
value_span.text('');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
count_span.show();
|
2017-05-24 20:00:04 +02:00
|
|
|
li.addClass("user-with-count");
|
|
|
|
value_span.text(count);
|
|
|
|
}
|
2016-11-11 20:48:13 +01:00
|
|
|
|
2017-05-24 20:00:04 +02:00
|
|
|
function update_group_count_in_dom(count_span, value_span, count) {
|
|
|
|
var li = count_span.parent();
|
|
|
|
|
|
|
|
if (count === 0) {
|
|
|
|
count_span.hide();
|
|
|
|
li.removeClass("group-with-count");
|
|
|
|
value_span.text('');
|
|
|
|
return;
|
2016-11-11 20:48:13 +01:00
|
|
|
}
|
2017-05-24 20:00:04 +02:00
|
|
|
|
|
|
|
count_span.show();
|
|
|
|
li.addClass("group-with-count");
|
2016-11-11 20:48:13 +01:00
|
|
|
value_span.text(count);
|
|
|
|
}
|
|
|
|
|
2017-05-24 20:00:04 +02:00
|
|
|
function get_pm_list_item(user_id) {
|
2017-03-17 19:38:24 +01:00
|
|
|
return $("li.user_sidebar_entry[data-user-id='" + user_id + "']");
|
|
|
|
}
|
|
|
|
|
2017-05-24 20:00:04 +02:00
|
|
|
function get_group_list_item(user_ids_string) {
|
2016-12-02 21:34:35 +01:00
|
|
|
return $("li.group-pms-sidebar-entry[data-user-ids='" + user_ids_string + "']");
|
2016-11-11 20:48:13 +01:00
|
|
|
}
|
|
|
|
|
2017-05-24 20:00:04 +02:00
|
|
|
function set_pm_count(user_ids_string, count) {
|
|
|
|
var count_span = get_pm_list_item(user_ids_string).find('.count');
|
2016-11-11 20:48:13 +01:00
|
|
|
var value_span = count_span.find('.value');
|
2017-05-24 20:00:04 +02:00
|
|
|
update_pm_count_in_dom(count_span, value_span, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
function set_group_count(user_ids_string, count) {
|
|
|
|
var count_span = get_group_list_item(user_ids_string).find('.count');
|
|
|
|
var value_span = count_span.find('.value');
|
|
|
|
update_group_count_in_dom(count_span, value_span, count);
|
2016-11-11 20:48:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.update_dom_with_unread_counts = function (counts) {
|
|
|
|
// counts is just a data object that gets calculated elsewhere
|
|
|
|
// Our job is to update some DOM elements.
|
|
|
|
|
2016-11-16 00:09:09 +01:00
|
|
|
counts.pm_count.each(function (count, user_ids_string) {
|
|
|
|
// TODO: just use user_ids_string in our markup
|
2017-05-24 20:00:04 +02:00
|
|
|
var is_pm = user_ids_string.indexOf(',') < 0;
|
|
|
|
if (is_pm) {
|
|
|
|
set_pm_count(user_ids_string, count);
|
|
|
|
} else {
|
|
|
|
set_group_count(user_ids_string, count);
|
|
|
|
}
|
2016-11-11 20:48:13 +01:00
|
|
|
});
|
|
|
|
};
|
2014-02-27 01:46:17 +01:00
|
|
|
|
2013-10-23 20:44:31 +02:00
|
|
|
exports.process_loaded_messages = function (messages) {
|
2016-11-11 19:30:04 +01:00
|
|
|
var need_resize = false;
|
|
|
|
|
2013-10-23 20:44:31 +02:00
|
|
|
_.each(messages, function (message) {
|
2017-02-05 00:22:16 +01:00
|
|
|
var huddle_string = people.huddle_string(message);
|
2016-11-17 23:16:29 +01:00
|
|
|
|
2017-02-05 00:22:16 +01:00
|
|
|
if (huddle_string) {
|
|
|
|
var old_timestamp = huddle_timestamps.get(huddle_string);
|
2016-11-17 23:16:29 +01:00
|
|
|
|
2017-02-05 00:22:16 +01:00
|
|
|
if (!old_timestamp || (old_timestamp < message.timestamp)) {
|
|
|
|
huddle_timestamps.set(huddle_string, message.timestamp);
|
|
|
|
need_resize = true;
|
2013-10-23 20:44:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-11-11 18:00:09 +01:00
|
|
|
|
|
|
|
exports.update_huddles();
|
2016-11-11 19:30:04 +01:00
|
|
|
|
|
|
|
if (need_resize) {
|
|
|
|
resize.resize_page_components(); // big hammer
|
|
|
|
}
|
2013-10-23 20:44:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_huddles = function () {
|
|
|
|
var huddles = huddle_timestamps.keys();
|
|
|
|
huddles = _.sortBy(huddles, function (huddle) {
|
|
|
|
return huddle_timestamps.get(huddle);
|
|
|
|
});
|
|
|
|
return huddles.reverse();
|
|
|
|
};
|
|
|
|
|
2013-10-23 22:22:34 +02:00
|
|
|
exports.full_huddle_name = function (huddle) {
|
2016-11-17 23:16:29 +01:00
|
|
|
var user_ids = huddle.split(',');
|
2013-10-23 22:22:34 +02:00
|
|
|
|
2016-11-17 23:16:29 +01:00
|
|
|
var names = _.map(user_ids, function (user_id) {
|
|
|
|
var person = people.get_person_from_user_id(user_id);
|
|
|
|
return person.full_name;
|
2013-10-23 22:22:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return names.join(', ');
|
|
|
|
};
|
|
|
|
|
2013-11-04 22:59:03 +01:00
|
|
|
exports.short_huddle_name = function (huddle) {
|
2016-11-17 23:16:29 +01:00
|
|
|
var user_ids = huddle.split(',');
|
2013-11-04 22:59:03 +01:00
|
|
|
|
2013-12-05 22:20:20 +01:00
|
|
|
var num_to_show = 3;
|
2016-11-23 17:25:43 +01:00
|
|
|
var names = _.map(user_ids, function (user_id) {
|
2016-11-17 23:16:29 +01:00
|
|
|
var person = people.get_person_from_user_id(user_id);
|
|
|
|
return person.full_name;
|
2013-11-04 22:59:03 +01:00
|
|
|
});
|
2016-11-23 17:25:43 +01:00
|
|
|
|
|
|
|
names = _.sortBy(names, function (name) { return name.toLowerCase(); });
|
|
|
|
names = names.slice(0, num_to_show);
|
2016-11-17 23:16:29 +01:00
|
|
|
var others = user_ids.length - num_to_show;
|
2013-11-04 22:59:03 +01:00
|
|
|
|
|
|
|
if (others === 1) {
|
|
|
|
names.push("+ 1 other");
|
|
|
|
} else if (others >= 2) {
|
|
|
|
names.push("+ " + others + " others");
|
|
|
|
}
|
|
|
|
|
|
|
|
return names.join(', ');
|
|
|
|
};
|
|
|
|
|
2017-03-06 16:14:52 +01:00
|
|
|
exports.huddle_fraction_present = function (huddle) {
|
2016-11-17 23:16:29 +01:00
|
|
|
var user_ids = huddle.split(',');
|
2013-11-04 21:56:56 +01:00
|
|
|
|
|
|
|
var num_present = 0;
|
2016-11-17 23:16:29 +01:00
|
|
|
_.each(user_ids, function (user_id) {
|
2017-03-30 20:04:01 +02:00
|
|
|
if (presence.is_not_offline(user_id)) {
|
|
|
|
num_present += 1;
|
2013-11-04 21:56:56 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-11-17 23:16:29 +01:00
|
|
|
var ratio = num_present / user_ids.length;
|
2013-11-04 21:56:56 +01:00
|
|
|
|
|
|
|
return ratio.toFixed(2);
|
|
|
|
};
|
|
|
|
|
2017-03-06 16:14:52 +01:00
|
|
|
function compare_function(a, b) {
|
2013-02-12 20:40:28 +01:00
|
|
|
|
2017-03-08 19:13:25 +01:00
|
|
|
function level(status) {
|
|
|
|
switch (status) {
|
|
|
|
case 'active':
|
|
|
|
return 1;
|
|
|
|
case 'idle':
|
|
|
|
return 2;
|
|
|
|
default:
|
|
|
|
return 3;
|
|
|
|
}
|
2017-03-06 16:14:52 +01:00
|
|
|
}
|
2013-02-07 19:57:45 +01:00
|
|
|
|
2017-03-30 20:04:01 +02:00
|
|
|
var level_a = level(presence.get_status(a));
|
|
|
|
var level_b = level(presence.get_status(b));
|
2017-03-08 19:13:25 +01:00
|
|
|
var diff = level_a - level_b;
|
|
|
|
if (diff !== 0) {
|
|
|
|
return diff;
|
2017-03-06 16:14:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sort equivalent PM names alphabetically
|
2017-03-08 19:02:39 +01:00
|
|
|
var person_a = people.get_person_from_user_id(a);
|
|
|
|
var person_b = people.get_person_from_user_id(b);
|
|
|
|
|
|
|
|
var full_name_a = person_a ? person_a.full_name : '';
|
|
|
|
var full_name_b = person_b ? person_b.full_name : '';
|
|
|
|
|
2017-03-06 16:14:52 +01:00
|
|
|
return util.strcmp(full_name_a, full_name_b);
|
2017-02-25 19:59:29 +01:00
|
|
|
}
|
2013-02-07 19:57:45 +01:00
|
|
|
|
2017-03-06 16:14:52 +01:00
|
|
|
function sort_users(user_ids) {
|
2017-02-25 19:59:29 +01:00
|
|
|
// TODO sort by unread count first, once we support that
|
2017-03-06 16:14:52 +01:00
|
|
|
user_ids.sort(compare_function);
|
2016-11-17 21:43:12 +01:00
|
|
|
return user_ids;
|
2013-02-07 19:57:45 +01:00
|
|
|
}
|
|
|
|
|
2013-08-09 14:42:49 +02:00
|
|
|
// for testing:
|
|
|
|
exports._sort_users = sort_users;
|
|
|
|
|
2013-02-07 19:57:45 +01:00
|
|
|
function focus_lost() {
|
2017-03-06 15:42:58 +01:00
|
|
|
// When we become idle, we don't immediately send anything to the
|
|
|
|
// server; instead, we wait for our next periodic update, since
|
|
|
|
// this data is fundamentally not timely.
|
2013-08-20 21:18:56 +02:00
|
|
|
exports.has_focus = false;
|
2013-02-07 19:57:45 +01:00
|
|
|
}
|
|
|
|
|
2016-11-17 21:43:12 +01:00
|
|
|
function filter_user_ids(user_ids) {
|
2017-10-03 03:16:37 +02:00
|
|
|
var user_list = meta.$user_list_filter;
|
2014-02-28 16:19:26 +01:00
|
|
|
if (user_list.length === 0) {
|
|
|
|
// We may have received an activity ping response after
|
|
|
|
// initiating a reload, in which case the user list may no
|
2016-01-19 21:03:56 +01:00
|
|
|
// longer be available.
|
|
|
|
// Return user list: useful for testing user list performance fix
|
2016-11-17 21:43:12 +01:00
|
|
|
return user_ids;
|
2014-02-28 16:19:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var search_term = user_list.expectOne().val().trim();
|
2014-01-13 19:34:24 +01:00
|
|
|
if (search_term === '') {
|
2016-11-17 21:43:12 +01:00
|
|
|
return user_ids;
|
2014-01-13 19:34:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var search_terms = search_term.toLowerCase().split(",");
|
|
|
|
search_terms = _.map(search_terms, function (s) {
|
|
|
|
return s.trim();
|
|
|
|
});
|
|
|
|
|
2016-11-17 21:43:12 +01:00
|
|
|
var persons = _.map(user_ids, function (user_id) {
|
|
|
|
return people.get_person_from_user_id(user_id);
|
2014-01-13 19:34:24 +01:00
|
|
|
});
|
|
|
|
|
2017-01-26 13:00:27 +01:00
|
|
|
var user_id_dict = people.filter_people_by_search_terms(persons, search_terms);
|
|
|
|
return user_id_dict.keys();
|
2014-01-13 19:34:24 +01:00
|
|
|
}
|
|
|
|
|
2017-02-26 20:00:17 +01:00
|
|
|
function matches_filter(user_id) {
|
|
|
|
// This is a roundabout way of checking a user if you look
|
|
|
|
// too hard at it, but it should be fine for now.
|
|
|
|
return (filter_user_ids([user_id]).length === 1);
|
|
|
|
}
|
|
|
|
|
2017-03-30 20:04:01 +02:00
|
|
|
function filter_and_sort(user_ids) {
|
2016-11-17 21:43:12 +01:00
|
|
|
user_ids = filter_user_ids(user_ids);
|
2017-03-06 16:14:52 +01:00
|
|
|
user_ids = sort_users(user_ids);
|
2016-11-17 21:43:12 +01:00
|
|
|
return user_ids;
|
2016-01-19 21:03:56 +01:00
|
|
|
}
|
2016-11-14 23:52:03 +01:00
|
|
|
|
2016-01-19 21:03:56 +01:00
|
|
|
exports._filter_and_sort = filter_and_sort;
|
|
|
|
|
2017-01-23 19:17:16 +01:00
|
|
|
function get_num_unread(user_id) {
|
|
|
|
if (unread.suppress_unread_counts) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return unread.num_unread_for_person(user_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
function info_for(user_id) {
|
2017-03-30 20:04:01 +02:00
|
|
|
var status = presence.get_status(user_id);
|
2017-01-23 19:17:16 +01:00
|
|
|
var person = people.get_person_from_user_id(user_id);
|
2017-10-03 03:18:49 +02:00
|
|
|
|
|
|
|
// if the user is you or a bot, do not show in presence data.
|
|
|
|
if (person.is_bot || person.user_id === page_params.user_id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-23 19:17:16 +01:00
|
|
|
return {
|
|
|
|
href: narrow.pm_with_uri(person.email),
|
|
|
|
name: person.full_name,
|
|
|
|
user_id: user_id,
|
|
|
|
num_unread: get_num_unread(user_id),
|
2017-03-30 20:04:01 +02:00
|
|
|
type: status,
|
|
|
|
type_desc: presence_descriptions[status],
|
2017-01-23 19:17:16 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-26 20:00:17 +01:00
|
|
|
exports.insert_user_into_list = function (user_id) {
|
2017-02-28 23:34:14 +01:00
|
|
|
if (page_params.realm_presence_disabled) {
|
2016-07-27 02:09:10 +02:00
|
|
|
return;
|
2016-01-19 21:03:56 +01:00
|
|
|
}
|
|
|
|
|
2017-02-26 20:00:17 +01:00
|
|
|
if (!matches_filter(user_id)) {
|
2017-02-21 00:53:08 +01:00
|
|
|
return;
|
2016-01-19 21:03:56 +01:00
|
|
|
}
|
2017-02-21 00:53:08 +01:00
|
|
|
|
2017-02-26 20:00:17 +01:00
|
|
|
var info = info_for(user_id);
|
|
|
|
$('#user_presences').find('[data-user-id="' + user_id + '"]').remove();
|
|
|
|
var html = templates.render('user_presence_row', info);
|
2013-10-23 19:12:40 +02:00
|
|
|
|
2017-02-26 20:00:17 +01:00
|
|
|
var items = $('#user_presences li').toArray();
|
2017-02-21 00:53:08 +01:00
|
|
|
|
2017-02-26 20:00:17 +01:00
|
|
|
function insert() {
|
|
|
|
var i = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < items.length; i += 1) {
|
|
|
|
var li = $(items[i]);
|
|
|
|
var list_user_id = li.attr('data-user-id');
|
2017-03-06 16:14:52 +01:00
|
|
|
if (compare_function(user_id, list_user_id) < 0) {
|
2017-02-26 20:00:17 +01:00
|
|
|
li.before(html);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$('#user_presences').append(html);
|
|
|
|
}
|
|
|
|
|
|
|
|
insert();
|
2017-09-27 20:41:19 +02:00
|
|
|
exports.update_scrollbar.users();
|
2017-02-21 00:53:08 +01:00
|
|
|
|
2017-05-24 20:00:04 +02:00
|
|
|
var elt = get_pm_list_item(user_id);
|
2017-02-26 23:53:19 +01:00
|
|
|
compose_fade.update_one_user_row(elt);
|
2017-02-21 00:53:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.build_user_sidebar = function () {
|
2017-02-28 23:34:14 +01:00
|
|
|
if (page_params.realm_presence_disabled) {
|
2017-02-21 00:53:08 +01:00
|
|
|
return;
|
2016-01-19 21:03:56 +01:00
|
|
|
}
|
2013-10-23 19:02:18 +02:00
|
|
|
|
2017-10-03 03:18:49 +02:00
|
|
|
var user_ids;
|
2017-02-21 00:53:08 +01:00
|
|
|
|
2017-10-03 03:18:49 +02:00
|
|
|
if (meta.$user_list_filter.val().length) {
|
|
|
|
user_ids = filter_and_sort(presence.get_user_ids());
|
|
|
|
} else {
|
|
|
|
user_ids = filter_and_sort(people.get_realm_persons().map(function (person) {
|
|
|
|
return person.user_id;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
var user_info = _.map(user_ids, info_for).filter(function (person) {
|
|
|
|
// filtered bots and yourself are set to "undefined" in the `info_for`
|
|
|
|
// function.
|
|
|
|
return typeof person !== "undefined";
|
|
|
|
});
|
2017-02-21 00:53:08 +01:00
|
|
|
var html = templates.render('user_presence_rows', {users: user_info});
|
|
|
|
$('#user_presences').html(html);
|
|
|
|
|
2013-10-23 19:02:18 +02:00
|
|
|
// Update user fading, if necessary.
|
|
|
|
compose_fade.update_faded_users();
|
2016-01-19 21:03:56 +01:00
|
|
|
|
2017-02-26 20:00:17 +01:00
|
|
|
resize.resize_page_components();
|
|
|
|
|
2017-02-21 00:53:08 +01:00
|
|
|
return user_info; // for testing
|
2016-03-20 07:03:00 +01:00
|
|
|
};
|
2014-01-13 19:34:24 +01:00
|
|
|
|
2017-09-29 02:11:47 +02:00
|
|
|
var update_users_for_search = _.throttle(exports.build_user_sidebar, 50);
|
2014-01-13 19:34:24 +01:00
|
|
|
|
2016-12-05 07:02:18 +01:00
|
|
|
function show_huddles() {
|
2017-01-04 19:34:55 +01:00
|
|
|
$('#group-pm-list').addClass("show");
|
2016-11-11 19:30:04 +01:00
|
|
|
}
|
|
|
|
|
2016-12-05 07:02:18 +01:00
|
|
|
function hide_huddles() {
|
2017-01-04 19:34:55 +01:00
|
|
|
$('#group-pm-list').removeClass("show");
|
2016-11-11 19:30:04 +01:00
|
|
|
}
|
|
|
|
|
2013-10-18 00:56:49 +02:00
|
|
|
exports.update_huddles = function () {
|
2017-02-28 23:34:14 +01:00
|
|
|
if (page_params.realm_presence_disabled) {
|
2016-07-27 02:09:10 +02:00
|
|
|
return;
|
2013-11-28 22:13:01 +01:00
|
|
|
}
|
|
|
|
|
2013-10-18 00:56:49 +02:00
|
|
|
var huddles = exports.get_huddles().slice(0, 10);
|
|
|
|
|
2013-12-03 16:44:42 +01:00
|
|
|
if (huddles.length === 0) {
|
2016-11-11 19:30:04 +01:00
|
|
|
hide_huddles();
|
2013-10-18 00:56:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var group_pms = _.map(huddles, function (huddle) {
|
|
|
|
return {
|
2016-11-17 23:16:29 +01:00
|
|
|
user_ids_string: huddle,
|
2013-10-18 00:56:49 +02:00
|
|
|
name: exports.full_huddle_name(huddle),
|
2017-01-06 15:01:14 +01:00
|
|
|
href: narrow.huddle_with_uri(huddle),
|
2017-03-06 16:14:52 +01:00
|
|
|
fraction_present: exports.huddle_fraction_present(huddle),
|
2017-01-12 00:17:43 +01:00
|
|
|
short_name: exports.short_huddle_name(huddle),
|
2013-10-18 00:56:49 +02:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
var html = templates.render('group_pms', {group_pms: group_pms});
|
|
|
|
$('#group-pms').expectOne().html(html);
|
2013-11-08 00:53:12 +01:00
|
|
|
|
2016-11-17 23:16:29 +01:00
|
|
|
_.each(huddles, function (user_ids_string) {
|
2016-11-18 17:02:06 +01:00
|
|
|
var count = unread.num_unread_for_person(user_ids_string);
|
2017-05-24 20:00:04 +02:00
|
|
|
set_group_count(user_ids_string, count);
|
2013-11-08 00:53:12 +01:00
|
|
|
});
|
|
|
|
|
2016-11-11 19:30:04 +01:00
|
|
|
show_huddles();
|
2017-09-27 20:41:19 +02:00
|
|
|
exports.update_scrollbar.group_pms();
|
2013-10-18 00:56:49 +02:00
|
|
|
};
|
|
|
|
|
2014-02-27 01:46:17 +01:00
|
|
|
|
2017-03-06 15:24:43 +01:00
|
|
|
function focus_ping(want_redraw) {
|
2014-01-07 23:41:55 +01:00
|
|
|
channel.post({
|
2016-04-03 07:58:06 +02:00
|
|
|
url: '/json/users/me/presence',
|
2014-01-07 23:41:55 +01:00
|
|
|
data: {status: (exports.has_focus) ? exports.ACTIVE : exports.IDLE,
|
2017-03-31 01:46:45 +02:00
|
|
|
ping_only: !want_redraw,
|
2014-01-07 23:41:55 +01:00
|
|
|
new_user_input: exports.new_user_input},
|
|
|
|
idempotent: true,
|
|
|
|
success: function (data) {
|
|
|
|
|
|
|
|
// Update Zephyr mirror activity warning
|
|
|
|
if (data.zephyr_mirror_active === false) {
|
2017-04-05 23:33:14 +02:00
|
|
|
$('#zephyr-mirror-error').addClass("show");
|
2014-01-07 23:41:55 +01:00
|
|
|
} else {
|
2017-04-05 23:33:14 +02:00
|
|
|
$('#zephyr-mirror-error').removeClass("show");
|
2014-01-07 23:41:55 +01:00
|
|
|
}
|
2013-06-12 19:58:25 +02:00
|
|
|
|
2014-01-07 23:41:55 +01:00
|
|
|
exports.new_user_input = false;
|
2013-02-07 19:57:45 +01:00
|
|
|
|
2017-05-04 02:20:25 +02:00
|
|
|
// Zulip has 2 data feeds coming from the server to the
|
|
|
|
// client: The server_events data, and this presence feed.
|
|
|
|
// Everything in server_events is nicely serialized, but
|
|
|
|
// if we've been offline and not running for a while
|
|
|
|
// (e.g. due to suspend), we can end up throwing
|
|
|
|
// exceptions due to users appearing in presence that we
|
|
|
|
// haven't learned about yet. We handle this in 2 stages.
|
|
|
|
// First, here, we make sure that we've confirmed whether
|
|
|
|
// we are indeed in the unsuspend case. Then, in
|
|
|
|
// `presence.set_info`, we only complain about unknown
|
|
|
|
// users if server_events does not suspect we're offline.
|
|
|
|
server_events.check_for_unsuspend();
|
|
|
|
|
2017-03-06 15:24:43 +01:00
|
|
|
if (want_redraw) {
|
2017-03-30 20:04:01 +02:00
|
|
|
presence.set_info(data.presences, data.server_timestamp);
|
2017-03-06 15:24:43 +01:00
|
|
|
exports.build_user_sidebar();
|
|
|
|
exports.update_huddles();
|
|
|
|
}
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2013-02-07 19:57:45 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function focus_gained() {
|
2013-08-20 21:18:56 +02:00
|
|
|
if (!exports.has_focus) {
|
|
|
|
exports.has_focus = true;
|
2017-03-06 15:24:43 +01:00
|
|
|
focus_ping(false);
|
2013-02-07 19:57:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.initialize = function () {
|
2017-05-24 22:32:17 +02:00
|
|
|
$("html").on("mousemove", function () {
|
|
|
|
exports.new_user_input = true;
|
|
|
|
});
|
|
|
|
|
2013-02-07 19:57:45 +01:00
|
|
|
$(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});
|
|
|
|
|
2017-04-24 21:23:50 +02:00
|
|
|
presence.set_info(page_params.presences,
|
|
|
|
page_params.initial_servertime);
|
2017-04-29 06:30:56 +02:00
|
|
|
delete page_params.presences;
|
2017-04-24 21:23:50 +02:00
|
|
|
|
2017-02-21 02:10:54 +01:00
|
|
|
exports.build_user_sidebar();
|
|
|
|
exports.update_huddles();
|
2017-03-06 15:24:43 +01:00
|
|
|
|
|
|
|
// Let the server know we're here, but pass "false" for
|
|
|
|
// want_redraw, since we just got all this info in page_params.
|
|
|
|
focus_ping(false);
|
|
|
|
|
|
|
|
function get_full_presence_list_update() {
|
|
|
|
focus_ping(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
setInterval(get_full_presence_list_update, ACTIVE_PING_INTERVAL_MS);
|
2017-09-27 20:41:19 +02:00
|
|
|
|
|
|
|
ui.set_up_scrollbar($("#user_presences"));
|
|
|
|
ui.set_up_scrollbar($("#group-pms"));
|
2013-04-03 22:00:02 +02:00
|
|
|
};
|
|
|
|
|
2017-03-30 20:04:01 +02:00
|
|
|
exports.set_user_status = function (email, info, server_time) {
|
2017-02-26 17:10:36 +01:00
|
|
|
if (people.is_current_user(email)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-30 20:04:01 +02:00
|
|
|
|
2017-02-26 17:10:36 +01:00
|
|
|
var user_id = people.get_user_id(email);
|
2017-03-30 20:04:01 +02:00
|
|
|
if (!user_id) {
|
2017-02-26 17:10:36 +01:00
|
|
|
blueslip.warn('unknown email: ' + email);
|
2017-03-30 20:04:01 +02:00
|
|
|
return;
|
2017-02-26 17:10:36 +01:00
|
|
|
}
|
2013-02-07 19:57:45 +01:00
|
|
|
|
2017-03-30 20:04:01 +02:00
|
|
|
presence.set_user_status(user_id, info, server_time);
|
|
|
|
exports.insert_user_into_list(user_id);
|
2013-10-18 00:56:49 +02:00
|
|
|
exports.update_huddles();
|
2013-02-07 19:57:45 +01:00
|
|
|
};
|
|
|
|
|
2017-01-04 23:54:59 +01:00
|
|
|
exports.redraw = function () {
|
2017-02-21 00:53:08 +01:00
|
|
|
exports.build_user_sidebar();
|
2017-01-04 23:54:59 +01:00
|
|
|
exports.update_huddles();
|
|
|
|
};
|
|
|
|
|
2014-01-14 17:36:52 +01:00
|
|
|
exports.searching = function () {
|
|
|
|
return $('.user-list-filter').expectOne().is(':focus');
|
|
|
|
};
|
|
|
|
|
2017-02-18 17:08:28 +01:00
|
|
|
function update_clear_search_button() {
|
|
|
|
var focused = $('.user-list-filter').is(':focus');
|
|
|
|
|
|
|
|
// Show button iff the search input is focused, or has non-empty contents
|
|
|
|
if (focused || $('.user-list-filter').val()) {
|
2017-06-30 00:57:46 +02:00
|
|
|
$('#clear_search_people_button').prop('disabled', false);
|
2017-02-18 17:08:28 +01:00
|
|
|
} else {
|
|
|
|
$('#clear_search_people_button').attr('disabled', 'disabled');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-14 22:36:20 +01:00
|
|
|
exports.escape_search = function () {
|
|
|
|
var filter = $('.user-list-filter').expectOne();
|
|
|
|
if (filter.val() === '') {
|
|
|
|
filter.blur();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
filter.val('');
|
2017-02-18 17:08:28 +01:00
|
|
|
update_clear_search_button();
|
2014-01-14 17:36:52 +01:00
|
|
|
update_users_for_search();
|
|
|
|
};
|
2014-01-13 19:34:24 +01:00
|
|
|
|
2014-02-14 22:49:26 +01:00
|
|
|
exports.initiate_search = function () {
|
|
|
|
var filter = $('.user-list-filter').expectOne();
|
|
|
|
filter.focus();
|
|
|
|
};
|
|
|
|
|
2014-01-14 17:57:34 +01:00
|
|
|
exports.blur_search = function () {
|
|
|
|
$('.user-list-filter').blur();
|
2017-02-18 17:08:28 +01:00
|
|
|
update_clear_search_button();
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.clear_search = function () {
|
|
|
|
$('.user-list-filter').val('');
|
|
|
|
$('.user-list-filter').blur();
|
|
|
|
update_clear_search_button();
|
|
|
|
update_users_for_search();
|
2014-01-14 17:57:34 +01:00
|
|
|
};
|
|
|
|
|
2016-12-05 07:02:18 +01:00
|
|
|
function maybe_select_person(e) {
|
2014-02-13 23:08:41 +01:00
|
|
|
if (e.keyCode === 13) {
|
|
|
|
// Enter key was pressed
|
|
|
|
|
|
|
|
// Prevent a newline from being entered into the soon-to-be-opened composebox
|
|
|
|
e.preventDefault();
|
2017-04-19 09:53:42 +02:00
|
|
|
e.stopPropagation();
|
2014-02-13 23:08:41 +01:00
|
|
|
|
2016-11-17 23:16:29 +01:00
|
|
|
var topPerson = $('#user_presences li.user_sidebar_entry').first().attr('data-user-id');
|
2017-10-03 03:16:37 +02:00
|
|
|
var user_list = meta.$user_list_filter;
|
2016-12-09 20:42:28 +01:00
|
|
|
var search_term = user_list.expectOne().val().trim();
|
|
|
|
if ((topPerson !== undefined) && (search_term !== '')) {
|
2014-02-13 23:08:41 +01:00
|
|
|
// undefined if there are no results
|
2016-11-17 23:16:29 +01:00
|
|
|
var email = people.get_person_from_user_id(topPerson).email;
|
2017-03-07 04:22:58 +01:00
|
|
|
narrow.by('pm-with', email, {select_first_unread: true, trigger: 'user sidebar'});
|
2017-03-18 17:41:47 +01:00
|
|
|
compose_actions.start('private', {
|
|
|
|
trigger: 'sidebar enter key',
|
|
|
|
private_message_recipient: email});
|
2014-02-13 23:08:41 +01:00
|
|
|
}
|
2016-04-29 23:48:51 +02:00
|
|
|
// Clear the user filter
|
|
|
|
exports.escape_search();
|
2014-02-13 23:08:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-05 07:02:18 +01:00
|
|
|
function focus_user_filter(e) {
|
2014-03-04 17:03:30 +01:00
|
|
|
e.stopPropagation();
|
2017-02-18 17:08:28 +01:00
|
|
|
update_clear_search_button();
|
2014-03-04 17:03:30 +01:00
|
|
|
}
|
|
|
|
|
2017-10-05 06:06:42 +02:00
|
|
|
exports.initialize_filter_state = function () {
|
|
|
|
// Exported just for the test suite.
|
2017-10-03 03:16:37 +02:00
|
|
|
meta.$user_list_filter = $(".user-list-filter");
|
|
|
|
|
|
|
|
meta.$user_list_filter.expectOne()
|
2014-03-04 17:03:30 +01:00
|
|
|
.on('click', focus_user_filter)
|
2014-02-13 23:08:41 +01:00
|
|
|
.on('input', update_users_for_search)
|
2017-02-18 17:08:28 +01:00
|
|
|
.on('keydown', maybe_select_person)
|
|
|
|
.on('blur', update_clear_search_button);
|
|
|
|
$('#clear_search_people_button').on('click', exports.clear_search);
|
2017-10-05 06:06:42 +02:00
|
|
|
};
|
2014-01-13 19:34:24 +01:00
|
|
|
|
2017-10-05 06:06:42 +02:00
|
|
|
$(function () {
|
|
|
|
exports.initialize_filter_state();
|
|
|
|
});
|
2014-01-13 19:34:24 +01:00
|
|
|
|
2013-02-07 19:57:45 +01:00
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|
2013-08-09 14:42:49 +02:00
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = activity;
|
|
|
|
}
|