diff --git a/kibicara/platforms/email/bot.py b/kibicara/platforms/email/bot.py index 052031c..ec65def 100644 --- a/kibicara/platforms/email/bot.py +++ b/kibicara/platforms/email/bot.py @@ -3,11 +3,11 @@ # SPDX-License-Identifier: 0BSD from kibicara.platforms.email.model import EmailRecipients, Email +from kibicara.model import Hood from kibicara.platformapi import Censor, Spawner, Message -from logging import getLogger from kibicara.email import send_email - -logger = getLogger(__name__) +from kibicara.config import config +import jwt class EmailBot(Censor): @@ -18,9 +18,20 @@ class EmailBot(Censor): async def run(self): while True: + hood_name = await Hood.objects.get(id=self.model.hood).name message = await self.receive() - for recipient in EmailRecipients(hood=self.hood): - send_email(recipient.email, "Kibicara " + self.hood, body=message.text) + for recipient in EmailRecipients(hood=self.model.hood): + json = { + 'email': recipient.email, + 'hood': self.model.hood, + } + token = jwt.encode(json, self.model.secret).decode('ascii') + unsubscribe_link = config['root_url'] + 'api/email/unsubscribe/' + token + message.text += ( + "\n\n--\nIf you want to stop receiving these mails, " + "follow this link: " + unsubscribe_link + ) + send_email(recipient.email, "Kibicara " + hood_name, body=message.text) spawner = Spawner(Email, EmailBot) diff --git a/kibicara/platforms/email/webapi.py b/kibicara/platforms/email/webapi.py index 97ad01a..0f0ad9a 100644 --- a/kibicara/platforms/email/webapi.py +++ b/kibicara/platforms/email/webapi.py @@ -96,6 +96,10 @@ async def email_recipient_confirm(token): # delete EmailRecipient +@router.get('/unsubscribe/{token}', status_code=status.HTTP_200_OK) +async def email_recipient_unsubscribe(token): + json = jwt.decode(token) + await EmailRecipients.objects.delete_many(hood=json['hood'], email=json['email']) @router.post('/messages/')