2020-07-05 21:49:32 +00:00
|
|
|
# Copyright (C) 2020 by Maike <maike@systemli.org>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: 0BSD
|
|
|
|
|
2020-07-06 18:51:07 +00:00
|
|
|
from kibicara.platforms.email.model import EmailSubscribers, Email
|
2020-07-06 17:59:00 +00:00
|
|
|
from kibicara.platformapi import Censor, Spawner
|
2020-07-06 14:31:30 +00:00
|
|
|
from kibicara.email import send_email
|
2020-07-06 16:47:35 +00:00
|
|
|
from kibicara.config import config
|
2020-07-07 13:08:18 +00:00
|
|
|
from kibicara.webapi.admin import to_token
|
2020-07-05 21:49:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EmailBot(Censor):
|
|
|
|
def __init__(self, email_model):
|
|
|
|
super().__init__(email_model.hood)
|
|
|
|
self.model = email_model
|
|
|
|
self.messages = []
|
|
|
|
|
|
|
|
async def run(self):
|
2020-07-06 18:51:07 +00:00
|
|
|
""" Loop which waits for new messages and sends emails to all subscribers. """
|
2020-07-05 21:49:32 +00:00
|
|
|
while True:
|
|
|
|
message = await self.receive()
|
2020-07-07 13:08:18 +00:00
|
|
|
for subscriber in EmailSubscribers(hood=self.hood.id):
|
|
|
|
payload = {
|
2020-07-06 18:51:07 +00:00
|
|
|
'email': subscriber.email,
|
2020-07-07 13:08:18 +00:00
|
|
|
'hood': self.hood.id,
|
2020-07-06 16:47:35 +00:00
|
|
|
}
|
2020-07-07 13:08:18 +00:00
|
|
|
token = to_token(email=subscriber.email, hood=self.hood.id)
|
2020-07-06 17:14:12 +00:00
|
|
|
unsubscribe_link = (
|
|
|
|
config['root_url']
|
|
|
|
+ 'api/'
|
|
|
|
+ self.model.id
|
|
|
|
+ '/email/unsubscribe/'
|
2020-07-07 13:08:18 +00:00
|
|
|
+ token
|
2020-07-06 17:14:12 +00:00
|
|
|
)
|
2020-07-06 16:47:35 +00:00
|
|
|
message.text += (
|
|
|
|
"\n\n--\nIf you want to stop receiving these mails, "
|
|
|
|
"follow this link: " + unsubscribe_link
|
|
|
|
)
|
2020-07-07 13:08:18 +00:00
|
|
|
send_email(subscriber.email, "Kibicara " + self.hood.name, body=message.text)
|
2020-07-05 21:49:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
spawner = Spawner(Email, EmailBot)
|