Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config.env.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
DEBUG = False
IP = environ.get("PACKET_IP", "localhost")
PORT = environ.get("PACKET_PORT", "8000")
PROTOCOL = environ.get("PACKET_PROTOCOL", "https://")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need to be a thing? Couldn't we just always assume https? Not questioning it; just curious.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When testing locally we need to use http, so it will almost always be https, but can't be guaranteed

SERVER_NAME = environ.get("PACKET_SERVER_NAME", IP + ":" + PORT)
SECRET_KEY = environ.get("PACKET_SECRET_KEY", "PLEASE_REPLACE_ME")

Expand Down
6 changes: 6 additions & 0 deletions packet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import json

import csh_ldap
import onesignal
from flask import Flask
from flask_gzip import Gzip
from flask_migrate import Migrate
Expand Down Expand Up @@ -44,6 +45,11 @@
client_metadata=ClientMetadata(app.config["OIDC_CLIENT_ID"],
app.config["OIDC_CLIENT_SECRET"]))

# Initialize Onesignal Notification apps
onesignal_client = onesignal.Client(user_auth_key=app.config["ONESIGNAL_USER_AUTH_KEY"],
app_auth_key=app.config["ONESIGNAL_APP_AUTH_KEY"],
app_id=app.config["ONESIGNAL_APP_ID"])

auth = OIDCAuthentication({'app': APP_CONFIG}, app)

# LDAP
Expand Down
46 changes: 46 additions & 0 deletions packet/notifications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import onesignal

from packet import app, onesignal_client
from packet.models import NotificationSubscription

post_body = {
"content": {"en": "Default message"},
"headings": {"en": "Default Title"},
"included_segments": ["Active Users", "Inactive Users"],
"chrome_web_icon": app.config["PROTOCOL"] + app.config["SERVER_NAME"] + "/static/android-chrome-512x512.png",
"chrome_web_badge": app.config["PROTOCOL"] + app.config["SERVER_NAME"] + "/static/android-chrome-512x512.png",
"url": app.config["PROTOCOL"] + app.config["SERVER_NAME"]
}


def packet_signed_notification(packet, signer):
subscriptions = NotificationSubscription.query.filter_by(freshman_username=packet.freshman_username)
if subscriptions:
tokens = list(filter(lambda subscription: subscription.token, subscriptions))

notification = onesignal.Notification(post_body=post_body)
notification.post_body["content"]["en"] = signer + ' signed your packet! Congrats or I\'m Sorry'
notification.post_body["headings"]["en"] = 'New Packet Signature!'
notification.post_body["chrome_web_icon"] = 'https://profiles.csh.rit.edu/image/' + signer
notification.post_body["include_player_ids"] = tokens

onesignal_response = onesignal_client.send_notification(notification)
if onesignal_response.status_code == 200:
app.logger.info("The notification ({}) sent out successfully".format(notification.post_body))


def packet_100_percent_notification(packet):
subscriptions = NotificationSubscription.query.all()
if subscriptions:
tokens = list(filter(lambda subscription: subscription.token, subscriptions))

notification = onesignal.Notification(post_body=post_body)
notification.post_body["content"]["en"] = packet.freshman.name + ' got 💯 on packet!'
notification.post_body["headings"]["en"] = 'New 100% on Packet!'
# TODO: Issue #156
notification.post_body["chrome_web_icon"] = 'https://profiles.csh.rit.edu/image/' + packet.freshman_username
notification.post_body["include_player_ids"] = tokens

onesignal_response = onesignal_client.send_notification(notification)
if onesignal_response.status_code == 200:
app.logger.info("The notification ({}) sent out successfully".format(notification.post_body))
5 changes: 5 additions & 0 deletions packet/routes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from packet.mail import send_report_mail
from packet.utils import before_request, packet_auth, notify_slack
from packet.models import Packet, MiscSignature, NotificationSubscription
from packet.notifications import packet_signed_notification, packet_100_percent_notification


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

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

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

return "Success: Signed Packet: " + packet.freshman_username
34 changes: 17 additions & 17 deletions packet/templates/include/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,27 @@
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async=""></script>
<script>
var OneSignal = window.OneSignal || [];
OneSignal.push(function() {
OneSignal.push(function () {
OneSignal.init({
appId: "{{ config['ONESIGNAL_APP_ID'] }}",
autoResubscribe: true,
appId: "{{ config['ONESIGNAL_APP_ID'] }}",
autoResubscribe: true,
allowLocalhostAsSecureOrigin: true,
});
OneSignal.showNativePrompt();
OneSignal.on("subscriptionChange", function(){
OneSignal.getUserId().then(function(result){
$.ajax({
url: "/api/v1/subscribe/",
method: "POST",
data: {
token: result
},
success: function (data) {
console.log(data);
}
});
});
});
OneSignal.on("subscriptionChange", function () {
OneSignal.getUserId().then(function (result) {
$.ajax({
url: "/api/v1/subscribe/",
method: "POST",
data: {
token: result
},
success: function (data) {
console.log(data);
}
});
});
});
});
</script>

Expand Down