def create_news_post_notifications_task(forum_post_id, email_all): # Create notifications for the players active in the past month or all users with valid emails recipients = db.session.query( Player.id, User.id ).outerjoin( User ).join( PlayerStats ).filter( or_( and_( PlayerStats.server_id == app.config.get('MAIN_SERVER_ID'), PlayerStats.last_seen > datetime.utcnow() - timedelta(days=30) ), User.email != None ) ).distinct( Player.id, User.id ).all() for player_id, user_id in recipients: Notification.create( notifications.NEWS_POST, user_id=user_id, player_id=player_id, post_id=forum_post_id, send_email=email_all )
def new_notification(): if g.server.id != app.config['MAIN_SERVER_ID']: return jsonify({'err': 1, 'message': 'Not main server'}) body = request.json type = body.get('type') data = body.get('data') uuid = body.pop('uuid') player = Player.query.options(joinedload( Player.user)).filter_by(uuid=uuid).first() if not player: return jsonify({'err': 1, 'message': 'Invalid player uuid'}), 400 player_id = player.id user_id = None if player.user: user_id = player.user.id try: Notification.create(type, data=data, user_id=user_id, player_id=player_id) except InvalidNotificationError: return jsonify({'err': 1, 'message': 'Invalid notification data'}), 400 return jsonify({'err': 0})
def create_subscrobed_post_notifications_task(forum_post_id): post = ForumPost.query.get(forum_post_id) topic = post.topic subscriptions = ForumTopicSubscription.query.options( joinedload(ForumTopicSubscription.user)).filter( ForumTopicSubscription.topic == topic, ForumTopicSubscription.user_id != post.user_id).all() for subscription in subscriptions: Notification.create(notifications.SUBSCRIBED_TOPIC_POST, user_id=subscription.user.id, player_id=subscription.user.player_id, post_id=forum_post_id)
def new_notification(): if g.server.id != app.config['MAIN_SERVER_ID']: return jsonify({ 'err': 1, 'message': 'Not main server' }) body = request.json type = body.get('type') data = body.get('data') uuid = body.pop('uuid') player = Player.query.options( joinedload(Player.user) ).filter_by(uuid=uuid).first() if not player: return jsonify({ 'err': 1, 'message': 'Invalid player uuid' }), 400 player_id = player.id user_id = None if player.user: user_id = player.user.id try: Notification.create( type, data=data, user_id=user_id, player_id=player_id ) except InvalidNotificationError: return jsonify({ 'err': 1, 'message': 'Invalid notification data' }), 400 return jsonify({ 'err': 0 })
def create_subscrobed_post_notifications_task(forum_post_id): post = ForumPost.query.get(forum_post_id) topic = post.topic subscriptions = ForumTopicSubscription.query.options( joinedload(ForumTopicSubscription.user) ).filter( ForumTopicSubscription.topic == topic, ForumTopicSubscription.user_id != post.user_id ).all() for subscription in subscriptions: Notification.create( notifications.SUBSCRIBED_TOPIC_POST, user_id=subscription.user.id, player_id=subscription.user.player_id, post_id=forum_post_id )
def create_news_post_notifications_task(forum_post_id, email_all): # Create notifications for the players active in the past month or all users with valid emails recipients = db.session.query( Player.id, User.id).outerjoin(User).outerjoin( ForumBan, ForumBan.user_id == User.id).join(PlayerStats).filter( or_( and_( PlayerStats.server_id == app.config.get( 'MAIN_SERVER_ID'), PlayerStats.last_seen > datetime.utcnow() - timedelta(days=30)), User.email != None)).filter( ForumBan.id == None, Player.banned == False).distinct(Player.id, User.id).all() for player_id, user_id in recipients: Notification.create(notifications.NEWS_POST, user_id=user_id, player_id=player_id, post_id=forum_post_id, send_email=email_all)