12922 - Use absolute user URLs on production (#12968)

This commit is contained in:
Bart Enkelaar
2021-04-29 22:23:42 +02:00
committed by GitHub
parent 591c9bc98c
commit 920a093b0e
2 changed files with 49 additions and 28 deletions

View File

@@ -11,14 +11,14 @@ describe('highlightMentions', () => {
lean () { lean () {
return this; return this;
}, },
exec () { async exec () {
return Promise.resolve([ return [
{ auth: { local: { username: 'user' } }, _id: '111' }, { auth: { local: { username: 'user' } }, _id: '111' },
{ auth: { local: { username: 'user2' } }, _id: '222' }, { auth: { local: { username: 'user2' } }, _id: '222' },
{ auth: { local: { username: 'user3' } }, _id: '333' }, { auth: { local: { username: 'user3' } }, _id: '333' },
{ auth: { local: { username: 'user-dash' } }, _id: '444' }, { auth: { local: { username: 'user-dash' } }, _id: '444' },
{ auth: { local: { username: 'user_underscore' } }, _id: '555' }, { auth: { local: { username: 'user_underscore' } }, _id: '555' },
]); ];
}, },
}; };
@@ -168,6 +168,7 @@ describe('highlightMentions', () => {
}); });
}); });
describe('issues', () => {
it('github issue 12118, method crashes when square brackets are used', async () => { it('github issue 12118, method crashes when square brackets are used', async () => {
const text = '[test]'; const text = '[test]';
@@ -199,4 +200,18 @@ describe('highlightMentions', () => {
expect(result[0]).to.equal(text); expect(result[0]).to.equal(text);
}); });
});
describe('base url', () => {
it('should prefix habitica.com if it is production', async () => {
const OLD_NODE_ENV = process.env.NODE_ENV;
process.env.NODE_ENV = 'production';
const text = '@user';
const result = await highlightMentions(text);
expect(result[0]).to.equal('[@user](https://habitica.com/profile/111)');
process.env.NODE_ENV = OLD_NODE_ENV;
});
});
}); });

View File

@@ -144,6 +144,11 @@ function findTextBlocks (text) {
return new TextBlocks(blocks); return new TextBlocks(blocks);
} }
function determineBaseUrl () {
// eslint-disable-next-line no-process-env
return process.env.NODE_ENV === 'production' ? 'https://habitica.com' : '';
}
/** /**
* Replaces `@user` mentions by `[@user](/profile/{user-id})` markup to inject * Replaces `@user` mentions by `[@user](/profile/{user-id})` markup to inject
* a link towards the user's profile page. * a link towards the user's profile page.
@@ -164,10 +169,11 @@ export default async function highlightMentions (text) {
.select(['auth.local.username', '_id', 'preferences.pushNotifications', 'pushDevices', 'party', 'guilds']) .select(['auth.local.username', '_id', 'preferences.pushNotifications', 'pushDevices', 'party', 'guilds'])
.lean() .lean()
.exec(); .exec();
const baseUrl = determineBaseUrl();
members.forEach(member => { members.forEach(member => {
const { username } = member.auth.local; const { username } = member.auth.local;
const regex = new RegExp(`@${username}(?![\\-\\w])`, 'g'); const regex = new RegExp(`@${username}(?![\\-\\w])`, 'g');
const replacement = `[@${username}](/profile/${member._id})`; const replacement = `[@${username}](${baseUrl}/profile/${member._id})`;
textBlocks.transformValidBlocks(blockText => blockText.replace(regex, replacement)); textBlocks.transformValidBlocks(blockText => blockText.replace(regex, replacement));
}); });