]> git.ipfire.org Git - ipfire.org.git/commitdiff
backend: Add option to send campaign emails
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 26 Nov 2024 16:45:38 +0000 (16:45 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 26 Nov 2024 16:45:38 +0000 (16:45 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/accounts.py
src/backend/base.py
src/backend/blog.py
src/backend/campaigns.py

index f517b6d3d4e24e3038dc2af6768e40b583fc91a5..31f904f751e6bf5d97f61e3681aadfabfa4f3a4d 100644 (file)
@@ -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):
index c7b4fd02df0d771c5de82a36467014aaf202dec5..09eb6f93031a2d680b372a299744978931a23635 100644 (file)
@@ -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,
index bde543e81c6e55ac6a6eb25f589a11763d423bc2..67ed507488d7bff724c81486036608081c25cbdf 100644 (file)
@@ -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)
index cf191b61a94a177fb52180d4aff8157d18b39f1e..d11cd8a118488bd50fe54cf968ba175ce4385e60 100644 (file)
@@ -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):