diff --git a/config.env.py b/config.env.py index e403b052..0eb3f4e6 100644 --- a/config.env.py +++ b/config.env.py @@ -38,5 +38,10 @@ MAIL_PASSWORD = environ.get("PACKET_MAIL_PASSWORD", None) MAIL_USE_TLS = strtobool(environ.get("PACKET_MAIL_TLS", 'True')) +# 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") + # Slack URL for pushing to #general SLACK_WEBHOOK_URL = environ.get("PACKET_SLACK_URL", None) diff --git a/migrations/versions/53768f0a4850_notifications.py b/migrations/versions/53768f0a4850_notifications.py new file mode 100644 index 00000000..3f10330a --- /dev/null +++ b/migrations/versions/53768f0a4850_notifications.py @@ -0,0 +1,35 @@ +"""notifications + +Revision ID: 53768f0a4850 +Revises: eecf30892d0e +Create Date: 2019-08-06 22:15:04.400982 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '53768f0a4850' +down_revision = 'eecf30892d0e' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('notification_subscriptions', + sa.Column('member', sa.String(length=36), nullable=True), + sa.Column('freshman_username', sa.String(length=10), nullable=True), + sa.Column('token', sa.String(length=256), nullable=False), + sa.ForeignKeyConstraint(['freshman_username'], ['freshman.rit_username'], ), + sa.PrimaryKeyConstraint('token') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('notification_subscriptions') + # ### end Alembic commands ### + diff --git a/packet/models.py b/packet/models.py index c3e6b4dc..b251661c 100644 --- a/packet/models.py +++ b/packet/models.py @@ -154,3 +154,10 @@ class MiscSignature(db.Model): updated = Column(DateTime, default=datetime.now, onupdate=datetime.now, nullable=False) packet = relationship("Packet", back_populates="misc_signatures") + + +class NotificationSubscription(db.Model): + __tablename__ = "notification_subscriptions" + member = Column(String(36), nullable=True) + freshman_username = Column(ForeignKey("freshman.rit_username"), nullable=True) + token = Column(String(256), primary_key=True, nullable=False) diff --git a/packet/routes/api.py b/packet/routes/api.py index abae590c..5e0bc576 100644 --- a/packet/routes/api.py +++ b/packet/routes/api.py @@ -7,7 +7,7 @@ from packet.context_processors import get_rit_name from packet.mail import send_report_mail from packet.utils import before_request, packet_auth, notify_slack -from packet.models import Packet, MiscSignature +from packet.models import Packet, MiscSignature, NotificationSubscription @app.route("/api/v1/sign//", methods=["POST"]) @@ -40,6 +40,20 @@ def sign(packet_id, info): return "Error: Signature not valid. Reason: Unknown" +@app.route("/api/v1/subscribe/", methods=["POST"]) +@packet_auth +@before_request +def subscribe(info): + data = request.form + if app.config["REALM"] == "csh": + subscription = NotificationSubscription(token=data['token'], member=info["uid"]) + else: + subscription = NotificationSubscription(token=data['token'], freshman_username=info["uid"]) + db.session.add(subscription) + db.session.commit() + return "Token subscribed for " + info["uid"] + + @app.route("/api/v1/report/", methods=["POST"]) @packet_auth @before_request diff --git a/packet/routes/shared.py b/packet/routes/shared.py index 3d894baa..8f4422ca 100644 --- a/packet/routes/shared.py +++ b/packet/routes/shared.py @@ -68,3 +68,15 @@ def packets(info=None): open_packets.sort(key=packet_sort_key, reverse=True) return render_template("active_packets.html", info=info, packets=open_packets) + + +@app.route('/sw.js', methods=['GET']) +@app.route('/OneSignalSDKWorker.js', methods=['GET']) +def service_worker(): + return app.send_static_file('js/sw.js') + + +@app.route('/update-sw.js', methods=['GET']) +@app.route('/OneSignalSDKUpdaterWorker.js', methods=['GET']) +def update_service_worker(): + return app.send_static_file('js/update-sw.js') diff --git a/packet/static/js/sw.js b/packet/static/js/sw.js new file mode 100644 index 00000000..81157226 --- /dev/null +++ b/packet/static/js/sw.js @@ -0,0 +1,2 @@ +importScripts('https://cdn.onesignal.com/sdks/OneSignalSDKWorker.js'); + diff --git a/packet/static/js/update-sw.js b/packet/static/js/update-sw.js new file mode 100644 index 00000000..81157226 --- /dev/null +++ b/packet/static/js/update-sw.js @@ -0,0 +1,2 @@ +importScripts('https://cdn.onesignal.com/sdks/OneSignalSDKWorker.js'); + diff --git a/packet/static/manifest.json b/packet/static/manifest.json index 1425b006..d7916ac9 100644 --- a/packet/static/manifest.json +++ b/packet/static/manifest.json @@ -20,5 +20,6 @@ "sizes": "512x512", "type": "image/png" } - ] + ], + "gcm_sender_id": "482941778795" } diff --git a/packet/templates/include/head.html b/packet/templates/include/head.html index 677e611f..2b394dae 100644 --- a/packet/templates/include/head.html +++ b/packet/templates/include/head.html @@ -34,6 +34,35 @@ + + + + +