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
This commit is contained in:
Luna Mendes 2018-11-02 19:14:41 -03:00
parent 2b1f9489b7
commit 378809bdd6
8 changed files with 57 additions and 6 deletions

View File

@ -13,7 +13,11 @@ log = Logger(__name__)
async def raw_token_check(token, db=None): async def raw_token_check(token, db=None):
db = db or app.db 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: try:
user_id = base64.b64decode(user_id.encode()) user_id = base64.b64decode(user_id.encode())

View File

@ -65,7 +65,7 @@ async def register():
new_id = get_snowflake() new_id = get_snowflake()
new_discrim = str(random.randint(1, 9999)) new_discrim = random.randint(1, 9999)
new_discrim = '%04d' % new_discrim new_discrim = '%04d' % new_discrim
pwd_hash = await hash_data(password) pwd_hash = await hash_data(password)

View File

@ -51,7 +51,13 @@ async def get_messages(channel_id):
user_id = await token_check() user_id = await token_check()
# TODO: check READ_MESSAGE_HISTORY permission # 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) limit = extract_limit(request, 50)
@ -166,6 +172,7 @@ async def create_message(channel_id):
if ctype == ChannelType.DM: if ctype == ChannelType.DM:
# guild id here is the peer's ID. # 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 _dm_pre_dispatch(channel_id, guild_id)
await app.dispatcher.dispatch('channel', channel_id, await app.dispatcher.dispatch('channel', channel_id,

View File

@ -233,7 +233,7 @@ async def get_pins(channel_id):
if message is not None: if message is not None:
res.append(message) res.append(message)
return jsonify(message) return jsonify(res)
@bp.route('/<int:channel_id>/pins/<int:message_id>', methods=['PUT']) @bp.route('/<int:channel_id>/pins/<int:message_id>', methods=['PUT'])

View File

@ -120,6 +120,12 @@ async def create_guild():
VALUES ($1, $2, $3, $4, $5) VALUES ($1, $2, $3, $4, $5)
""", guild_id, guild_id, '@everyone', 0, DEFAULT_EVERYONE_PERMS) """, 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. # create a single #general channel.
general_id = get_snowflake() general_id = get_snowflake()

View File

@ -185,7 +185,7 @@ async def use_invite(invite_code):
}) })
# subscribe new member to guild, so they get events n stuff # 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. # tell the new member that theres the guild it just joined.
# we use dispatch_user_guild so that we send the GUILD_CREATE # we use dispatch_user_guild so that we send the GUILD_CREATE

View File

@ -44,8 +44,38 @@ def encode_etf(payload) -> str:
return earl.pack(payload) 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): 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: class GatewayWebsocket:

View File

@ -173,6 +173,10 @@ class Storage:
return { return {
'user': await self.get_user(member_id), 'user': await self.get_user(member_id),
'nick': row['nickname'], '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], 'roles': [r['role_id'] for r in roles],
'joined_at': row['joined_at'].isoformat(), 'joined_at': row['joined_at'].isoformat(),
'deaf': row['deafened'], 'deaf': row['deafened'],