[email] Send unsubscribe links with every message

This commit is contained in:
maike 2020-07-06 18:47:35 +02:00 committed by dl6tom
parent 4eb5b18807
commit 261e41c6a9
2 changed files with 20 additions and 5 deletions

View file

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

View file

@ -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/')