|
1 | 1 | import onesignal |
2 | 2 |
|
3 | | -from packet import app, onesignal_client |
| 3 | +from packet import app, intro_onesignal_client, csh_onesignal_client |
4 | 4 | from packet.models import NotificationSubscription |
5 | 5 |
|
6 | 6 | post_body = { |
|
12 | 12 | } |
13 | 13 |
|
14 | 14 |
|
| 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 | + |
15 | 27 | def packet_signed_notification(packet, signer): |
16 | 28 | subscriptions = NotificationSubscription.query.filter_by(freshman_username=packet.freshman_username) |
17 | 29 | if subscriptions: |
18 | | - tokens = list(map(lambda subscription: subscription.token, subscriptions)) |
| 30 | + notification_body = post_body |
| 31 | + notification_body["contents"]["en"] = signer + ' signed your packet! Congrats or I\'m Sorry' |
| 32 | + notification_body["headings"]["en"] = 'New Packet Signature!' |
| 33 | + notification_body["chrome_web_icon"] = 'https://profiles.csh.rit.edu/image/' + signer |
| 34 | + notification_body["url"] = app.config["PROTOCOL"] + app.config["PACKET_INTRO"] |
19 | 35 |
|
20 | | - notification = onesignal.Notification(post_body=post_body) |
21 | | - notification.post_body["contents"]["en"] = signer + ' signed your packet! Congrats or I\'m Sorry' |
22 | | - notification.post_body["headings"]["en"] = 'New Packet Signature!' |
23 | | - notification.post_body["chrome_web_icon"] = 'https://profiles.csh.rit.edu/image/' + signer |
24 | | - notification.post_body["include_player_ids"] = tokens |
25 | | - |
26 | | - onesignal_response = onesignal_client.send_notification(notification) |
27 | | - if onesignal_response.status_code == 200: |
28 | | - app.logger.info("The notification ({}) sent out successfully".format(notification.post_body)) |
| 36 | + send_notification(notification_body, subscriptions, intro_onesignal_client) |
29 | 37 |
|
30 | 38 |
|
31 | 39 | def packet_100_percent_notification(packet): |
32 | | - subscriptions = NotificationSubscription.query.all() |
33 | | - if subscriptions: |
34 | | - tokens = list(map(lambda subscription: subscription.token, subscriptions)) |
| 40 | + member_subscriptions = NotificationSubscription.query.filter(NotificationSubscription.member.isnot(None)) |
| 41 | + intro_subscriptions = NotificationSubscription.query.filter(NotificationSubscription.freshman_username.isnot(None)) |
35 | 42 |
|
36 | | - notification = onesignal.Notification(post_body=post_body) |
37 | | - notification.post_body["contents"]["en"] = packet.freshman.name + ' got 💯 on packet!' |
38 | | - notification.post_body["headings"]["en"] = 'New 100% on Packet!' |
| 43 | + if member_subscriptions or intro_subscriptions: |
| 44 | + notification_body = post_body |
| 45 | + notification_body["contents"]["en"] = packet.freshman.name + ' got 💯 on packet!' |
| 46 | + notification_body["headings"]["en"] = 'New 100% on Packet!' |
39 | 47 | # TODO: Issue #156 |
40 | | - notification.post_body["chrome_web_icon"] = 'https://profiles.csh.rit.edu/image/' + packet.freshman_username |
41 | | - notification.post_body["include_player_ids"] = tokens |
| 48 | + notification_body["chrome_web_icon"] = 'https://profiles.csh.rit.edu/image/' + packet.freshman_username |
42 | 49 |
|
43 | | - onesignal_response = onesignal_client.send_notification(notification) |
44 | | - if onesignal_response.status_code == 200: |
45 | | - app.logger.info("The notification ({}) sent out successfully".format(notification.post_body)) |
| 50 | + send_notification(notification_body, member_subscriptions, csh_onesignal_client) |
| 51 | + send_notification(notification_body, intro_subscriptions, intro_onesignal_client) |
0 commit comments