diff --git a/notifications.py b/notifications.py new file mode 100644 index 0000000..08281a9 --- /dev/null +++ b/notifications.py @@ -0,0 +1,21 @@ +import sys +import requests + +def send_notification(app, target_user, mapname, old_replay, new_replay): + '''Build notification and handoff to dispatcher''' + + url = app.config["DISPATCH_SERVER"] + + # send to event dispatcher # + message = "Trackmania: Record broken on {}".format(mapname) + message += "Old time: {}".format(old_replay.get_human_readable_time()) + message += "New time: {}".format(new_replay.get_human_readable_time()) + message += "by {}".format(new_replay.clean_login()) + + payload = { "users": [user], "msg" : message } + + r = requests.post(app.config["DISPATCH_SERVER"] + "/smart-send", + json=payload, auth=app.config["DISPATCH_AUTH"]) + + if not r.ok: + print("Error handing off notification to dispatch ({})".format(r.status_code), file=sys.stderr) diff --git a/server.py b/server.py index 8c1af4e..82d72ad 100755 --- a/server.py +++ b/server.py @@ -81,6 +81,8 @@ class UserSettings(db.Model): __tablename__ = "user_settings" + user = Column(String, primary_key=True) + show_tm_2020 = Column(Boolean) show_tmnf = Column(Boolean) show_tm_2020_current = Column(Boolean) @@ -114,7 +116,7 @@ class ParsedReplay(db.Model): else: return self.login - def get_human_readable_time(self): + def get_human_readable_time(self, thousands=False): t = datetime.timedelta(microseconds=self.race_time*1000) t_string = str(t) if t.seconds < 60*60: @@ -419,11 +421,21 @@ def upload(): def check_replay_trigger(replay): - # get replay rank - # get second best - # uploader = second best owner - # check notifications on - # request to dispatch + map_obj = db.session.get(Map).filter(Map.map_uid == replay.map_uid).first() + assert(map_uid) + + best = map_obj.get_best_replay() + second = map_obj.get_second_best_replay() + + if replay.filehash != best.filehash: + return + + if second.uploader == replay.uploader: + return + + settings = db.session.query(UserSettings).filter(UserSettings.user == second.uploader).first() + if settings and settings.notifcations_self: + notifications.send_notification(app, settings.user, map_obj.map_uid, second, replay) def create_app(): db.create_all()