From e5bc3d00973f1f46b61f4472270766070f509f3f Mon Sep 17 00:00:00 2001 From: NotNite Date: Mon, 20 Sep 2021 15:28:57 -0400 Subject: [PATCH 1/2] Remove outdated docs --- litecord/system_messages.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/litecord/system_messages.py b/litecord/system_messages.py index 5f1dfe4..8431aeb 100644 --- a/litecord/system_messages.py +++ b/litecord/system_messages.py @@ -150,8 +150,6 @@ async def send_sys_message( Parameters ---------- - app - The app instance. channel_id The channel ID to send the system message to. m_type From 787d83cd809a12ee6ee144b9ca74571fba94d197 Mon Sep 17 00:00:00 2001 From: NotNite Date: Mon, 20 Sep 2021 16:49:10 -0400 Subject: [PATCH 2/2] Add guild join system message --- litecord/common/guilds.py | 15 ++++++++-- litecord/system_messages.py | 21 ++++++++++++++ tests/test_invites.py | 58 +++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 tests/test_invites.py diff --git a/litecord/common/guilds.py b/litecord/common/guilds.py index a9f3b88..c00ce80 100644 --- a/litecord/common/guilds.py +++ b/litecord/common/guilds.py @@ -23,8 +23,9 @@ from quart import current_app as app from ..permissions import get_role_perms, get_permissions from ..utils import dict_get, maybe_lazy_guild_dispatch -from ..enums import ChannelType +from ..enums import ChannelType, MessageType from litecord.pubsub.member import dispatch_member +from litecord.system_messages import send_sys_message log = Logger(__name__) @@ -323,7 +324,17 @@ async def add_member(guild_id: int, user_id: int, *, basic=False): if basic: return - # TODO: system message for member join + system_channel_id = await app.db.fetchval( + """ + SELECT system_channel_id FROM guilds + WHERE id = $1 + """, + guild_id, + ) + if system_channel_id: + await send_sys_message( + system_channel_id, MessageType.GUILD_MEMBER_JOIN, user_id + ) await app.db.execute( """ diff --git a/litecord/system_messages.py b/litecord/system_messages.py index 8431aeb..b32168d 100644 --- a/litecord/system_messages.py +++ b/litecord/system_messages.py @@ -46,6 +46,26 @@ async def _handle_pin_msg(channel_id, _pinned_id, author_id): return new_id +async def _handle_guild_join_msg(channel_id, user_id): + """Handle the system join message.""" + new_id = app.winter_factory.snowflake() + + await app.db.execute( + """ + INSERT INTO messages + (id, channel_id, guild_id, author_id, content, message_type) + VALUES + ($1, $2, NULL, $3, '', $4) + """, + new_id, + channel_id, + user_id, + MessageType.GUILD_MEMBER_JOIN.value, + ) + + return new_id + + # TODO: decrease repetition between add and remove handlers async def _handle_recp_add(channel_id, author_id, peer_id): new_id = app.winter_factory.snowflake() @@ -163,6 +183,7 @@ async def send_sys_message( try: handler = { MessageType.CHANNEL_PINNED_MESSAGE: _handle_pin_msg, + MessageType.GUILD_MEMBER_JOIN: _handle_guild_join_msg, # gdm specific MessageType.RECIPIENT_ADD: _handle_recp_add, MessageType.RECIPIENT_REMOVE: _handle_recp_rmv, diff --git a/tests/test_invites.py b/tests/test_invites.py new file mode 100644 index 0000000..71320b6 --- /dev/null +++ b/tests/test_invites.py @@ -0,0 +1,58 @@ +""" + +Litecord +Copyright (C) 2018-2019 Luna Mendes + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 3 of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +""" + +import pytest + +pytestmark = pytest.mark.asyncio + +# todo: maybe add more tests lol +# :) + + +async def test_invite_join(test_cli_user): + guild = await test_cli_user.create_guild() + channel = guild.channels[0] + + resp = await test_cli_user.patch( + f"/api/v9/guilds/{guild.id}", + json={ + "system_channel_id": channel["id"], + }, + ) + + assert resp.status_code == 200 + rjson = await resp.json + assert rjson["system_channel_id"] == channel["id"] + + resp = await test_cli_user.post( + f'/api/v9/channels/{channel["id"]}/invites', json={} + ) + + assert resp.status_code == 200 + rjson = await resp.json + invite_code = rjson["code"] + assert rjson["channel"]["id"] == channel["id"] + assert rjson["guild"]["id"] == str(guild.id) + + user = await test_cli_user.create_user() + + resp = await test_cli_user.post( + f"/api/v9/invites/{invite_code}", headers={"Authorization": user.token} + ) + assert resp.status_code == 200