/ blottertrax / applications / modmail.py
modmail.py
 1  import datetime
 2  from multiprocessing import Lock
 3  
 4  import prawcore
 5  from praw import Reddit
 6  
 7  from blottertrax.config import Config
 8  from blottertrax.database import Database
 9  from blottertrax.helper import templates
10  from blottertrax.logger import Logger
11  
12  
13  class ModMail:
14      def __init__(self, lock: Lock):
15          self.config = Config()
16          self.logger = Logger()
17          self.database = Database(lock)
18  
19          try:
20              self.reddit = Reddit(client_id=self.config.REDDIT.CLIENT_ID, client_secret=self.config.REDDIT.CLIENT_SECRET,
21                                   password=self.config.REDDIT.PASSWORD, user_agent=self.config.REDDIT.USER_AGENT,
22                                   username=self.config.REDDIT.USER_NAME)
23  
24          except KeyError:
25              self.logger.exception('Check if the configuration is set right')
26              exit()
27  
28      def run(self):
29          for message in self.reddit.subreddit(self.config.REDDIT.SUBREDDIT).mod.stream.modmail_conversations(state="new"):
30              if self.database.known_mod_mail(message) is True:
31                  continue
32  
33              try:
34                  current_time_utc = datetime.datetime.utcnow()
35                  self.logger.info(f"Handling message {message.id}")
36  
37                  """
38                  Ensure no other mod has already replied to this message.
39                  We don't want to step on active conversations.
40                  Check the user has a recent submission.
41                  If not, they might not be contacting about a post.
42                  """
43                  if message.last_mod_update is None and len(message.user.recent_posts) > 0:
44                      account_creation_time = datetime.datetime.fromtimestamp(round(message.user.created_utc))
45                      account_age = current_time_utc - account_creation_time
46  
47                      hours_since_creation = account_age.days * 24
48  
49                      if hours_since_creation < self.config.REDDIT.MINIMUM_ACCOUNT_AGE / 3600 \
50                              or message.user.comment_karma < self.config.REDDIT.MINIMUM_COMMENT_KARMA:
51                          self.logger.info(f"Notifying  {message.user}, that their account is too new")
52                          """ Users account is too new, notify them. """
53                          message.reply(templates.get_modmail_reply_new_account(
54                              message.user.name,
55                              message.user.recent_posts[0]),
56                              author_hidden=True
57                          )
58                          message.read()
59                          message.archive()
60              except prawcore.exceptions.NotFound:
61                  """ User is likely shadowbanned. """
62              except AttributeError:
63                  """ Likely that the users account has been deleted. """
64              finally:
65                  self.database.save_mod_mail(message)