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