Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f63ec2e373 |
No files matched your search
@@ -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) {
|
||||
<Text size="L400">Advanced Options</Text>
|
||||
<RoomUpgrade permissions={permissions} requestClose={requestClose} />
|
||||
</Box>
|
||||
<Box direction="Column" gap="100">
|
||||
<Text size="L400">Display</Text>
|
||||
<RoomLinkPreviews />
|
||||
</Box>
|
||||
<Box direction="Column" gap="100">
|
||||
<Text size="L400">Privacy</Text>
|
||||
<DeleteMyMessages />
|
||||
|
||||
@@ -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<boolean>();
|
||||
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 (
|
||||
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
|
||||
<SettingTile
|
||||
title="Show link previews"
|
||||
description={
|
||||
roomChoice === undefined
|
||||
? 'Only affects you. Following your global setting (Settings → General).'
|
||||
: 'Only affects you. Set for this room; overrides your global setting.'
|
||||
}
|
||||
after={
|
||||
<Switch
|
||||
variant="Primary"
|
||||
value={pending ?? shown}
|
||||
onChange={handleToggle}
|
||||
disabled={saving}
|
||||
/>
|
||||
}
|
||||
>
|
||||
{roomChoice !== undefined && (
|
||||
<Box>
|
||||
<Button
|
||||
size="300"
|
||||
variant="Secondary"
|
||||
fill="Soft"
|
||||
radii="300"
|
||||
disabled={saving}
|
||||
onClick={() => submit({})}
|
||||
>
|
||||
<Text size="B300">Use global setting</Text>
|
||||
</Button>
|
||||
</Box>
|
||||
)}
|
||||
{submitState.status === AsyncStatus.Error && (
|
||||
<Text style={{ color: color.Critical.Main }} size="T200">
|
||||
Couldn't save:{' '}
|
||||
{submitState.error instanceof Error ? submitState.error.message : 'unknown error'}
|
||||
</Text>
|
||||
)}
|
||||
</SettingTile>
|
||||
</SequenceCard>
|
||||
);
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
}
|
||||
@@ -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);
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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',
|
||||
|
||||
|
||||
Reference in new issue
Block a user