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
5 changes: 5 additions & 0 deletions config.env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a test ID that only works on localhost:6969 for testing

Copy link
Member

Choose a reason for hiding this comment

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

You may want to note that in a line comment then. It'd help us track what is needed for local testing.


# Slack URL for pushing to #general
SLACK_WEBHOOK_URL = environ.get("PACKET_SLACK_URL", None)
35 changes: 35 additions & 0 deletions migrations/versions/53768f0a4850_notifications.py
Original file line number Diff line number Diff line change
@@ -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 ###

7 changes: 7 additions & 0 deletions packet/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
16 changes: 15 additions & 1 deletion packet/routes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<packet_id>/", methods=["POST"])
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions packet/routes/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
2 changes: 2 additions & 0 deletions packet/static/js/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
importScripts('https://cdn.onesignal.com/sdks/OneSignalSDKWorker.js');

2 changes: 2 additions & 0 deletions packet/static/js/update-sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
importScripts('https://cdn.onesignal.com/sdks/OneSignalSDKWorker.js');

3 changes: 2 additions & 1 deletion packet/static/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
"sizes": "512x512",
"type": "image/png"
}
]
],
"gcm_sender_id": "482941778795"
}
29 changes: 29 additions & 0 deletions packet/templates/include/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,35 @@

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

<!-- Push Notifications -->
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async=""></script>
<script>
Copy link
Member Author

Choose a reason for hiding this comment

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

As much as possible from this will be moved into a notifications.js file in Phase 2

var OneSignal = window.OneSignal || [];
OneSignal.push(function() {
OneSignal.init({
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);
}
});
});
});
});
</script>

<!-- Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id={{ config['ANALYTICS_ID'] }}"></script>
<script>
window.dataLayer = window.dataLayer || [];
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Flask-Migrate~=2.2.1
pylint~=2.3.1
gunicorn~=19.7.1
csh_ldap~=2.1.0
onesignal-sdk~=1.0.0