Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/cold-vans-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@rocket.chat/core-typings': patch
'@rocket.chat/meteor': patch
---

Added per-channel link preview setting
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type RoomSettings = {
retentionIgnoreThreads: boolean;
retentionOverrideGlobal: boolean;
encrypted: boolean;
linksEmbed: boolean;
favorite: {
favorite: boolean;
defaultValue: boolean;
Expand Down Expand Up @@ -265,6 +266,10 @@ const settingSavers: RoomSettingsSavers = {
await saveRoomTopic(rid, value, user);
}
},
async linksEmbed({ value, rid }) {
// This saves the value directly to the room document in MongoDB
await Rooms.updateOne({ _id: rid }, { $set: { linksEmbed: value } });
},
async roomAnnouncement({ value, room, rid, user }) {
if (!value && !room.announcement) {
return;
Expand Down Expand Up @@ -386,6 +391,8 @@ const fields: (keyof RoomSettings)[] = [
'retentionIgnoreThreads',
'retentionOverrideGlobal',
'encrypted',

'linksEmbed',
'favorite',
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ const EditRoomInfo = ({ room, onClickClose, onClickBack }: EditRoomInfoProps) =>
const retentionExcludePinnedField = useId();
const retentionFilesOnlyField = useId();
const retentionIgnoreThreads = useId();
const linksEmbedField = useId();

const showAdvancedSettings = canViewReadOnly || readOnly || canViewArchived || canViewJoinCode || canViewHideSysMes;
const showRetentionPolicy = canEditRoomRetentionPolicy && retentionPolicy?.enabled;
Expand Down Expand Up @@ -489,6 +490,26 @@ const EditRoomInfo = ({ room, onClickClose, onClickBack }: EditRoomInfoProps) =>
</FieldRow>
</Field>
)}

<Field>
<FieldRow>
<FieldLabel htmlFor={linksEmbedField}>{t('Enable_Link_Previews' as any)}</FieldLabel>
<Controller
control={control}
name='linksEmbed'
render={({ field: { value, ...field } }) => (
<ToggleSwitch
id={linksEmbedField}
{...field}
checked={value}
disabled={isFederated}
aria-describedby={`${linksEmbedField}-hint`}
/>
)}
/>
</FieldRow>
<FieldHint id={`${linksEmbedField}-hint`}>{t('Enable_Link_Previews_Description' as any)}</FieldHint>
</Field>
</FieldGroup>
</AccordionItem>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type EditRoomInfoFormData = {
showChannels: boolean;
showDiscussions: boolean;
joinCode: string;
linksEmbed: boolean;
systemMessages: MessageTypesValues[];
};

Expand All @@ -52,6 +53,7 @@ export const useEditRoomInitialValues = (room: IRoomWithRetentionPolicy): Partia
joinCodeRequired: !!joinCodeRequired,
systemMessages: Array.isArray(sysMes) ? sysMes : [],
hideSysMes: Array.isArray(sysMes) ? !!sysMes?.length : !!sysMes,
linksEmbed: room.linksEmbed !== false,
encrypted,
...(canEditRoomRetentionPolicy &&
retentionPolicy?.enabled && {
Expand Down
17 changes: 7 additions & 10 deletions apps/meteor/server/services/messages/hooks/AfterSaveOEmbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
} from '@rocket.chat/core-typings';
import { isOEmbedUrlWithMetadata } from '@rocket.chat/core-typings';
import { Logger } from '@rocket.chat/logger';
import { OEmbedCache, Messages } from '@rocket.chat/models';
import { OEmbedCache, Messages, Rooms } from '@rocket.chat/models';
import { serverFetch as fetch } from '@rocket.chat/server-fetch';
import he from 'he';
import iconv from 'iconv-lite';
Expand Down Expand Up @@ -327,44 +327,41 @@ const getRelevantMetaTags = function (metaObj: OEmbedMeta): Record<string, strin
const insertMaxWidthInOembedHtml = (oembedHtml?: string): string | undefined =>
oembedHtml?.replace('iframe', 'iframe style="max-width: 100%;width:400px;height:225px"');



const rocketUrlParser = async function (message: IMessage): Promise<IMessage> {
log.debug({ msg: 'Parsing message URLs' });

if (!settings.get('API_Embed')) {
return message;
}

if (!Array.isArray(message.urls)) {
return message;
}

const room = await Rooms.findOneById(message.rid, { projection: { linksEmbed: 1 } });
if (room?.linksEmbed === false) {
return message;
}
log.debug({ msg: 'URLs found in message', count: message.urls.length });

if (
(message.attachments && message.attachments.length > 0) ||
message.urls.filter((item) => !item.url.includes(settings.get('Site_Url'))).length > MAX_EXTERNAL_URL_PREVIEWS
) {
log.debug({ msg: 'All URLs ignored for OEmbed' });
return message;
}

let changed = false;
for await (const item of message.urls) {
if (item.ignoreParse === true) {
log.debug({ msg: 'URL ignored for OEmbed', url: item.url });
continue;
}

const { urlPreview, foundMeta } = await parseUrl(item.url);

Object.assign(item, foundMeta ? urlPreview : {});
changed = changed || foundMeta;
}

if (changed === true) {
await Messages.setUrlsById(message._id, message.urls);
}

return message;
};

Expand Down
8 changes: 8 additions & 0 deletions packages/core-typings/src/IRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import type { RoomType } from './RoomType';
import type { Branded } from './utils';

export interface IRoom extends IRocketChatRecord {
/**
* Enable or disable link previews for this room. If undefined, falls back to global setting.
*/
linksEmbed?: boolean;
t: RoomType;
name?: string;
fname?: string;
Expand Down Expand Up @@ -361,6 +365,10 @@ export type RoomAdminFieldsType =
| 'abacAttributes';

export interface IRoomWithRetentionPolicy extends IRoom {
/**
* Enable or disable link previews for this room. If undefined, falls back to global setting.
*/
linksEmbed?: boolean;
retention: {
enabled?: boolean;
maxAge: number;
Expand Down
1 change: 1 addition & 0 deletions packages/i18n/src/locales/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -2062,6 +2062,7 @@
"Enterprise_capabilities": "Enterprise capabilities",
"Enterprise_capability": "Enterprise capability",
"Entertainment": "Entertainment",
"Enable_Link_Previews": "Enable Link Previews",
"Error": "Error",
"Error_404": "Error:404",
"Error_RocketChat_requires_oplog_tailing_when_running_in_multiple_instances": "Error: Rocket.Chat requires oplog tailing when running in multiple instances",
Expand Down