82e52e1bc7
B1 of the Matrix protocol-gaps roadmap, gate-green (688 tests): - StateEvent.RoomRetention + a shared utils/retention.ts (presets, isExpired, getRoomRetentionMs) with tests. - RoomRetention settings control (PL-gated preset buttons Off/1d/1w/1m) in Room Settings → General → Message Retention. - Timeline hides events past the room's max_lifetime (gated behind Show Hidden Events, like redactions) — messages visually disappear, losslessly. - Opt-in setting enforceRetentionLocally (default OFF) + a headless RetentionSweeper that permanently redacts the user's OWN expired messages (own-only, loaded-timeline scope, dedupe + retry). Nothing auto-deletes unless the user opts in. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
81 lines
3.2 KiB
TypeScript
81 lines
3.2 KiB
TypeScript
import React, { useCallback } from 'react';
|
|
import { Box, Button, color, Spinner, Text } from 'folds';
|
|
import { MatrixError } from 'matrix-js-sdk';
|
|
import { SequenceCard } from '../../../components/sequence-card';
|
|
import { SequenceCardStyle } from '../../room-settings/styles.css';
|
|
import { SettingTile } from '../../../components/setting-tile';
|
|
import { useMatrixClient } from '../../../hooks/useMatrixClient';
|
|
import { useRoom } from '../../../hooks/useRoom';
|
|
import { StateEvent } from '../../../../types/matrix/room';
|
|
import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback';
|
|
import { useStateEvent } from '../../../hooks/useStateEvent';
|
|
import { RoomPermissionsAPI } from '../../../hooks/useRoomPermissions';
|
|
import { RetentionContent, RETENTION_PRESETS } from '../../../utils/retention';
|
|
|
|
type RoomRetentionProps = {
|
|
permissions: RoomPermissionsAPI;
|
|
};
|
|
export function RoomRetention({ permissions }: RoomRetentionProps) {
|
|
const mx = useMatrixClient();
|
|
const room = useRoom();
|
|
|
|
const canEdit = permissions.stateEvent(StateEvent.RoomRetention, mx.getSafeUserId());
|
|
|
|
const event = useStateEvent(room, StateEvent.RoomRetention);
|
|
const currentMs = event?.getContent<RetentionContent>().max_lifetime ?? 0;
|
|
|
|
const [submitState, submit] = useAsyncCallback(
|
|
useCallback(
|
|
async (ms: number) => {
|
|
const content: RetentionContent = ms > 0 ? { max_lifetime: ms } : {};
|
|
// Lotus custom-state convention: cast the type key (RoomRetention isn't a
|
|
// typed key in the SDK's StateEvents map).
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
await mx.sendStateEvent(room.roomId, StateEvent.RoomRetention as any, content);
|
|
},
|
|
[mx, room.roomId],
|
|
),
|
|
);
|
|
const submitting = submitState.status === AsyncStatus.Loading;
|
|
|
|
return (
|
|
<SequenceCard
|
|
className={SequenceCardStyle}
|
|
variant="SurfaceVariant"
|
|
direction="Column"
|
|
gap="400"
|
|
>
|
|
<SettingTile
|
|
title="Message Retention"
|
|
description="Messages older than this window disappear from the timeline. Each member can opt in to permanently delete their own expired messages in Settings → General; full server-side deletion also requires homeserver retention to be configured."
|
|
>
|
|
<Box gap="200" alignItems="Center" style={{ flexWrap: 'wrap' }}>
|
|
{RETENTION_PRESETS.map((preset) => {
|
|
const active = currentMs === preset.ms;
|
|
return (
|
|
<Button
|
|
key={preset.label}
|
|
type="button"
|
|
size="300"
|
|
variant={active ? 'Primary' : 'Secondary'}
|
|
fill={active ? 'Solid' : 'Soft'}
|
|
radii="300"
|
|
disabled={!canEdit || submitting}
|
|
onClick={() => submit(preset.ms)}
|
|
>
|
|
<Text size="B300">{preset.label}</Text>
|
|
</Button>
|
|
);
|
|
})}
|
|
{submitting && <Spinner size="100" variant="Secondary" />}
|
|
</Box>
|
|
{submitState.status === AsyncStatus.Error && (
|
|
<Text style={{ color: color.Critical.Main }} size="T200">
|
|
{(submitState.error as MatrixError).message}
|
|
</Text>
|
|
)}
|
|
</SettingTile>
|
|
</SequenceCard>
|
|
);
|
|
}
|