Skip to content

Instantly share code, notes, and snippets.

@iamfunsho iamfunsho/block6.js Secret
Created Oct 24, 2017

Embed
What would you like to do?
export let createUser = router.post("/user", (req, res) => {
let users;
let user = req.body.user;
fetchUsers().then(u => {
users = u;
if (users.indexOf(user) === -1) {
helper.addActiveUser(user).then(
() => {
client().then(
client => {
let msg = {
message:
req.body.user +
" just joined the chat room",
user: "system"
};
client.publish("chatMessages", JSON.stringify(msg));
client.publish(
"activeUsers",
JSON.stringify(fetchUsers())
);
helper.addMessage(JSON.stringify(msg)).then(
() => {
res.send({
status: 200,
message: "User joined"
});
},
err => {
console.log(err);
}
);
},
err => {
console.log(err);
}
);
},
err => {
console.log(err);
}
);
} else {
res.send({ status: 403, message: "User already exist" });
}
});
});
export let deleteUser = router.delete("/user", (req, res) => {
let users;
let user = req.body.user;
fetchUsers().then(u => {
users = u;
if (users.indexOf(user) !== -1) {
helper.removeActiveUser(user).then(
() => {
client().then(
client => {
let msg = {
message: req.body.user + " just left the chat room",
user: "system"
};
client.publish("chatMessages", JSON.stringify(msg));
client.publish(
"activeUsers",
JSON.stringify(fetchUsers())
);
helper.addMessage(JSON.stringify(msg)).then(
() => {
res.send({
status: 200,
message: "User removed"
});
},
err => {
console.log(err);
}
);
},
err => {
console.log(err);
}
);
},
err => {
console.log(err);
}
);
} else {
res.send({ status: 403, message: "User does not exist" });
}
});
});
export let createMessage = router.post("/message", (req, res) => {
let msg = {
message: req.body.msg,
user: req.body.user
};
client().then(
client => {
client.publish("chatMessages", JSON.stringify(msg));
helper.addMessage(JSON.stringify(msg)).then(
() => {
res.send({
status: 200,
message: "Message sent"
});
},
err => {
console.log(err);
}
);
},
err => {
console.log(err);
}
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.