From 962c12f6b05737ce827649274afb63404bb37b0b Mon Sep 17 00:00:00 2001 From: "Antonio J. Delgado" Date: Sat, 3 Aug 2024 15:11:24 +0300 Subject: [PATCH] Add replies from reblogs and use jinja2 template for sender and recipient --- mastodon_email_bridge/mastodon_email_bridge.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/mastodon_email_bridge/mastodon_email_bridge.py b/mastodon_email_bridge/mastodon_email_bridge.py index 602da78..105013b 100755 --- a/mastodon_email_bridge/mastodon_email_bridge.py +++ b/mastodon_email_bridge/mastodon_email_bridge.py @@ -129,12 +129,19 @@ class MastodonEmailBridge: ) return url for data in result.json(): + data['meb_reply_to'] = [] if data['in_reply_to_id']: self._log.debug( "This post is a reply to '%s', fetching it", data['in_reply_to_id'] ) - data['meb_reply_to'] = self.get_post(data['in_reply_to_id']) + data['meb_reply_to'].append(self.get_post(data['in_reply_to_id'])) + if data['reblog'] and data['reblog']['in_reply_to_id']: + self._log.debug( + "This post is a reblog of a reply to '%s', fetching it", + data['reblog']['in_reply_to_id'] + ) + data['meb_reply_to'].append(self.get_post(data['reblog']['in_reply_to_id'])) if int(data['id']) not in self.sent_items: self.send_mail(data) else: @@ -200,7 +207,9 @@ class MastodonEmailBridge: if self.config['mail_user'] is not None: conn.login(self.config['mail_user'], self.config['mail_pass']) self._log.debug("Sending email for post with id '%s'...", data['id']) - conn.sendmail(self.config['sender'], self.config['recipient'], msg.as_string()) + sender = self._str_replace_template(self.config['sender'], data) + recipient = self._str_replace_template(self.config['recipient'], data) + conn.sendmail(sender, recipient, msg.as_string()) conn.quit() self._log.debug("Adding entry to database...") cur = self.sqlite.cursor() @@ -209,6 +218,10 @@ class MastodonEmailBridge: self.sent_items.append(data['id']) return True + def _str_replace_template(self, template_string, data): + template = self.j2env.from_string(template_string) + return template.render(data=data) + def _init_log(self): ''' Initialize log object ''' self._log = logging.getLogger("mastodon_email_bridge")