From 4ee4869f82807284646df69c729c6d2ea6ec8054 Mon Sep 17 00:00:00 2001 From: Oskar Date: Fri, 11 Jun 2021 01:04:44 +0200 Subject: [PATCH] Minor fixes & improvements - Move push subscription handling to Notifier class - Allow duplicate options in config - Only save subscription in frontend if sub call was successful --- ftracker/config.py | 2 +- ftracker/core.py | 9 ++------- ftracker/notifier.py | 11 +++++++++++ web/main.js | 4 +++- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/ftracker/config.py b/ftracker/config.py index 19cdf74..4c4cd8b 100644 --- a/ftracker/config.py +++ b/ftracker/config.py @@ -19,7 +19,7 @@ class Config: configfile = findConfigFile() if configfile: - self.config = ConfigParser() + self.config = ConfigParser(strict=False) self.config.read(configfile) else: raise Exception("No config file found") diff --git a/ftracker/core.py b/ftracker/core.py index a2e824c..8c79207 100644 --- a/ftracker/core.py +++ b/ftracker/core.py @@ -180,12 +180,7 @@ def post_pushsub(): except ValueError as e: return 'Error: JSON decode error:\n' + str(e), 400 - name = slugify(data['name']) - data['name'] = name - print(json.dumps(data, indent=SPACES)) - - Entry = Query() - pushsubs.upsert(data, Entry.name == name) + notifier.subscribe_user(data) return 'OK', 201 @@ -211,6 +206,6 @@ def testpush(name): error = notifier.notify_user(arrivals[0]) if error: - return 'Error: ' + error, 201 + return 'Error: ' + error, 404 return 'OK', 201 diff --git a/ftracker/notifier.py b/ftracker/notifier.py index 5cb7286..68ab7aa 100644 --- a/ftracker/notifier.py +++ b/ftracker/notifier.py @@ -1,4 +1,5 @@ import json +from slugify import slugify from threading import Thread, Event from datetime import datetime, timedelta @@ -10,6 +11,16 @@ ONE_HOUR_IN_S = 60 * 60 class Notifier(Thread): + def subscribe_user(self, data): + + pushsubs = self.db.table('pushsubs') + + name = slugify(data['name']) + data['name'] = name + + Entry = Query() + pushsubs.upsert(data, Entry.name == name) + def notify_user(self, arrival): pushsubs = self.db.table('pushsubs') diff --git a/web/main.js b/web/main.js index 6e6b6f0..0ee42c9 100644 --- a/web/main.js +++ b/web/main.js @@ -250,7 +250,6 @@ function initPush(name) { applicationServerKey: pushServerPublicKey }).then(function(subscription) { console.log("User is subscribed:", subscription); - localStorage.setItem('pushsub', subscription); fetch('/pushsubscribe', { method: "POST", @@ -259,6 +258,9 @@ function initPush(name) { name: name, sub: subscription }) + }).then(function(res) { + if (res.ok) + localStorage.setItem('pushsub', subscription); }); }); });