Skip to content

Commit f6c2506

Browse files
authored
Merge pull request #153 from devinmatte/onesignal_notifications
Notifications Phase 1: Subscription
2 parents fe8ef31 + ee28245 commit f6c2506

File tree

10 files changed

+110
-2
lines changed

10 files changed

+110
-2
lines changed

config.env.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,10 @@
3838
MAIL_PASSWORD = environ.get("PACKET_MAIL_PASSWORD", None)
3939
MAIL_USE_TLS = strtobool(environ.get("PACKET_MAIL_TLS", 'True'))
4040

41+
# OneSignal Config
42+
ONESIGNAL_USER_AUTH_KEY = environ.get("PACKET_ONESIGNAL_USER_AUTH_KEY", None)
43+
ONESIGNAL_APP_AUTH_KEY = environ.get("PACKET_ONESIGNAL_APP_AUTH_KEY", None)
44+
ONESIGNAL_APP_ID = environ.get("PACKET_ONESIGNAL_APP_ID", "6eff123a-0852-4027-804e-723044756f00")
45+
4146
# Slack URL for pushing to #general
4247
SLACK_WEBHOOK_URL = environ.get("PACKET_SLACK_URL", None)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""notifications
2+
3+
Revision ID: 53768f0a4850
4+
Revises: eecf30892d0e
5+
Create Date: 2019-08-06 22:15:04.400982
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = '53768f0a4850'
14+
down_revision = 'eecf30892d0e'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
op.create_table('notification_subscriptions',
22+
sa.Column('member', sa.String(length=36), nullable=True),
23+
sa.Column('freshman_username', sa.String(length=10), nullable=True),
24+
sa.Column('token', sa.String(length=256), nullable=False),
25+
sa.ForeignKeyConstraint(['freshman_username'], ['freshman.rit_username'], ),
26+
sa.PrimaryKeyConstraint('token')
27+
)
28+
# ### end Alembic commands ###
29+
30+
31+
def downgrade():
32+
# ### commands auto generated by Alembic - please adjust! ###
33+
op.drop_table('notification_subscriptions')
34+
# ### end Alembic commands ###
35+

packet/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,10 @@ class MiscSignature(db.Model):
154154
updated = Column(DateTime, default=datetime.now, onupdate=datetime.now, nullable=False)
155155

156156
packet = relationship("Packet", back_populates="misc_signatures")
157+
158+
159+
class NotificationSubscription(db.Model):
160+
__tablename__ = "notification_subscriptions"
161+
member = Column(String(36), nullable=True)
162+
freshman_username = Column(ForeignKey("freshman.rit_username"), nullable=True)
163+
token = Column(String(256), primary_key=True, nullable=False)

packet/routes/api.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from packet.context_processors import get_rit_name
88
from packet.mail import send_report_mail
99
from packet.utils import before_request, packet_auth, notify_slack
10-
from packet.models import Packet, MiscSignature
10+
from packet.models import Packet, MiscSignature, NotificationSubscription
1111

1212

1313
@app.route("/api/v1/sign/<packet_id>/", methods=["POST"])
@@ -40,6 +40,20 @@ def sign(packet_id, info):
4040
return "Error: Signature not valid. Reason: Unknown"
4141

4242

43+
@app.route("/api/v1/subscribe/", methods=["POST"])
44+
@packet_auth
45+
@before_request
46+
def subscribe(info):
47+
data = request.form
48+
if app.config["REALM"] == "csh":
49+
subscription = NotificationSubscription(token=data['token'], member=info["uid"])
50+
else:
51+
subscription = NotificationSubscription(token=data['token'], freshman_username=info["uid"])
52+
db.session.add(subscription)
53+
db.session.commit()
54+
return "Token subscribed for " + info["uid"]
55+
56+
4357
@app.route("/api/v1/report/", methods=["POST"])
4458
@packet_auth
4559
@before_request

packet/routes/shared.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,15 @@ def packets(info=None):
6868
open_packets.sort(key=packet_sort_key, reverse=True)
6969

7070
return render_template("active_packets.html", info=info, packets=open_packets)
71+
72+
73+
@app.route('/sw.js', methods=['GET'])
74+
@app.route('/OneSignalSDKWorker.js', methods=['GET'])
75+
def service_worker():
76+
return app.send_static_file('js/sw.js')
77+
78+
79+
@app.route('/update-sw.js', methods=['GET'])
80+
@app.route('/OneSignalSDKUpdaterWorker.js', methods=['GET'])
81+
def update_service_worker():
82+
return app.send_static_file('js/update-sw.js')

packet/static/js/sw.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
importScripts('https://cdn.onesignal.com/sdks/OneSignalSDKWorker.js');
2+

packet/static/js/update-sw.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
importScripts('https://cdn.onesignal.com/sdks/OneSignalSDKWorker.js');
2+

packet/static/manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020
"sizes": "512x512",
2121
"type": "image/png"
2222
}
23-
]
23+
],
24+
"gcm_sender_id": "482941778795"
2425
}

packet/templates/include/head.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,35 @@
3434

3535
<link rel="stylesheet" href="{{ url_for('static', filename='css/packet.min.css') }}">
3636

37+
<!-- Push Notifications -->
38+
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async=""></script>
39+
<script>
40+
var OneSignal = window.OneSignal || [];
41+
OneSignal.push(function() {
42+
OneSignal.init({
43+
appId: "{{ config['ONESIGNAL_APP_ID'] }}",
44+
autoResubscribe: true,
45+
allowLocalhostAsSecureOrigin: true,
46+
});
47+
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+
});
62+
});
63+
</script>
64+
65+
<!-- Analytics -->
3766
<script async src="https://www.googletagmanager.com/gtag/js?id={{ config['ANALYTICS_ID'] }}"></script>
3867
<script>
3968
window.dataLayer = window.dataLayer || [];

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ Flask-Migrate~=2.2.1
88
pylint~=2.3.1
99
gunicorn~=19.7.1
1010
csh_ldap~=2.1.0
11+
onesignal-sdk~=1.0.0

0 commit comments

Comments
 (0)