From 65109f890f41767b21608498eb619135bdb27875 Mon Sep 17 00:00:00 2001 From: Devin Matte Date: Sat, 17 Aug 2019 20:37:03 -0400 Subject: [PATCH 1/3] Splitting into two onesignal clients --- config.env.py | 6 ++++-- packet/__init__.py | 11 ++++++++--- packet/notifications.py | 5 +++-- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/config.env.py b/config.env.py index 786fe437..5bb0d6c4 100644 --- a/config.env.py +++ b/config.env.py @@ -41,8 +41,10 @@ # OneSignal Config ONESIGNAL_USER_AUTH_KEY = environ.get("PACKET_ONESIGNAL_USER_AUTH_KEY", None) -ONESIGNAL_APP_AUTH_KEY = environ.get("PACKET_ONESIGNAL_APP_AUTH_KEY", None) -ONESIGNAL_APP_ID = environ.get("PACKET_ONESIGNAL_APP_ID", "6eff123a-0852-4027-804e-723044756f00") +ONESIGNAL_CSH_APP_AUTH_KEY = environ.get("PACKET_ONESIGNAL_CSH_APP_AUTH_KEY", None) +ONESIGNAL_CSH_APP_ID = environ.get("PACKET_ONESIGNAL_CSH_APP_ID", "6eff123a-0852-4027-804e-723044756f00") +ONESIGNAL_INTRO_APP_AUTH_KEY = environ.get("PACKET_ONESIGNAL_INTRO_APP_AUTH_KEY", None) +ONESIGNAL_INTRO_APP_ID = environ.get("PACKET_ONESIGNAL_INTRO_APP_ID", "6eff123a-0852-4027-804e-723044756f00") # Slack URL for pushing to #general SLACK_WEBHOOK_URL = environ.get("PACKET_SLACK_URL", None) diff --git a/packet/__init__.py b/packet/__init__.py index 93610e24..2fe64b94 100644 --- a/packet/__init__.py +++ b/packet/__init__.py @@ -46,10 +46,15 @@ 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"]) +csh_onesignal_client = onesignal.Client(user_auth_key=app.config["ONESIGNAL_USER_AUTH_KEY"], + app_auth_key=app.config["ONESIGNAL_CSH_APP_AUTH_KEY"], + app_id=app.config["ONESIGNAL_CSH_APP_ID"]) +intro_onesignal_client = onesignal.Client(user_auth_key=app.config["ONESIGNAL_USER_AUTH_KEY"], + app_auth_key=app.config["ONESIGNAL_INTRO_APP_AUTH_KEY"], + app_id=app.config["ONESIGNAL_INTRO_APP_ID"]) + +# OIDC Auth auth = OIDCAuthentication({'app': APP_CONFIG}, app) # LDAP diff --git a/packet/notifications.py b/packet/notifications.py index 50f7e582..70933509 100644 --- a/packet/notifications.py +++ b/packet/notifications.py @@ -1,6 +1,6 @@ import onesignal -from packet import app, onesignal_client +from packet import app, intro_onesignal_client from packet.models import NotificationSubscription post_body = { @@ -23,12 +23,13 @@ def packet_signed_notification(packet, signer): 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) + onesignal_response = intro_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): + # TODO: Split into csh and intro subscriptions subscriptions = NotificationSubscription.query.all() if subscriptions: tokens = list(map(lambda subscription: subscription.token, subscriptions)) From b30322e6de578b0202ad8efc728bb5d72947d00b Mon Sep 17 00:00:00 2001 From: Devin Matte Date: Mon, 19 Aug 2019 17:16:02 -0400 Subject: [PATCH 2/3] Dual Client setup --- config.env.py | 4 ++++ packet/notifications.py | 36 +++++++++++++++++++++++------------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/config.env.py b/config.env.py index 5bb0d6c4..33fc530a 100644 --- a/config.env.py +++ b/config.env.py @@ -48,3 +48,7 @@ # Slack URL for pushing to #general SLACK_WEBHOOK_URL = environ.get("PACKET_SLACK_URL", None) + +# Packet Config +PACKET_UPPER = environ.get("PACKET_UPPER", "packet.csh.rit.edu") +PACKET_INTRO = environ.get("PACKET_INTRO", "freshmen-packet.csh.rit.edu") diff --git a/packet/notifications.py b/packet/notifications.py index 70933509..590b7dfe 100644 --- a/packet/notifications.py +++ b/packet/notifications.py @@ -1,6 +1,6 @@ import onesignal -from packet import app, intro_onesignal_client +from packet import app, intro_onesignal_client, csh_onesignal_client from packet.models import NotificationSubscription post_body = { @@ -12,6 +12,18 @@ } +def send_notification(notification_body, subscriptions, client): + tokens = list(map(lambda subscription: subscription.token, subscriptions)) + if tokens: + notification = onesignal.Notification(post_body=notification_body) + notification.post_body["include_player_ids"] = tokens + onesignal_response = client.send_notification(notification) + if onesignal_response.status_code == 200: + app.logger.info("The notification ({}) sent out successfully".format(notification.post_body)) + else: + app.logger.warn("The notification ({}) was unsuccessful".format(notification.post_body)) + + def packet_signed_notification(packet, signer): subscriptions = NotificationSubscription.query.filter_by(freshman_username=packet.freshman_username) if subscriptions: @@ -22,6 +34,7 @@ def packet_signed_notification(packet, signer): 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 + notification.post_body["url"] = app.config["PROTOCOL"] + app.config["PACKET_INTRO"] onesignal_response = intro_onesignal_client.send_notification(notification) if onesignal_response.status_code == 200: @@ -29,18 +42,15 @@ def packet_signed_notification(packet, signer): def packet_100_percent_notification(packet): - # TODO: Split into csh and intro subscriptions - subscriptions = NotificationSubscription.query.all() - if subscriptions: - tokens = list(map(lambda subscription: subscription.token, subscriptions)) + member_subscriptions = NotificationSubscription.query.filter(NotificationSubscription.member.isnot(None)) + intro_subscriptions = NotificationSubscription.query.filter(NotificationSubscription.freshman_username.isnot(None)) - notification = onesignal.Notification(post_body=post_body) - notification.post_body["contents"]["en"] = packet.freshman.name + ' got 💯 on packet!' - notification.post_body["headings"]["en"] = 'New 100% on Packet!' + if member_subscriptions or intro_subscriptions: + notification_body = post_body + notification_body["contents"]["en"] = packet.freshman.name + ' got 💯 on packet!' + notification_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 + notification_body["chrome_web_icon"] = 'https://profiles.csh.rit.edu/image/' + packet.freshman_username - 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)) + send_notification(notification_body, member_subscriptions, csh_onesignal_client) + send_notification(notification_body, intro_subscriptions, intro_onesignal_client) From 7147d85ead446d395e39557322ff757b6cb09ec1 Mon Sep 17 00:00:00 2001 From: Devin Matte Date: Mon, 19 Aug 2019 20:00:47 -0400 Subject: [PATCH 3/3] Using send_notification in both --- packet/notifications.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/packet/notifications.py b/packet/notifications.py index 590b7dfe..8d11449b 100644 --- a/packet/notifications.py +++ b/packet/notifications.py @@ -27,18 +27,13 @@ def send_notification(notification_body, subscriptions, client): def packet_signed_notification(packet, signer): subscriptions = NotificationSubscription.query.filter_by(freshman_username=packet.freshman_username) if subscriptions: - tokens = list(map(lambda subscription: subscription.token, subscriptions)) - - notification = onesignal.Notification(post_body=post_body) - notification.post_body["contents"]["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 - notification.post_body["url"] = app.config["PROTOCOL"] + app.config["PACKET_INTRO"] + notification_body = post_body + notification_body["contents"]["en"] = signer + ' signed your packet! Congrats or I\'m Sorry' + notification_body["headings"]["en"] = 'New Packet Signature!' + notification_body["chrome_web_icon"] = 'https://profiles.csh.rit.edu/image/' + signer + notification_body["url"] = app.config["PROTOCOL"] + app.config["PACKET_INTRO"] - onesignal_response = intro_onesignal_client.send_notification(notification) - if onesignal_response.status_code == 200: - app.logger.info("The notification ({}) sent out successfully".format(notification.post_body)) + send_notification(notification_body, subscriptions, intro_onesignal_client) def packet_100_percent_notification(packet):