mirror of https://gitlab.com/litecord/litecord.git
litecord.blueprints: add channels blueprint
This commit is contained in:
parent
d62db421b0
commit
4ea3d353b3
|
|
@ -2,3 +2,4 @@ from .gateway import bp as gateway
|
|||
from .auth import bp as auth
|
||||
from .users import bp as users
|
||||
from .guilds import bp as guilds
|
||||
from .channels import bp as channels
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -5,3 +5,21 @@ class ChannelType:
|
|||
GUILD_VOICE = 2
|
||||
GROUP_DM = 3
|
||||
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
|
||||
|
|
|
|||
|
|
@ -30,6 +30,10 @@ class GuildNotFound(LitecordError):
|
|||
status_code = 404
|
||||
|
||||
|
||||
class MessageNotFound(LitecordError):
|
||||
status_code = 404
|
||||
|
||||
|
||||
class WebsocketClose(Exception):
|
||||
@property
|
||||
def code(self):
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ class Storage:
|
|||
if drow['system_channel_id'] else None
|
||||
|
||||
return {**drow, **{
|
||||
'roles': [],
|
||||
# TODO: those
|
||||
'emojis': [],
|
||||
}}
|
||||
|
||||
|
|
|
|||
3
run.py
3
run.py
|
|
@ -9,7 +9,7 @@ from logbook import StreamHandler, Logger
|
|||
from logbook.compat import redirect_logging
|
||||
|
||||
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.errors import LitecordError
|
||||
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(users, url_prefix='/api/v6/users')
|
||||
app.register_blueprint(guilds, url_prefix='/api/v6/guilds')
|
||||
app.register_blueprint(channels, url_prefix='/api/v6/channels')
|
||||
|
||||
|
||||
@app.before_serving
|
||||
|
|
|
|||
Loading…
Reference in New Issue