From f63ec2e373c055ff1e98a9e2d46bb4d5face965b Mon Sep 17 00:00:00 2001 From: Jared Vititoe Date: Wed, 25 Nov 2026 12:43:05 -0500 Subject: [PATCH] feat(room): per-room "Show link previews" toggle (#493) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rooms that post nothing but links (e.g. World News RSS) fill the timeline with preview cards, and the only fix was turning previews off everywhere. Room settings → General → Display now has "Show link previews" for that room only, for this user only. It is saved as the standard org.matrix.room.preview_urls room account data ({ disable }), the same event Element reads, so it syncs across devices and works in Element. The room's choice wins over the global setting; "Use global setting" saves {} to go back. Room timeline and threads both follow it. Closes #493 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PPmy3tPq869XDW4njjVaKA --- .../room-settings/general/General.tsx | 5 ++ .../general/RoomLinkPreviews.tsx | 90 +++++++++++++++++++ src/app/features/room/RoomTimeline.tsx | 6 +- .../features/room/thread/ThreadTimeline.tsx | 6 +- src/app/hooks/useRoomUrlPreview.ts | 44 +++++++++ src/app/utils/roomUrlPreview.test.ts | 31 +++++++ src/app/utils/roomUrlPreview.ts | 36 ++++++++ src/types/matrix/accountData.ts | 3 + 8 files changed, 215 insertions(+), 6 deletions(-) create mode 100644 src/app/features/room-settings/general/RoomLinkPreviews.tsx create mode 100644 src/app/hooks/useRoomUrlPreview.ts create mode 100644 src/app/utils/roomUrlPreview.test.ts create mode 100644 src/app/utils/roomUrlPreview.ts diff --git a/src/app/features/room-settings/general/General.tsx b/src/app/features/room-settings/general/General.tsx index 140517172..b1e0b8177 100644 --- a/src/app/features/room-settings/general/General.tsx +++ b/src/app/features/room-settings/general/General.tsx @@ -20,6 +20,7 @@ import { } from '../../common-settings/general'; import { useRoomCreators } from '../../../hooks/useRoomCreators'; import { useRoomPermissions } from '../../../hooks/useRoomPermissions'; +import { RoomLinkPreviews } from './RoomLinkPreviews'; type GeneralProps = { requestClose: () => void; @@ -80,6 +81,10 @@ export function General({ requestClose }: GeneralProps) { Advanced Options + + Display + + Privacy diff --git a/src/app/features/room-settings/general/RoomLinkPreviews.tsx b/src/app/features/room-settings/general/RoomLinkPreviews.tsx new file mode 100644 index 000000000..80f96b9ef --- /dev/null +++ b/src/app/features/room-settings/general/RoomLinkPreviews.tsx @@ -0,0 +1,90 @@ +import React, { useCallback, useEffect, useState } from 'react'; +import { Box, Button, Switch, Text, color } from 'folds'; +import { SequenceCard } from '../../../components/sequence-card'; +import { SequenceCardStyle } from '../styles.css'; +import { SettingTile } from '../../../components/setting-tile'; +import { useMatrixClient } from '../../../hooks/useMatrixClient'; +import { useRoom } from '../../../hooks/useRoom'; +import { useRoomPreviewChoice, useRoomUrlPreview } from '../../../hooks/useRoomUrlPreview'; +import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback'; +import { AccountDataEvent } from '../../../../types/matrix/accountData'; +import { setRoomAccountData } from '../../../utils/accountData'; +import { RoomPreviewUrlsContent } from '../../../utils/roomUrlPreview'; + +/** + * [Gitea #493] "Show link previews" for this room only, for this user only. + * Saved as `org.matrix.room.preview_urls` room account data (synced across + * devices, also read by Element). "Use global setting" saves `{}`, which every + * Lotus device reads as "follow the global setting". + */ +export function RoomLinkPreviews() { + const mx = useMatrixClient(); + const room = useRoom(); + const roomChoice = useRoomPreviewChoice(room); + const shown = useRoomUrlPreview(room); + + const [submitState, submit] = useAsyncCallback( + useCallback( + (content: RoomPreviewUrlsContent) => + setRoomAccountData(mx, room.roomId, AccountDataEvent.RoomPreviewUrls, content), + [mx, room.roomId], + ), + ); + const saving = submitState.status === AsyncStatus.Loading; + + // Optimistic value until the write comes back through /sync. + const [pending, setPending] = useState(); + useEffect(() => { + if (submitState.status === AsyncStatus.Error) setPending(undefined); + }, [submitState.status]); + useEffect(() => { + if (pending !== undefined && shown === pending) setPending(undefined); + }, [pending, shown]); + + const handleToggle = (next: boolean) => { + setPending(next); + submit({ disable: !next }); + }; + + return ( + + + } + > + {roomChoice !== undefined && ( + + + + )} + {submitState.status === AsyncStatus.Error && ( + + Couldn't save:{' '} + {submitState.error instanceof Error ? submitState.error.message : 'unknown error'} + + )} + + + ); +} diff --git a/src/app/features/room/RoomTimeline.tsx b/src/app/features/room/RoomTimeline.tsx index f75277358..595990eb3 100644 --- a/src/app/features/room/RoomTimeline.tsx +++ b/src/app/features/room/RoomTimeline.tsx @@ -148,6 +148,7 @@ import { useAccessiblePowerTagColors, useGetMemberPowerTag } from '../../hooks/u import { useTheme } from '../../hooks/useTheme'; import { useRoomCreatorsTag } from '../../hooks/useRoomCreatorsTag'; import { usePowerLevelTags } from '../../hooks/usePowerLevelTags'; +import { useRoomUrlPreview } from '../../hooks/useRoomUrlPreview'; import { EditHistoryModal } from './message/EditHistoryModal'; const TimelineFloat = as<'div', css.TimelineFloatVariants>( @@ -490,9 +491,8 @@ export function RoomTimeline({ room, eventId, roomInputRef, editor }: RoomTimeli const [, setSeparatedTick] = useState(0); const [hideNickAvatarEvents] = useSetting(settingsAtom, 'hideNickAvatarEvents'); const [mediaAutoLoad] = useSetting(settingsAtom, 'mediaAutoLoad'); - const [urlPreview] = useSetting(settingsAtom, 'urlPreview'); - const [encUrlPreview] = useSetting(settingsAtom, 'encUrlPreview'); - const showUrlPreview = room.hasEncryptionStateEvent() ? encUrlPreview : urlPreview; + // [Gitea #493] The room's own link-preview choice, else the global setting. + const showUrlPreview = useRoomUrlPreview(room); const [showHiddenEvents] = useSetting(settingsAtom, 'showHiddenEvents'); // MSC1763 retention: messages older than this window are hidden from the // timeline (unless "show hidden events" is on). Reactive so a policy change diff --git a/src/app/features/room/thread/ThreadTimeline.tsx b/src/app/features/room/thread/ThreadTimeline.tsx index 14995cac4..0144e4002 100644 --- a/src/app/features/room/thread/ThreadTimeline.tsx +++ b/src/app/features/room/thread/ThreadTimeline.tsx @@ -108,6 +108,7 @@ import { import { useTheme } from '../../../hooks/useTheme'; import { useRoomCreatorsTag } from '../../../hooks/useRoomCreatorsTag'; import { usePowerLevelTags } from '../../../hooks/usePowerLevelTags'; +import { useRoomUrlPreview } from '../../../hooks/useRoomUrlPreview'; import { roomToParentsAtom } from '../../../state/room/roomToParents'; import { EditHistoryModal } from '../message/EditHistoryModal'; import { @@ -267,9 +268,8 @@ export function ThreadTimeline({ room, thread, editor }: ThreadTimelineProps) { const [showDeveloperTools] = useSetting(settingsAtom, 'developerTools'); const [lotusTerminal] = useSetting(settingsAtom, 'lotusTerminal'); const [mediaAutoLoad] = useSetting(settingsAtom, 'mediaAutoLoad'); - const [urlPreview] = useSetting(settingsAtom, 'urlPreview'); - const [encUrlPreview] = useSetting(settingsAtom, 'encUrlPreview'); - const showUrlPreview = room.hasEncryptionStateEvent() ? encUrlPreview : urlPreview; + // [Gitea #493] The room's own link-preview choice, else the global setting. + const showUrlPreview = useRoomUrlPreview(room); const [hour24Clock] = useSetting(settingsAtom, 'hour24Clock'); const [hapticFeedback] = useSetting(settingsAtom, 'hapticFeedback'); const { navigateRoom } = useRoomNavigate(); diff --git a/src/app/hooks/useRoomUrlPreview.ts b/src/app/hooks/useRoomUrlPreview.ts new file mode 100644 index 000000000..09b011228 --- /dev/null +++ b/src/app/hooks/useRoomUrlPreview.ts @@ -0,0 +1,44 @@ +import { useCallback, useSyncExternalStore } from 'react'; +import { MatrixEvent, Room, RoomEvent } from 'matrix-js-sdk'; +import { AccountDataEvent } from '../../types/matrix/accountData'; +import { useSetting } from '../state/hooks/settings'; +import { settingsAtom } from '../state/settings'; +import { resolveUrlPreview, roomPreviewChoice } from '../utils/roomUrlPreview'; + +/** + * [Gitea #493] This user's saved link-preview choice for `room` (true = show, + * false = hide, undefined = follow the global setting). Live: updates when the + * account data changes on any device. + */ +export function useRoomPreviewChoice(room: Room): boolean | undefined { + const subscribe = useCallback( + (onChange: () => void) => { + const handler = (event: MatrixEvent) => { + if (event.getType() === AccountDataEvent.RoomPreviewUrls) onChange(); + }; + room.on(RoomEvent.AccountData, handler); + return () => { + room.removeListener(RoomEvent.AccountData, handler); + }; + }, + [room], + ); + const get = useCallback( + () => roomPreviewChoice(room.getAccountData(AccountDataEvent.RoomPreviewUrls)?.getContent()), + [room], + ); + return useSyncExternalStore(subscribe, get); +} + +/** Whether link previews show in `room`: its own choice, else the global setting. */ +export function useRoomUrlPreview(room: Room): boolean { + const [urlPreview] = useSetting(settingsAtom, 'urlPreview'); + const [encUrlPreview] = useSetting(settingsAtom, 'encUrlPreview'); + const roomChoice = useRoomPreviewChoice(room); + return resolveUrlPreview({ + encrypted: room.hasEncryptionStateEvent(), + urlPreview, + encUrlPreview, + roomChoice, + }); +} diff --git a/src/app/utils/roomUrlPreview.test.ts b/src/app/utils/roomUrlPreview.test.ts new file mode 100644 index 000000000..2d6e12c8d --- /dev/null +++ b/src/app/utils/roomUrlPreview.test.ts @@ -0,0 +1,31 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { resolveUrlPreview, roomPreviewChoice } from './roomUrlPreview'; + +test('room choice reads the disable flag', () => { + assert.equal(roomPreviewChoice({ disable: true }), false); + assert.equal(roomPreviewChoice({ disable: false }), true); +}); + +test('missing, empty or malformed content means no room choice', () => { + assert.equal(roomPreviewChoice(undefined), undefined); + assert.equal(roomPreviewChoice(null), undefined); + assert.equal(roomPreviewChoice({}), undefined); + assert.equal(roomPreviewChoice({ disable: 'yes' }), undefined); + assert.equal(roomPreviewChoice('disable'), undefined); +}); + +test('without a room choice the global setting applies', () => { + const base = { urlPreview: true, encUrlPreview: false, roomChoice: undefined }; + assert.equal(resolveUrlPreview({ ...base, encrypted: false }), true); + assert.equal(resolveUrlPreview({ ...base, encrypted: true }), false); +}); + +test('a room choice wins over the global setting both ways', () => { + const on = { urlPreview: true, encUrlPreview: true }; + const off = { urlPreview: false, encUrlPreview: false }; + assert.equal(resolveUrlPreview({ ...on, encrypted: false, roomChoice: false }), false); + assert.equal(resolveUrlPreview({ ...on, encrypted: true, roomChoice: false }), false); + assert.equal(resolveUrlPreview({ ...off, encrypted: false, roomChoice: true }), true); + assert.equal(resolveUrlPreview({ ...off, encrypted: true, roomChoice: true }), true); +}); diff --git a/src/app/utils/roomUrlPreview.ts b/src/app/utils/roomUrlPreview.ts new file mode 100644 index 000000000..e942e66fe --- /dev/null +++ b/src/app/utils/roomUrlPreview.ts @@ -0,0 +1,36 @@ +/** + * [Gitea #493] Per-room link previews. A user can turn link preview cards on + * or off for one room with the standard `org.matrix.room.preview_urls` room + * account data (`{ disable: boolean }`), the same event Element reads. When a + * room has no choice saved, the global setting applies as before. + */ + +export type RoomPreviewUrlsContent = { + disable?: boolean; +}; + +/** + * The room's own choice: true = show previews, false = hide them, undefined = + * no choice saved (missing event, `{}`, or a non-boolean `disable`). + */ +export function roomPreviewChoice(content: unknown): boolean | undefined { + if (!content || typeof content !== 'object') return undefined; + const { disable } = content as RoomPreviewUrlsContent; + return typeof disable === 'boolean' ? !disable : undefined; +} + +/** Whether to render link previews in a room. */ +export function resolveUrlPreview({ + encrypted, + urlPreview, + encUrlPreview, + roomChoice, +}: { + encrypted: boolean; + urlPreview: boolean; + encUrlPreview: boolean; + roomChoice: boolean | undefined; +}): boolean { + if (roomChoice !== undefined) return roomChoice; + return encrypted ? encUrlPreview : urlPreview; +} diff --git a/src/types/matrix/accountData.ts b/src/types/matrix/accountData.ts index 72f1d0854..5521222d6 100644 --- a/src/types/matrix/accountData.ts +++ b/src/types/matrix/accountData.ts @@ -4,6 +4,9 @@ export enum AccountDataEvent { IgnoredUserList = 'm.ignored_user_list', // [MSC2867] Per-room "mark as unread" flag (room account data). MarkedUnread = 'm.marked_unread', + // [Gitea #493] Per-room link-preview choice (room account data, { disable }). + // Same type Element reads, so the choice works in both clients. + RoomPreviewUrls = 'org.matrix.room.preview_urls', CinnySpaces = 'in.cinny.spaces',