Skip to content

Commit

Permalink
core: User's last activity status improved.
Browse files Browse the repository at this point in the history
Last activity improvements:
- Currently a user's last activity is represented by a datetime object.
  A function is added that returns a user's last activity in words,
  e.g, '1 hour ago', '3 days ago'.
- All timestamps before 2 weeks are returned as 'more than 2 weeks ago'.

The above two points ensure equivalence with webapp.

Test added (user presence status).

Fixes #511.
  • Loading branch information
Ezio-Sarthak committed Jan 4, 2021
1 parent 2b9fb99 commit df964b1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
31 changes: 31 additions & 0 deletions tests/core/test_core.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime, timedelta
from platform import platform
from typing import Any

Expand Down Expand Up @@ -283,6 +284,36 @@ def test_stream_muting_confirmation_popup(self, mocker, controller,
+ "' ?"), "center")
pop_up.assert_called_once_with(controller, text(), partial())

@pytest.mark.parametrize('presence, time_in_sec, expected_status', [
('active', 0, 'Active now'),
('idle', 10, 'Just now'),
('idle', 100, '1 minute ago'),
('idle', 150, '2 minutes ago'),
('idle', 10000, '2 hours ago'),
('idle', 200000, '2 days ago'),
('offline', 2000000, 'More than 2 weeks ago'),
], ids=[
'active_now', 'active_few_seconds_ago',
'active_one_minute_ago', 'active_few_minutes_ago',
'active_few_hours_ago', 'active_few_days_ago',
'active_more_than_two_weeks_ago'
])
def test_get_user_presence_status(self, presence, controller, mocker,
time_in_sec, expected_status):

def get_user_presence(email: str):
data = {'presence': {}}
data['presence']['aggregated'] = {
'status': presence,
'timestamp': (datetime.timestamp(datetime.now()
- timedelta(seconds=time_in_sec))),
}
return data
controller.client.get_user_presence = get_user_presence

assert (controller.get_user_presence_status('[email protected]')
== expected_status)

@pytest.mark.parametrize('initial_narrow, final_narrow', [
([], [['search', 'FOO']]),
([['search', 'BOO']], [['search', 'FOO']]),
Expand Down
29 changes: 25 additions & 4 deletions zulipterminal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,17 +240,38 @@ def show_user_info(self, user_id: int) -> None:
if user_info_data['Timezone']:
user_info_data['Local time'] = datetime.now(
pytz.timezone(str(res_data['timezone']))).strftime("%H:%M")
presence = self.client.get_user_presence(str(res_data['email']))
if presence['presence']['aggregated']['status'] != 'active':
user_info_data['Last active'] = str(datetime.fromtimestamp(
presence['presence']['aggregated']['timestamp']))
user_info_data['Last active'] = (self.get_user_presence_status(
res_data['email']))
role = [key for key, value in user_role.items() if value]
if len(role):
user_info_data['Role'] = role[0]
show_userinfo_view = UserInfoView(self, user_info_data,
'User Info (up/down scrolls)')
self.show_pop_up(show_userinfo_view)

def get_user_presence_status(self, email: str) -> str:
status = 'Active now'
prefix = ['days ago', 'hours ago', 'minutes ago']
sec = [86400, 3600, 60]

presence = (self.client.get_user_presence(email)['presence']
['aggregated'])
if presence['status'] != 'active':
diff = int((datetime.now() - datetime.fromtimestamp(
presence['timestamp'])).total_seconds())
indices = [i for i in range(len(sec)) if diff >= sec[i]]
if len(indices) == 0:
return 'Just now'
time = int(diff / sec[indices[0]])
if len(indices) == len(sec) and time > 14:
status = 'More than 2 weeks ago'
else:
status = '{} {}'.format(time, prefix[indices[0]])
if time == 1:
new_res = status[:-5] + status[-4:]
status = new_res
return status

def show_edit_history(
self, message: Message,
message_links: 'OrderedDict[str, Tuple[str, int, bool]]',
Expand Down

0 comments on commit df964b1

Please sign in to comment.