Add replies from reblogs and use jinja2 template for sender and recipient

This commit is contained in:
Antonio J. Delgado 2024-08-03 15:11:24 +03:00
parent ca0a09c7ba
commit 962c12f6b0

View file

@ -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")