mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-16 14:17:22 +01:00
23 lines
755 B
JavaScript
23 lines
755 B
JavaScript
import {model as User} from '../models/user';
|
|
|
|
const mentionRegex = new RegExp('\\B@[-\\w]+', 'g');
|
|
|
|
export async function highlightMentions (text) {
|
|
const mentions = text.match(mentionRegex);
|
|
if (mentions !== null && mentions.length <= 5) {
|
|
const usernames = mentions.map((mention) => {
|
|
return mention.substr(1);
|
|
});
|
|
let members = await User
|
|
.find({'auth.local.username': {$in: usernames}, 'flags.verifiedUsername': true})
|
|
.select(['auth.local.username', '_id'])
|
|
.lean()
|
|
.exec();
|
|
members.forEach((member) => {
|
|
const username = member.auth.local.username;
|
|
text = text.replace(new RegExp(`@${username}(?![\\-\\w])`, 'g'), `[@${username}](/profile/${member._id})`);
|
|
});
|
|
}
|
|
return text;
|
|
}
|