From 378809bdd6d7ce6a31dd30ca39203afd5aacd0eb Mon Sep 17 00:00:00 2001 From: Luna Mendes Date: Fri, 2 Nov 2018 19:14:41 -0300 Subject: [PATCH] all: misc fixes - gateway.websocket: fix etf dict decode - auth: better token parsing - auth: fix new_discrim calc - channel.messages: call _dm_pre_dispatch on get_messages - channels: fix get_pins - guilds: make sure guild owner has guild everyone role - invites: replace sub_guild to sub --- litecord/auth.py | 6 ++++- litecord/blueprints/auth.py | 2 +- litecord/blueprints/channel/messages.py | 9 ++++++- litecord/blueprints/channels.py | 2 +- litecord/blueprints/guilds.py | 6 +++++ litecord/blueprints/invites.py | 2 +- litecord/gateway/websocket.py | 32 ++++++++++++++++++++++++- litecord/storage.py | 4 ++++ 8 files changed, 57 insertions(+), 6 deletions(-) diff --git a/litecord/auth.py b/litecord/auth.py index 9b5e9c7..af241b5 100644 --- a/litecord/auth.py +++ b/litecord/auth.py @@ -13,7 +13,11 @@ log = Logger(__name__) async def raw_token_check(token, db=None): db = db or app.db - user_id, _hmac = token.split('.') + + # just try by fragments instead of + # unpacking + fragments = token.split('.') + user_id = fragments[0] try: user_id = base64.b64decode(user_id.encode()) diff --git a/litecord/blueprints/auth.py b/litecord/blueprints/auth.py index 17e77f3..ce5544e 100644 --- a/litecord/blueprints/auth.py +++ b/litecord/blueprints/auth.py @@ -65,7 +65,7 @@ async def register(): new_id = get_snowflake() - new_discrim = str(random.randint(1, 9999)) + new_discrim = random.randint(1, 9999) new_discrim = '%04d' % new_discrim pwd_hash = await hash_data(password) diff --git a/litecord/blueprints/channel/messages.py b/litecord/blueprints/channel/messages.py index 7ea70fb..231f83d 100644 --- a/litecord/blueprints/channel/messages.py +++ b/litecord/blueprints/channel/messages.py @@ -51,7 +51,13 @@ async def get_messages(channel_id): user_id = await token_check() # TODO: check READ_MESSAGE_HISTORY permission - await channel_check(user_id, channel_id) + ctype, peer_id = await channel_check(user_id, channel_id) + + if ctype == ChannelType.DM: + # make sure both parties will be subbed + # to a dm + await _dm_pre_dispatch(channel_id, user_id) + await _dm_pre_dispatch(channel_id, peer_id) limit = extract_limit(request, 50) @@ -166,6 +172,7 @@ async def create_message(channel_id): if ctype == ChannelType.DM: # guild id here is the peer's ID. + await _dm_pre_dispatch(channel_id, user_id) await _dm_pre_dispatch(channel_id, guild_id) await app.dispatcher.dispatch('channel', channel_id, diff --git a/litecord/blueprints/channels.py b/litecord/blueprints/channels.py index e982007..2259b3e 100644 --- a/litecord/blueprints/channels.py +++ b/litecord/blueprints/channels.py @@ -233,7 +233,7 @@ async def get_pins(channel_id): if message is not None: res.append(message) - return jsonify(message) + return jsonify(res) @bp.route('//pins/', methods=['PUT']) diff --git a/litecord/blueprints/guilds.py b/litecord/blueprints/guilds.py index caaf319..cd40ec2 100644 --- a/litecord/blueprints/guilds.py +++ b/litecord/blueprints/guilds.py @@ -120,6 +120,12 @@ async def create_guild(): VALUES ($1, $2, $3, $4, $5) """, guild_id, guild_id, '@everyone', 0, DEFAULT_EVERYONE_PERMS) + # add the @everyone role to the guild creator + await app.db.execute(""" + INSERT INTO member_roles (user_id, guild_id, role_id) + VALUES ($1, $2, $3) + """, user_id, guild_id, guild_id) + # create a single #general channel. general_id = get_snowflake() diff --git a/litecord/blueprints/invites.py b/litecord/blueprints/invites.py index 246b8d0..2172ea7 100644 --- a/litecord/blueprints/invites.py +++ b/litecord/blueprints/invites.py @@ -185,7 +185,7 @@ async def use_invite(invite_code): }) # subscribe new member to guild, so they get events n stuff - app.dispatcher.sub_guild(guild_id, user_id) + await app.dispatcher.sub('guild', guild_id, user_id) # tell the new member that theres the guild it just joined. # we use dispatch_user_guild so that we send the GUILD_CREATE diff --git a/litecord/gateway/websocket.py b/litecord/gateway/websocket.py index 515eac6..90bbc07 100644 --- a/litecord/gateway/websocket.py +++ b/litecord/gateway/websocket.py @@ -44,8 +44,38 @@ def encode_etf(payload) -> str: return earl.pack(payload) +def _etf_decode_dict(data): + # NOTE: this is a very slow implementation to + # decode the dictionary. + + if isinstance(data, bytes): + return data.decode() + + if not isinstance(data, dict): + return data + + _copy = dict(data) + result = {} + + for key in _copy.keys(): + # assuming key is bytes rn. + new_k = key.decode() + + # maybe nested dicts, so... + result[new_k] = _etf_decode_dict(data[key]) + + return result + def decode_etf(data: bytes): - return earl.unpack(data) + res = earl.unpack(data) + + if isinstance(res, bytes): + return data.decode() + + if isinstance(res, dict): + return _etf_decode_dict(res) + + return res class GatewayWebsocket: diff --git a/litecord/storage.py b/litecord/storage.py index 0c141b3..61792da 100644 --- a/litecord/storage.py +++ b/litecord/storage.py @@ -173,6 +173,10 @@ class Storage: return { 'user': await self.get_user(member_id), 'nick': row['nickname'], + + # we don't send the @everyone role's id to + # the user since it is known that everyone has + # that role. 'roles': [r['role_id'] for r in roles], 'joined_at': row['joined_at'].isoformat(), 'deaf': row['deafened'],