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
10 changes: 8 additions & 2 deletions config.env.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@

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

# Slack URL for pushing to #general
SLACK_WEBHOOK_URL = environ.get("PACKET_SLACK_URL", None)

# Packet Config
PACKET_UPPER = environ.get("PACKET_UPPER", "packet.csh.rit.edu")
PACKET_INTRO = environ.get("PACKET_INTRO", "freshmen-packet.csh.rit.edu")
11 changes: 8 additions & 3 deletions packet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,15 @@
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"])
csh_onesignal_client = onesignal.Client(user_auth_key=app.config["ONESIGNAL_USER_AUTH_KEY"],
app_auth_key=app.config["ONESIGNAL_CSH_APP_AUTH_KEY"],
app_id=app.config["ONESIGNAL_CSH_APP_ID"])

intro_onesignal_client = onesignal.Client(user_auth_key=app.config["ONESIGNAL_USER_AUTH_KEY"],
app_auth_key=app.config["ONESIGNAL_INTRO_APP_AUTH_KEY"],
app_id=app.config["ONESIGNAL_INTRO_APP_ID"])

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

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

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

post_body = {
Expand All @@ -12,34 +12,40 @@
}


def send_notification(notification_body, subscriptions, client):
tokens = list(map(lambda subscription: subscription.token, subscriptions))
if tokens:
notification = onesignal.Notification(post_body=notification_body)
notification.post_body["include_player_ids"] = tokens
onesignal_response = client.send_notification(notification)
if onesignal_response.status_code == 200:
app.logger.info("The notification ({}) sent out successfully".format(notification.post_body))
else:
app.logger.warn("The notification ({}) was unsuccessful".format(notification.post_body))


def packet_signed_notification(packet, signer):
subscriptions = NotificationSubscription.query.filter_by(freshman_username=packet.freshman_username)
if subscriptions:
tokens = list(map(lambda subscription: subscription.token, subscriptions))
notification_body = post_body
notification_body["contents"]["en"] = signer + ' signed your packet! Congrats or I\'m Sorry'
notification_body["headings"]["en"] = 'New Packet Signature!'
notification_body["chrome_web_icon"] = 'https://profiles.csh.rit.edu/image/' + signer
notification_body["url"] = app.config["PROTOCOL"] + app.config["PACKET_INTRO"]

notification = onesignal.Notification(post_body=post_body)
notification.post_body["contents"]["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))
send_notification(notification_body, subscriptions, intro_onesignal_client)


def packet_100_percent_notification(packet):
subscriptions = NotificationSubscription.query.all()
if subscriptions:
tokens = list(map(lambda subscription: subscription.token, subscriptions))
member_subscriptions = NotificationSubscription.query.filter(NotificationSubscription.member.isnot(None))
intro_subscriptions = NotificationSubscription.query.filter(NotificationSubscription.freshman_username.isnot(None))

notification = onesignal.Notification(post_body=post_body)
notification.post_body["contents"]["en"] = packet.freshman.name + ' got 💯 on packet!'
notification.post_body["headings"]["en"] = 'New 100% on Packet!'
if member_subscriptions or intro_subscriptions:
notification_body = post_body
notification_body["contents"]["en"] = packet.freshman.name + ' got 💯 on packet!'
notification_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
notification_body["chrome_web_icon"] = 'https://profiles.csh.rit.edu/image/' + packet.freshman_username

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))
send_notification(notification_body, member_subscriptions, csh_onesignal_client)
send_notification(notification_body, intro_subscriptions, intro_onesignal_client)