litecord.blueprints: add channels blueprint

This commit is contained in:
Luna Mendes 2018-07-02 05:06:57 -03:00
parent d62db421b0
commit 4ea3d353b3
6 changed files with 109 additions and 2 deletions

View File

@ -2,3 +2,4 @@ from .gateway import bp as gateway
from .auth import bp as auth from .auth import bp as auth
from .users import bp as users from .users import bp as users
from .guilds import bp as guilds from .guilds import bp as guilds
from .channels import bp as channels

View File

@ -0,0 +1,83 @@
from quart import Blueprint, request, current_app as app, jsonify
from ..auth import token_check
from ..snowflake import get_snowflake
from ..enums import ChannelType
from ..errors import Forbidden, BadRequest, MessageNotFound
from ..schemas import validate
from .guilds import guild_check
bp = Blueprint('channels', __name__)
async def channel_check(user_id, channel_id):
ctype = await app.db.fetchval("""
SELECT channel_type
FROM channels
WHERE channels.id = $1
""", channel_id)
if ctype in (ChannelType.GUILD_TEXT, ChannelType.GUILD_VOICE,
ChannelType.GUILD_CATEGORY):
guild_id = await app.db.fetchval("""
SELECT guild_id
FROM guild_channels
WHERE channel_id = $1
""", channel_id)
await guild_check(user_id, guild_id)
@bp.route('/<int:channel_id>', methods=['GET'])
async def get_channel(channel_id):
user_id = await token_check()
await channel_check(user_id, channel_id)
return '', 204
@bp.route('/<int:channel_id>/messages', methods=['GET'])
async def get_messages(channel_id):
user_id = await token_check()
await channel_check(user_id, channel_id)
# TODO: before, after, around keys
await app.db.fetch(f"""
SELECT *
FROM messages
WHERE channel_id = $1
ORDER BY id ASC
LIMIT 100
""")
@bp.route('/<int:channel_id>/messages/<int:message_id>', methods=['GET'])
async def get_single_message(channel_id, message_id):
user_id = await token_check()
await channel_check(user_id, channel_id)
# TODO: check READ_MESSAGE_HISTORY permissions
message = await app.db.fetchrow("""
SELECT *
FROM messages
WHERE channel_id = $1 AND messages.id = $2
""", channel_id, message_id)
if not message:
raise MessageNotFound()
@bp.route('/<int:channel_id>/messages', methods=['POST'])
async def create_message(channel_id):
user_id = await token_check()
await channel_check(user_id, channel_id)
# TODO: check SEND_MESSAGES permission
# TODO: check SEND_TTS_MESSAGES
# TODO: check connection to the gateway
# TODO: parse payload, make schema
# TODO: insert and dispatch message

View File

@ -5,3 +5,21 @@ class ChannelType:
GUILD_VOICE = 2 GUILD_VOICE = 2
GROUP_DM = 3 GROUP_DM = 3
GUILD_CATEGORY = 4 GUILD_CATEGORY = 4
class MessageType:
DEFAULT = 0
RECIPIENT_ADD = 1
RECIPIENT_REMOVE = 2
CALL = 3
CHANNEL_NAME_CHANGE = 4
CHANNEL_ICON_CHANGE = 5
CHANNEL_PINNED_MESSAGE = 6
GUILD_MEMBER_JOIN = 7
class MessageActivityType:
JOIN = 1
SPECTATE = 2
LISTEN = 3
JOIN_REQUEST = 5

View File

@ -30,6 +30,10 @@ class GuildNotFound(LitecordError):
status_code = 404 status_code = 404
class MessageNotFound(LitecordError):
status_code = 404
class WebsocketClose(Exception): class WebsocketClose(Exception):
@property @property
def code(self): def code(self):

View File

@ -63,7 +63,7 @@ class Storage:
if drow['system_channel_id'] else None if drow['system_channel_id'] else None
return {**drow, **{ return {**drow, **{
'roles': [], # TODO: those
'emojis': [], 'emojis': [],
}} }}

3
run.py
View File

@ -9,7 +9,7 @@ from logbook import StreamHandler, Logger
from logbook.compat import redirect_logging from logbook.compat import redirect_logging
import config import config
from litecord.blueprints import gateway, auth, users, guilds from litecord.blueprints import gateway, auth, users, guilds, channels
from litecord.gateway import websocket_handler from litecord.gateway import websocket_handler
from litecord.errors import LitecordError from litecord.errors import LitecordError
from litecord.gateway.state_manager import StateManager from litecord.gateway.state_manager import StateManager
@ -40,6 +40,7 @@ app.register_blueprint(gateway, url_prefix='/api/v6')
app.register_blueprint(auth, url_prefix='/api/v6') app.register_blueprint(auth, url_prefix='/api/v6')
app.register_blueprint(users, url_prefix='/api/v6/users') app.register_blueprint(users, url_prefix='/api/v6/users')
app.register_blueprint(guilds, url_prefix='/api/v6/guilds') app.register_blueprint(guilds, url_prefix='/api/v6/guilds')
app.register_blueprint(channels, url_prefix='/api/v6/channels')
@app.before_serving @app.before_serving