habitica/website/server/libs/highlightMentions.js

24 lines
939 B
JavaScript
Raw Normal View History

import { model as User } from '../models/user';
const mentionRegex = new RegExp('\\B@[-\\w]+', 'g');
export async function highlightMentions (text) { // eslint-disable-line import/prefer-default-export
const mentions = text.match(mentionRegex);
2019-09-30 13:10:20 +00:00
let members = [];
if (mentions !== null && mentions.length <= 5) {
const usernames = mentions.map(mention => mention.substr(1));
2019-09-30 13:10:20 +00:00
members = await User
.find({ 'auth.local.username': { $in: usernames }, 'flags.verifiedUsername': true })
.select(['auth.local.username', '_id', 'preferences.pushNotifications', 'pushDevices', 'party', 'guilds'])
.lean()
.exec();
members.forEach(member => {
const { username } = member.auth.local;
// eslint-disable-next-line no-param-reassign
2018-11-27 12:20:45 +00:00
text = text.replace(new RegExp(`@${username}(?![\\-\\w])`, 'g'), `[@${username}](/profile/${member._id})`);
});
}
2019-09-30 13:10:20 +00:00
return [text, mentions, members];
}