From c43a34cd81b155c0d116dd1d6ba9f992d86c3968 Mon Sep 17 00:00:00 2001 From: Jason Robinson Date: Sun, 27 Feb 2022 02:46:04 +0200 Subject: [PATCH] =?UTF-8?q?Add=20possibility=20to=20get=20another=20articl?= =?UTF-8?q?e=20by=20reacting=20with=20=F0=9F=91=8D=20or=20=E2=9E=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Release v0.2.3 --- CHANGELOG.md | 4 ++++ maubot.yaml | 2 +- pocket/bot.py | 43 ++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3509eaa..689468f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## v0.2.3 (2022-02-27) + +* Add possibility to get another article by reacting with 👍 or ➕. + ## v0.2.0 (2021-08-08) * Initial version with the following features diff --git a/maubot.yaml b/maubot.yaml index be5dedd..f1262ee 100644 --- a/maubot.yaml +++ b/maubot.yaml @@ -5,7 +5,7 @@ maubot: 0.1.2 id: me.jasonrobinson.pocket # A PEP 440 compliant version string. -version: 0.2.0 +version: 0.2.3 # The SPDX license identifier for the plugin. https://spdx.org/licenses/ # Optional, assumes all rights reserved if omitted. diff --git a/pocket/bot.py b/pocket/bot.py index 9acd1ab..0b4e76d 100644 --- a/pocket/bot.py +++ b/pocket/bot.py @@ -21,6 +21,45 @@ def do_update(self, helper: ConfigUpdateHelper) -> None: class PocketPlugin(Plugin): db: Database + @command.passive( + event_type=EventType.REACTION, + field=lambda e: e.content.relates_to.key, + msgtypes=[], + regex=r"\U00002795|\U0001F44D", + ) + async def another(self, event: ReactionEvent, _: Tuple[str]) -> None: + item_event = self.db.get_user_event(event.sender, event.content.relates_to.event_id) + if not item_event: + return + + user = self.db.get_user_by_id(event.sender) + if not user or not user.access_token: + await self.client.react( + event.room_id, + event.content.relates_to.event_id, + "❌", + ) + return + + article = await self.get_random_article(user) + if not article: + await self.client.react( + event.room_id, + event.content.relates_to.event_id, + "🤷", + ) + return + event_id = await self.client.send_markdown( + room_id=event.room_id, + markdown=self.format_article_message(article), + ) + self.db.store_user_event(event.sender, event_id, article["item_id"]) + + @staticmethod + def format_article_message(article): + return f"{article['resolved_title']} - {article['resolved_url']} " \ + f"(✅ to archive, [view in Pocket](https://getpocket.com/read/{article['item_id']}), 👍/➕ for another)" + @command.passive( event_type=EventType.REACTION, field=lambda e: e.content.relates_to.key, @@ -137,9 +176,7 @@ async def handler(self, event: MessageEvent) -> None: if not article: await event.respond("Didn't find any saved articles. Is your Pocket empty? Or did we hit an error?") return - event_id = await event.respond( - f"{article['resolved_title']} - {article['resolved_url']} " - f"(react ✅ to archive, [open in Pocket](https://getpocket.com/read/{article['item_id']}))") + event_id = await event.respond(self.format_article_message(article)) self.db.store_user_event(event.sender, event_id, article["item_id"]) @handler.subcommand(help="Authenticate with Pocket")