user_storage: add UserStorage.get_user_guilds

So that we're consistent.

 - gateway.websocket, presence: change to UserStorage for
    get_user_guilds
This commit is contained in:
Luna Mendes 2018-11-17 18:41:54 -03:00
parent 9547b5e536
commit 0aa679677f
5 changed files with 22 additions and 16 deletions

View File

@ -329,7 +329,7 @@ class GatewayWebsocket:
raise InvalidShard('Shard count > Total shards')
async def _guild_ids(self):
guild_ids = await self.storage.get_user_guilds(
guild_ids = await self.user_storage.get_user_guilds(
self.state.user_id
)
@ -667,7 +667,8 @@ class GatewayWebsocket:
"""Handle OP 12 Guild Sync."""
data = payload['d']
gids = await self.storage.get_user_guilds(self.state.user_id)
gids = await self.user_storage.get_user_guilds(
self.state.user_id)
for guild_id in data:
try:

View File

@ -59,8 +59,9 @@ async def _pres(storage, user_id: int, status_obj: dict) -> dict:
class PresenceManager:
"""Presence related functions."""
def __init__(self, storage, state_manager, dispatcher):
def __init__(self, storage, user_storage, state_manager, dispatcher):
self.storage = storage
self.user_storage = user_storage
self.state_manager = state_manager
self.dispatcher = dispatcher
@ -171,7 +172,7 @@ class PresenceManager:
state['status'] = 'offline'
# TODO: shard-aware
guild_ids = await self.storage.get_user_guilds(user_id)
guild_ids = await self.user_storage.get_user_guilds(user_id)
for guild_id in guild_ids:
await self.dispatch_guild_pres(

View File

@ -147,16 +147,6 @@ class Storage:
return drow
async def get_user_guilds(self, user_id: int) -> List[int]:
"""Get all guild IDs a user is on."""
guild_ids = await self.db.fetch("""
SELECT guild_id
FROM members
WHERE user_id = $1
""", user_id)
return [row['guild_id'] for row in guild_ids]
async def _member_basic(self, guild_id: int, member_id: int):
return await self.db.fetchrow("""
SELECT user_id, nickname, joined_at, deafened, muted

View File

@ -278,3 +278,13 @@ class UserStorage:
}})
return res
async def get_user_guilds(self, user_id: int) -> List[int]:
"""Get all guild IDs a user is on."""
guild_ids = await self.db.fetch("""
SELECT guild_id
FROM members
WHERE user_id = $1
""", user_id)
return [row['guild_id'] for row in guild_ids]

8
run.py
View File

@ -194,8 +194,12 @@ def init_app_managers(app):
app.icons = IconManager(app)
app.dispatcher = EventDispatcher(app)
app.presence = PresenceManager(app.storage,
app.state_manager, app.dispatcher)
# TODO: only pass app
app.presence = PresenceManager(
app.storage, app.user_storage,
app.state_manager, app.dispatcher
)
app.storage.presence = app.presence