From 4e3b9ef416429ebc1b6e320e6441d928dda25bc5 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 21 Nov 2019 23:50:56 +0200 Subject: [PATCH] Move RSS subscriptions on room upgrade --- rss/bot.py | 13 ++++++++++--- rss/db.py | 5 +++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/rss/bot.py b/rss/bot.py index 95535b8..3463e58 100644 --- a/rss/bot.py +++ b/rss/bot.py @@ -13,7 +13,7 @@ # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from typing import Type, List, Any, Dict, Tuple, Awaitable, Callable +from typing import Type, List, Any, Dict, Tuple, Awaitable from datetime import datetime from time import mktime, time from string import Template @@ -23,9 +23,10 @@ import aiohttp import feedparser from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper -from mautrix.types import EventType, MessageType, RoomID, EventID, PowerLevelStateEventContent +from mautrix.types import (StateEvent, EventType, MessageType, RoomID, EventID, + PowerLevelStateEventContent) from maubot import Plugin, MessageEvent -from maubot.handlers import command +from maubot.handlers import command, event from .db import Database, Feed, Entry, Subscription @@ -243,3 +244,9 @@ class RSSBot(Plugin): + "\n".join(f"* {feed.id} - [{feed.title}]({feed.url}) (subscribed by " f"[{subscriber}](https://matrix.to/#/{subscriber}))" for feed, subscriber in subscriptions)) + + @event.on(EventType.ROOM_TOMBSTONE) + async def tombstone(self, evt: StateEvent) -> None: + if not evt.content.replacement_room: + return + self.db.update_room_id(evt.room_id, evt.content.replacement_room) diff --git a/rss/db.py b/rss/db.py index 5cd9a60..539a505 100644 --- a/rss/db.py +++ b/rss/db.py @@ -132,6 +132,11 @@ class Database: except (ValueError, StopIteration): return (None, None) + def update_room_id(self, old: RoomID, new: RoomID) -> None: + self.db.execute(self.subscription.update() + .where(self.subscription.c.room_id == old) + .values(room_id=new)) + def create_feed(self, url: str, title: str, subtitle: str, link: str) -> Feed: res = self.db.execute(self.feed.insert().values(url=url, title=title, subtitle=subtitle, link=link))