fix unreads

seems to require a timeline item sadly
This commit is contained in:
7w1 committed 2026-07-15 17:41:29 -05:00
1 parent 071b29bff7
commit b68bc73e80
4 files changed
+105 -30

No files matched your search

+48 -24
View File
@@ -21,7 +21,7 @@ import {
} from '$utils/room';
import { useSyncState } from '$hooks/useSyncState';
import { useRoomsNotificationPreferencesContext } from '$hooks/useRoomsNotificationPreferences';
import { getClientSyncDiagnostics } from '$client/initMatrix';
import { getClientSyncDiagnostics, getSlidingSyncManager } from '$client/initMatrix';
import { mDirectAtom } from '$state/mDirectList';
import { roomToParentsAtom } from './roomToParents';
@@ -184,16 +184,40 @@ export const useBindRoomToUnreadAtom = (mx: MatrixClient, unreadAtom: typeof roo
() => getClientSyncDiagnostics(mx).transport === 'sliding',
[mx]
);
const publishUnreadAction = useCallback(
(action: RoomToUnreadAction) => {
if (getClientSyncDiagnostics(mx).transport === 'sliding') {
const manager = getSlidingSyncManager(mx);
if (manager?.isResponseProcessing()) return;
}
setUnreadAtom(action);
},
[mx, setUnreadAtom]
);
useEffect(() => {
setUnreadAtom({
const manager = getSlidingSyncManager(mx);
if (!manager) return undefined;
return manager.subscribeToResponseSettled(() => {
setUnreadAtom({
type: 'RESET',
unreadInfos: getUnreadInfos(mx, {
applyFixup: true,
mDirects,
}),
});
});
}, [mx, setUnreadAtom, mDirects]);
useEffect(() => {
publishUnreadAction({
type: 'RESET',
unreadInfos: getUnreadInfos(mx, {
applyFixup: shouldApplyUnreadFixup(),
mDirects,
}),
});
}, [mx, setUnreadAtom, shouldApplyUnreadFixup, mDirects, roomToParents]);
}, [mx, publishUnreadAction, shouldApplyUnreadFixup, mDirects, roomToParents]);
useSyncState(
mx,
@@ -203,7 +227,7 @@ export const useBindRoomToUnreadAtom = (mx: MatrixClient, unreadAtom: typeof roo
(state === SyncState.Prepared && prevState === null) ||
(state === SyncState.Syncing && prevState !== SyncState.Syncing)
) {
setUnreadAtom({
publishUnreadAction({
type: 'RESET',
unreadInfos: getUnreadInfos(mx, {
applyFixup: shouldApplyUnreadFixup(),
@@ -212,7 +236,7 @@ export const useBindRoomToUnreadAtom = (mx: MatrixClient, unreadAtom: typeof roo
});
}
},
[mx, setUnreadAtom, shouldApplyUnreadFixup, mDirects]
[mx, publishUnreadAction, shouldApplyUnreadFixup, mDirects]
)
);
@@ -226,7 +250,7 @@ export const useBindRoomToUnreadAtom = (mx: MatrixClient, unreadAtom: typeof roo
) => {
if (!room || room.isSpaceRoom()) return;
if (getNotificationType(mx, room.roomId) === NotificationType.Mute) {
setUnreadAtom({
publishUnreadAction({
type: 'DELETE',
roomId: room.roomId,
});
@@ -240,7 +264,7 @@ export const useBindRoomToUnreadAtom = (mx: MatrixClient, unreadAtom: typeof roo
applyFixup: shouldApplyUnreadFixup(),
mDirects,
});
setUnreadAtom({
publishUnreadAction({
type: 'PUT',
unreadInfo,
});
@@ -257,7 +281,7 @@ export const useBindRoomToUnreadAtom = (mx: MatrixClient, unreadAtom: typeof roo
mDirects,
});
if (unreadInfo.total > 0 || unreadInfo.highlight > 0) {
setUnreadAtom({
publishUnreadAction({
type: 'PUT',
unreadInfo,
});
@@ -268,7 +292,7 @@ export const useBindRoomToUnreadAtom = (mx: MatrixClient, unreadAtom: typeof roo
return () => {
mx.removeListener(RoomEvent.Timeline, handleTimelineEvent);
};
}, [mx, setUnreadAtom, shouldApplyUnreadFixup, mDirects]);
}, [mx, publishUnreadAction, shouldApplyUnreadFixup, mDirects]);
useEffect(() => {
const handleReceipt = (mEvent: MatrixEvent, room: Room) => {
@@ -288,17 +312,17 @@ export const useBindRoomToUnreadAtom = (mx: MatrixClient, unreadAtom: typeof roo
mDirects,
});
if (unreadInfo.total === 0 && unreadInfo.highlight === 0) {
setUnreadAtom({ type: 'DELETE', roomId: room.roomId });
publishUnreadAction({ type: 'DELETE', roomId: room.roomId });
return;
}
setUnreadAtom({ type: 'PUT', unreadInfo });
publishUnreadAction({ type: 'PUT', unreadInfo });
}
};
mx.on(RoomEvent.Receipt, handleReceipt);
return () => {
mx.removeListener(RoomEvent.Receipt, handleReceipt);
};
}, [mx, setUnreadAtom, shouldApplyUnreadFixup, mDirects]);
}, [mx, publishUnreadAction, shouldApplyUnreadFixup, mDirects]);
useEffect(() => {
const roomListeners = new Map<Room, RoomEventHandlerMap[RoomEvent.UnreadNotifications]>();
@@ -316,10 +340,10 @@ export const useBindRoomToUnreadAtom = (mx: MatrixClient, unreadAtom: typeof roo
mDirects,
});
if (unreadInfo.total === 0 && unreadInfo.highlight === 0) {
setUnreadAtom({ type: 'DELETE', roomId: room.roomId });
publishUnreadAction({ type: 'DELETE', roomId: room.roomId });
return;
}
setUnreadAtom({ type: 'PUT', unreadInfo });
publishUnreadAction({ type: 'PUT', unreadInfo });
};
room.on(RoomEvent.UnreadNotifications, handleUnreadNotifications);
@@ -335,7 +359,7 @@ export const useBindRoomToUnreadAtom = (mx: MatrixClient, unreadAtom: typeof roo
room.removeListener(RoomEvent.UnreadNotifications, listener);
});
};
}, [mx, setUnreadAtom, shouldApplyUnreadFixup, mDirects]);
}, [mx, publishUnreadAction, shouldApplyUnreadFixup, mDirects]);
useEffect(() => {
const handleRoomAccountData = (mEvent: MatrixEvent, room: Room) => {
@@ -347,31 +371,31 @@ export const useBindRoomToUnreadAtom = (mx: MatrixClient, unreadAtom: typeof roo
mDirects,
});
if (unreadInfo.total === 0 && unreadInfo.highlight === 0) {
setUnreadAtom({ type: 'DELETE', roomId: room.roomId });
publishUnreadAction({ type: 'DELETE', roomId: room.roomId });
return;
}
setUnreadAtom({ type: 'PUT', unreadInfo });
publishUnreadAction({ type: 'PUT', unreadInfo });
};
mx.on(RoomEvent.AccountData, handleRoomAccountData);
return () => {
mx.removeListener(RoomEvent.AccountData, handleRoomAccountData);
};
}, [mx, setUnreadAtom, shouldApplyUnreadFixup, mDirects]);
}, [mx, publishUnreadAction, shouldApplyUnreadFixup, mDirects]);
useEffect(() => {
setUnreadAtom({
publishUnreadAction({
type: 'RESET',
unreadInfos: getUnreadInfos(mx, {
applyFixup: shouldApplyUnreadFixup(),
mDirects,
}),
});
}, [mx, setUnreadAtom, roomsNotificationPreferences, shouldApplyUnreadFixup, mDirects]);
}, [mx, publishUnreadAction, roomsNotificationPreferences, shouldApplyUnreadFixup, mDirects]);
useEffect(() => {
const handleMembershipChange = (room: Room, membership: string) => {
if (membership !== (KnownMembership.Join as string)) {
setUnreadAtom({
publishUnreadAction({
type: 'DELETE',
roomId: room.roomId,
});
@@ -381,7 +405,7 @@ export const useBindRoomToUnreadAtom = (mx: MatrixClient, unreadAtom: typeof roo
return () => {
mx.removeListener(RoomEvent.MyMembership, handleMembershipChange);
};
}, [mx, setUnreadAtom]);
}, [mx, publishUnreadAction]);
// Seed badge state immediately when a room is first registered with the client
// (e.g. after joining or receiving an invite that gets auto-accepted).
@@ -396,12 +420,12 @@ export const useBindRoomToUnreadAtom = (mx: MatrixClient, unreadAtom: typeof roo
mDirects,
});
if (unreadInfo.total > 0 || unreadInfo.highlight > 0) {
setUnreadAtom({ type: 'PUT', unreadInfo });
publishUnreadAction({ type: 'PUT', unreadInfo });
}
};
mx.on(ClientEvent.Room, handleRoomAdded as (room: Room) => void);
return () => {
mx.removeListener(ClientEvent.Room, handleRoomAdded as (room: Room) => void);
};
}, [mx, setUnreadAtom, shouldApplyUnreadFixup, mDirects]);
}, [mx, publishUnreadAction, shouldApplyUnreadFixup, mDirects]);
};
+12 -4
View File
@@ -457,10 +457,18 @@ export const getUnreadInfo = (room: Room, options?: UnreadInfoOptions): UnreadIn
// If we have no read receipt, SDK counts may be unreliable. Always check timeline.
if (!readUpToId) {
const liveEvents = room.getLiveTimeline().getEvents();
const hasActivity = liveEvents.some(
(event) => event.getSender() !== userId && isNotificationEvent(event, room, userId)
);
const fullyReadEventId = room
.getAccountData(EventType.FullyRead)
?.getContent<{ event_id?: string }>()?.event_id;
let hasActivity = false;
for (let i = liveEvents.length - 1; i >= 0; i -= 1) {
const event = liveEvents[i];
if (!event || event.getId() === fullyReadEventId) break;
if (event.getSender() !== userId && isNotificationEvent(event, room, userId)) {
hasActivity = true;
break;
}
}
if (hasActivity) {
// If SDK already has counts, use those. Otherwise show dot badge (count=1).
+19
View File
@@ -111,6 +111,7 @@ describe('SlidingSyncManager initial request', () => {
};
expect(joined?.ranges).toEqual([[0, 29]]);
expect(joined?.timeline_limit).toBe(1);
expect(joined?.required_state).toHaveLength(8);
expect(joined?.required_state).toContainEqual([EventType.RoomJoinRules, '']);
expect(joined?.required_state).not.toContainEqual(['m.space.child', '*']);
@@ -118,6 +119,24 @@ describe('SlidingSyncManager initial request', () => {
expect(mocks.slidingSyncConstructorArgs?.[4]).toBe(45000);
});
it('settles response processing after post-response work can finish', async () => {
const manager = makeManager(makeMockMx());
const settled = vi.fn<() => void>();
manager.subscribeToResponseSettled(settled);
manager.attach();
fireLifecycle(SlidingSyncState.RequestFinished, {});
expect(manager.isResponseProcessing()).toBe(true);
fireLifecycle(SlidingSyncState.Complete, {});
expect(manager.isResponseProcessing()).toBe(true);
expect(settled).not.toHaveBeenCalled();
await Promise.resolve();
expect(manager.isResponseProcessing()).toBe(false);
expect(settled).toHaveBeenCalledOnce();
});
it('includes the selected room subscription before the first request', () => {
const manager = new SlidingSyncManager(makeMockMx(), 'https://sliding.example.com', {
initialRoomIds: ['!selected:example.com'],
+26 -2
View File
@@ -33,7 +33,7 @@ export const LIST_INVITES = 'invites';
export const LIST_SEARCH = 'search';
export const LIST_ROOM_SEARCH = 'room_search';
export const LIST_SPACE = 'space';
const LIST_TIMELINE_LIMIT = 0;
const LIST_TIMELINE_LIMIT = 1;
const LIST_PAGE_SIZE = 30;
const DEFAULT_POLL_TIMEOUT_MS = 45000;
@@ -223,6 +223,10 @@ export class SlidingSyncManager {
private responseProcessingStartedAt: number | undefined;
private responseProcessing = false;
private readonly responseSettledListeners = new Set<() => void>();
private previousListCounts: Map<string, number> = new Map();
private readonly requestedListRangeEnds = new Map<string, number>();
@@ -310,7 +314,10 @@ export class SlidingSyncManager {
}
if (state === SlidingSyncState.RequestFinished) {
if (!err && resp) this.responseProcessingStartedAt = performance.now();
if (!err && resp) {
this.responseProcessing = true;
this.responseProcessingStartedAt = performance.now();
}
return;
}
@@ -402,6 +409,12 @@ export class SlidingSyncManager {
totalRoomCount,
});
}
globalThis.queueMicrotask(() => {
if (this.disposed) return;
this.responseProcessing = false;
this.responseSettledListeners.forEach((listener) => listener());
});
};
this.onMembershipLeave = (_event, member) => {
@@ -455,6 +468,8 @@ export class SlidingSyncManager {
});
this.pendingRoomDataListeners.clear();
this.responseProcessing = false;
this.responseSettledListeners.clear();
this.roomDataAwaitingSyncCompletion.clear();
this.roomSubscriptionStatusListeners.forEach((listeners) =>
listeners.forEach((listener) => listener(false))
@@ -721,6 +736,15 @@ export class SlidingSyncManager {
return this.activeRoomSubscriptions.has(roomId);
}
public isResponseProcessing(): boolean {
return this.responseProcessing;
}
public subscribeToResponseSettled(listener: () => void): () => void {
this.responseSettledListeners.add(listener);
return () => this.responseSettledListeners.delete(listener);
}
public reconcileRoomMembership(
roomId: string,
membership: KnownMembership.Join | KnownMembership.Leave