Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Riott/chat-gui into issue…
Browse files Browse the repository at this point in the history
…-284
  • Loading branch information
Riott committed Aug 15, 2023
2 parents 9c73aa1 + b72855b commit c5222e1
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AmazonAssociatesTagInjector } from './formatters';
import AmazonAssociatesTagInjector from './AmazonAssociatesTagInjector';

const chatStub = {
config: {
Expand Down
23 changes: 19 additions & 4 deletions assets/chat/js/formatters/UrlFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,40 @@ export default class UrlFormatter {
const m = decodedUrl.match(self.linkregex);
if (m) {
const encodedUrl = self.encodeUrl(m[0]);
const normalizedUrl = this.normalizeUrl(encodedUrl);

let embedHashLink = '';
try {
embedHashLink = this.hashLinkConverter.convert(encodedUrl);
embedHashLink = this.hashLinkConverter.convert(normalizedUrl);
} catch (err) {
// ignore
}

const maxUrlLength = 90;
let urlText = encodedUrl;
let urlText = normalizedUrl;
if (urlText.length > maxUrlLength) {
urlText = `${urlText.slice(0, 40)}...${urlText.slice(-40)}`;
}
const extra = self.encodeUrl(decodedUrl.substring(m[0].length));
const href = `${scheme ? '' : 'http://'}${encodedUrl}`;
const href = `${scheme ? '' : 'http://'}${normalizedUrl}`;
return embedHashLink
? `<a target="_blank" class="externallink ${extraclass}" href="${href}" rel="nofollow">${urlText}</a><a class="embed-button" href="/bigscreen${embedHashLink}"><svg xmlns="http://www.w3.org/2000/svg" width="18" height="14.4" viewBox="0 0 640 512"><!--! Font Awesome Free 6.4.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc. --><path d="M64 64V352H576V64H64zM0 64C0 28.7 28.7 0 64 0H576c35.3 0 64 28.7 64 64V352c0 35.3-28.7 64-64 64H64c-35.3 0-64-28.7-64-64V64zM128 448H512c17.7 0 32 14.3 32 32s-14.3 32-32 32H128c-17.7 0-32-14.3-32-32s14.3-32 32-32z" fill="#fff"/></svg></a>`
: `<a target="_blank" class="externallink ${extraclass}" href="${href}" rel="nofollow">${urlText}</a>${extra}`;
}
return url;
});
}

/**
* @param {string} url
* @return {string} The normalized URL.
*/
normalizeUrl(url) {
if (/(x|twitter)\.com\/\w{1,15}\/status\/\d{2,19}\?/i.test(url)) {
// Remove the query string from xeet URLs to protect users from clicking
// on a link to a xeet they've already seen.
return url.split('?')[0];
}

return url;
}
}
29 changes: 29 additions & 0 deletions assets/chat/js/formatters/UrlFormatter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import UrlFormatter from './UrlFormatter';

const urlFormatter = new UrlFormatter();

describe('Normalizing URLs', () => {
test('Remove the query string from a tweet URL', () => {
expect(
urlFormatter.normalizeUrl('https://twitter.com/jack/status/20?lang=en')
).toBe('https://twitter.com/jack/status/20');
});

test('Remove the query string from a xeet URL', () => {
expect(
urlFormatter.normalizeUrl('https://x.com/jack/status/20?lang=en')
).toBe('https://x.com/jack/status/20');
});

test("Don't modify a URL to a tweet that doesn't contain a query string", () => {
expect(urlFormatter.normalizeUrl('https://x.com/jack/status/20')).toBe(
'https://x.com/jack/status/20'
);
});

test("Don't modify a URL that isn't Twitter or X", () => {
expect(
urlFormatter.normalizeUrl('https://www.twitch.tv/search?term=vtuber')
).toBe('https://www.twitch.tv/search?term=vtuber');
});
});
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dgg-chat-gui",
"version": "2.33.0",
"version": "2.34.0",
"description": "Destiny.gg chat client front-end",
"main": "destiny",
"scripts": {
Expand Down

0 comments on commit c5222e1

Please sign in to comment.