blueprints.channels: add route to delete read_state of a channel

- blueprints.checks: change return type from snowflake to (ChannelType,
    snowflake)
This commit is contained in:
Luna Mendes 2018-10-11 17:53:32 -03:00
parent b7f0bd1bb4
commit 5372292f0d
4 changed files with 29 additions and 13 deletions

View File

@ -141,7 +141,7 @@ async def close_channel(channel_id):
ctype = ChannelType(chan_type)
if ctype in GUILD_CHANS:
guild_id = await channel_check(user_id, channel_id)
_, guild_id = await channel_check(user_id, channel_id)
chan = await app.storage.get_channel(channel_id)
# the selected function will take care of checking
@ -256,7 +256,7 @@ async def get_single_message(channel_id, message_id):
@bp.route('/<int:channel_id>/messages', methods=['POST'])
async def create_message(channel_id):
user_id = await token_check()
guild_id = await channel_check(user_id, channel_id)
_ctype, guild_id = await channel_check(user_id, channel_id)
j = validate(await request.get_json(), MESSAGE_CREATE)
message_id = get_snowflake()
@ -304,7 +304,7 @@ async def create_message(channel_id):
@bp.route('/<int:channel_id>/messages/<int:message_id>', methods=['PATCH'])
async def edit_message(channel_id, message_id):
user_id = await token_check()
guild_id = await channel_check(user_id, channel_id)
_ctype, guild_id = await channel_check(user_id, channel_id)
author_id = await app.db.fetchval("""
SELECT author_id FROM messages
@ -339,7 +339,7 @@ async def edit_message(channel_id, message_id):
@bp.route('/<int:channel_id>/messages/<int:message_id>', methods=['DELETE'])
async def delete_message(channel_id, message_id):
user_id = await token_check()
guild_id = await channel_check(user_id, channel_id)
_ctype, guild_id = await channel_check(user_id, channel_id)
author_id = await app.db.fetchval("""
SELECT author_id FROM messages
@ -392,7 +392,7 @@ async def get_pins(channel_id):
@bp.route('/<int:channel_id>/pins/<int:message_id>', methods=['PUT'])
async def add_pin(channel_id, message_id):
user_id = await token_check()
guild_id = await channel_check(user_id, channel_id)
_ctype, guild_id = await channel_check(user_id, channel_id)
# TODO: check MANAGE_MESSAGES permission
@ -422,7 +422,7 @@ async def add_pin(channel_id, message_id):
@bp.route('/<int:channel_id>/pins/<int:message_id>', methods=['DELETE'])
async def delete_pin(channel_id, message_id):
user_id = await token_check()
guild_id = await channel_check(user_id, channel_id)
_ctype, guild_id = await channel_check(user_id, channel_id)
# TODO: check MANAGE_MESSAGES permission
@ -453,7 +453,7 @@ async def delete_pin(channel_id, message_id):
@bp.route('/<int:channel_id>/typing', methods=['POST'])
async def trigger_typing(channel_id):
user_id = await token_check()
guild_id = await channel_check(user_id, channel_id)
_ctype, guild_id = await channel_check(user_id, channel_id)
# TODO: dispatch_channel
await app.dispatcher.dispatch_guild(guild_id, 'TYPING_START', {
@ -508,7 +508,10 @@ async def channel_ack(user_id, guild_id, channel_id, message_id: int = None):
@bp.route('/<int:channel_id>/messages/<int:message_id>/ack', methods=['POST'])
async def ack_channel(channel_id, message_id):
user_id = await token_check()
guild_id = await channel_check(user_id, channel_id)
ctype, guild_id = await channel_check(user_id, channel_id)
if ctype == ChannelType.DM:
guild_id = None
await channel_ack(user_id, guild_id, channel_id, message_id)
@ -518,3 +521,16 @@ async def ack_channel(channel_id, message_id):
# so we never use it.
'token': None
})
@bp.route('/<int:channel_id>/messages/ack', methods=['DELETE'])
async def delete_read_state(channel_id):
user_id = await token_check()
await channel_check(user_id, channel_id)
await app.db.execute("""
DELETE FROM user_read_state
WHERE user_id = $1 AND channel_id = $2
""", user_id, channel_id)
return '', 204

View File

@ -34,7 +34,7 @@ async def channel_check(user_id, channel_id):
""", channel_id)
await guild_check(user_id, guild_id)
return guild_id
return ctype, guild_id
if ctype == ChannelType.DM:
parties = await app.db.fetchrow("""
@ -47,4 +47,4 @@ async def channel_check(user_id, channel_id):
# get the id of the other party
parties.remove(user_id)
return parties[0]
return ctype, parties[0]

View File

@ -6,7 +6,7 @@ from ..enums import ChannelType
from ..errors import Forbidden, GuildNotFound, BadRequest
from ..schemas import validate, GUILD_UPDATE
from .channels import channel_ack
from .checks import guild_check, channel_check
from .checks import guild_check
bp = Blueprint('guilds', __name__)

View File

@ -21,7 +21,7 @@ async def create_invite(channel_id):
user_id = await token_check()
j = validate(await request.get_json(), INVITE)
guild_id = await channel_check(user_id, channel_id)
_ctype, guild_id = await channel_check(user_id, channel_id)
# TODO: check CREATE_INSTANT_INVITE permission
@ -125,7 +125,7 @@ async def get_guild_invites(guild_id: int):
@bp.route('/channels/<int:channel_id>/invites', methods=['GET'])
async def get_channel_invites(channel_id: int):
user_id = await token_check()
guild_id = await channel_check(user_id, channel_id)
_ctype, guild_id = await channel_check(user_id, channel_id)
inv_codes = await app.db.fetch("""
SELECT code