channel.pins: deny pins from system messages

suggested by @Cynosphere
This commit is contained in:
Luna 2019-01-25 19:40:56 -03:00
parent 935b4e15aa
commit 307a22e135
3 changed files with 31 additions and 2 deletions

View File

@ -25,11 +25,17 @@ from litecord.snowflake import snowflake_datetime
from litecord.types import timestamp_
from litecord.system_messages import send_sys_message
from litecord.enums import MessageType
from litecord.enums import MessageType, SYS_MESSAGES
from litecord.errors import BadRequest
bp = Blueprint('channel_pins', __name__)
class SysMsgInvalidAction(BadRequest):
"""Invalid action on a system message."""
error_code = 50021
@bp.route('/<int:channel_id>/pins', methods=['GET'])
async def get_pins(channel_id):
"""Get the pins for a channel"""
@ -62,6 +68,16 @@ async def add_pin(channel_id, message_id):
await channel_perm_check(user_id, channel_id, 'manage_messages')
mtype = await app.db.fetchval("""
SELECT message_type
FROM messages
WHERE id = $1
""", message_id)
if mtype in SYS_MESSAGES:
raise SysMsgInvalidAction(
'Cannot execute action on a system message')
await app.db.execute("""
INSERT INTO channel_pins (channel_id, message_id)
VALUES ($1, $2)

View File

@ -101,6 +101,17 @@ class MessageType(EasyEnum):
GUILD_MEMBER_JOIN = 7
SYS_MESSAGES = (
MessageType.RECIPIENT_ADD,
MessageType.RECIPIENT_REMOVE,
MessageType.CALL,
MessageType.CHANNEL_NAME_CHANGE,
MessageType.CHANNEL_ICON_CHANGE,
MessageType.CHANNEL_PINNED_MESSAGE,
MessageType.GUILD_MEMBER_JOIN
)
class MessageActivityType(EasyEnum):
JOIN = 1
SPECTATE = 2

View File

@ -42,7 +42,7 @@ async def _handle_pin_msg(app, channel_id, pinned_id, author_id):
async def send_sys_message(app, channel_id: int, m_type: MessageType,
*args, **kwargs):
*args, **kwargs) -> int:
"""Send a system message."""
handler = {
MessageType.CHANNEL_PINNED_MESSAGE: _handle_pin_msg,
@ -55,3 +55,5 @@ async def send_sys_message(app, channel_id: int, m_type: MessageType,
await app.dispatcher.dispatch(
'channel', channel_id, 'MESSAGE_CREATE', message
)
return message_id