Skip to content

Commit b30322e

Browse files
committed
Dual Client setup
1 parent 65109f8 commit b30322e

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

config.env.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,7 @@
4848

4949
# Slack URL for pushing to #general
5050
SLACK_WEBHOOK_URL = environ.get("PACKET_SLACK_URL", None)
51+
52+
# Packet Config
53+
PACKET_UPPER = environ.get("PACKET_UPPER", "packet.csh.rit.edu")
54+
PACKET_INTRO = environ.get("PACKET_INTRO", "freshmen-packet.csh.rit.edu")

packet/notifications.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import onesignal
22

3-
from packet import app, intro_onesignal_client
3+
from packet import app, intro_onesignal_client, csh_onesignal_client
44
from packet.models import NotificationSubscription
55

66
post_body = {
@@ -12,6 +12,18 @@
1212
}
1313

1414

15+
def send_notification(notification_body, subscriptions, client):
16+
tokens = list(map(lambda subscription: subscription.token, subscriptions))
17+
if tokens:
18+
notification = onesignal.Notification(post_body=notification_body)
19+
notification.post_body["include_player_ids"] = tokens
20+
onesignal_response = client.send_notification(notification)
21+
if onesignal_response.status_code == 200:
22+
app.logger.info("The notification ({}) sent out successfully".format(notification.post_body))
23+
else:
24+
app.logger.warn("The notification ({}) was unsuccessful".format(notification.post_body))
25+
26+
1527
def packet_signed_notification(packet, signer):
1628
subscriptions = NotificationSubscription.query.filter_by(freshman_username=packet.freshman_username)
1729
if subscriptions:
@@ -22,25 +34,23 @@ def packet_signed_notification(packet, signer):
2234
notification.post_body["headings"]["en"] = 'New Packet Signature!'
2335
notification.post_body["chrome_web_icon"] = 'https://profiles.csh.rit.edu/image/' + signer
2436
notification.post_body["include_player_ids"] = tokens
37+
notification.post_body["url"] = app.config["PROTOCOL"] + app.config["PACKET_INTRO"]
2538

2639
onesignal_response = intro_onesignal_client.send_notification(notification)
2740
if onesignal_response.status_code == 200:
2841
app.logger.info("The notification ({}) sent out successfully".format(notification.post_body))
2942

3043

3144
def packet_100_percent_notification(packet):
32-
# TODO: Split into csh and intro subscriptions
33-
subscriptions = NotificationSubscription.query.all()
34-
if subscriptions:
35-
tokens = list(map(lambda subscription: subscription.token, subscriptions))
45+
member_subscriptions = NotificationSubscription.query.filter(NotificationSubscription.member.isnot(None))
46+
intro_subscriptions = NotificationSubscription.query.filter(NotificationSubscription.freshman_username.isnot(None))
3647

37-
notification = onesignal.Notification(post_body=post_body)
38-
notification.post_body["contents"]["en"] = packet.freshman.name + ' got 💯 on packet!'
39-
notification.post_body["headings"]["en"] = 'New 100% on Packet!'
48+
if member_subscriptions or intro_subscriptions:
49+
notification_body = post_body
50+
notification_body["contents"]["en"] = packet.freshman.name + ' got 💯 on packet!'
51+
notification_body["headings"]["en"] = 'New 100% on Packet!'
4052
# TODO: Issue #156
41-
notification.post_body["chrome_web_icon"] = 'https://profiles.csh.rit.edu/image/' + packet.freshman_username
42-
notification.post_body["include_player_ids"] = tokens
53+
notification_body["chrome_web_icon"] = 'https://profiles.csh.rit.edu/image/' + packet.freshman_username
4354

44-
onesignal_response = onesignal_client.send_notification(notification)
45-
if onesignal_response.status_code == 200:
46-
app.logger.info("The notification ({}) sent out successfully".format(notification.post_body))
55+
send_notification(notification_body, member_subscriptions, csh_onesignal_client)
56+
send_notification(notification_body, intro_subscriptions, intro_onesignal_client)

0 commit comments

Comments
 (0)