2017-11-08 18:38:52 +01:00
|
|
|
zrequire('people');
|
|
|
|
zrequire('presence');
|
2017-05-24 17:55:31 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const return_false = function () { return false; };
|
2018-07-31 07:17:46 +02:00
|
|
|
|
2017-06-16 01:53:12 +02:00
|
|
|
set_global('server_events', {});
|
2018-08-04 15:40:25 +02:00
|
|
|
set_global('reload_state', {
|
2018-07-31 07:17:46 +02:00
|
|
|
is_in_progress: return_false,
|
|
|
|
});
|
2020-02-07 14:50:30 +01:00
|
|
|
set_global('XDate', function (ms) { return {seconds: ms}; });
|
2017-06-16 01:53:12 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const OFFLINE_THRESHOLD_SECS = 140;
|
2017-05-24 17:55:31 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const me = {
|
2017-05-24 17:55:31 +02:00
|
|
|
email: 'me@zulip.com',
|
2020-04-03 15:31:17 +02:00
|
|
|
user_id: 101,
|
2017-05-24 17:55:31 +02:00
|
|
|
full_name: 'Me Myself',
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const alice = {
|
2017-05-24 17:55:31 +02:00
|
|
|
email: 'alice@zulip.com',
|
|
|
|
user_id: 1,
|
|
|
|
full_name: 'Alice Smith',
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const fred = {
|
2017-05-24 17:55:31 +02:00
|
|
|
email: 'fred@zulip.com',
|
|
|
|
user_id: 2,
|
|
|
|
full_name: "Fred Flintstone",
|
|
|
|
};
|
|
|
|
|
2020-02-07 14:50:30 +01:00
|
|
|
const sally = {
|
|
|
|
email: 'sally@example.com',
|
|
|
|
user_id: 3,
|
|
|
|
full_name: 'Sally Jones',
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const zoe = {
|
2017-05-24 17:55:31 +02:00
|
|
|
email: 'zoe@example.com',
|
|
|
|
user_id: 6,
|
|
|
|
full_name: 'Zoe Yang',
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const bot = {
|
2017-06-07 18:36:26 +02:00
|
|
|
email: 'bot@zulip.com',
|
|
|
|
user_id: 7,
|
|
|
|
full_name: 'The Bot',
|
|
|
|
is_bot: true,
|
|
|
|
};
|
|
|
|
|
2020-05-29 18:38:52 +02:00
|
|
|
const john = {
|
|
|
|
email: 'john@zulip.com',
|
|
|
|
user_id: 8,
|
|
|
|
full_name: "John Doe",
|
|
|
|
};
|
|
|
|
|
|
|
|
const jane = {
|
|
|
|
email: 'jane@zulip.com',
|
|
|
|
user_id: 9,
|
|
|
|
full_name: "Jane Doe",
|
|
|
|
};
|
|
|
|
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(me);
|
|
|
|
people.add_active_user(alice);
|
|
|
|
people.add_active_user(fred);
|
|
|
|
people.add_active_user(sally);
|
|
|
|
people.add_active_user(zoe);
|
|
|
|
people.add_active_user(bot);
|
2020-05-29 18:38:52 +02:00
|
|
|
people.add_active_user(john);
|
|
|
|
people.add_active_user(jane);
|
2017-05-24 17:55:31 +02:00
|
|
|
people.initialize_current_user(me.user_id);
|
|
|
|
|
2019-01-03 16:44:06 +01:00
|
|
|
run_test('my user', () => {
|
|
|
|
assert.equal(presence.get_status(me.user_id), 'active');
|
|
|
|
});
|
|
|
|
|
2020-04-03 15:31:17 +02:00
|
|
|
run_test('unknown user', () => {
|
|
|
|
const unknown_user_id = 999;
|
|
|
|
const now = 888888;
|
|
|
|
const presences = {};
|
|
|
|
presences[unknown_user_id.toString()] = 'does-not-matter';
|
|
|
|
|
|
|
|
blueslip.expect('error', 'Unknown user ID in presence data: 999');
|
|
|
|
presence.set_info(presences, now);
|
|
|
|
|
|
|
|
// If the server is suspected to be offline or reloading,
|
|
|
|
// then we suppress errors. The use case here is that we
|
|
|
|
// haven't gotten info for a brand new user yet.
|
|
|
|
server_events.suspect_offline = true;
|
|
|
|
presence.set_info(presences, now);
|
|
|
|
server_events.suspect_offline = false;
|
|
|
|
|
|
|
|
reload_state.is_in_progress = () => true;
|
|
|
|
presence.set_info(presences, now);
|
|
|
|
reload_state.is_in_progress = () => false;
|
|
|
|
});
|
|
|
|
|
2020-02-07 14:50:30 +01:00
|
|
|
run_test('status_from_raw', () => {
|
|
|
|
const status_from_raw = presence.status_from_raw;
|
2017-05-24 17:55:31 +02:00
|
|
|
|
2020-02-07 14:50:30 +01:00
|
|
|
const now = 5000;
|
|
|
|
let raw;
|
2017-05-24 17:55:31 +02:00
|
|
|
|
2020-02-07 14:50:30 +01:00
|
|
|
raw = {
|
|
|
|
server_timestamp: now,
|
|
|
|
active_timestamp: now - OFFLINE_THRESHOLD_SECS / 2,
|
2017-05-24 17:55:31 +02:00
|
|
|
};
|
2020-02-07 14:50:30 +01:00
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
status_from_raw(raw),
|
|
|
|
{
|
|
|
|
status: 'active',
|
|
|
|
last_active: raw.active_timestamp,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
raw = {
|
|
|
|
server_timestamp: now,
|
|
|
|
active_timestamp: now - OFFLINE_THRESHOLD_SECS * 2,
|
2017-06-17 00:04:27 +02:00
|
|
|
};
|
2020-02-07 14:50:30 +01:00
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
status_from_raw(raw),
|
|
|
|
{
|
|
|
|
status: 'offline',
|
|
|
|
last_active: raw.active_timestamp,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
raw = {
|
|
|
|
server_timestamp: now,
|
|
|
|
idle_timestamp: now - OFFLINE_THRESHOLD_SECS / 2,
|
2017-06-17 00:04:27 +02:00
|
|
|
};
|
2020-04-03 17:23:56 +02:00
|
|
|
|
2020-02-07 14:50:30 +01:00
|
|
|
assert.deepEqual(
|
|
|
|
status_from_raw(raw),
|
|
|
|
{
|
|
|
|
status: 'idle',
|
2020-05-29 18:38:52 +02:00
|
|
|
last_active: raw.idle_timestamp,
|
2020-02-07 14:50:30 +01:00
|
|
|
}
|
|
|
|
);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-05-24 17:55:31 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('set_presence_info', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const presences = {};
|
2020-02-07 14:50:30 +01:00
|
|
|
const now = 5000;
|
|
|
|
const recent = now + 1 - OFFLINE_THRESHOLD_SECS;
|
|
|
|
const a_while_ago = now - OFFLINE_THRESHOLD_SECS * 2;
|
2017-05-24 17:55:31 +02:00
|
|
|
|
2020-02-02 17:29:05 +01:00
|
|
|
presences[alice.user_id.toString()] = {
|
2020-02-07 14:50:30 +01:00
|
|
|
active_timestamp: recent,
|
2017-05-24 17:55:31 +02:00
|
|
|
};
|
|
|
|
|
2020-02-02 17:29:05 +01:00
|
|
|
presences[fred.user_id.toString()] = {
|
2020-02-07 14:50:30 +01:00
|
|
|
active_timestamp: a_while_ago,
|
|
|
|
idle_timestamp: now,
|
2017-05-24 17:55:31 +02:00
|
|
|
};
|
|
|
|
|
2020-02-02 17:29:05 +01:00
|
|
|
presences[me.user_id.toString()] = {
|
2020-02-07 14:50:30 +01:00
|
|
|
active_timestamp: now,
|
|
|
|
};
|
|
|
|
|
|
|
|
presences[sally.user_id.toString()] = {
|
|
|
|
active_timestamp: a_while_ago,
|
2018-09-08 14:25:06 +02:00
|
|
|
};
|
|
|
|
|
2020-05-29 18:38:52 +02:00
|
|
|
presences[john.user_id.toString()] = {
|
|
|
|
idle_timestamp: a_while_ago,
|
|
|
|
};
|
|
|
|
|
|
|
|
presences[jane.user_id.toString()] = {
|
|
|
|
idle_timestamp: now,
|
|
|
|
};
|
|
|
|
|
2020-02-25 12:16:26 +01:00
|
|
|
const params = {};
|
|
|
|
params.presences = presences;
|
2020-02-07 14:50:30 +01:00
|
|
|
params.initial_servertime = now;
|
2020-02-25 12:16:26 +01:00
|
|
|
presence.initialize(params);
|
2017-05-24 17:55:31 +02:00
|
|
|
|
2020-02-06 04:21:07 +01:00
|
|
|
assert.deepEqual(presence.presence_info.get(alice.user_id),
|
2020-02-07 14:50:30 +01:00
|
|
|
{ status: 'active', last_active: recent}
|
|
|
|
);
|
|
|
|
assert.equal(presence.get_status(alice.user_id), 'active');
|
|
|
|
assert.deepEqual(
|
|
|
|
presence.last_active_date(alice.user_id),
|
|
|
|
{seconds: recent * 1000}
|
2017-05-24 17:55:31 +02:00
|
|
|
);
|
|
|
|
|
2020-02-06 04:21:07 +01:00
|
|
|
assert.deepEqual(presence.presence_info.get(fred.user_id),
|
2020-05-29 18:38:52 +02:00
|
|
|
{ status: 'idle', last_active: now}
|
2017-05-24 17:55:31 +02:00
|
|
|
);
|
2020-02-07 14:50:30 +01:00
|
|
|
assert.equal(presence.get_status(fred.user_id), 'idle');
|
2017-05-24 17:55:31 +02:00
|
|
|
|
2020-02-06 04:21:07 +01:00
|
|
|
assert.deepEqual(presence.presence_info.get(me.user_id),
|
2020-02-07 14:50:30 +01:00
|
|
|
{ status: 'active', last_active: now}
|
|
|
|
);
|
|
|
|
assert.equal(presence.get_status(me.user_id), 'active');
|
|
|
|
|
|
|
|
assert.deepEqual(presence.presence_info.get(sally.user_id),
|
|
|
|
{ status: 'offline', last_active: a_while_ago}
|
2018-09-08 14:41:41 +02:00
|
|
|
);
|
2020-02-07 14:50:30 +01:00
|
|
|
assert.equal(presence.get_status(sally.user_id), 'offline');
|
2018-09-08 14:41:41 +02:00
|
|
|
|
2020-02-06 04:21:07 +01:00
|
|
|
assert.deepEqual(presence.presence_info.get(zoe.user_id),
|
2020-02-05 15:08:51 +01:00
|
|
|
{ status: 'offline', last_active: undefined}
|
2017-05-24 17:55:31 +02:00
|
|
|
);
|
2020-02-07 14:50:30 +01:00
|
|
|
assert.equal(presence.get_status(zoe.user_id), 'offline');
|
|
|
|
assert.equal(presence.last_active_date(zoe.user_id), undefined);
|
2017-06-07 18:36:26 +02:00
|
|
|
|
2020-02-06 04:21:07 +01:00
|
|
|
assert(!presence.presence_info.has(bot.user_id));
|
2020-02-07 14:50:30 +01:00
|
|
|
assert.equal(presence.get_status(bot.user_id), 'offline');
|
2020-05-29 18:38:52 +02:00
|
|
|
|
|
|
|
assert.deepEqual(presence.presence_info.get(john.user_id),
|
|
|
|
{ status: 'offline', last_active: a_while_ago}
|
|
|
|
);
|
|
|
|
assert.equal(presence.get_status(john.user_id), 'offline');
|
|
|
|
|
|
|
|
assert.deepEqual(presence.presence_info.get(jane.user_id),
|
|
|
|
{ status: 'idle', last_active: now}
|
|
|
|
);
|
|
|
|
assert.equal(presence.get_status(jane.user_id), 'idle');
|
|
|
|
|
2020-02-07 14:50:30 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
run_test('falsy values', () => {
|
|
|
|
/*
|
|
|
|
When a user does not have a relevant active timestamp,
|
|
|
|
the server just leaves off the `active_timestamp` field
|
|
|
|
to save bandwidth, which looks like `undefined` to us
|
|
|
|
if we try to dereference it.
|
|
|
|
|
|
|
|
Our code should just treat all falsy values the same way,
|
|
|
|
though, to defend against bugs where we say the person
|
|
|
|
was last online in 1970 or silly things like that.
|
|
|
|
*/
|
|
|
|
const now = 2000000;
|
|
|
|
const a_bit_ago = now - 5;
|
|
|
|
const presences = {};
|
|
|
|
|
|
|
|
for (const falsy_value of [undefined, 0, null]) {
|
|
|
|
presences[zoe.user_id.toString()] = {
|
|
|
|
active_timestamp: falsy_value,
|
|
|
|
idle_timestamp: a_bit_ago,
|
|
|
|
};
|
|
|
|
|
|
|
|
presence.set_info(presences, now);
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
presence.presence_info.get(zoe.user_id),
|
2020-05-29 18:38:52 +02:00
|
|
|
{ status: 'idle', last_active: a_bit_ago }
|
|
|
|
);
|
|
|
|
|
|
|
|
presences[zoe.user_id.toString()] = {
|
|
|
|
active_timestamp: falsy_value,
|
|
|
|
idle_timestamp: falsy_value,
|
|
|
|
};
|
|
|
|
|
|
|
|
presence.set_info(presences, now);
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
presence.presence_info.get(zoe.user_id),
|
|
|
|
{ status: 'offline', last_active: undefined }
|
2020-02-07 14:50:30 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
run_test('big realms', () => {
|
|
|
|
const presences = {};
|
|
|
|
const now = 5000;
|
|
|
|
|
|
|
|
presences[sally.user_id.toString()] = {
|
|
|
|
active_timestamp: now,
|
|
|
|
};
|
2017-06-16 01:53:12 +02:00
|
|
|
|
2020-02-07 14:50:30 +01:00
|
|
|
// Make it seem like realm has a lot of people, in
|
|
|
|
// which case we will not provide default values for
|
|
|
|
// users that aren't in our presences payload.
|
2020-03-21 13:58:40 +01:00
|
|
|
const get_active_human_count = people.get_active_human_count;
|
|
|
|
people.get_active_human_count = function () { return 1000; };
|
2020-02-07 14:50:30 +01:00
|
|
|
presence.set_info(presences, now);
|
|
|
|
assert(presence.presence_info.has(sally.user_id));
|
|
|
|
assert(!presence.presence_info.has(zoe.user_id));
|
2020-03-21 13:58:40 +01:00
|
|
|
people.get_active_human_count = get_active_human_count;
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-05-24 17:55:31 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('last_active_date', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const unknown_id = 42;
|
2020-02-06 04:21:07 +01:00
|
|
|
presence.presence_info.clear();
|
|
|
|
presence.presence_info.set(alice.user_id, { last_active: 500 });
|
|
|
|
presence.presence_info.set(fred.user_id, {});
|
2017-06-16 01:54:49 +02:00
|
|
|
|
|
|
|
assert.equal(presence.last_active_date(unknown_id), undefined);
|
|
|
|
assert.equal(presence.last_active_date(fred.user_id), undefined);
|
|
|
|
assert.deepEqual(presence.last_active_date(alice.user_id), {seconds: 500000});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-16 01:54:49 +02:00
|
|
|
|
2020-02-07 17:19:03 +01:00
|
|
|
run_test('update_info_from_event', () => {
|
2020-02-07 14:50:30 +01:00
|
|
|
let info;
|
|
|
|
|
|
|
|
info = {
|
2017-06-17 00:06:21 +02:00
|
|
|
website: {
|
|
|
|
status: "active",
|
2020-02-07 14:50:30 +01:00
|
|
|
timestamp: 500,
|
2017-06-17 00:06:21 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-02-06 04:21:07 +01:00
|
|
|
presence.presence_info.delete(alice.user_id);
|
2020-02-07 14:50:30 +01:00
|
|
|
presence.update_info_from_event(alice.user_id, info, 500);
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
presence.presence_info.get(alice.user_id),
|
|
|
|
{ status: 'active', last_active: 500 }
|
|
|
|
);
|
|
|
|
|
|
|
|
info = {
|
|
|
|
mobile: {
|
|
|
|
status: "idle",
|
|
|
|
timestamp: 510,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
presence.update_info_from_event(alice.user_id, info, 510);
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
presence.presence_info.get(alice.user_id),
|
2020-05-29 18:38:52 +02:00
|
|
|
{ status: 'active', last_active: 510 }
|
2020-02-07 14:50:30 +01:00
|
|
|
);
|
2017-06-17 00:06:21 +02:00
|
|
|
|
2020-02-07 14:50:30 +01:00
|
|
|
info = {
|
|
|
|
mobile: {
|
|
|
|
status: "idle",
|
|
|
|
timestamp: 1000,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
presence.update_info_from_event(alice.user_id, info, 1000);
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
presence.presence_info.get(alice.user_id),
|
2020-05-29 18:38:52 +02:00
|
|
|
{ status: 'idle', last_active: 1000 }
|
2020-02-07 14:50:30 +01:00
|
|
|
);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|