Skip to content

Commit dcaf83b

Browse files
authored
Merge pull request #161 from devinmatte/onesignal_notifications
Notifications Phase 3: Using two clients
2 parents 24b4df8 + 7147d85 commit dcaf83b

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

config.env.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@
4141

4242
# OneSignal Config
4343
ONESIGNAL_USER_AUTH_KEY = environ.get("PACKET_ONESIGNAL_USER_AUTH_KEY", None)
44-
ONESIGNAL_APP_AUTH_KEY = environ.get("PACKET_ONESIGNAL_APP_AUTH_KEY", None)
45-
ONESIGNAL_APP_ID = environ.get("PACKET_ONESIGNAL_APP_ID", "6eff123a-0852-4027-804e-723044756f00")
44+
ONESIGNAL_CSH_APP_AUTH_KEY = environ.get("PACKET_ONESIGNAL_CSH_APP_AUTH_KEY", None)
45+
ONESIGNAL_CSH_APP_ID = environ.get("PACKET_ONESIGNAL_CSH_APP_ID", "6eff123a-0852-4027-804e-723044756f00")
46+
ONESIGNAL_INTRO_APP_AUTH_KEY = environ.get("PACKET_ONESIGNAL_INTRO_APP_AUTH_KEY", None)
47+
ONESIGNAL_INTRO_APP_ID = environ.get("PACKET_ONESIGNAL_INTRO_APP_ID", "6eff123a-0852-4027-804e-723044756f00")
4648

4749
# Slack URL for pushing to #general
4850
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/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,15 @@
4646
app.config["OIDC_CLIENT_SECRET"]))
4747

4848
# Initialize Onesignal Notification apps
49-
onesignal_client = onesignal.Client(user_auth_key=app.config["ONESIGNAL_USER_AUTH_KEY"],
50-
app_auth_key=app.config["ONESIGNAL_APP_AUTH_KEY"],
51-
app_id=app.config["ONESIGNAL_APP_ID"])
49+
csh_onesignal_client = onesignal.Client(user_auth_key=app.config["ONESIGNAL_USER_AUTH_KEY"],
50+
app_auth_key=app.config["ONESIGNAL_CSH_APP_AUTH_KEY"],
51+
app_id=app.config["ONESIGNAL_CSH_APP_ID"])
5252

53+
intro_onesignal_client = onesignal.Client(user_auth_key=app.config["ONESIGNAL_USER_AUTH_KEY"],
54+
app_auth_key=app.config["ONESIGNAL_INTRO_APP_AUTH_KEY"],
55+
app_id=app.config["ONESIGNAL_INTRO_APP_ID"])
56+
57+
# OIDC Auth
5358
auth = OIDCAuthentication({'app': APP_CONFIG}, app)
5459

5560
# LDAP

packet/notifications.py

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

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

66
post_body = {
@@ -12,34 +12,40 @@
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:
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"]
1935

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)
2937

3038

3139
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))
3542

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!'
3947
# 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
4249

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

Comments
 (0)