[timestamp, session["userid"]])
db.commit()
- comment_push = False
- song_push = False
- if "subid" in session:
- row = db.query(
- "SELECT settings FROM users_push_subscriptions WHERE subid = ?",
- [session["subid"]], one=True)
- if row:
- comment_push = (row["settings"] & push_notifications.SubscriptionSetting.COMMENTS) > 0
- song_push = (row["settings"] & push_notifications.SubscriptionSetting.SONGS) > 0
-
- return render_template("activity.html", comments=notifications, comment_push=comment_push, song_push=song_push)
+ return render_template("activity.html", comments=notifications)
@bp.get("/new-activity")
def new_activity():
import enum
import pywebpush
-from flask import Blueprint, current_app, g, request, session
+from flask import Blueprint, current_app, g, request
from . import auth, datadir, db
return {"status": "success", "subid": row["subid"]}
+@bp.get("/settings")
+@auth.requires_login
+def get_settings():
+ subid = request.args["subid"]
+ row = db.query(
+ """
+ SELECT settings FROM users_push_subscriptions
+ WHERE subid = ? AND userid = ?
+ """,
+ [subid, g.userid], expect_one=True)
+
+ comments = (row["settings"] & SubscriptionSetting.COMMENTS) > 0
+ songs = (row["settings"] & SubscriptionSetting.SONGS) > 0
+
+ return {"comments": comments, "songs": songs}
+
@bp.post("/update-settings")
@auth.requires_login
def update_settings():
# Request must contain valid subscription JSON
abort(400)
-
bitfield = 0
settings = request.json
[bitfield, subid, g.userid])
db.commit()
- current_app.logger.info(f"{g.username} updated push subscription settings: {bitfield:04x}")
+ current_app.logger.info(f"{g.username} updated push subscription settings: ({subid}) {bitfield:04x}")
return {"status": "success"}
// Register service worker
if ("serviceWorker" in navigator)
{
- navigator.serviceWorker.register("service.js");
+ navigator.serviceWorker.register("/service.js");
}
});
-function onLinkClick(event) {
+async function onLinkClick(event) {
if (event.defaultPrevented) {
return;
}
if (urlIsOnSameSite(targetUrl)) {
event.preventDefault();
event.stopPropagation();
+ if (targetUrl.pathname == "/logout")
+ {
+ console.log("logging out");
+ // Logging out - delete notification subscription
+ window.localStorage.removeItem("subid");
+ const registration = await navigator.serviceWorker.getRegistration();
+ const existingSubscription = await registration.pushManager.getSubscription();
+ if (existingSubscription)
+ {
+ existingSubscription.unsubscribe();
+ }
+ }
fetch(targetUrl, {redirect: "follow"}).then(handleAjaxResponse).catch((err) => console.log(err));
}
}
<!--<button onclick="enablePushNotifications()" class="button" style="float: right;">Enable</button>-->
Send me a push notification on this device when:
<br/>
- <label><input type="checkbox" onclick="updateSettings()" id="comment-push" {% if comment_push %}checked{% endif %}>I get a new comment</label>
+ <label><input type="checkbox" onclick="updateSettings()" id="comment-push">I get a new comment</label>
<br/>
- <label><input type="checkbox" onclick="updateSettings()" id="song-push" {% if song_push %}checked{% endif %}>Anyone uploads a new song</label>
+ <label><input type="checkbox" onclick="updateSettings()" id="song-push">Anyone uploads a new song</label>
</div>
<br/>
<br/>
<script>
+
+function updateSelections() {
+ // Prevent this from getting called again on the next page load
+ document.removeEventListener("DOMContentLoaded", updateSelections);
+
+ if (window.localStorage.getItem("subid"))
+ {
+ const params = new URLSearchParams({subid: window.localStorage.getItem("subid")});
+ fetch(`/push-notifications/settings?${params}`).then((r) => {
+ r.json().then((j) => {
+ console.log(j);
+ document.getElementById("comment-push").checked = j.comments;
+ document.getElementById("song-push").checked = j.songs;
+ });
+ })
+ }
+}
+document.addEventListener("DOMContentLoaded", updateSelections());
+
function showSettings() {
document.getElementById("activity-settings").hidden = false;
document.getElementById("btn-show-settings").hidden = true;