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()
 | 
							configfile = findConfigFile()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if configfile:
 | 
							if configfile:
 | 
				
			||||||
			self.config = ConfigParser()
 | 
								self.config = ConfigParser(strict=False)
 | 
				
			||||||
			self.config.read(configfile)
 | 
								self.config.read(configfile)
 | 
				
			||||||
		else:
 | 
							else:
 | 
				
			||||||
			raise Exception("No config file found")
 | 
								raise Exception("No config file found")
 | 
				
			||||||
 | 
				
			|||||||
@ -180,12 +180,7 @@ def post_pushsub():
 | 
				
			|||||||
	except ValueError as e:
 | 
						except ValueError as e:
 | 
				
			||||||
		return 'Error: JSON decode error:\n' + str(e), 400
 | 
							return 'Error: JSON decode error:\n' + str(e), 400
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	name = slugify(data['name'])
 | 
						notifier.subscribe_user(data)
 | 
				
			||||||
	data['name'] = name
 | 
					 | 
				
			||||||
	print(json.dumps(data, indent=SPACES))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	Entry = Query()
 | 
					 | 
				
			||||||
	pushsubs.upsert(data, Entry.name == name)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return 'OK', 201
 | 
						return 'OK', 201
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -211,6 +206,6 @@ def testpush(name):
 | 
				
			|||||||
	error = notifier.notify_user(arrivals[0])
 | 
						error = notifier.notify_user(arrivals[0])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if error:
 | 
						if error:
 | 
				
			||||||
		return 'Error: ' + error, 201
 | 
							return 'Error: ' + error, 404
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return 'OK', 201
 | 
						return 'OK', 201
 | 
				
			||||||
 | 
				
			|||||||
@ -1,4 +1,5 @@
 | 
				
			|||||||
import json
 | 
					import json
 | 
				
			||||||
 | 
					from slugify import slugify
 | 
				
			||||||
from threading import Thread, Event
 | 
					from threading import Thread, Event
 | 
				
			||||||
from datetime import datetime, timedelta
 | 
					from datetime import datetime, timedelta
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -10,6 +11,16 @@ ONE_HOUR_IN_S = 60 * 60
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
class Notifier(Thread):
 | 
					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):
 | 
						def notify_user(self, arrival):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		pushsubs = self.db.table('pushsubs')
 | 
							pushsubs = self.db.table('pushsubs')
 | 
				
			||||||
 | 
				
			|||||||
@ -250,7 +250,6 @@ function initPush(name) {
 | 
				
			|||||||
			applicationServerKey: pushServerPublicKey
 | 
								applicationServerKey: pushServerPublicKey
 | 
				
			||||||
		}).then(function(subscription) {
 | 
							}).then(function(subscription) {
 | 
				
			||||||
			console.log("User is subscribed:", subscription);
 | 
								console.log("User is subscribed:", subscription);
 | 
				
			||||||
			localStorage.setItem('pushsub', subscription);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
			fetch('/pushsubscribe', {
 | 
								fetch('/pushsubscribe', {
 | 
				
			||||||
				method: "POST",
 | 
									method: "POST",
 | 
				
			||||||
@ -259,6 +258,9 @@ function initPush(name) {
 | 
				
			|||||||
					name: name,
 | 
										name: name,
 | 
				
			||||||
					sub: subscription
 | 
										sub: subscription
 | 
				
			||||||
				})
 | 
									})
 | 
				
			||||||
 | 
								}).then(function(res) {
 | 
				
			||||||
 | 
									if (res.ok)
 | 
				
			||||||
 | 
										localStorage.setItem('pushsub', subscription);
 | 
				
			||||||
			});
 | 
								});
 | 
				
			||||||
		});
 | 
							});
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user