Created
September 15, 2020 14:07
-
-
Save alexkubica/8c6fc3ae3bc7364fcb1b8f948138f55c to your computer and use it in GitHub Desktop.
Notify users by mentions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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