diff options
author | Philipp Hörist <philipp@hoerist.com> | 2023-10-14 20:31:21 +0300 |
---|---|---|
committer | Philipp Hörist <philipp@hoerist.com> | 2023-10-14 20:31:21 +0300 |
commit | 012559069517c8e37dbe660a9ee4be73badcf80c (patch) | |
tree | 0c059ba6e6b7b645bf4c9a417650692c318bc655 | |
parent | 0a48786c870ad2daa95325e271572aaf7ee9d356 (diff) |
refactor: Use datetime object for idle time
-rw-r--r-- | gajim/common/events.py | 1 | ||||
-rw-r--r-- | gajim/common/modules/contacts.py | 14 | ||||
-rw-r--r-- | gajim/common/modules/presence.py | 1 | ||||
-rw-r--r-- | gajim/common/structs.py | 24 | ||||
-rw-r--r-- | gajim/common/util/datetime.py | 23 | ||||
-rw-r--r-- | gajim/gtk/tooltips.py | 11 |
6 files changed, 52 insertions, 22 deletions
diff --git a/gajim/common/events.py b/gajim/common/events.py index b7a6dbb4a..68fd21220 100644 --- a/gajim/common/events.py +++ b/gajim/common/events.py @@ -634,7 +634,6 @@ class PresenceReceived(ApplicationEvent): timestamp: float avatar_sha: str | None user_nick: str | None - idle_time: float | None show: str new_show: str old_show: str diff --git a/gajim/common/modules/contacts.py b/gajim/common/modules/contacts.py index 9e3b9212f..e8c861143 100644 --- a/gajim/common/modules/contacts.py +++ b/gajim/common/modules/contacts.py @@ -468,13 +468,13 @@ class BareContact(CommonContact): return res.status @property - def idle_time(self) -> float | None: + def idle_datetime(self) -> datetime | None: if not self._resources: - return self._presence.idle_time + return self._presence.idle_datetime res = self.get_active_resource() assert res is not None - return res.idle_time + return res.idle_datetime @property def chatstate(self) -> Chatstate | None: @@ -709,8 +709,8 @@ class ResourceContact(CommonContact): return self._presence.priority @property - def idle_time(self) -> float | None: - return self._presence.idle_time + def idle_datetime(self) -> datetime | None: + return self._presence.idle_datetime @property def chatstate(self) -> Chatstate | None: @@ -984,8 +984,8 @@ class GroupchatParticipant(CommonContact): return self._presence.status @property - def idle_time(self) -> float | None: - return self._presence.idle_time + def idle_datetime(self) -> datetime | None: + return self._presence.idle_datetime @property def name(self) -> str: diff --git a/gajim/common/modules/presence.py b/gajim/common/modules/presence.py index 4379b1c0e..8e1845731 100644 --- a/gajim/common/modules/presence.py +++ b/gajim/common/modules/presence.py @@ -141,7 +141,6 @@ class Presence(BaseModule): 'timestamp': properties.timestamp, 'avatar_sha': properties.avatar_sha, 'user_nick': properties.nickname, - 'idle_time': properties.idle_timestamp, 'show': show, 'new_show': show, 'old_show': 0, diff --git a/gajim/common/structs.py b/gajim/common/structs.py index cc25f1d04..0706d5492 100644 --- a/gajim/common/structs.py +++ b/gajim/common/structs.py @@ -21,6 +21,7 @@ from typing import TypeVar import time from dataclasses import dataclass from dataclasses import fields +from datetime import datetime from gi.repository import GLib from nbxmpp.const import Affiliation @@ -37,6 +38,7 @@ from gajim.common.const import KindConstant from gajim.common.const import MUCJoinedState from gajim.common.const import PresenceShowExt from gajim.common.const import URIType +from gajim.common.util.datetime import convert_epoch_to_local_datetime _T = TypeVar('_T') @@ -208,22 +210,27 @@ class PresenceData: show: PresenceShow status: str priority: int - idle_time: float | None + idle_datetime: datetime | None available: bool @classmethod def from_presence(cls, properties: PresenceProperties) -> PresenceData: + idle_datetime = None + if properties.idle_timestamp is not None: + idle_datetime = convert_epoch_to_local_datetime( + properties.idle_timestamp) + return cls(show=properties.show, status=properties.status, priority=properties.priority, - idle_time=properties.idle_timestamp, + idle_datetime=idle_datetime, available=properties.type.is_available) UNKNOWN_PRESENCE = PresenceData(show=PresenceShowExt.OFFLINE, status='', priority=0, - idle_time=0, + idle_datetime=None, available=False) @@ -231,7 +238,7 @@ UNKNOWN_PRESENCE = PresenceData(show=PresenceShowExt.OFFLINE, class MUCPresenceData: show: PresenceShow status: str - idle_time: float | None + idle_datetime: datetime | None available: bool affiliation: Affiliation role: Role @@ -244,9 +251,14 @@ class MUCPresenceData: occupant_id: str | None ) -> MUCPresenceData: + idle_datetime = None + if properties.idle_timestamp is not None: + idle_datetime = convert_epoch_to_local_datetime( + properties.idle_timestamp) + return cls(show=properties.show, status=properties.status, - idle_time=properties.idle_timestamp, + idle_datetime=idle_datetime, available=properties.type.is_available, affiliation=properties.muc_user.affiliation, role=properties.muc_user.role, @@ -256,7 +268,7 @@ class MUCPresenceData: UNKNOWN_MUC_PRESENCE = MUCPresenceData(show=PresenceShowExt.OFFLINE, status='', - idle_time=0, + idle_datetime=None, available=False, affiliation=Affiliation.NONE, role=Role.NONE, diff --git a/gajim/common/util/datetime.py b/gajim/common/util/datetime.py new file mode 100644 index 000000000..3c8288996 --- /dev/null +++ b/gajim/common/util/datetime.py @@ -0,0 +1,23 @@ +# This file is part of Gajim. +# +# Gajim is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published +# by the Free Software Foundation; version 3 only. +# +# Gajim is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Gajim. If not, see <http://www.gnu.org/licenses/>. + +from __future__ import annotations + +from datetime import datetime +from datetime import timezone + + +def convert_epoch_to_local_datetime(utc_timestamp: float) -> datetime: + utc = datetime.fromtimestamp(utc_timestamp, tz=timezone.utc) + return utc.astimezone() diff --git a/gajim/gtk/tooltips.py b/gajim/gtk/tooltips.py index 254d8d094..c694ccb32 100644 --- a/gajim/gtk/tooltips.py +++ b/gajim/gtk/tooltips.py @@ -28,7 +28,6 @@ from __future__ import annotations import logging import os -import time from datetime import datetime from gi.repository import GdkPixbuf @@ -241,16 +240,14 @@ class ContactTooltip: ) resource_box.add(status_label) - if contact.idle_time: - idle_time = time.localtime(contact.idle_time) - idle_time = datetime(*(idle_time[:6])) + if idle_datetime := contact.idle_datetime: current = datetime.now() - if idle_time.date() == current.date(): + if idle_datetime.date() == current.date(): format_string = app.settings.get('time_format') - formatted = idle_time.strftime(format_string) + formatted = idle_datetime.strftime(format_string) else: format_string = app.settings.get('date_time_format') - formatted = idle_time.strftime(format_string) + formatted = idle_datetime.strftime(format_string) idle_text = _('Idle since: %s') % formatted idle_label = Gtk.Label( halign=Gtk.Align.START, |