diff --git a/flask_discord/models/connections.py b/flask_discord/models/connections.py index 3409dea..f67d3ba 100644 --- a/flask_discord/models/connections.py +++ b/flask_discord/models/connections.py @@ -1,5 +1,6 @@ from .base import DiscordModelsBase from .integration import Integration +from flask import current_app, session class UserConnection(DiscordModelsBase): @@ -53,3 +54,32 @@ class UserConnection(DiscordModelsBase): def is_visible(self): """A property returning bool if this integration is visible to everyone.""" return bool(self.visibility) + + @classmethod + def fetch_from_api(cls, cache=True): + """A class method which returns an instance or list of instances of this model by implicitly making an + API call to Discord. If an instance of :py:class:`flask_discord.User` exists in the users internal cache + who are attached to these connections then, the cached property :py:attr:`flask_discord.User.connections` + is updated. + + Parameters + ---------- + cache : bool + Determines if the :py:attr:`flask_discord.User.guilds` cache should be updated with the new guilds. + + Returns + ------- + list[flask_discord.UserConnection, ...] + List of instances of :py:class:`flask_discord.UserConnection` to which this user belongs. + + """ + connections = super().fetch_from_api() + + if cache: + user = current_app.discord.users_cache.get(session.get("DISCORD_USER_ID")) + try: + user.connections = connections + except AttributeError: + pass + + return connections diff --git a/flask_discord/models/user.py b/flask_discord/models/user.py index a9cd1d1..b201fb8 100644 --- a/flask_discord/models/user.py +++ b/flask_discord/models/user.py @@ -186,7 +186,7 @@ class User(DiscordModelsBase): A list of :py:class:`flask_discord.UserConnection` instances. """ - self.connections = UserConnection.fetch_from_api() + self.connections = UserConnection.fetch_from_api(cache=False) return self.connections