[user_activity] Consider the user's activities on Phabricator (#1842)

This commit is contained in:
Suhaib Mujahid 2023-01-19 09:18:25 -05:00 коммит произвёл GitHub
Родитель 7f63a3bcb9
Коммит b9a0093156
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 34 добавлений и 5 удалений

Просмотреть файл

@ -4,7 +4,7 @@
from datetime import timedelta
from enum import Enum, auto
from typing import List
from typing import List, Optional
from libmozdata import utils as lmdutils
from libmozdata.bugzilla import BugzillaUser
@ -68,6 +68,7 @@ class UserActivity:
).timestamp()
self.activity_limit = lmdutils.get_date("today", self.activity_weeks_count * 7)
self.activity_limit_ts = lmdutils.get_date_ymd(self.activity_limit).timestamp()
self.seen_limit = lmdutils.get_date("today", self.absent_weeks_count * 7)
def _get_phab(self):
@ -189,7 +190,7 @@ class UserActivity:
return users
def _get_status_from_phab_user(self, user: dict) -> UserStatus:
def _get_status_from_phab_user(self, user: dict) -> Optional[UserStatus]:
if "disabled" in user["fields"]["roles"]:
return UserStatus.DISABLED
@ -203,7 +204,7 @@ class UserActivity:
):
return UserStatus.UNAVAILABLE
return UserStatus.ACTIVE
return None
def get_phab_users_with_status(
self, user_phids: List[str], keep_active: bool = False
@ -240,8 +241,15 @@ class UserActivity:
for _user_phids in Connection.chunks(user_phids, PHAB_CHUNK_SIZE):
for phab_user in self._fetch_phab_users(_user_phids):
user = users[phab_user["phid"]]
if user["status"] == UserStatus.ACTIVE:
user["status"] = self._get_status_from_phab_user(phab_user)
phab_status = self._get_status_from_phab_user(phab_user)
if phab_status:
user["status"] = phab_status
elif user["status"] in (
UserStatus.ABSENT,
UserStatus.INACTIVE,
) and self.is_active_on_phab(phab_user["phid"]):
user["status"] = UserStatus.ACTIVE
if not keep_active and user["status"] == UserStatus.ACTIVE:
del users[phab_user["phid"]]
@ -254,6 +262,27 @@ class UserActivity:
return users
def is_active_on_phab(self, user_phid: str) -> bool:
"""Check if the user has recent activities on Phabricator.
Args:
user_phid: The user PHID.
Returns:
True if the user is active on Phabricator, False otherwise.
"""
feed = self._get_phab().request(
"feed.query",
filterPHIDs=[user_phid],
limit=1,
)
for story in feed.values():
if story["epoch"] >= self.activity_limit_ts:
return True
return False
def get_string_status(self, status: UserStatus):
"""Get a string representation of the user status."""