diff --git a/src/app/features/common-settings/cosmetics/Cosmetics.tsx b/src/app/features/common-settings/cosmetics/Cosmetics.tsx index f3a827b26..463468548 100644 --- a/src/app/features/common-settings/cosmetics/Cosmetics.tsx +++ b/src/app/features/common-settings/cosmetics/Cosmetics.tsx @@ -156,34 +156,44 @@ export function Cosmetics({ requestClose }: CosmeticsProps) { variant="SurfaceVariant" direction="Column" gap="400" - style={{ padding: '16px' }} > - - - /color [hex] - - Set room-specific name color. (e.g. /color #ff00ff) - - - - /font [name] - - Set room-specific name font. (e.g. /font monospace) - - - - /pronoun [pronouns] - - Set room-specific pronoun set. (e.g. /pronoun "they\them, it\its") - - - - /gcolor | /gfont | /gpronoun - - Apply colors/fonts/pronouns to the entire space. - - - + + + + + + + + + + diff --git a/src/app/features/room/RoomTimeline.tsx b/src/app/features/room/RoomTimeline.tsx index 2157caef3..15c77115d 100644 --- a/src/app/features/room/RoomTimeline.tsx +++ b/src/app/features/room/RoomTimeline.tsx @@ -75,6 +75,8 @@ import { renderMatrixMention, } from '$plugins/react-custom-html-parser'; import { + roomHaveNotification, + roomHaveUnread, canEditEvent, decryptAllTimelineEvent, getEditedEvent, @@ -259,6 +261,7 @@ type RoomTimelineProps = { }; const PAGINATION_LIMIT = 60; +const EVENT_TIMELINE_LOAD_TIMEOUT_MS = 12000; type Timeline = { linkedTimelines: EventTimeline[]; @@ -273,12 +276,38 @@ const useEventTimelineLoader = ( ) => useCallback( async (eventId: string) => { + const withTimeout = async (promise: Promise, timeoutMs: number): Promise => + new Promise((resolve, reject) => { + const timeoutId = window.setTimeout(() => { + reject(new Error('Timed out loading event timeline')); + }, timeoutMs); + + promise + .then((value) => { + window.clearTimeout(timeoutId); + resolve(value); + }) + .catch((error) => { + window.clearTimeout(timeoutId); + reject(error); + }); + }); + if (!room.getUnfilteredTimelineSet().getTimelineForEvent(eventId)) { - await mx.roomInitialSync(room.roomId, PAGINATION_LIMIT); - await mx.getLatestTimeline(room.getUnfilteredTimelineSet()); + await withTimeout( + mx.roomInitialSync(room.roomId, PAGINATION_LIMIT), + EVENT_TIMELINE_LOAD_TIMEOUT_MS + ); + await withTimeout( + mx.getLatestTimeline(room.getUnfilteredTimelineSet()), + EVENT_TIMELINE_LOAD_TIMEOUT_MS + ); } const [err, replyEvtTimeline] = await to( - mx.getEventTimeline(room.getUnfilteredTimelineSet(), eventId) + withTimeout( + mx.getEventTimeline(room.getUnfilteredTimelineSet(), eventId), + EVENT_TIMELINE_LOAD_TIMEOUT_MS + ) ); if (!replyEvtTimeline) { onError(err ?? null); @@ -359,31 +388,34 @@ const useTimelinePagination = ( } fetching = true; - const [err] = await to( - mx.paginateEventTimeline(timelineToPaginate, { - backwards, - limit, - }) - ); - if (err) { - // TODO: handle pagination error. - return; - } - const fetchedTimeline = - timelineToPaginate.getNeighbouringTimeline( - backwards ? Direction.Backward : Direction.Forward - ) ?? timelineToPaginate; - // Decrypt all event ahead of render cycle - const roomId = fetchedTimeline.getRoomId(); - const room = roomId ? mx.getRoom(roomId) : null; + try { + const [err] = await to( + mx.paginateEventTimeline(timelineToPaginate, { + backwards, + limit, + }) + ); + if (err) { + // TODO: handle pagination error. + return; + } + const fetchedTimeline = + timelineToPaginate.getNeighbouringTimeline( + backwards ? Direction.Backward : Direction.Forward + ) ?? timelineToPaginate; + // Decrypt all event ahead of render cycle + const roomId = fetchedTimeline.getRoomId(); + const room = roomId ? mx.getRoom(roomId) : null; - if (room?.hasEncryptionStateEvent()) { - await to(decryptAllTimelineEvent(mx, fetchedTimeline)); - } + if (room?.hasEncryptionStateEvent()) { + await to(decryptAllTimelineEvent(mx, fetchedTimeline)); + } - fetching = false; - if (alive()) { - recalibratePagination(lTimelines, timelinesEventsCount, backwards); + if (alive()) { + recalibratePagination(lTimelines, timelinesEventsCount, backwards); + } + } finally { + fetching = false; } }; }, [mx, alive, setTimeline, limit]); @@ -450,6 +482,8 @@ const getEmptyTimeline = () => ({ }); const getRoomUnreadInfo = (room: Room, scrollTo = false) => { + if (!roomHaveNotification(room) && !roomHaveUnread(room.client, room)) return undefined; + const readUptoEventId = room.getEventReadUpTo(room.client.getUserId() ?? ''); if (!readUptoEventId) return undefined; const evtTimeline = getEventTimeline(room, readUptoEventId); @@ -718,7 +752,6 @@ export function RoomTimeline({ highlight, }); } else { - setTimeline(getEmptyTimeline()); loadEventTimeline(evtId); } }, @@ -815,20 +848,10 @@ export function RoomTimeline({ useCallback( (inFocus) => { if (inFocus && atBottomRef.current) { - if (unreadInfo?.inLiveTimeline) { - handleOpenEvent(unreadInfo.readUptoEventId, false, (scrolled) => { - // the unread event is already in view - // so, try mark as read; - if (!scrolled) { - tryAutoMarkAsRead(); - } - }); - return; - } tryAutoMarkAsRead(); } }, - [tryAutoMarkAsRead, unreadInfo, handleOpenEvent] + [tryAutoMarkAsRead] ) ); @@ -1000,7 +1023,6 @@ export function RoomTimeline({ const handleJumpToUnread = () => { if (unreadInfo?.readUptoEventId) { - setTimeline(getEmptyTimeline()); loadEventTimeline(unreadInfo.readUptoEventId); } }; diff --git a/src/app/features/settings/about/About.tsx b/src/app/features/settings/about/About.tsx index 1e83a947f..15c4b65a5 100644 --- a/src/app/features/settings/about/About.tsx +++ b/src/app/features/settings/about/About.tsx @@ -67,7 +67,7 @@ export function About({ requestClose }: AboutProps) { } - /> + > + {/* FIXME: these two things below, even before my changes, don't really seem to ever appear? */} + {deregisterState.status === AsyncStatus.Error && ( + +
+ Failed to deregister devices. Please try again. +
+ )} + {deregisterState.status === AsyncStatus.Success && ( + +
+ Successfully deregistered all devices. +
+ )} +
); } diff --git a/src/app/features/settings/notifications/SystemNotification.tsx b/src/app/features/settings/notifications/SystemNotification.tsx index 67b40c2a1..04e31db79 100644 --- a/src/app/features/settings/notifications/SystemNotification.tsx +++ b/src/app/features/settings/notifications/SystemNotification.tsx @@ -169,6 +169,14 @@ export function SystemNotification() { settingsAtom, 'isNotificationSounds' ); + const [showMessageContent, setShowMessageContent] = useSetting( + settingsAtom, + 'showMessageContentInNotifications' + ); + const [showEncryptedMessageContent, setShowEncryptedMessageContent] = useSetting( + settingsAtom, + 'showMessageContentInEncryptedNotifications' + ); return ( @@ -193,6 +201,36 @@ export function SystemNotification() { after={} /> + + } + /> + + + + } + /> + { const payload = buildRoomMessageNotification({ roomName, roomAvatar, username, - previewText: 'new message', + previewText, silent: true, eventId, }); @@ -267,6 +278,7 @@ function MessageNotifications() { } if (showNotifications && notificationPermission('granted')) { + const isEncryptedRoom = !!getStateEvent(room, StateEvent.RoomEncryption); const avatarMxc = room.getAvatarFallbackMember()?.getMxcAvatarUrl() ?? room.getMxcAvatarUrl(); notify({ @@ -280,6 +292,13 @@ function MessageNotifications() { sender, roomId: room.roomId, eventId, + previewText: resolveNotificationPreviewText({ + content: mEvent.getContent(), + eventType: mEvent.getType(), + isEncryptedRoom, + showMessageContent, + showEncryptedMessageContent, + }), }); } @@ -296,6 +315,8 @@ function MessageNotifications() { notificationSound, notificationSelected, showNotifications, + showMessageContent, + showEncryptedMessageContent, usePushNotifications, forcePushOnMobile, playSound, @@ -386,6 +407,11 @@ function HandleNotificationClick() { function SyncNotificationSettingsWithServiceWorker() { const [notificationSound] = useSetting(settingsAtom, 'isNotificationSounds'); const [usePushNotifications] = useSetting(settingsAtom, 'usePushNotifications'); + const [showMessageContent] = useSetting(settingsAtom, 'showMessageContentInNotifications'); + const [showEncryptedMessageContent] = useSetting( + settingsAtom, + 'showMessageContentInEncryptedNotifications' + ); useEffect(() => { if (!('serviceWorker' in navigator)) return; @@ -394,13 +420,15 @@ function SyncNotificationSettingsWithServiceWorker() { type: 'setNotificationSettings' as const, notificationSoundEnabled: notificationSound, preferPushOnMobile, + showMessageContent, + showEncryptedMessageContent, }; navigator.serviceWorker.controller?.postMessage(payload); navigator.serviceWorker.ready.then((registration) => { registration.active?.postMessage(payload); }); - }, [notificationSound, usePushNotifications]); + }, [notificationSound, usePushNotifications, showMessageContent, showEncryptedMessageContent]); return null; } diff --git a/src/app/state/settings.ts b/src/app/state/settings.ts index fe104bc5b..a68f8dfc5 100644 --- a/src/app/state/settings.ts +++ b/src/app/state/settings.ts @@ -46,6 +46,8 @@ export interface Settings { usePushNotifications: boolean; useInAppNotifications: boolean; isNotificationSounds: boolean; + showMessageContentInNotifications: boolean; + showMessageContentInEncryptedNotifications: boolean; hour24Clock: boolean; dateFormatString: string; @@ -97,6 +99,8 @@ const defaultSettings: Settings = { usePushNotifications: false, useInAppNotifications: true, isNotificationSounds: true, + showMessageContentInNotifications: false, + showMessageContentInEncryptedNotifications: false, hour24Clock: false, dateFormatString: 'D MMM YYYY', diff --git a/src/app/utils/notificationStyle.ts b/src/app/utils/notificationStyle.ts index a3e5a1938..0bfd85ad4 100644 --- a/src/app/utils/notificationStyle.ts +++ b/src/app/utils/notificationStyle.ts @@ -1,5 +1,7 @@ export const DEFAULT_NOTIFICATION_ICON = '/public/res/apple/apple-touch-icon-180x180.png'; export const DEFAULT_NOTIFICATION_BADGE = '/public/res/apple/apple-touch-icon-72x72.png'; +export const DEFAULT_MESSAGE_PREVIEW = 'new message'; +export const ENCRYPTED_MESSAGE_PREVIEW = 'Encrypted message'; type RoomMessageNotificationInput = { roomName?: string; @@ -16,12 +18,50 @@ type NotificationPayload = { options: NotificationOptions; }; +type NotificationPreviewInput = { + content?: unknown; + eventType?: string; + isEncryptedRoom?: boolean; + showMessageContent: boolean; + showEncryptedMessageContent: boolean; +}; + const getString = (value: unknown, fallback: string): string => { if (typeof value !== 'string') return fallback; const normalized = value.trim(); return normalized.length > 0 ? normalized : fallback; }; +const getBodyFromContent = (content: unknown): string | undefined => { + if (!content || typeof content !== 'object') return undefined; + const { body } = content as Record; + if (typeof body !== 'string') return undefined; + const normalized = body.trim(); + return normalized.length > 0 ? normalized : undefined; +}; + +export const resolveNotificationPreviewText = ({ + content, + eventType, + isEncryptedRoom, + showMessageContent, + showEncryptedMessageContent, +}: NotificationPreviewInput): string => { + const encryptedContext = isEncryptedRoom || eventType === 'm.room.encrypted'; + + if (!showMessageContent) { + return encryptedContext ? ENCRYPTED_MESSAGE_PREVIEW : DEFAULT_MESSAGE_PREVIEW; + } + if (encryptedContext && !showEncryptedMessageContent) { + return ENCRYPTED_MESSAGE_PREVIEW; + } + + const body = getBodyFromContent(content); + if (body) return body; + + return encryptedContext ? ENCRYPTED_MESSAGE_PREVIEW : DEFAULT_MESSAGE_PREVIEW; +}; + export const buildRoomMessageNotification = ({ roomName, username, @@ -33,7 +73,7 @@ export const buildRoomMessageNotification = ({ }: RoomMessageNotificationInput): NotificationPayload => { const sender = getString(username, 'Someone'); const room = getString(roomName, 'Unknown'); - const message = getString(previewText, 'new message'); + const message = getString(previewText, DEFAULT_MESSAGE_PREVIEW); const avatar = getString(roomAvatar, DEFAULT_NOTIFICATION_ICON); return { diff --git a/src/sw.ts b/src/sw.ts index 5fd27a359..51c5124ad 100644 --- a/src/sw.ts +++ b/src/sw.ts @@ -9,8 +9,12 @@ declare const self: ServiceWorkerGlobalScope; let notificationSoundEnabled = true; let preferPushOnMobile = false; +let showMessageContent = false; +let showEncryptedMessageContent = false; const { handlePushNotificationPushData } = createPushNotifications(self, () => ({ notificationSoundEnabled, + showMessageContent, + showEncryptedMessageContent, })); type SessionInfo = { @@ -124,6 +128,16 @@ self.addEventListener('message', (event: ExtendableMessageEvent) => { if (typeof (data as { preferPushOnMobile?: unknown }).preferPushOnMobile === 'boolean') { preferPushOnMobile = (data as { preferPushOnMobile: boolean }).preferPushOnMobile; } + if (typeof (data as { showMessageContent?: unknown }).showMessageContent === 'boolean') { + showMessageContent = (data as { showMessageContent: boolean }).showMessageContent; + } + if ( + typeof (data as { showEncryptedMessageContent?: unknown }).showEncryptedMessageContent === + 'boolean' + ) { + showEncryptedMessageContent = (data as { showEncryptedMessageContent: boolean }) + .showEncryptedMessageContent; + } } }); diff --git a/src/sw/pushNotification.ts b/src/sw/pushNotification.ts index 19f95226a..1459df547 100644 --- a/src/sw/pushNotification.ts +++ b/src/sw/pushNotification.ts @@ -3,10 +3,13 @@ import { buildRoomMessageNotification, DEFAULT_NOTIFICATION_ICON, DEFAULT_NOTIFICATION_BADGE, + resolveNotificationPreviewText, } from '../app/utils/notificationStyle'; type NotificationSettings = { notificationSoundEnabled: boolean; + showMessageContent: boolean; + showEncryptedMessageContent: boolean; }; export const createPushNotifications = ( @@ -49,7 +52,13 @@ export const createPushNotifications = ( roomName: pushData?.room_name, username: pushData?.sender_display_name, roomAvatar: pushData?.room_avatar_url, - previewText: 'new message', + previewText: resolveNotificationPreviewText({ + content: pushData?.content, + eventType: pushData?.type, + isEncryptedRoom: false, + showMessageContent: getNotificationSettings().showMessageContent, + showEncryptedMessageContent: getNotificationSettings().showEncryptedMessageContent, + }), silent: resolveSilent(pushData?.silent), eventId: pushData?.event_id, data, @@ -77,7 +86,13 @@ export const createPushNotifications = ( roomName: pushData?.room_name, username: pushData?.sender_display_name, roomAvatar: pushData?.room_avatar_url, - previewText: 'Encrypted message', + previewText: resolveNotificationPreviewText({ + content: pushData?.content, + eventType: pushData?.type, + isEncryptedRoom: true, + showMessageContent: getNotificationSettings().showMessageContent, + showEncryptedMessageContent: getNotificationSettings().showEncryptedMessageContent, + }), silent: resolveSilent(pushData?.silent), eventId: pushData?.event_id, data,