11import onesignal
22
3- from packet import app , intro_onesignal_client
3+ from packet import app , intro_onesignal_client , csh_onesignal_client
44from packet .models import NotificationSubscription
55
66post_body = {
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+
1527def 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
3144def 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