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")
|
||||
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) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
reject(err);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
data = data.map(data => JSON.parse(data));
|
||||
resolve(data);
|
||||
}))
|
||||
}
|
||||
data = data.map(data => JSON.parse(data));
|
||||
resolve(data);
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
const clearHistory = () => {
|
||||
return new Promise((resolve, reject) => client.del("messages", (err, success) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
reject(err);
|
||||
}
|
||||
resolve(success == 1 ? true : false);
|
||||
}))
|
||||
}
|
||||
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,
|
||||
history,
|
||||
clearHistory
|
||||
};
|
||||
};
|
||||
|
||||
@@ -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,19 +71,22 @@ const saveToDatabase = async subscription => {
|
||||
}
|
||||
};
|
||||
|
||||
router.route("/send-notification").post(mustBeAuthenticated, async (req, res) => {
|
||||
const message = JSON.stringify({
|
||||
message: req.body.message,
|
||||
title: "Vinlotteri!"
|
||||
router
|
||||
.route("/send-notification")
|
||||
.post(mustBeAuthenticated, async (req, res) => {
|
||||
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(
|
||||
`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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user