From 87b1257e06a43cdda20106828a74539f41c08175 Mon Sep 17 00:00:00 2001 From: Luna Date: Sat, 10 Jul 2021 23:28:17 -0300 Subject: [PATCH] move code to dedicated read_states blueprint --- litecord/blueprints/__init__.py | 2 + litecord/blueprints/channels.py | 46 ---------------- litecord/blueprints/guilds.py | 15 ------ litecord/blueprints/read_states.py | 87 ++++++++++++++++++++++++++++++ litecord/common/channels.py | 5 +- run.py | 2 + 6 files changed, 95 insertions(+), 62 deletions(-) create mode 100644 litecord/blueprints/read_states.py diff --git a/litecord/blueprints/__init__.py b/litecord/blueprints/__init__.py index cc15930..1911c12 100644 --- a/litecord/blueprints/__init__.py +++ b/litecord/blueprints/__init__.py @@ -33,6 +33,7 @@ from .nodeinfo import bp as nodeinfo from .static import bp as static from .attachments import bp as attachments from .dm_channels import bp as dm_channels +from .read_states import bp as read_states __all__ = [ "gateway", @@ -51,4 +52,5 @@ __all__ = [ "static", "attachments", "dm_channels", + "read_states", ] diff --git a/litecord/blueprints/channels.py b/litecord/blueprints/channels.py index 2681b12..ae61820 100644 --- a/litecord/blueprints/channels.py +++ b/litecord/blueprints/channels.py @@ -42,10 +42,8 @@ from litecord.system_messages import send_sys_message from litecord.blueprints.dm_channels import gdm_remove_recipient, gdm_destroy from litecord.utils import search_result_from_list from litecord.embed.messages import process_url_embed, msg_update_embeds -from litecord.common.channels import channel_ack from litecord.pubsub.user import dispatch_user from litecord.permissions import get_permissions, Permissions -from litecord.errors import GuildNotFound log = Logger(__name__) bp = Blueprint("channels", __name__) @@ -653,50 +651,6 @@ async def trigger_typing(channel_id): return "", 204 -@bp.route("//messages//ack", methods=["POST"]) -async def ack_channel(channel_id, message_id): - """Acknowledge a channel.""" - user_id = await token_check() - 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) - - return jsonify( - { - # token seems to be used for - # data collection activities, - # so we never use it. - "token": None - } - ) - - -@bp.route("//messages/ack", methods=["DELETE"]) -async def delete_read_state(channel_id): - """Delete the read state of a channel.""" - user_id = await token_check() - try: - await channel_check(user_id, channel_id) - except GuildNotFound: - # ignore when guild isn't found because we're deleting the - # read state regardless. - pass - - await app.db.execute( - """ - DELETE FROM user_read_state - WHERE user_id = $1 AND channel_id = $2 - """, - user_id, - channel_id, - ) - - return "", 204 - - @bp.route("//messages/search", methods=["GET"]) async def _search_channel(channel_id): """Search in DMs or group DMs""" diff --git a/litecord/blueprints/guilds.py b/litecord/blueprints/guilds.py index ff63520..d419a90 100644 --- a/litecord/blueprints/guilds.py +++ b/litecord/blueprints/guilds.py @@ -39,7 +39,6 @@ from ..schemas import ( VANITY_URL_PATCH, ) from .checks import guild_check, guild_owner_check, guild_perm_check -from ..common.channels import channel_ack from litecord.utils import to_update, search_result_from_list from litecord.errors import BadRequest from litecord.permissions import get_permissions @@ -438,20 +437,6 @@ async def search_messages(guild_id): return jsonify(await search_result_from_list(rows)) -@bp.route("//ack", methods=["POST"]) -async def ack_guild(guild_id): - """ACKnowledge all messages in the guild.""" - user_id = await token_check() - await guild_check(user_id, guild_id) - - chan_ids = await app.storage.get_channel_ids(guild_id) - - for chan_id in chan_ids: - await channel_ack(user_id, guild_id, chan_id) - - return "", 204 - - @bp.route("//vanity-url", methods=["GET"]) async def get_vanity_url(guild_id: int): """Get the vanity url of a guild.""" diff --git a/litecord/blueprints/read_states.py b/litecord/blueprints/read_states.py new file mode 100644 index 0000000..5df255a --- /dev/null +++ b/litecord/blueprints/read_states.py @@ -0,0 +1,87 @@ +""" + +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 . + +""" + + +from quart import Blueprint, current_app as app, jsonify +from ..auth import token_check +from ..enums import ChannelType +from ..common.channels import channel_ack +from litecord.errors import GuildNotFound +from litecord.blueprints.checks import channel_check, guild_check + + +bp = Blueprint("read_states", __name__) + + +@bp.route("/channels//messages//ack", methods=["POST"]) +async def ack_channel(channel_id, message_id): + """Acknowledge a channel.""" + user_id = await token_check() + ctype, guild_id = await channel_check(user_id, channel_id) + + if ctype == ChannelType.DM: + guild_id = None + + await channel_ack(user_id, channel_id, guild_id, message_id) + + return jsonify( + { + # token seems to be used for + # data collection activities, + # so we never use it. + "token": None + } + ) + + +@bp.route("/channels//messages/ack", methods=["DELETE"]) +async def delete_read_state(channel_id): + """Delete the read state of a channel.""" + user_id = await token_check() + try: + await channel_check(user_id, channel_id) + except GuildNotFound: + # ignore when guild isn't found because we're deleting the + # read state regardless. + pass + + await app.db.execute( + """ + DELETE FROM user_read_state + WHERE user_id = $1 AND channel_id = $2 + """, + user_id, + channel_id, + ) + + return "", 204 + + +@bp.route("/guilds//ack", methods=["POST"]) +async def ack_guild(guild_id): + """ACKnowledge all messages in the guild.""" + user_id = await token_check() + await guild_check(user_id, guild_id) + + chan_ids = await app.storage.get_channel_ids(guild_id) + + for chan_id in chan_ids: + await channel_ack(user_id, chan_id, guild_id) + + return "", 204 diff --git a/litecord/common/channels.py b/litecord/common/channels.py index 71f0c3d..08ff7f0 100644 --- a/litecord/common/channels.py +++ b/litecord/common/channels.py @@ -27,7 +27,10 @@ from litecord.pubsub.user import dispatch_user async def channel_ack( - user_id: int, guild_id: int, channel_id: int, message_id: Optional[int] = None + user_id: int, + channel_id: int, + guild_id: Optional[int] = None, + message_id: Optional[int] = None, ): """ACK a channel.""" diff --git a/run.py b/run.py index a9d69c0..2118c2c 100644 --- a/run.py +++ b/run.py @@ -50,6 +50,7 @@ from litecord.blueprints import ( static, attachments, dm_channels, + read_states, ) # those blueprints are separated from the "main" ones @@ -167,6 +168,7 @@ def set_blueprints(app_): guilds_admin: "/admin/guilds", users_admin: "/admin/users", instance_invites: "/admin/instance/invites", + read_states: "", } for bp, suffix in bps.items():