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):
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):
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,
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)
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):