Compare commits
2
Commits
76e5f960d1
...
a4e96dd01f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a4e96dd01f | ||
|
|
f0495cccca |
@@ -362,6 +362,7 @@ type RoomTimelineProps = {
|
||||
eventId?: string;
|
||||
roomInputRef: RefObject<HTMLElement>;
|
||||
editor: Editor;
|
||||
onViewportAnchorChange?: (eventId?: string) => void;
|
||||
};
|
||||
|
||||
const PAGINATION_LIMIT = 80;
|
||||
@@ -564,7 +565,13 @@ const getRoomUnreadInfo = (room: Room, scrollTo = false) => {
|
||||
};
|
||||
};
|
||||
|
||||
export function RoomTimeline({ room, eventId, roomInputRef, editor }: RoomTimelineProps) {
|
||||
export function RoomTimeline({
|
||||
room,
|
||||
eventId,
|
||||
roomInputRef,
|
||||
editor,
|
||||
onViewportAnchorChange,
|
||||
}: RoomTimelineProps) {
|
||||
const mx = useMatrixClient();
|
||||
const useAuthentication = useMediaAuthentication();
|
||||
const [hideActivity] = useSetting(settingsAtom, 'hideActivity');
|
||||
@@ -1189,6 +1196,29 @@ export function RoomTimeline({ room, eventId, roomInputRef, editor }: RoomTimeli
|
||||
};
|
||||
}, [room.roomId, eventId, persistTimelineScroll]);
|
||||
|
||||
// Report the message the user is actually looking at, so the activity bar
|
||||
// reflects the shown message instead of the newest event in the room.
|
||||
useEffect(() => {
|
||||
if (!onViewportAnchorChange) return undefined;
|
||||
const scrollEl = scrollRef.current;
|
||||
if (!scrollEl) return undefined;
|
||||
|
||||
let raf = 0;
|
||||
const report = () => {
|
||||
window.cancelAnimationFrame(raf);
|
||||
raf = window.requestAnimationFrame(() => {
|
||||
onViewportAnchorChange(eventId ?? getViewportAnchorEventId() ?? getRangeCenterEventId());
|
||||
});
|
||||
};
|
||||
|
||||
report();
|
||||
scrollEl.addEventListener('scroll', report, { passive: true });
|
||||
return () => {
|
||||
window.cancelAnimationFrame(raf);
|
||||
scrollEl.removeEventListener('scroll', report);
|
||||
};
|
||||
}, [onViewportAnchorChange, eventId, getViewportAnchorEventId, getRangeCenterEventId]);
|
||||
|
||||
// Restore saved scroll or default to bottom on first open.
|
||||
useLayoutEffect(() => {
|
||||
const scrollEl = scrollRef.current;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import React, { useCallback, useRef } from 'react';
|
||||
import React, { useCallback, useRef, useState } from 'react';
|
||||
import { Box, Text, config } from 'folds';
|
||||
import { EventType, Room } from 'matrix-js-sdk';
|
||||
import { ReactEditor } from 'slate-react';
|
||||
@@ -65,6 +65,7 @@ export function RoomView({ room, eventId }: { room: Room; eventId?: string }) {
|
||||
const roomViewRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
const [hideActivity] = useSetting(settingsAtom, 'hideActivity');
|
||||
const [viewportEventId, setViewportEventId] = useState<string>();
|
||||
|
||||
const { roomId } = room;
|
||||
const editor = useEditor();
|
||||
@@ -117,10 +118,11 @@ export function RoomView({ room, eventId }: { room: Room; eventId?: string }) {
|
||||
eventId={eventId}
|
||||
roomInputRef={roomInputRef}
|
||||
editor={editor}
|
||||
onViewportAnchorChange={setViewportEventId}
|
||||
/>
|
||||
<div className={roomViewCss.RoomViewBottomFloat}>
|
||||
<RoomViewTyping room={room} />
|
||||
{!hideActivity && <RoomViewFollowing room={room} />}
|
||||
{!hideActivity && <RoomViewFollowing room={room} eventId={viewportEventId} />}
|
||||
</div>
|
||||
</Box>
|
||||
<Box shrink="No" direction="Column">
|
||||
|
||||
@@ -11,7 +11,6 @@ import * as css from './RoomViewFollowing.css';
|
||||
import { useMatrixClient } from '../../hooks/useMatrixClient';
|
||||
import { useRoomLatestRenderedEvent } from '../../hooks/useRoomLatestRenderedEvent';
|
||||
import { useRoomEventReaders } from '../../hooks/useRoomEventReaders';
|
||||
import { useRoomEventDelivery } from '../../hooks/useRoomEventDelivery';
|
||||
import { EventReaders } from '../../components/event-readers';
|
||||
import { stopPropagation } from '../../utils/keyboard';
|
||||
|
||||
@@ -21,27 +20,23 @@ export function RoomViewFollowingPlaceholder() {
|
||||
|
||||
export type RoomViewFollowingProps = {
|
||||
room: Room;
|
||||
/** Message the user is looking at. Falls back to the latest rendered event. */
|
||||
eventId?: string;
|
||||
};
|
||||
export const RoomViewFollowing = as<'div', RoomViewFollowingProps>(
|
||||
({ className, room, ...props }, ref) => {
|
||||
({ className, room, eventId, ...props }, ref) => {
|
||||
const mx = useMatrixClient();
|
||||
const myUserId = mx.getUserId();
|
||||
const [open, setOpen] = useState(false);
|
||||
const latestEvent = useRoomLatestRenderedEvent(room);
|
||||
const eventId = latestEvent?.getId();
|
||||
const latestEventReaders = useRoomEventReaders(room, eventId);
|
||||
const received = useRoomEventDelivery(room, eventId, myUserId);
|
||||
const shownEventId = eventId ?? latestEvent?.getId();
|
||||
const latestEventReaders = useRoomEventReaders(room, shownEventId);
|
||||
|
||||
// Received but not read shows a single tick, read shows the double tick.
|
||||
const receivedOnly = received && !latestEventReaders.includes(myUserId);
|
||||
|
||||
const getName = (userId: string) =>
|
||||
getMemberDisplayName(room, userId) ?? getMxIdLocalPart(userId) ?? userId;
|
||||
|
||||
const names = [
|
||||
...(receivedOnly && myUserId ? [myUserId] : []),
|
||||
...latestEventReaders.filter((readerId) => readerId !== myUserId),
|
||||
].map(getName);
|
||||
const names = latestEventReaders
|
||||
.filter((readerId) => readerId !== myUserId)
|
||||
.map(
|
||||
(readerId) => getMemberDisplayName(room, readerId) ?? getMxIdLocalPart(readerId) ?? readerId
|
||||
);
|
||||
|
||||
if (names.length === 0) {
|
||||
return null;
|
||||
@@ -49,7 +44,7 @@ export const RoomViewFollowing = as<'div', RoomViewFollowingProps>(
|
||||
|
||||
return (
|
||||
<>
|
||||
{eventId && (
|
||||
{shownEventId && (
|
||||
<Overlay open={open} backdrop={<OverlayBackdrop />}>
|
||||
<OverlayCenter>
|
||||
<FocusTrap
|
||||
@@ -61,7 +56,7 @@ export const RoomViewFollowing = as<'div', RoomViewFollowingProps>(
|
||||
}}
|
||||
>
|
||||
<Modal variant="Surface" size="300">
|
||||
<EventReaders room={room} eventId={eventId} requestClose={() => setOpen(false)} />
|
||||
<EventReaders room={room} eventId={shownEventId} requestClose={() => setOpen(false)} />
|
||||
</Modal>
|
||||
</FocusTrap>
|
||||
</OverlayCenter>
|
||||
@@ -77,11 +72,7 @@ export const RoomViewFollowing = as<'div', RoomViewFollowingProps>(
|
||||
{...props}
|
||||
ref={ref}
|
||||
>
|
||||
<Icon
|
||||
style={{ opacity: config.opacity.P300 }}
|
||||
size="100"
|
||||
src={receivedOnly ? Icons.Check : Icons.CheckTwice}
|
||||
/>
|
||||
<Icon style={{ opacity: config.opacity.P300 }} size="100" src={Icons.CheckTwice} />
|
||||
<Text size="T300" truncate>
|
||||
{names.length === 1 && <b>{names[0]}</b>}
|
||||
{names.length === 2 && (
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { MatrixEvent, Room, RoomEvent } from 'matrix-js-sdk';
|
||||
import { PAARROT_RECEIVED_EVENT, hasReceivedMarker } from '../utils/paarrotDelivery';
|
||||
|
||||
/**
|
||||
* Whether `userId` has recorded a delivery marker for `eventId` - received, but
|
||||
* not necessarily read. Read state still comes from the read receipt.
|
||||
*/
|
||||
export const useRoomEventDelivery = (room: Room, eventId?: string, userId?: string): boolean => {
|
||||
const [received, setReceived] = useState(() =>
|
||||
eventId && userId ? hasReceivedMarker(room, eventId, userId) : false,
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
setReceived(eventId && userId ? hasReceivedMarker(room, eventId, userId) : false);
|
||||
if (!eventId || !userId) return;
|
||||
|
||||
const update = () => setReceived(hasReceivedMarker(room, eventId, userId));
|
||||
|
||||
const handleTimeline = (event: MatrixEvent, r: Room) => {
|
||||
if (r.roomId !== room.roomId) return;
|
||||
if (event.getRelation()?.event_id !== eventId) return;
|
||||
if (event.getType() !== PAARROT_RECEIVED_EVENT && !event.isRedaction()) return;
|
||||
|
||||
update();
|
||||
};
|
||||
|
||||
room.on(RoomEvent.Timeline, handleTimeline);
|
||||
return () => {
|
||||
room.removeListener(RoomEvent.Timeline, handleTimeline);
|
||||
};
|
||||
}, [room, eventId, userId]);
|
||||
|
||||
return received;
|
||||
};
|
||||
@@ -22,7 +22,6 @@ import {
|
||||
getOrphanParents,
|
||||
guessPerfectParent,
|
||||
} from '../../utils/room';
|
||||
import { sendReceivedMarker } from '../../utils/paarrotDelivery';
|
||||
import { NotificationType, UnreadInfo } from '../../../types/matrix/room';
|
||||
import { getMxIdLocalPart, mxcUrlToHttp, getCanonicalAliasOrRoomId } from '../../utils/matrix';
|
||||
import { mDirectAtom } from '../../state/mDirectList';
|
||||
@@ -388,14 +387,6 @@ function MessageNotifications() {
|
||||
(notificationType === NotificationType.Default && !isDm);
|
||||
if (mentionsOnly && unreadInfo.highlight === 0) return;
|
||||
|
||||
// Record that Paarrot received this message. This is the single tick; the
|
||||
// read receipt below is what turns it into the double tick.
|
||||
if (showNotifications) {
|
||||
sendReceivedMarker(mx, room, eventId).catch((error) => {
|
||||
console.error('[PaarrotDelivery] Failed to send delivery marker:', error);
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
showNotifications &&
|
||||
((isTauri() && !isElectron()) || isCapacitorNative() || notificationPermission('granted'))
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
import { MatrixClient, RelationType, Room } from 'matrix-js-sdk';
|
||||
|
||||
/**
|
||||
* Client-defined event marking that Paarrot has received a message.
|
||||
*
|
||||
* The spec has no delivery receipt type - Synapse only accepts `m.read`,
|
||||
* `m.read.private` and `m.fully_read` on the receipts endpoint - so delivery is
|
||||
* recorded as a custom event related to the target message with `m.reference`.
|
||||
*
|
||||
* It never shows in the cinny timeline (events with a relation are skipped
|
||||
* unless "show hidden events" is on), and other clients only see an unknown
|
||||
* event. Delivery is always self-reported: in E2EE, only the receiving client
|
||||
* can know it actually got the event.
|
||||
*/
|
||||
export const PAARROT_RECEIVED_EVENT = 'com.paarrot.received';
|
||||
|
||||
export const getReceivedContent = (eventId: string) => ({
|
||||
'm.relates_to': {
|
||||
event_id: eventId,
|
||||
rel_type: RelationType.Reference,
|
||||
},
|
||||
});
|
||||
|
||||
export const getEventDeliveryRelations = (room: Room, eventId: string) =>
|
||||
room
|
||||
.getUnfilteredTimelineSet()
|
||||
.relations.getChildEventsForEvent(eventId, RelationType.Reference, PAARROT_RECEIVED_EVENT);
|
||||
|
||||
export const hasReceivedMarker = (room: Room, eventId: string, userId: string): boolean => {
|
||||
const relations = getEventDeliveryRelations(room, eventId);
|
||||
if (!relations) return false;
|
||||
|
||||
return relations
|
||||
.getRelations()
|
||||
.some(
|
||||
(rel) =>
|
||||
!rel.isRedacted() && rel.getSender() === userId && rel.getRelation()?.event_id === eventId,
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Sends the delivery marker for `eventId`. Skips events that are still local
|
||||
* echoes (no `$` id yet) and events this user already marked, so a room that
|
||||
* re-syncs does not spam markers.
|
||||
*/
|
||||
export async function sendReceivedMarker(mx: MatrixClient, room: Room, eventId: string) {
|
||||
const userId = mx.getUserId();
|
||||
if (!userId || !eventId.startsWith('$')) return false;
|
||||
if (hasReceivedMarker(room, eventId, userId)) return false;
|
||||
|
||||
await mx.sendEvent(room.roomId, PAARROT_RECEIVED_EVENT, getReceivedContent(eventId));
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user