Skip to content

Commit ead921d

Browse files
authored
Merge pull request #155 from devinmatte/onesignal_notifications
Phase 2: Notifications sent on Events
2 parents f6c2506 + ebb19c6 commit ead921d

File tree

5 files changed

+75
-17
lines changed

5 files changed

+75
-17
lines changed

config.env.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
DEBUG = False
1010
IP = environ.get("PACKET_IP", "localhost")
1111
PORT = environ.get("PACKET_PORT", "8000")
12+
PROTOCOL = environ.get("PACKET_PROTOCOL", "https://")
1213
SERVER_NAME = environ.get("PACKET_SERVER_NAME", IP + ":" + PORT)
1314
SECRET_KEY = environ.get("PACKET_SECRET_KEY", "PLEASE_REPLACE_ME")
1415

packet/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import json
88

99
import csh_ldap
10+
import onesignal
1011
from flask import Flask
1112
from flask_gzip import Gzip
1213
from flask_migrate import Migrate
@@ -44,6 +45,11 @@
4445
client_metadata=ClientMetadata(app.config["OIDC_CLIENT_ID"],
4546
app.config["OIDC_CLIENT_SECRET"]))
4647

48+
# 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"])
52+
4753
auth = OIDCAuthentication({'app': APP_CONFIG}, app)
4854

4955
# LDAP

packet/notifications.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import onesignal
2+
3+
from packet import app, onesignal_client
4+
from packet.models import NotificationSubscription
5+
6+
post_body = {
7+
"content": {"en": "Default message"},
8+
"headings": {"en": "Default Title"},
9+
"included_segments": ["Active Users", "Inactive Users"],
10+
"chrome_web_icon": app.config["PROTOCOL"] + app.config["SERVER_NAME"] + "/static/android-chrome-512x512.png",
11+
"chrome_web_badge": app.config["PROTOCOL"] + app.config["SERVER_NAME"] + "/static/android-chrome-512x512.png",
12+
"url": app.config["PROTOCOL"] + app.config["SERVER_NAME"]
13+
}
14+
15+
16+
def packet_signed_notification(packet, signer):
17+
subscriptions = NotificationSubscription.query.filter_by(freshman_username=packet.freshman_username)
18+
if subscriptions:
19+
tokens = list(filter(lambda subscription: subscription.token, subscriptions))
20+
21+
notification = onesignal.Notification(post_body=post_body)
22+
notification.post_body["content"]["en"] = signer + ' signed your packet! Congrats or I\'m Sorry'
23+
notification.post_body["headings"]["en"] = 'New Packet Signature!'
24+
notification.post_body["chrome_web_icon"] = 'https://profiles.csh.rit.edu/image/' + signer
25+
notification.post_body["include_player_ids"] = tokens
26+
27+
onesignal_response = onesignal_client.send_notification(notification)
28+
if onesignal_response.status_code == 200:
29+
app.logger.info("The notification ({}) sent out successfully".format(notification.post_body))
30+
31+
32+
def packet_100_percent_notification(packet):
33+
subscriptions = NotificationSubscription.query.all()
34+
if subscriptions:
35+
tokens = list(filter(lambda subscription: subscription.token, subscriptions))
36+
37+
notification = onesignal.Notification(post_body=post_body)
38+
notification.post_body["content"]["en"] = packet.freshman.name + ' got 💯 on packet!'
39+
notification.post_body["headings"]["en"] = 'New 100% on Packet!'
40+
# 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
43+
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))

packet/routes/api.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from packet.mail import send_report_mail
99
from packet.utils import before_request, packet_auth, notify_slack
1010
from packet.models import Packet, MiscSignature, NotificationSubscription
11+
from packet.notifications import packet_signed_notification, packet_100_percent_notification
1112

1213

1314
@app.route("/api/v1/sign/<packet_id>/", methods=["POST"])
@@ -23,17 +24,20 @@ def sign(packet_id, info):
2324
for sig in filter(lambda sig: sig.member == info["uid"], packet.upper_signatures):
2425
sig.signed = True
2526
app.logger.info("Member {} signed packet {} as an upperclassman".format(info["uid"], packet_id))
27+
packet_signed_notification(packet, info["uid"])
2628
return commit_sig(packet, was_100)
2729

2830
# The CSHer is a misc so add a new row
2931
db.session.add(MiscSignature(packet=packet, member=info["uid"]))
3032
app.logger.info("Member {} signed packet {} as a misc".format(info["uid"], packet_id))
33+
packet_signed_notification(packet, info["uid"])
3134
return commit_sig(packet, was_100)
3235
else:
3336
# Check if the freshman is onfloor and if so, sign that row
3437
for sig in filter(lambda sig: sig.freshman_username == info["uid"], packet.fresh_signatures):
3538
sig.signed = True
3639
app.logger.info("Freshman {} signed packet {}".format(info["uid"], packet_id))
40+
packet_signed_notification(packet, info["uid"])
3741
return commit_sig(packet, was_100)
3842

3943
app.logger.warn("Failed to add {}'s signature to packet {}".format(info["uid"], packet_id))
@@ -66,6 +70,7 @@ def report(info):
6670
def commit_sig(packet, was_100):
6771
db.session.commit()
6872
if not was_100 and packet.is_100():
73+
packet_100_percent_notification(packet)
6974
notify_slack(packet.freshman.name)
7075

7176
return "Success: Signed Packet: " + packet.freshman_username

packet/templates/include/head.html

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,27 @@
3838
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async=""></script>
3939
<script>
4040
var OneSignal = window.OneSignal || [];
41-
OneSignal.push(function() {
41+
OneSignal.push(function () {
4242
OneSignal.init({
43-
appId: "{{ config['ONESIGNAL_APP_ID'] }}",
44-
autoResubscribe: true,
43+
appId: "{{ config['ONESIGNAL_APP_ID'] }}",
44+
autoResubscribe: true,
4545
allowLocalhostAsSecureOrigin: true,
4646
});
4747
OneSignal.showNativePrompt();
48-
OneSignal.on("subscriptionChange", function(){
49-
OneSignal.getUserId().then(function(result){
50-
$.ajax({
51-
url: "/api/v1/subscribe/",
52-
method: "POST",
53-
data: {
54-
token: result
55-
},
56-
success: function (data) {
57-
console.log(data);
58-
}
59-
});
60-
});
61-
});
48+
OneSignal.on("subscriptionChange", function () {
49+
OneSignal.getUserId().then(function (result) {
50+
$.ajax({
51+
url: "/api/v1/subscribe/",
52+
method: "POST",
53+
data: {
54+
token: result
55+
},
56+
success: function (data) {
57+
console.log(data);
58+
}
59+
});
60+
});
61+
});
6262
});
6363
</script>
6464

0 commit comments

Comments
 (0)