mirror of
https://github.com/yawaflua/Flask-Discord.git
synced 2025-12-10 12:29:30 +02:00
Override default fetch_from_api and add a caching layer
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user