Skip to content

Instantly share code, notes, and snippets.

@alexkubica
Created September 15, 2020 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexkubica/8c6fc3ae3bc7364fcb1b8f948138f55c to your computer and use it in GitHub Desktop.
Save alexkubica/8c6fc3ae3bc7364fcb1b8f948138f55c to your computer and use it in GitHub Desktop.
Notify users by mentions
const _ = require('lodash');
const users = {
1: {
id: 1,
name: "alex",
teamdIds: [
10, 20
]
},
2: {
id: 2,
name: "bob",
teamdIds: [
20
]
},
}
function notify(userIds, msg) {
console.log(userIds);
userIds.forEach(userId => {
console.log("hello " + users[userId].name + ", " + msg);
})
}
function getMentions(update) {
return [
{
type: "user",
id: 1
},
{
type: "team",
id: 20
}
]
}
function notifyUpdate(update) {
let mentions = getMentions(update);
let userIdsToNotify = getUserIdsToNotify(mentions);
notify(userIdsToNotify, "You were mentioned")
}
function getUserIdsToNotify(mentions) {
let mappedMentions = mentions.map(mention => {
if (mention.type === "user") {
return mention.id;
}
if (mention.type === "team") {
return _.filter(users, user => {
return user.teamdIds.includes(mention.id);
}).map(user => user.id);
}
})
return _.uniq(_.flatten(mappedMentions));
}
notifyUpdate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment