From: Michael Tremer Date: Tue, 26 Nov 2024 16:45:38 +0000 (+0000) Subject: backend: Add option to send campaign emails X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b9cb2dee549d1936be86095e1f6d7eaacf3fc496;p=ipfire.org.git backend: Add option to send campaign emails Signed-off-by: Michael Tremer --- diff --git a/src/backend/accounts.py b/src/backend/accounts.py index f517b6d3..31f904f7 100644 --- a/src/backend/accounts.py +++ b/src/backend/accounts.py @@ -665,6 +665,16 @@ class Accounts(Object): for mail in mails: print(mail.decode()) + # Subscribed + + @property + def subscribed(self): + """ + Returns a list of all accounts that have consented to promotional messages + """ + return self.backend.groups.get_by_gid("promotional-consent") + + class Account(LDAPObject): def __str__(self): @@ -1414,6 +1424,29 @@ class Account(LDAPObject): set_contents_to_promotional_emails, ) + def promotional_message_sent(self): + """ + Called after a promotional message has been sent + """ + self.db.execute(""" + INSERT INTO + account_promotional_messages + ( + uid + ) + VALUES + ( + %s + ) + ON CONFLICT + ( + uid + ) + DO UPDATE SET + last_sent_at = CURRENT_TIMESTAMP + """, self.uid, + ) + # Messages def send_message(self, template, **kwargs): diff --git a/src/backend/base.py b/src/backend/base.py index c7b4fd02..09eb6f93 100644 --- a/src/backend/base.py +++ b/src/backend/base.py @@ -160,6 +160,7 @@ class Backend(object): tasks = { "accounts:delete" : self.accounts._delete, "announce-blog-posts" : self.blog.announce, + "campaigns:send" : self.campaigns.send, "check-mirrors" : self.mirrors.check_all, "cleanup" : self.cleanup, "get-all-emails" : self.accounts.get_all_emails, diff --git a/src/backend/blog.py b/src/backend/blog.py index bde543e8..67ed5074 100644 --- a/src/backend/blog.py +++ b/src/backend/blog.py @@ -392,16 +392,9 @@ class Post(misc.Object): self.backend.blog.refresh() async def announce(self): - # Get people who should receive this message - group = self.backend.groups.get_by_gid("promotional-consent") - if not group: - return - - with self.db.transaction(): - # Generate an email for everybody in this group - for account in group: - account.send_message("blog/messages/announcement", post=self) + # Send emails + self.campaigns.send("blog/messages/announcement", promotional=False, post=self) - # Mark this post as announced - self.db.execute("UPDATE blog SET announced_at = CURRENT_TIMESTAMP \ - WHERE id = %s", self.id) + # Mark this post as announced + self.db.execute("UPDATE blog SET announced_at = CURRENT_TIMESTAMP \ + WHERE id = %s", self.id) diff --git a/src/backend/campaigns.py b/src/backend/campaigns.py index cf191b61..d11cd8a1 100644 --- a/src/backend/campaigns.py +++ b/src/backend/campaigns.py @@ -42,6 +42,18 @@ class Campaigns(Object): for email in emails: email.send() + async def send(self, template, promotional=True, **kwargs): + """ + Sends a message to all users that have consented to promotional messages + """ + for account in self.backend.accounts.subscribed: + # Send the message + account.send_message(template, **kwargs) + + # Remember that we sent a promotional message + if promotional: + account.promotional_message_sent() + class CampaignEmail(Object): def init(self, id, data=None):