[email] Send unsubscribe links with every message
This commit is contained in:
parent
4eb5b18807
commit
261e41c6a9
|
@ -3,11 +3,11 @@
|
||||||
# SPDX-License-Identifier: 0BSD
|
# SPDX-License-Identifier: 0BSD
|
||||||
|
|
||||||
from kibicara.platforms.email.model import EmailRecipients, Email
|
from kibicara.platforms.email.model import EmailRecipients, Email
|
||||||
|
from kibicara.model import Hood
|
||||||
from kibicara.platformapi import Censor, Spawner, Message
|
from kibicara.platformapi import Censor, Spawner, Message
|
||||||
from logging import getLogger
|
|
||||||
from kibicara.email import send_email
|
from kibicara.email import send_email
|
||||||
|
from kibicara.config import config
|
||||||
logger = getLogger(__name__)
|
import jwt
|
||||||
|
|
||||||
|
|
||||||
class EmailBot(Censor):
|
class EmailBot(Censor):
|
||||||
|
@ -18,9 +18,20 @@ class EmailBot(Censor):
|
||||||
|
|
||||||
async def run(self):
|
async def run(self):
|
||||||
while True:
|
while True:
|
||||||
|
hood_name = await Hood.objects.get(id=self.model.hood).name
|
||||||
message = await self.receive()
|
message = await self.receive()
|
||||||
for recipient in EmailRecipients(hood=self.hood):
|
for recipient in EmailRecipients(hood=self.model.hood):
|
||||||
send_email(recipient.email, "Kibicara " + self.hood, body=message.text)
|
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)
|
spawner = Spawner(Email, EmailBot)
|
||||||
|
|
|
@ -96,6 +96,10 @@ async def email_recipient_confirm(token):
|
||||||
|
|
||||||
|
|
||||||
# delete EmailRecipient
|
# 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/')
|
@router.post('/messages/')
|
||||||
|
|
Loading…
Reference in a new issue