Fixed redis error, and added link on click on notifications
This commit is contained in:
		
							
								
								
									
										59
									
								
								api/redis.js
									
									
									
									
									
								
							
							
						
						
									
										59
									
								
								api/redis.js
									
									
									
									
									
								
							| @@ -1,17 +1,45 @@ | ||||
| const redis = require("redis") | ||||
| const client = redis.createClient() | ||||
|  | ||||
| let client; | ||||
| try { | ||||
|   const redis = require("redis"); | ||||
|   console.log("trying to create"); | ||||
|   client = redis.createClient(); | ||||
|   client.on("error", function(err) { | ||||
|     client.quit(); | ||||
|     console.error("Missing redis-configurations.."); | ||||
|     client = { | ||||
|       rpush: function() { | ||||
|         console.log("redis lpush", arguments); | ||||
|         if (typeof arguments[arguments.length - 1] == "function") { | ||||
|           arguments[arguments.length - 1](null); | ||||
|         } | ||||
|       }, | ||||
|       lrange: function() { | ||||
|         console.log("redis lrange", arguments); | ||||
|         if (typeof arguments[arguments.length - 1] == "function") { | ||||
|           arguments[arguments.length - 1](null); | ||||
|         } | ||||
|       }, | ||||
|       del: function() { | ||||
|         console.log("redis del", arguments); | ||||
|         if (typeof arguments[arguments.length - 1] == "function") { | ||||
|           arguments[arguments.length - 1](null); | ||||
|         } | ||||
|       } | ||||
|     }; | ||||
|   }); | ||||
| } catch (e) {} | ||||
|  | ||||
| const addMessage = message => { | ||||
|   const json = JSON.stringify(message); | ||||
|   client.rpush("messages", json) | ||||
|   client.rpush("messages", json); | ||||
|  | ||||
|   return message | ||||
| } | ||||
|   return message; | ||||
| }; | ||||
|  | ||||
| const history = (skip=0, take=20) => { | ||||
|   skip = (1 + skip) * -1     // negate to get FIFO | ||||
|   return new Promise((resolve, reject) => client.lrange("messages", (skip * take), skip, (err, data) => { | ||||
| const history = (skip = 0, take = 20) => { | ||||
|   skip = (1 + skip) * -1; // negate to get FIFO | ||||
|   return new Promise((resolve, reject) => | ||||
|     client.lrange("messages", skip * take, skip, (err, data) => { | ||||
|       if (err) { | ||||
|         console.log(err); | ||||
|         reject(err); | ||||
| @@ -19,18 +47,21 @@ const history = (skip=0, take=20) => { | ||||
|  | ||||
|       data = data.map(data => JSON.parse(data)); | ||||
|       resolve(data); | ||||
|   })) | ||||
| } | ||||
|     }) | ||||
|   ); | ||||
| }; | ||||
|  | ||||
| const clearHistory = () => { | ||||
|   return new Promise((resolve, reject) => client.del("messages", (err, success) => { | ||||
|   return new Promise((resolve, reject) => | ||||
|     client.del("messages", (err, success) => { | ||||
|       if (err) { | ||||
|         console.log(err); | ||||
|         reject(err); | ||||
|       } | ||||
|       resolve(success == 1 ? true : false); | ||||
|   })) | ||||
| } | ||||
|     }) | ||||
|   ); | ||||
| }; | ||||
|  | ||||
| module.exports = { | ||||
|   addMessage, | ||||
|   | ||||
| @@ -9,7 +9,9 @@ mongoose.connect("mongodb://localhost:27017/vinlottis", { | ||||
|   useNewUrlParser: true | ||||
| }); | ||||
|  | ||||
| const mustBeAuthenticated = require(path.join(__dirname + "/../middleware/mustBeAuthenticated")) | ||||
| const mustBeAuthenticated = require(path.join( | ||||
|   __dirname + "/../middleware/mustBeAuthenticated" | ||||
| )); | ||||
|  | ||||
| const config = require(path.join(__dirname + "/../config/defaults/push")); | ||||
| const Subscription = require(path.join(__dirname + "/../schemas/Subscription")); | ||||
| @@ -69,10 +71,13 @@ const saveToDatabase = async subscription => { | ||||
|   } | ||||
| }; | ||||
|  | ||||
| router.route("/send-notification").post(mustBeAuthenticated, async (req, res) => { | ||||
| router | ||||
|   .route("/send-notification") | ||||
|   .post(mustBeAuthenticated, async (req, res) => { | ||||
|     const message = JSON.stringify({ | ||||
|       message: req.body.message, | ||||
|     title: "Vinlotteri!" | ||||
|       title: "Vinlotteri!", | ||||
|       link: req.body.link | ||||
|     }); | ||||
|     let subs = await Subscription.find(); | ||||
|     for (let i = 0; i < subs.length; i++) { | ||||
| @@ -81,7 +86,7 @@ router.route("/send-notification").post(mustBeAuthenticated, async (req, res) => | ||||
|     } | ||||
|     res.json(true); | ||||
|     return; | ||||
| }); | ||||
|   }); | ||||
|  | ||||
| schedule.scheduleJob( | ||||
|   `0 50 ${lotteryConfig.hours - 1} * * ${lotteryConfig.date}`, | ||||
| @@ -91,7 +96,8 @@ schedule.scheduleJob( | ||||
|       let subscription = subs[i]; //get subscription from your databse here. | ||||
|       const message = JSON.stringify({ | ||||
|         message: "Husk vinlotteriet, det begynner om 10 minutter!", | ||||
|         title: "Vinlotteri!" | ||||
|         title: "Vinlotteri!", | ||||
|         link: "/" | ||||
|       }); | ||||
|       sendNotification(subscription, message); | ||||
|     } | ||||
|   | ||||
							
								
								
									
										39
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										39
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							| @@ -1349,9 +1349,9 @@ | ||||
|       "dev": true | ||||
|     }, | ||||
|     "@zxing/library": { | ||||
|       "version": "0.16.0", | ||||
|       "resolved": "https://registry.npmjs.org/@zxing/library/-/library-0.16.0.tgz", | ||||
|       "integrity": "sha512-z3+kNQDQXXuS1O0Q5AHVFXK9O2j/rG4vCIEnvjW4vTrkyA6HuJKQR3BIKqkbZeiMf4VjDPCbrPk3n8ZSw7n/xw==", | ||||
|       "version": "0.15.2", | ||||
|       "resolved": "https://registry.npmjs.org/@zxing/library/-/library-0.15.2.tgz", | ||||
|       "integrity": "sha512-J+N88Eyg6eI2SKIk2YIkjjNICbMSqmLZnB3oD1S21Bi3k+Ddg2eKe/nW+Hce4NKAFAZtY1mdDM08Bj9eu87HSg==", | ||||
|       "requires": { | ||||
|         "text-encoding": "^0.7.0", | ||||
|         "ts-custom-error": "^3.0.0" | ||||
| @@ -9646,6 +9646,39 @@ | ||||
|         "strip-indent": "^1.0.1" | ||||
|       } | ||||
|     }, | ||||
|     "redis": { | ||||
|       "version": "3.0.2", | ||||
|       "resolved": "https://registry.npmjs.org/redis/-/redis-3.0.2.tgz", | ||||
|       "integrity": "sha512-PNhLCrjU6vKVuMOyFu7oSP296mwBkcE6lrAjruBYG5LgdSqtRBoVQIylrMyVZD/lkF24RSNNatzvYag6HRBHjQ==", | ||||
|       "dev": true, | ||||
|       "requires": { | ||||
|         "denque": "^1.4.1", | ||||
|         "redis-commands": "^1.5.0", | ||||
|         "redis-errors": "^1.2.0", | ||||
|         "redis-parser": "^3.0.0" | ||||
|       } | ||||
|     }, | ||||
|     "redis-commands": { | ||||
|       "version": "1.5.0", | ||||
|       "resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.5.0.tgz", | ||||
|       "integrity": "sha512-6KxamqpZ468MeQC3bkWmCB1fp56XL64D4Kf0zJSwDZbVLLm7KFkoIcHrgRvQ+sk8dnhySs7+yBg94yIkAK7aJg==", | ||||
|       "dev": true | ||||
|     }, | ||||
|     "redis-errors": { | ||||
|       "version": "1.2.0", | ||||
|       "resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz", | ||||
|       "integrity": "sha1-62LSrbFeTq9GEMBK/hUpOEJQq60=", | ||||
|       "dev": true | ||||
|     }, | ||||
|     "redis-parser": { | ||||
|       "version": "3.0.0", | ||||
|       "resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz", | ||||
|       "integrity": "sha1-tm2CjNyv5rS4pCin3vTGvKwxyLQ=", | ||||
|       "dev": true, | ||||
|       "requires": { | ||||
|         "redis-errors": "^1.0.0" | ||||
|       } | ||||
|     }, | ||||
|     "referrer-policy": { | ||||
|       "version": "1.2.0", | ||||
|       "resolved": "https://registry.npmjs.org/referrer-policy/-/referrer-policy-1.2.0.tgz", | ||||
|   | ||||
| @@ -13,6 +13,12 @@ | ||||
|           v-model="pushMessage" | ||||
|           placeholder="Push meldingtekst" | ||||
|         /> | ||||
|         <input | ||||
|           id="notification-link" | ||||
|           type="text" | ||||
|           v-model="pushLink" | ||||
|           placeholder="Push-click link" | ||||
|         /> | ||||
|       </div> | ||||
|     </div> | ||||
|     <div class="button-container"> | ||||
| @@ -173,6 +179,7 @@ export default { | ||||
|       winners: [], | ||||
|       wines: [], | ||||
|       pushMessage: "", | ||||
|       pushLink: "/", | ||||
|       toastText: undefined, | ||||
|       showToast: false, | ||||
|       showCamera: false, | ||||
| @@ -239,7 +246,7 @@ export default { | ||||
|           // 'Content-Type': 'application/x-www-form-urlencoded', | ||||
|         }, | ||||
|         method: "POST", | ||||
|         body: JSON.stringify({ message: this.pushMessage }) | ||||
|         body: JSON.stringify({ message: this.pushMessage, link: this.pushLink }) | ||||
|       }); | ||||
|       let response = await _response.json(); | ||||
|       if (response) { | ||||
|   | ||||
| @@ -14,6 +14,18 @@ self.addEventListener("activate", event => { | ||||
|   event.waitUntil(addCache(CACHE_NAME, STATIC_CACHE_URLS)); | ||||
| }); | ||||
|  | ||||
| self.addEventListener("notificationclick", function(event) { | ||||
|   event.notification.close(); | ||||
|   if ( | ||||
|     event.notification.data != undefined && | ||||
|     event.notification.data.link != undefined | ||||
|   ) { | ||||
|     event.waitUntil(clients.openWindow(event.notification.data.link)); | ||||
|   } else { | ||||
|     event.waitUntil(clients.openWindow("/")); | ||||
|   } | ||||
| }); | ||||
|  | ||||
| self.addEventListener("message", event => { | ||||
|   if (!__PUBLICKEY__) { | ||||
|     return; | ||||
| @@ -52,8 +64,17 @@ self.addEventListener("message", event => { | ||||
| self.addEventListener("push", function(event) { | ||||
|   if (event.data) { | ||||
|     var message = JSON.parse(event.data.text()); | ||||
|     var link = "/"; | ||||
|     if (message.link != undefined) { | ||||
|       link = message.link; | ||||
|     } | ||||
|  | ||||
|     showLocalNotification(message.title, message.message, self.registration); | ||||
|     showLocalNotification( | ||||
|       message.title, | ||||
|       message.message, | ||||
|       link, | ||||
|       self.registration | ||||
|     ); | ||||
|   } else { | ||||
|   } | ||||
| }); | ||||
| @@ -105,12 +126,13 @@ self.addEventListener("fetch", event => { | ||||
|   } | ||||
| }); | ||||
|  | ||||
| function showLocalNotification(title, body, swRegistration) { | ||||
| function showLocalNotification(title, body, link, swRegistration) { | ||||
|   const options = { | ||||
|     body, | ||||
|     icon: "https://lottis.vin/public/assets/images/favicon.png", | ||||
|     image: "https://lottis.vin/public/assets/images/favicon.png", | ||||
|     vibrate: [300] | ||||
|     vibrate: [300], | ||||
|     data: { link: link } | ||||
|   }; | ||||
|   swRegistration.showNotification(title, options); | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user