From a926ed47ae4e50d8c9183dd744531f17f9f08980 Mon Sep 17 00:00:00 2001 From: Luna Mendes Date: Fri, 19 Oct 2018 04:45:56 -0300 Subject: [PATCH] storage: add Storage.get_guild_settings - gateway.websocket: use Storage.get_guild_settings - schema.sql: chande defaults on message_notifications to 0 --- litecord/gateway/websocket.py | 4 ++-- litecord/storage.py | 41 +++++++++++++++++++++++++++++++++++ schema.sql | 4 ++-- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/litecord/gateway/websocket.py b/litecord/gateway/websocket.py index 289c0e6..5281dda 100644 --- a/litecord/gateway/websocket.py +++ b/litecord/gateway/websocket.py @@ -218,11 +218,11 @@ class GatewayWebsocket: 'relationships': relationships, 'presences': friend_presences, 'read_state': await self.storage.get_read_state(user_id), + 'user_guild_settings': await self.storage.get_guild_settings( + user_id), 'friend_suggestion_count': 0, - # TODO - 'user_guild_settings': [], 'connected_accounts': [], 'experiments': [], diff --git a/litecord/storage.py b/litecord/storage.py index 34fb1d1..4edb591 100644 --- a/litecord/storage.py +++ b/litecord/storage.py @@ -908,3 +908,44 @@ class Storage: parties.remove(user_id) return parties[0] + + async def get_guild_settings(self, user_id: int): + """Get the specific User Guild Settings, + for all guilds a user is on.""" + + res = {} + + settings = await self.db.fetch(""" + SELECT guild_id, suppress_everyone, muted + message_notifications, mobile_push + FROM guild_settings + WHERE user_id = $1 + """, user_id) + + for row in settings: + gid = row['guild_id'] + drow = dict(row) + drow.pop('guild_id') + + chan_ids = await self.get_channel_ids(gid) + + chan_overrides = {} + + for chan_id in chan_ids: + chan_row = await self.db.fetchrow(""" + SELECT muted, message_notifications + FROM guild_setting_channel_overrides + WHERE + guild_id = $1 + AND user_id = $2 + AND channel_id = $3 + """, gid, user_id, chan_id) + + chan_overrides[str(chan_id)] = dict(chan_row) + + res[str(gid)] = {**drow, **{ + 'channel_overrides': chan_overrides + }} + + return res + diff --git a/schema.sql b/schema.sql index eb9f6d5..30c5a1d 100644 --- a/schema.sql +++ b/schema.sql @@ -274,7 +274,7 @@ CREATE TABLE IF NOT EXISTS guild_settings ( suppress_everyone bool DEFAULT false, muted bool DEFAULT false, - message_notifications int DEFAULT 3, + message_notifications int DEFAULT 0, mobile_push bool DEFAULT true, PRIMARY KEY (user_id, guild_id) @@ -287,7 +287,7 @@ CREATE TABLE IF NOT EXISTS guild_settings_channel_overrides ( channel_id bigint REFERENCES channels (id) ON DELETE CASCADE, muted bool DEFAULT false, - message_notifications bool DEFAULT 3, -- ?? + message_notifications bool DEFAULT 0, PRIMARY KEY (user_id, guild_id, channel_id) );