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
This commit is contained in:
		@ -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")
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
@ -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')
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
			});
 | 
			
		||||
		});
 | 
			
		||||
	});
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user