mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
Create links to users profile in chat messages
This commit is contained in:
22
website/server/libs/highlightMentions.js
Normal file
22
website/server/libs/highlightMentions.js
Normal file
@@ -0,0 +1,22 @@
|
||||
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}\\b`, 'g'), `[@${username}](https://habitica.com/members/${member._id})`);
|
||||
});
|
||||
}
|
||||
return text;
|
||||
}
|
||||
Reference in New Issue
Block a user