Skip to content

Commit 3c85533

Browse files
committed
profile: Profile.of(user) instead of user_profile
1 parent 9201a4f commit 3c85533

File tree

4 files changed

+38
-21
lines changed

4 files changed

+38
-21
lines changed

openemail/app/profile.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
# SPDX-FileContributor: kramo
44

55
from collections.abc import Iterator
6-
from typing import Any
6+
from typing import Any, Self
77

88
from gi.repository import Gdk, GdkPixbuf, Gio, GLib, GObject
99

1010
from openemail import app
1111
from openemail.core import client, model
1212
from openemail.core.client import WriteError, user
1313
from openemail.core.crypto import KeyPair
14-
from openemail.core.model import Address
14+
from openemail.core.model import Address, User
1515

1616
from . import Notifier
1717

@@ -149,6 +149,8 @@ class Profile(GObject.Object):
149149
_name: str | None = None
150150
_image: Gdk.Paintable | None = None
151151

152+
_user: Self | None = None
153+
152154
def set_from_profile(self, profile: model.Profile | None) -> None:
153155
"""Set the properties of `self` from `profile`."""
154156
self._profile = profile
@@ -215,13 +217,26 @@ def image(self, image: Gdk.Paintable | None) -> None:
215217
self._image = image
216218
self.has_image = bool(image)
217219

218-
@staticmethod
219-
def of(address: Address) -> "Profile":
220-
"""Get the profile associated with `address`."""
221-
from .store import profiles # noqa: PLC0415
220+
@classmethod
221+
def of(cls, user: Address | User, /) -> "Profile":
222+
"""Get the profile associated with `user`.
223+
224+
If `user` is a User object instead of an Address,
225+
returns a `Profile` object that always represents the data of
226+
the currently logged in user, even after a relogin.
227+
"""
228+
match user:
229+
case Address():
230+
from .store import profiles # noqa: PLC0415
231+
232+
(profile := profiles[user]).address = str(user)
233+
return profile
222234

223-
(profile := profiles[address]).address = str(address)
224-
return profile
235+
case User():
236+
if not cls._user:
237+
cls._user = cls()
238+
239+
return cls._user
225240

226241
def value_of(self, ident: str) -> Any:
227242
"""Get the value of the field identified by `ident` in `self`."""
@@ -244,10 +259,10 @@ def set_receives_broadcasts(self, value: bool) -> None:
244259

245260
async def refresh() -> None:
246261
"""Update the profile of the user by fetching new data remotely."""
247-
from .store import user_profile # noqa: PLC0415
248-
249-
user_profile.updating = True
250-
user_profile.set_from_profile(profile := await client.fetch_profile(user.address))
262+
Profile.of(client.user).updating = True
263+
Profile.of(client.user).set_from_profile(
264+
profile := await client.fetch_profile(user.address)
265+
)
251266

252267
if profile:
253268
user.signing_keys = KeyPair(
@@ -262,15 +277,15 @@ async def refresh() -> None:
262277
)
263278

264279
try:
265-
user_profile.image = Gdk.Texture.new_from_bytes(
280+
Profile.of(client.user).image = Gdk.Texture.new_from_bytes(
266281
GLib.Bytes.new(await client.fetch_profile_image(user.address))
267282
)
268283
except GLib.Error:
269-
user_profile.image = None
284+
Profile.of(client.user).image = None
270285

271-
Profile.of(user.address).image = user_profile.image
286+
Profile.of(user.address).image = Profile.of(client.user).image
272287
Profile.of(user.address).set_from_profile(profile)
273-
user_profile.updating = False
288+
Profile.of(client.user).updating = False
274289

275290

276291
async def update(values: dict[str, str]) -> None:

openemail/app/store.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ async def _update(self) -> None:
230230
profiles: defaultdict[Address, Profile] = defaultdict(Profile)
231231
address_book = _AddressBook()
232232
contact_requests = _ContactRequests()
233-
user_profile = Profile()
234233

235234

236235
class MessageStore(DictStore[str, Message]):

openemail/widgets/content.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
from gi.repository import Adw, Gdk, GObject, Gtk
99

10-
from openemail.app import APP_ID, PREFIX, Notifier, store
10+
from openemail.app import APP_ID, PREFIX, Notifier
11+
from openemail.app.profile import Profile
12+
from openemail.core import client
1113

1214
from .compose_sheet import ComposeSheet
1315
from .contacts import Contacts # noqa: F401
@@ -64,7 +66,7 @@ def __init__(self, **kwargs: Any) -> None:
6466
GObject.BindingFlags.SYNC_CREATE,
6567
)
6668

67-
store.user_profile.bind_property(
69+
Profile.of(client.user).bind_property(
6870
"image",
6971
self,
7072
"profile-image",

openemail/widgets/profile_settings.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
from gi.repository import Adw, Gdk, GdkPixbuf, Gio, GLib, GObject, Gtk
1010

1111
from openemail import app
12-
from openemail.app import PREFIX, profile, store
12+
from openemail.app import PREFIX, profile
1313
from openemail.app.profile import Profile, ProfileField
14+
from openemail.core import client
1415
from openemail.core.client import WriteError
1516

1617
from .form import Form
@@ -113,7 +114,7 @@ def __init__(self, **kwargs: Any) -> None:
113114
"about": self.about.get_text,
114115
}
115116

116-
store.user_profile.connect(
117+
Profile.of(client.user).connect(
117118
"notify::updating",
118119
lambda p, _: self.set_property("profile", None if p.updating else p), # pyright: ignore[reportUnknownArgumentType, reportUnknownMemberType]
119120
)

0 commit comments

Comments
 (0)