fix: Show reactions as last events and refactor hasNewMessage

Reactions are triggering push
notifications and should therefore
be displayed as last events
in the room list of a client.
The body should just display
the reaction key.
This fixes that rooms with
new reactions can't set to
read.
This commit is contained in:
Christian Pauly 2022-02-14 14:22:30 +01:00
parent f8cea9e82b
commit 68fcee98cb
6 changed files with 49 additions and 10 deletions

View File

@ -190,6 +190,7 @@ class Client extends MatrixApi {
EventTypes.Message, EventTypes.Message,
EventTypes.Encrypted, EventTypes.Encrypted,
EventTypes.Sticker, EventTypes.Sticker,
EventTypes.Reaction,
]); ]);
// register all the default commands // register all the default commands

View File

@ -167,11 +167,7 @@ class Room {
return; return;
} }
final isMessageEvent = [ final isMessageEvent = client.roomPreviewLastEvents.contains(state.type);
EventTypes.Message,
EventTypes.Sticker,
EventTypes.Encrypted,
].contains(state.type);
// We ignore events editing events older than the current-latest here so // We ignore events editing events older than the current-latest here so
// i.e. newly sent edits for older events don't show up in room preview // i.e. newly sent edits for older events don't show up in room preview
@ -514,9 +510,19 @@ class Room {
/// Checks if the last event has a read marker of the user. /// Checks if the last event has a read marker of the user.
bool get hasNewMessages { bool get hasNewMessages {
final lastEvent = this.lastEvent; final lastEvent = this.lastEvent;
return lastEvent != null &&
!lastEvent.receipts // There is no known event or the last event is only a state fallback event,
.any((receipt) => receipt.user.senderId == client.userID!); // we assume there is no new messages.
if (lastEvent == null ||
!client.roomPreviewLastEvents.contains(lastEvent.type)) return false;
// Read marker is on the last event so no new messages.
if (lastEvent.receipts
.any((receipt) => receipt.user.senderId == client.userID!)) {
return false;
}
return true;
} }
/// Returns true if this room is unread. To check if there are new messages /// Returns true if this room is unread. To check if there are new messages

View File

@ -213,7 +213,12 @@ abstract class EventLocalizations {
_localizedBodyNormalMessage(event, i18n, body), _localizedBodyNormalMessage(event, i18n, body),
EventTypes.Message: (event, i18n, body) => EventTypes.Message: (event, i18n, body) =>
_localizedBodyNormalMessage(event, i18n, body), _localizedBodyNormalMessage(event, i18n, body),
EventTypes.Reaction: (event, i18n, body) => EventTypes.Reaction: (event, i18n, body) => i18n.sentReaction(
_localizedBodyNormalMessage(event, i18n, body), event.sender.calcDisplayname(),
event.content
.tryGetMap<String, dynamic>('m.relates_to')
?.tryGet<String>('key') ??
body,
),
}; };
} }

View File

@ -122,6 +122,8 @@ abstract class MatrixLocalizations {
String sentAVideo(String senderName); String sentAVideo(String senderName);
String sentReaction(String senderName, String reactionKey);
String sharedTheLocation(String senderName); String sharedTheLocation(String senderName);
String couldNotDecryptMessage(String errorText); String couldNotDecryptMessage(String errorText);

View File

@ -225,4 +225,9 @@ class MatrixDefaultLocalizations extends MatrixLocalizations {
String startedACall(String senderName) { String startedACall(String senderName) {
return 'startedACall'; return 'startedACall';
} }
@override
String sentReaction(String senderName, String reactionKey) {
return '$senderName reacted with $reactionKey';
}
} }

View File

@ -258,6 +258,26 @@ void main() {
expect(room.lastEvent?.eventId, '5'); expect(room.lastEvent?.eventId, '5');
expect(room.lastEvent?.body, 'edited cdc'); expect(room.lastEvent?.body, 'edited cdc');
expect(room.lastEvent?.status, EventStatus.sent); expect(room.lastEvent?.status, EventStatus.sent);
// Are reactions coming through?
room.setState(
Event(
senderId: '@test:example.com',
type: EventTypes.Reaction,
room: room,
eventId: '123456',
originServerTs: DateTime.now(),
content: {
'm.relates_to': {
'rel_type': RelationshipTypes.reaction,
'event_id': '1234',
'key': ':-)',
}
},
stateKey: '',
),
);
expect(room.lastEvent?.eventId, '123456');
expect(room.lastEvent?.type, EventTypes.Reaction);
}); });
test('lastEvent when reply parent edited', () async { test('lastEvent when reply parent edited', () async {
room.setState( room.setState(