-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
570 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
""" | ||
Notifications App | ||
Push/SMS notifcations and management thereof | ||
""" | ||
|
||
from flask import Blueprint | ||
|
||
notifications = Blueprint("notifications", __name__) | ||
|
||
from . import views # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from apps.common import json_response | ||
from main import db | ||
from flask import render_template, request, current_app as app | ||
from flask_login import current_user, login_required | ||
|
||
from . import notifications | ||
from models.web_push import public_key, WebPushTarget | ||
|
||
|
||
@notifications.route("/") | ||
@login_required | ||
def index(): | ||
return render_template("notifications/index.html", public_key=public_key()) | ||
|
||
|
||
@notifications.route("/register", methods=["POST"]) | ||
@json_response | ||
@login_required | ||
def register(): | ||
payload = request.json | ||
|
||
target = WebPushTarget.query.filter_by( | ||
user=current_user, endpoint=payload["endpoint"] | ||
).first() | ||
|
||
if target is None: | ||
app.logger.info("Creating new target") | ||
target = WebPushTarget( | ||
user=current_user, | ||
endpoint=payload["endpoint"], | ||
subscription_info=payload, | ||
expires=payload.get("expires", None), | ||
) | ||
|
||
db.session.add(target) | ||
db.session.commit() | ||
else: | ||
app.logger.info("Using existing target") | ||
|
||
return { | ||
"id": target.id, | ||
"user_id": target.user_id, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.state { | ||
display: none; | ||
} | ||
|
||
.state.visible { | ||
display: block; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
async function enableNotifications(event) { | ||
let vapid_key = document.querySelector("meta[name=vapid_key]").getAttribute("value"); | ||
let worker = await navigator.serviceWorker.ready; | ||
let result = await worker.pushManager.subscribe({ | ||
userVisibleOnly: true, | ||
applicationServerKey: vapid_key, | ||
}); | ||
|
||
let response = await fetch("/account/notifications/register", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json" | ||
}, | ||
body: JSON.stringify(result.toJSON()) | ||
}); | ||
} | ||
|
||
async function checkPermissions() { | ||
let worker = await navigator.serviceWorker.ready; | ||
|
||
if ("pushManager" in worker) { | ||
let permissions = await worker.pushManager.permissionState(); | ||
document.getElementById('notification-state').querySelectorAll('.state').forEach(el => { el.classList.add('visible') }) | ||
document.getElementById(`notification-state-${permissions}`).classList.add('visible') | ||
} else { | ||
console.log("No push notification support."); | ||
} | ||
} | ||
|
||
if ("serviceWorker" in navigator) { | ||
checkPermissions(); | ||
document.getElementById("enable-notifications").addEventListener("click", enableNotifications); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,26 @@ | ||
from datetime import datetime | ||
from main import db | ||
from flask import current_app as app | ||
from pywebpush import webpush | ||
|
||
from . import BaseModel | ||
|
||
|
||
def public_key(): | ||
return app.config["WEBPUSH_PUBLIC_KEY"] | ||
|
||
|
||
def notify(target, message): | ||
webpush( | ||
subscription_info=target.subscription_info, | ||
data=message, | ||
vapid_private_key=app.config["WEBPUSH_PRIVATE_KEY"], | ||
vapid_claims={ | ||
"sub": "mailto:[email protected]", | ||
}, | ||
) | ||
|
||
|
||
class WebPushTarget(BaseModel): | ||
__table_name__ = "web_push_target" | ||
id = db.Column(db.Integer, primary_key=True) | ||
|
Oops, something went wrong.