blueprints.users: fix user guild settings

- blueprints.users: make sure a row exists on guild_settings when
    patching
 - schemas: fix all 'bool' types to 'boolean'
 - schemas: fix GUILD_SETTINGS_CHAN_OVERRIDE
 - storage: add Storage.get_guild_settings_one
 - storage: fix Storage.get_guild_settings
This commit is contained in:
Luna Mendes 2018-10-19 17:36:54 -03:00
parent ef5d4f30e1
commit 79f91cd774
4 changed files with 93 additions and 32 deletions

View File

@ -422,6 +422,10 @@ async def patch_guild_settings(guild_id: int):
j = validate(await request.get_json(), GUILD_SETTINGS) j = validate(await request.get_json(), GUILD_SETTINGS)
# querying the guild settings information before modifying
# will make sure they exist in the table.
await app.storage.get_guild_settings_one(user_id, guild_id)
for field in (k for k in j.keys() if k != 'channel_overrides'): for field in (k for k in j.keys() if k != 'channel_overrides'):
await app.db.execute(f""" await app.db.execute(f"""
UPDATE guild_settings UPDATE guild_settings
@ -440,7 +444,7 @@ async def patch_guild_settings(guild_id: int):
continue continue
for field in chan_overrides: for field in chan_overrides:
await app.db.execute(f""" res = await app.db.execute(f"""
UPDATE guild_settings_channel_overrides UPDATE guild_settings_channel_overrides
SET {field} = $1 SET {field} = $1
WHERE user_id = $2 WHERE user_id = $2
@ -448,11 +452,16 @@ async def patch_guild_settings(guild_id: int):
AND channel_id = $4 AND channel_id = $4
""", chan_overrides[field], user_id, guild_id, chan_id) """, chan_overrides[field], user_id, guild_id, chan_id)
if res == 'UPDATE 0':
await app.db.execute(f"""
INSERT INTO guild_settings_channel_overrides
(user_id, guild_id, channel_id, {field})
VALUES ($1, $2, $3, $4)
""", user_id, guild_id, chan_id, chan_overrides[field])
settings = await app.storage.get_guild_settings_one(user_id, guild_id) settings = await app.storage.get_guild_settings_one(user_id, guild_id)
await app.dispatcher.dispatch_user(user_id, 'USER_GUILD_SETTINGS_UPDATE', { await app.dispatcher.dispatch_user(
**settings, user_id, 'USER_GUILD_SETTINGS_UPDATE', settings)
**{'guild_id': guild_id}
})
return jsonify(settings) return jsonify(settings)

View File

@ -86,7 +86,7 @@ class GatewayWebsocket:
encoded = self.encoder(payload) encoded = self.encoder(payload)
if len(encoded) < 1024: if len(encoded) < 1024:
log.debug('sending {}', pprint.pformat(payload)) log.debug('sending\n{}', pprint.pformat(payload))
else: else:
log.debug('sending {}', pprint.pformat(payload)) log.debug('sending {}', pprint.pformat(payload))
log.debug('sending op={} s={} t={} (too big)', log.debug('sending op={} s={} t={} (too big)',

View File

@ -180,8 +180,8 @@ MEMBER_UPDATE = {
'required': False, 'required': False,
}, },
'roles': {'type': 'list', 'required': False}, 'roles': {'type': 'list', 'required': False},
'mute': {'type': 'bool', 'required': False}, 'mute': {'type': 'boolean', 'required': False},
'deaf': {'type': 'bool', 'required': False}, 'deaf': {'type': 'boolean', 'required': False},
'channel_id': {'type': 'snowflake', 'required': False}, 'channel_id': {'type': 'snowflake', 'required': False},
} }
@ -366,27 +366,30 @@ SPECIFIC_FRIEND = {
} }
GUILD_SETTINGS_CHAN_OVERRIDE = { GUILD_SETTINGS_CHAN_OVERRIDE = {
'muted': { 'type': 'dict',
'type': 'bool', 'required': False}, 'schema': {
'message_notifications': { 'muted': {
'type': 'msg_notifications', 'type': 'boolean', 'required': False},
'required': False, 'message_notifications': {
'type': 'msg_notifications',
'required': False,
}
} }
} }
GUILD_SETTINGS = { GUILD_SETTINGS = {
'channel_overrides': { 'channel_overrides': {
'type': 'dict', 'type': 'dict',
'schema': GUILD_SETTINGS_CHAN_OVERRIDE, 'valueschema': GUILD_SETTINGS_CHAN_OVERRIDE,
'keyschema': {'type': 'snowflake'}, 'keyschema': {'type': 'snowflake'},
'required': False, 'required': False,
}, },
'supress_everyone': { 'suppress_everyone': {
'type': 'bool', 'required': False}, 'type': 'boolean', 'required': False},
'muted': { 'muted': {
'type': 'bool', 'required': False}, 'type': 'boolean', 'required': False},
'mobile_push': { 'mobile_push': {
'type': 'bool', 'required': False}, 'type': 'boolean', 'required': False},
'message_notifications': { 'message_notifications': {
'type': 'msg_notifications', 'type': 'msg_notifications',
'required': False, 'required': False,

View File

@ -909,6 +909,49 @@ class Storage:
return parties[0] return parties[0]
async def get_guild_settings_one(self, user_id: int,
guild_id: int) -> dict:
"""Get guild settings information for a single guild."""
row = await self.db.fetchrow("""
SELECT guild_id::text, suppress_everyone, muted,
message_notifications, mobile_push
FROM guild_settings
WHERE user_id = $1 AND guild_id = $2
""", user_id, guild_id)
if not row:
await self.db.execute("""
INSERT INTO guild_settings (user_id, guild_id)
VALUES ($1, $2)
""", user_id, guild_id)
return await self.get_guild_settings_one(user_id, guild_id)
gid = int(row['guild_id'])
drow = dict(row)
chan_overrides = {}
overrides = await self.db.fetch("""
SELECT channel_id::text, muted, message_notifications
FROM guild_settings_channel_overrides
WHERE
user_id = $1
AND guild_id = $2
""", user_id, gid)
for chan_row in overrides:
dcrow = dict(chan_row)
chan_id = dcrow['channel_id']
dcrow.pop('channel_id')
chan_overrides[chan_id] = dcrow
return {**drow, **{
'channel_overrides': chan_overrides
}}
async def get_guild_settings(self, user_id: int): async def get_guild_settings(self, user_id: int):
"""Get the specific User Guild Settings, """Get the specific User Guild Settings,
for all guilds a user is on.""" for all guilds a user is on."""
@ -916,31 +959,37 @@ class Storage:
res = [] res = []
settings = await self.db.fetch(""" settings = await self.db.fetch("""
SELECT guild_id, suppress_everyone, muted SELECT guild_id::text, suppress_everyone, muted,
message_notifications, mobile_push message_notifications, mobile_push
FROM guild_settings FROM guild_settings
WHERE user_id = $1 WHERE user_id = $1
""", user_id) """, user_id)
for row in settings: for row in settings:
gid = row['guild_id'] print(dict(row))
gid = int(row['guild_id'])
drow = dict(row) drow = dict(row)
chan_ids = await self.get_channel_ids(gid)
chan_overrides = {} chan_overrides = {}
for chan_id in chan_ids: overrides = await self.db.fetch("""
chan_row = await self.db.fetchrow(""" SELECT channel_id::text, muted, message_notifications
SELECT muted, message_notifications FROM guild_settings_channel_overrides
FROM guild_setting_channel_overrides WHERE
WHERE user_id = $1
guild_id = $1 AND guild_id = $2
AND user_id = $2 """, user_id, gid)
AND channel_id = $3
""", gid, user_id, chan_id)
chan_overrides[str(chan_id)] = dict(chan_row) for chan_row in overrides:
dcrow = dict(chan_row)
# channel_id isn't on the value of the dict
# so we query it (for the key) then pop
# from the value
chan_id = dcrow['channel_id']
dcrow.pop('channel_id')
chan_overrides[chan_id] = dcrow
res.append({**drow, **{ res.append({**drow, **{
'channel_overrides': chan_overrides 'channel_overrides': chan_overrides