mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-21 01:21:18 -08:00
fix: stop synthesizing unread badges without read evidence
This commit is contained in:
2 files changed
+94
-10
No files matched your search
@@ -9,6 +9,8 @@ const ROOM_EMPTY = '!empty:example.com';
|
||||
const ROOM_MUTED = '!muted:example.com';
|
||||
const ROOM_LEFT = '!left:example.com';
|
||||
const MISSING = '!missing:example.com';
|
||||
const ME = '@user:example.com';
|
||||
const OTHER = '@other:example.com';
|
||||
|
||||
const createEvent = (id: string, sender: string, type = 'm.room.message'): MatrixEvent =>
|
||||
({
|
||||
@@ -40,6 +42,7 @@ const createRoom = (
|
||||
highlight?: number;
|
||||
readUpTo?: string | null;
|
||||
events?: MatrixEvent[];
|
||||
fullyRead?: string;
|
||||
} = {}
|
||||
): Room => {
|
||||
const {
|
||||
@@ -49,6 +52,7 @@ const createRoom = (
|
||||
highlight = 0,
|
||||
readUpTo = '$read',
|
||||
events = [],
|
||||
fullyRead,
|
||||
} = opts;
|
||||
|
||||
const client = createClient({ [roomId]: null as unknown as Room });
|
||||
@@ -64,11 +68,22 @@ const createRoom = (
|
||||
}),
|
||||
getEventReadUpTo: () => readUpTo,
|
||||
getLiveTimeline: () => ({ getEvents: () => events }),
|
||||
getAccountData: () => undefined,
|
||||
getRoomUnreadNotificationCount: () => total,
|
||||
hasUserReadEvent: () => false,
|
||||
getAccountData: (type: string) =>
|
||||
fullyRead && type === 'm.fully_read'
|
||||
? { getContent: () => ({ event_id: fullyRead }) }
|
||||
: undefined,
|
||||
client,
|
||||
} as unknown as Room;
|
||||
};
|
||||
|
||||
const bindRoom = (room: Room): MatrixClient => {
|
||||
const mx = createClient({ [room.roomId]: room });
|
||||
(room as unknown as { client: MatrixClient }).client = mx;
|
||||
return mx;
|
||||
};
|
||||
|
||||
describe('getUnreadInfosForRooms', () => {
|
||||
it('skips space rooms instead of deleting them', () => {
|
||||
const unreadRoom = createRoom(ROOM_UNREAD, {
|
||||
@@ -125,6 +140,70 @@ describe('getUnreadInfosForRooms', () => {
|
||||
expect(deleted).toContain(ROOM_MUTED);
|
||||
});
|
||||
|
||||
it('badges an unvisited room that has no read data at all', () => {
|
||||
const room = createRoom(ROOM_UNREAD, {
|
||||
readUpTo: null,
|
||||
events: [createEvent('$a', OTHER)],
|
||||
});
|
||||
const mx = bindRoom(room);
|
||||
|
||||
const { unread } = getUnreadInfosForRooms(mx, [ROOM_UNREAD]);
|
||||
expect(unread.find((u) => u.roomId === ROOM_UNREAD)?.total).toBe(1);
|
||||
});
|
||||
|
||||
it('does not badge a room whose fully-read marker is outside the loaded window', () => {
|
||||
const room = createRoom(ROOM_UNREAD, {
|
||||
readUpTo: null,
|
||||
fullyRead: '$older',
|
||||
events: [createEvent('$a', OTHER)],
|
||||
});
|
||||
const mx = bindRoom(room);
|
||||
|
||||
const { unread, deleted } = getUnreadInfosForRooms(mx, [ROOM_UNREAD]);
|
||||
expect(unread).toHaveLength(0);
|
||||
expect(deleted).toContain(ROOM_UNREAD);
|
||||
});
|
||||
|
||||
it('does not badge a room without receipts where our own message is newest', () => {
|
||||
const room = createRoom(ROOM_UNREAD, {
|
||||
readUpTo: null,
|
||||
events: [createEvent('$a', OTHER), createEvent('$b', ME)],
|
||||
});
|
||||
const mx = bindRoom(room);
|
||||
|
||||
const { unread, deleted } = getUnreadInfosForRooms(mx, [ROOM_UNREAD]);
|
||||
expect(unread).toHaveLength(0);
|
||||
expect(deleted).toContain(ROOM_UNREAD);
|
||||
});
|
||||
|
||||
it('does not badge a room whose only activity is its creation event', () => {
|
||||
const room = createRoom(ROOM_UNREAD, {
|
||||
readUpTo: null,
|
||||
events: [createEvent('$create', OTHER, 'm.room.create')],
|
||||
});
|
||||
const mx = bindRoom(room);
|
||||
|
||||
const { unread, deleted } = getUnreadInfosForRooms(mx, [ROOM_UNREAD]);
|
||||
expect(unread).toHaveLength(0);
|
||||
expect(deleted).toContain(ROOM_UNREAD);
|
||||
});
|
||||
|
||||
it('does not badge a room where our own message follows the last foreign message', () => {
|
||||
const room = createRoom(ROOM_UNREAD, {
|
||||
readUpTo: '$missing',
|
||||
events: [
|
||||
createEvent('$a', OTHER),
|
||||
createEvent('$b', ME),
|
||||
createEvent('$c', OTHER, 'm.room.member'),
|
||||
],
|
||||
});
|
||||
const mx = bindRoom(room);
|
||||
|
||||
const { unread, deleted } = getUnreadInfosForRooms(mx, [ROOM_UNREAD]);
|
||||
expect(unread).toHaveLength(0);
|
||||
expect(deleted).toContain(ROOM_UNREAD);
|
||||
});
|
||||
|
||||
it('deletes rooms whose unread has dropped to zero', () => {
|
||||
const emptyRoom = createRoom(ROOM_EMPTY, { total: 0, highlight: 0, events: [] });
|
||||
const mx = createClient({ [ROOM_EMPTY]: emptyRoom });
|
||||
|
||||
@@ -92,7 +92,6 @@ export const getNotificationType = (mx: MatrixClient, roomId: string): Notificat
|
||||
};
|
||||
|
||||
const NOTIFICATION_EVENT_TYPES = new Set<string>([
|
||||
EventType.RoomCreate,
|
||||
EventType.RoomMessage,
|
||||
EventType.RoomMessageEncrypted,
|
||||
EventType.RoomMember,
|
||||
@@ -149,18 +148,15 @@ export const roomHaveUnread = (mx: MatrixClient, room: Room) => {
|
||||
return false;
|
||||
}
|
||||
|
||||
const latestEvent = liveEvents[liveEvents.length - 1];
|
||||
|
||||
if (latestEvent?.getSender() === userId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (let i = liveEvents.length - 1; i >= 0; i -= 1) {
|
||||
const event = liveEvents[i];
|
||||
if (!event) return false;
|
||||
if (event.getId() === readUpToId) {
|
||||
return false;
|
||||
}
|
||||
if (event.getSender() === userId) {
|
||||
return false;
|
||||
}
|
||||
if (isNotificationEvent(event, room, userId)) {
|
||||
return true;
|
||||
}
|
||||
@@ -273,15 +269,24 @@ export const getUnreadInfo = (room: Room, options?: UnreadInfoOptions): UnreadIn
|
||||
.getAccountData(EventType.FullyRead)
|
||||
?.getContent<{ event_id?: string }>()?.event_id;
|
||||
let hasActivity = false;
|
||||
let foundReadBoundary = 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)) {
|
||||
if (!event) break;
|
||||
if (event.getId() === fullyReadEventId || event.getSender() === userId) {
|
||||
foundReadBoundary = true;
|
||||
break;
|
||||
}
|
||||
if (isNotificationEvent(event, room, userId)) {
|
||||
hasActivity = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (hasActivity && !foundReadBoundary && fullyReadEventId) {
|
||||
hasActivity = false;
|
||||
}
|
||||
|
||||
if (hasActivity) {
|
||||
// If SDK already has counts, use those. Otherwise show dot badge (count=1).
|
||||
if (total === 0 && highlight === 0) {
|
||||
|
||||
Reference in new issue
Block a user