def send_message_after_chat(chat_from: int, chat: str, project_id: int): """ Send alert to user if they were @'d in a chat message """ # Because message-all run on background thread it needs it's own app context app = create_app() with app.app_context(): usernames = MessageService._parse_message_for_username( chat, project_id) if len(usernames) != 0: link = MessageService.get_project_link( project_id, include_chat_section=True) messages = [] for username in usernames: current_app.logger.debug(f"Searching for {username}") try: user = UserService.get_user_by_username(username) except NotFound: current_app.logger.error( f"Username {username} not found") continue # If we can't find the user, keep going no need to fail message = Message() message.message_type = MessageType.MENTION_NOTIFICATION.value message.project_id = project_id message.from_user_id = chat_from message.to_user_id = user.id message.subject = f"You were mentioned in {link} chat" message.message = chat messages.append(dict(message=message, user=user)) MessageService._push_messages(messages) query = """ select user_id from project_favorites where project_id = :project_id""" favorited_users_results = db.engine.execute(text(query), project_id=project_id) favorited_users = [r[0] for r in favorited_users_results] # Notify all contributors except the user that created the comment. contributed_users_results = (TaskHistory.query.with_entities( TaskHistory.user_id.distinct() ).filter(TaskHistory.project_id == project_id).filter( TaskHistory.user_id != chat_from).filter( TaskHistory.action == TaskAction.STATE_CHANGE.name).all()) contributed_users = [r[0] for r in contributed_users_results] users_to_notify = list(set(contributed_users + favorited_users)) if len(users_to_notify) != 0: from_user = User.query.get(chat_from) from_user_link = MessageService.get_user_link( from_user.username) project_link = MessageService.get_project_link( project_id, include_chat_section=True) messages = [] for user_id in users_to_notify: try: user = UserService.get_user_by_id(user_id) except NotFound: continue # If we can't find the user, keep going no need to fail message = Message() message.message_type = MessageType.PROJECT_CHAT_NOTIFICATION.value message.project_id = project_id message.from_user_id = chat_from message.to_user_id = user.id message.subject = ( f"{from_user_link} left a comment in {project_link}") message.message = chat messages.append(dict(message=message, user=user)) # it's important to keep that line inside the if to avoid duplicated emails MessageService._push_messages(messages)
def send_message_after_comment(comment_from: int, comment: str, task_id: int, project_id: int): """ Will send a canned message to anyone @'d in a comment """ usernames = MessageService._parse_message_for_username(comment) if len(usernames) != 0: task_link = MessageService.get_task_link(project_id, task_id) # project_title = ProjectService.get_project_title(project_id) messages = [] for username in usernames: try: user = UserService.get_user_by_username(username) except NotFound: continue # If we can't find the user, keep going no need to fail # Validate mention_notification. if user.mentions_notifications is False: continue message = Message() message.message_type = MessageType.MENTION_NOTIFICATION.value message.project_id = project_id message.task_id = task_id message.from_user_id = comment_from message.to_user_id = user.id message.subject = f"You were mentioned in a comment in Project {project_id} on {task_link}" message.message = comment messages.append(dict(message=message, user=user)) MessageService._push_messages(messages) # Notify all contributors except the user that created the comment. results = (TaskHistory.query.with_entities( TaskHistory.user_id.distinct() ).filter(TaskHistory.project_id == project_id).filter( TaskHistory.task_id == task_id).filter( TaskHistory.user_id != comment_from).filter( TaskHistory.action == TaskAction.STATE_CHANGE.name).all()) contributed_users = [r[0] for r in results] if len(contributed_users) != 0: user_from = User.query.get(comment_from) if user_from is None: raise ValueError("Username not found") task_link = MessageService.get_task_link(project_id, task_id) # project_title = ProjectService.get_project_title(project_id) messages = [] for user_id in contributed_users: try: user = UserService.get_user_dto_by_id(user_id) except NotFound: continue # If we can't find the user, keep going no need to fail if user.comments_notifications is False: continue message = Message() message.message_type = MessageType.TASK_COMMENT_NOTIFICATION.value message.project_id = project_id message.task_id = task_id message.to_user_id = user.id message.subject = f"{user_from.username} left a comment in Project {project_id} on {task_link}" message.message = comment messages.append(dict(message=message, user=user)) MessageService._push_messages(messages)
def send_message_after_chat(chat_from: int, chat: str, project_id: int): """ Send alert to user if they were @'d in a chat message """ current_app.logger.debug("Sending Message After Chat") usernames = MessageService._parse_message_for_username(chat) if len(usernames) == 0: return # Nobody @'d so return link = MessageService.get_project_link(project_id) messages = [] for username in usernames: current_app.logger.debug(f"Searching for {username}") try: user = UserService.get_user_by_username(username) except NotFound: current_app.logger.error(f"Username {username} not found") continue # If we can't find the user, keep going no need to fail # Validate mention_notification. if user.mentions_notifications is False: continue message = Message() message.message_type = MessageType.MENTION_NOTIFICATION.value message.project_id = project_id message.from_user_id = chat_from message.to_user_id = user.id message.subject = f"You were mentioned in Project Chat on {link}" message.message = chat messages.append(dict(message=message, user=user)) MessageService._push_messages(messages) query = ( """ select user_id from project_favorites where project_id = :project_id""" ) result = db.engine.execute(text(query), project_id=project_id) favorited_users = [r[0] for r in result] if len(favorited_users) != 0: project_link = MessageService.get_project_link(project_id) # project_title = ProjectService.get_project_title(project_id) messages = [] for user_id in favorited_users: try: user = UserService.get_user_dto_by_id(user_id) except NotFound: continue # If we can't find the user, keep going no need to fail if user.comments_notifications is False: continue message = Message() message.message_type = MessageType.PROJECT_CHAT_NOTIFICATION.value message.project_id = project_id message.to_user_id = user.id message.subject = ( f"{chat_from} left a comment in Project {project_link}" ) message.message = chat messages.append(dict(message=message, user=user)) MessageService._push_messages(messages)