mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 09:01:17 -08:00
Cleanup of some of the changes (#745)
<!-- Please read https://github.com/SableClient/Sable/blob/dev/CONTRIBUTING.md before submitting your pull request --> ### Description Fixes the issues with the sizing of the user-hero, small saving issue, previews now showing when put in the [](), and matrix.to links being added to link-previews even though they reasonably shouldn't. It also fixes the matrix.to links to be made with a trailing / to avoid weird tricks in the usage of them for malicious purposes This is general maintenance however so does not really need a changeset because its just a bunch of extremely small changes Fixes #740 #### Type of change - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update ### Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings ### AI disclosure: - [ ] Partially AI assisted (clarify which code was AI assisted and briefly explain what it does). - [ ] Fully AI generated (explain what all the generated code does in moderate detail). <!-- Write any explanation required here, but do not generate the explanation using AI!! You must prove you understand what the code in this PR does. --> I had a nightmare that the new version shipped incomplete code
This commit is contained in:
7 files changed
+60
-55
No files matched your search
@@ -8,6 +8,7 @@ import { isUserId } from '$utils/matrix';
|
||||
import type { CustomElement } from './slate';
|
||||
import { BlockType } from './types';
|
||||
import { getMarkdownCodeSpanRanges, isInsideMarkdownCodeSpan } from './utils';
|
||||
import { MATRIX_TO_BASE } from '$plugins/matrix-to';
|
||||
|
||||
export type OutputOptions = {
|
||||
/**
|
||||
@@ -294,6 +295,8 @@ export const getLinks = (serialized: Descendant | Descendant[]): string[] | unde
|
||||
continue;
|
||||
}
|
||||
|
||||
if (url.startsWith(MATRIX_TO_BASE)) continue;
|
||||
|
||||
finalList.add(url);
|
||||
}
|
||||
|
||||
|
||||
@@ -37,6 +37,7 @@ import {
|
||||
import { MessageTextBody } from './layout';
|
||||
import { unwrapForwardedContent } from './modals/MessageForward';
|
||||
import { LINKINPUTREGEX } from '$components/editor';
|
||||
import { MATRIX_TO_BASE } from '$plugins/matrix-to';
|
||||
|
||||
export interface BundleContent extends IPreviewUrlResponse {
|
||||
matched_url: string;
|
||||
@@ -118,7 +119,7 @@ const getUrlsFromContent = (
|
||||
const safeHtml = customBody
|
||||
.replace(/<pre[^>]*>.*?<\/pre>/gs, '')
|
||||
.replace(/<code[^>]*>.*?<\/code>/gs, '');
|
||||
const safeText = safeHtml.replace(/<[^>]*>/g, '');
|
||||
const safeText = safeHtml.replace(/<[^a][^>]*>/g, '');
|
||||
const safeUrlsMatch = safeText.match(LINKINPUTREGEX);
|
||||
let safeUrls = safeUrlsMatch ? [...new Set(safeUrlsMatch)] : [];
|
||||
safeUrls = safeUrls.map(
|
||||
@@ -129,7 +130,7 @@ const getUrlsFromContent = (
|
||||
url
|
||||
);
|
||||
const safeUrlsSet = new Set(safeUrls);
|
||||
urls = urls.filter((url) => safeUrlsSet.has(url));
|
||||
urls = urls.filter((url) => safeUrlsSet.has(url) && !url.startsWith(MATRIX_TO_BASE));
|
||||
}
|
||||
|
||||
let bundleContent = content['com.beeper.linkpreviews'] as BundleContent[];
|
||||
|
||||
@@ -631,42 +631,42 @@ export function UserRoomProfile({ userId, initialProfile }: Readonly<UserRoomPro
|
||||
/>
|
||||
)}
|
||||
</Box>
|
||||
</Box>
|
||||
{ignored && <IgnoredUserAlert />}
|
||||
{member && membership === bannedMembership && (
|
||||
<UserBanAlert
|
||||
userId={userId}
|
||||
reason={member.events.member?.getContent().reason}
|
||||
canUnban={canUnban}
|
||||
bannedBy={member.events.member?.getSender()}
|
||||
ts={member.events.member?.getTs()}
|
||||
/>
|
||||
)}
|
||||
{member &&
|
||||
membership === leftMembership &&
|
||||
member.events.member &&
|
||||
member.events.member.getSender() !== userId && (
|
||||
<UserKickAlert
|
||||
{ignored && <IgnoredUserAlert />}
|
||||
{member && membership === bannedMembership && (
|
||||
<UserBanAlert
|
||||
userId={userId}
|
||||
reason={member.events.member?.getContent().reason}
|
||||
kickedBy={member.events.member?.getSender()}
|
||||
canUnban={canUnban}
|
||||
bannedBy={member.events.member?.getSender()}
|
||||
ts={member.events.member?.getTs()}
|
||||
/>
|
||||
)}
|
||||
{member && membership === invitedMembership && (
|
||||
<UserInviteAlert
|
||||
{member &&
|
||||
membership === leftMembership &&
|
||||
member.events.member &&
|
||||
member.events.member.getSender() !== userId && (
|
||||
<UserKickAlert
|
||||
reason={member.events.member?.getContent().reason}
|
||||
kickedBy={member.events.member?.getSender()}
|
||||
ts={member.events.member?.getTs()}
|
||||
/>
|
||||
)}
|
||||
{member && membership === invitedMembership && (
|
||||
<UserInviteAlert
|
||||
userId={userId}
|
||||
reason={member.events.member?.getContent().reason}
|
||||
canKick={canKickUser}
|
||||
invitedBy={member.events.member?.getSender()}
|
||||
ts={member.events.member?.getTs()}
|
||||
/>
|
||||
)}
|
||||
<UserModeration
|
||||
userId={userId}
|
||||
reason={member.events.member?.getContent().reason}
|
||||
canKick={canKickUser}
|
||||
invitedBy={member.events.member?.getSender()}
|
||||
ts={member.events.member?.getTs()}
|
||||
canInvite={canInvite && membership === leftMembership}
|
||||
canKick={canKickUser && membership === joinedMembership}
|
||||
canBan={canBanUser && membership !== bannedMembership}
|
||||
/>
|
||||
)}
|
||||
<UserModeration
|
||||
userId={userId}
|
||||
canInvite={canInvite && membership === leftMembership}
|
||||
canKick={canKickUser && membership === joinedMembership}
|
||||
canBan={canBanUser && membership !== bannedMembership}
|
||||
/>
|
||||
</Box>
|
||||
</Box>
|
||||
</Box>
|
||||
);
|
||||
|
||||
@@ -148,7 +148,8 @@ export const MessageEditor = as<'div', MessageEditorProps>(
|
||||
const isHidden =
|
||||
(bundleContent?.length === 0 ||
|
||||
bundleContent.filter((b) => s.includes(b.matched_url)).length === 0) &&
|
||||
strippedS.match(LINKINPUTREGEX) !== null;
|
||||
strippedS.match(LINKINPUTREGEX) !== null &&
|
||||
strippedS.startsWith('https://matrix.to/');
|
||||
newBody += `${isHidden ? (isHTML && ((s.startsWith('<a') && `<${s[0]}`) || `${s[0]}<`)) || `${s[0]}<` : s[0]}${strippedS}${isHidden ? (isHTML && '>') || '>' : ''}`;
|
||||
});
|
||||
return newBody;
|
||||
|
||||
@@ -642,7 +642,7 @@ function ProfileExtended({ profile, userId }: Readonly<ProfileProps>) {
|
||||
onSave={(color) =>
|
||||
handleSaveField('chat.commet.profile_color_scheme', {
|
||||
color,
|
||||
brightness: profile?.heroColorScheme?.brightness,
|
||||
brightness: color ? profile?.heroColorScheme?.brightness : null,
|
||||
})
|
||||
}
|
||||
/>
|
||||
|
||||
@@ -25,7 +25,7 @@ describe('getMatrixToUser', () => {
|
||||
});
|
||||
|
||||
it('uses custom base when configured', () => {
|
||||
setMatrixToBase('https://matrix.example.org');
|
||||
setMatrixToBase('https://matrix.example.org/');
|
||||
expect(getMatrixToUser('@alice:example.com')).toBe(
|
||||
'https://matrix.example.org/#/@alice:example.com'
|
||||
);
|
||||
@@ -51,7 +51,7 @@ describe('getMatrixToRoom', () => {
|
||||
});
|
||||
|
||||
it('uses custom base when configured', () => {
|
||||
setMatrixToBase('https://matrix.example.org');
|
||||
setMatrixToBase('https://matrix.example.org/');
|
||||
expect(getMatrixToRoom('#general:example.com')).toBe(
|
||||
'https://matrix.example.org/#/#general:example.com'
|
||||
);
|
||||
@@ -72,7 +72,7 @@ describe('getMatrixToRoomEvent', () => {
|
||||
});
|
||||
|
||||
it('uses custom base when configured', () => {
|
||||
setMatrixToBase('https://matrix.example.org');
|
||||
setMatrixToBase('https://matrix.example.org/');
|
||||
expect(getMatrixToRoomEvent('!room:example.com', '$event123')).toBe(
|
||||
'https://matrix.example.org/#/!room:example.com/$event123'
|
||||
);
|
||||
@@ -97,12 +97,12 @@ describe('testMatrixTo', () => {
|
||||
});
|
||||
|
||||
it('matches custom base URLs after setMatrixToBase', () => {
|
||||
setMatrixToBase('https://matrix.example.org');
|
||||
setMatrixToBase('https://matrix.example.org/');
|
||||
expect(testMatrixTo('https://matrix.example.org/#/@alice:example.com')).toBe(true);
|
||||
});
|
||||
|
||||
it('still matches standard matrix.to after setMatrixToBase (cross-client compat)', () => {
|
||||
setMatrixToBase('https://matrix.example.org');
|
||||
setMatrixToBase('https://matrix.example.org/');
|
||||
expect(testMatrixTo('https://matrix.to/#/@alice:example.com')).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -121,14 +121,14 @@ describe('parseMatrixToUser', () => {
|
||||
});
|
||||
|
||||
it('parses user links from custom base', () => {
|
||||
setMatrixToBase('https://matrix.example.org');
|
||||
setMatrixToBase('https://matrix.example.org/');
|
||||
expect(parseMatrixToUser('https://matrix.example.org/#/@alice:example.com')).toBe(
|
||||
'@alice:example.com'
|
||||
);
|
||||
});
|
||||
|
||||
it('parses standard matrix.to user links even after custom base is set', () => {
|
||||
setMatrixToBase('https://matrix.example.org');
|
||||
setMatrixToBase('https://matrix.example.org/');
|
||||
expect(parseMatrixToUser('https://matrix.to/#/@alice:example.com')).toBe('@alice:example.com');
|
||||
});
|
||||
});
|
||||
@@ -166,7 +166,7 @@ describe('parseMatrixToRoom', () => {
|
||||
});
|
||||
|
||||
it('parses room links from custom base', () => {
|
||||
setMatrixToBase('https://matrix.example.org');
|
||||
setMatrixToBase('https://matrix.example.org/');
|
||||
expect(parseMatrixToRoom('https://matrix.example.org/#/!room:example.com')).toEqual({
|
||||
roomIdOrAlias: '!room:example.com',
|
||||
viaServers: undefined,
|
||||
@@ -174,7 +174,7 @@ describe('parseMatrixToRoom', () => {
|
||||
});
|
||||
|
||||
it('still parses standard matrix.to room links after custom base is set', () => {
|
||||
setMatrixToBase('https://matrix.example.org');
|
||||
setMatrixToBase('https://matrix.example.org/');
|
||||
expect(parseMatrixToRoom('https://matrix.to/#/!room:example.com')).toEqual({
|
||||
roomIdOrAlias: '!room:example.com',
|
||||
viaServers: undefined,
|
||||
@@ -210,7 +210,7 @@ describe('parseMatrixToRoomEvent', () => {
|
||||
});
|
||||
|
||||
it('parses event links from custom base', () => {
|
||||
setMatrixToBase('https://matrix.example.org');
|
||||
setMatrixToBase('https://matrix.example.org/');
|
||||
expect(
|
||||
parseMatrixToRoomEvent('https://matrix.example.org/#/!room:example.com/$event123')
|
||||
).toEqual({
|
||||
@@ -221,7 +221,7 @@ describe('parseMatrixToRoomEvent', () => {
|
||||
});
|
||||
|
||||
it('still parses standard matrix.to event links after custom base is set', () => {
|
||||
setMatrixToBase('https://matrix.example.org');
|
||||
setMatrixToBase('https://matrix.example.org/');
|
||||
expect(parseMatrixToRoomEvent('https://matrix.to/#/!room:example.com/$event123')).toEqual({
|
||||
roomIdOrAlias: '!room:example.com',
|
||||
eventId: '$event123',
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
let MATRIX_TO_BASE = 'https://matrix.to';
|
||||
export let MATRIX_TO_BASE = 'https://matrix.to/';
|
||||
|
||||
/**
|
||||
* Override the default matrix.to base URL (configurable per deployment).
|
||||
* Must be called before any getMatrixTo* functions are used.
|
||||
*/
|
||||
export const setMatrixToBase = (baseUrl?: string): void => {
|
||||
MATRIX_TO_BASE = baseUrl ? baseUrl.replace(/\/$/, '') : 'https://matrix.to';
|
||||
MATRIX_TO_BASE = baseUrl ? baseUrl.replace(/\/?$/, '/') : 'https://matrix.to/';
|
||||
};
|
||||
|
||||
export const getMatrixToUser = (userId: string): string => `${MATRIX_TO_BASE}/#/${userId}`;
|
||||
export const getMatrixToUser = (userId: string): string => `${MATRIX_TO_BASE}#/${userId}`;
|
||||
|
||||
const withViaServers = (fragment: string, viaServers: string[]): string =>
|
||||
`${fragment}?${viaServers.map((server) => `via=${server}`).join('&')}`;
|
||||
@@ -20,7 +20,7 @@ export const getMatrixToRoom = (roomIdOrAlias: string, viaServers?: string[]): s
|
||||
fragment = withViaServers(fragment, viaServers);
|
||||
}
|
||||
|
||||
return `${MATRIX_TO_BASE}/#/${fragment}`;
|
||||
return `${MATRIX_TO_BASE}#/${fragment}`;
|
||||
};
|
||||
|
||||
export const getMatrixToRoomEvent = (
|
||||
@@ -34,7 +34,7 @@ export const getMatrixToRoomEvent = (
|
||||
fragment = withViaServers(fragment, viaServers);
|
||||
}
|
||||
|
||||
return `${MATRIX_TO_BASE}/#/${fragment}`;
|
||||
return `${MATRIX_TO_BASE}#/${fragment}`;
|
||||
};
|
||||
|
||||
export type MatrixToRoom = {
|
||||
@@ -65,16 +65,16 @@ const getMatchRegexes = () => {
|
||||
if (cachedRegexBase === MATRIX_TO_BASE && cachedRegexes) return cachedRegexes;
|
||||
cachedRegexBase = MATRIX_TO_BASE;
|
||||
// Use https? so both http:// and https://matrix.to are accepted (original behaviour).
|
||||
const standard = `https?://${escapeForRegex('matrix.to')}`;
|
||||
const standard = `https?://${escapeForRegex('matrix.to/')}`;
|
||||
const b =
|
||||
MATRIX_TO_BASE !== 'https://matrix.to'
|
||||
MATRIX_TO_BASE !== 'https://matrix.to/'
|
||||
? `(?:${standard}|${escapeForRegex(MATRIX_TO_BASE)})`
|
||||
: standard;
|
||||
cachedRegexes = {
|
||||
any: new RegExp(`^${b}\\S*$`),
|
||||
user: new RegExp(`^${b}/#/(@[^:\\s]+:[^?/\\s]+)\\/?$`),
|
||||
room: new RegExp(`^${b}/#/([#!][^?/\\s]+)\\/?(\\?[\\S]*)?$`),
|
||||
event: new RegExp(`^${b}/#/([#!][^?/\\s]+)/(\\$[^?/\\s]+)\\/?([?\\S]*)?$`),
|
||||
user: new RegExp(`^${b}#/(@[^:\\s]+:[^?/\\s]+)\\/?$`),
|
||||
room: new RegExp(`^${b}#/([#!][^?/\\s]+)\\/?(\\?[\\S]*)?$`),
|
||||
event: new RegExp(`^${b}#/([#!][^?/\\s]+)/(\\$[^?/\\s]+)\\/?([?\\S]*)?$`),
|
||||
};
|
||||
return cachedRegexes;
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user