diff --git a/config.env.py b/config.env.py index 0eb3f4e6..786fe437 100644 --- a/config.env.py +++ b/config.env.py @@ -9,6 +9,7 @@ DEBUG = False IP = environ.get("PACKET_IP", "localhost") PORT = environ.get("PACKET_PORT", "8000") +PROTOCOL = environ.get("PACKET_PROTOCOL", "https://") SERVER_NAME = environ.get("PACKET_SERVER_NAME", IP + ":" + PORT) SECRET_KEY = environ.get("PACKET_SECRET_KEY", "PLEASE_REPLACE_ME") diff --git a/packet/__init__.py b/packet/__init__.py index f68c9184..93610e24 100644 --- a/packet/__init__.py +++ b/packet/__init__.py @@ -7,6 +7,7 @@ import json import csh_ldap +import onesignal from flask import Flask from flask_gzip import Gzip from flask_migrate import Migrate @@ -44,6 +45,11 @@ client_metadata=ClientMetadata(app.config["OIDC_CLIENT_ID"], app.config["OIDC_CLIENT_SECRET"])) +# Initialize Onesignal Notification apps +onesignal_client = onesignal.Client(user_auth_key=app.config["ONESIGNAL_USER_AUTH_KEY"], + app_auth_key=app.config["ONESIGNAL_APP_AUTH_KEY"], + app_id=app.config["ONESIGNAL_APP_ID"]) + auth = OIDCAuthentication({'app': APP_CONFIG}, app) # LDAP diff --git a/packet/notifications.py b/packet/notifications.py new file mode 100644 index 00000000..51ae12a9 --- /dev/null +++ b/packet/notifications.py @@ -0,0 +1,46 @@ +import onesignal + +from packet import app, onesignal_client +from packet.models import NotificationSubscription + +post_body = { + "content": {"en": "Default message"}, + "headings": {"en": "Default Title"}, + "included_segments": ["Active Users", "Inactive Users"], + "chrome_web_icon": app.config["PROTOCOL"] + app.config["SERVER_NAME"] + "/static/android-chrome-512x512.png", + "chrome_web_badge": app.config["PROTOCOL"] + app.config["SERVER_NAME"] + "/static/android-chrome-512x512.png", + "url": app.config["PROTOCOL"] + app.config["SERVER_NAME"] +} + + +def packet_signed_notification(packet, signer): + subscriptions = NotificationSubscription.query.filter_by(freshman_username=packet.freshman_username) + if subscriptions: + tokens = list(filter(lambda subscription: subscription.token, subscriptions)) + + notification = onesignal.Notification(post_body=post_body) + notification.post_body["content"]["en"] = signer + ' signed your packet! Congrats or I\'m Sorry' + notification.post_body["headings"]["en"] = 'New Packet Signature!' + notification.post_body["chrome_web_icon"] = 'https://profiles.csh.rit.edu/image/' + signer + notification.post_body["include_player_ids"] = tokens + + onesignal_response = onesignal_client.send_notification(notification) + if onesignal_response.status_code == 200: + app.logger.info("The notification ({}) sent out successfully".format(notification.post_body)) + + +def packet_100_percent_notification(packet): + subscriptions = NotificationSubscription.query.all() + if subscriptions: + tokens = list(filter(lambda subscription: subscription.token, subscriptions)) + + notification = onesignal.Notification(post_body=post_body) + notification.post_body["content"]["en"] = packet.freshman.name + ' got 💯 on packet!' + notification.post_body["headings"]["en"] = 'New 100% on Packet!' + # TODO: Issue #156 + notification.post_body["chrome_web_icon"] = 'https://profiles.csh.rit.edu/image/' + packet.freshman_username + notification.post_body["include_player_ids"] = tokens + + onesignal_response = onesignal_client.send_notification(notification) + if onesignal_response.status_code == 200: + app.logger.info("The notification ({}) sent out successfully".format(notification.post_body)) diff --git a/packet/routes/api.py b/packet/routes/api.py index 5e0bc576..2e772df0 100644 --- a/packet/routes/api.py +++ b/packet/routes/api.py @@ -8,6 +8,7 @@ from packet.mail import send_report_mail from packet.utils import before_request, packet_auth, notify_slack from packet.models import Packet, MiscSignature, NotificationSubscription +from packet.notifications import packet_signed_notification, packet_100_percent_notification @app.route("/api/v1/sign//", methods=["POST"]) @@ -23,17 +24,20 @@ def sign(packet_id, info): for sig in filter(lambda sig: sig.member == info["uid"], packet.upper_signatures): sig.signed = True app.logger.info("Member {} signed packet {} as an upperclassman".format(info["uid"], packet_id)) + packet_signed_notification(packet, info["uid"]) return commit_sig(packet, was_100) # The CSHer is a misc so add a new row db.session.add(MiscSignature(packet=packet, member=info["uid"])) app.logger.info("Member {} signed packet {} as a misc".format(info["uid"], packet_id)) + packet_signed_notification(packet, info["uid"]) return commit_sig(packet, was_100) else: # Check if the freshman is onfloor and if so, sign that row for sig in filter(lambda sig: sig.freshman_username == info["uid"], packet.fresh_signatures): sig.signed = True app.logger.info("Freshman {} signed packet {}".format(info["uid"], packet_id)) + packet_signed_notification(packet, info["uid"]) return commit_sig(packet, was_100) app.logger.warn("Failed to add {}'s signature to packet {}".format(info["uid"], packet_id)) @@ -66,6 +70,7 @@ def report(info): def commit_sig(packet, was_100): db.session.commit() if not was_100 and packet.is_100(): + packet_100_percent_notification(packet) notify_slack(packet.freshman.name) return "Success: Signed Packet: " + packet.freshman_username diff --git a/packet/templates/include/head.html b/packet/templates/include/head.html index 2b394dae..acdd27b8 100644 --- a/packet/templates/include/head.html +++ b/packet/templates/include/head.html @@ -38,27 +38,27 @@