feat: Implement get mentions from event content

This commit is contained in:
Christian Kußowski 2025-09-12 10:13:57 +02:00
parent 2d6f043861
commit 77fb2b29e7
No known key found for this signature in database
GPG Key ID: E067ECD60F1A0652
2 changed files with 34 additions and 0 deletions

View File

@ -1180,6 +1180,17 @@ class Event extends MatrixEvent {
(fileSendingStatus) => fileSendingStatus.name == status, (fileSendingStatus) => fileSendingStatus.name == status,
); );
} }
/// Returns the mentioned userIds and wether the event includes an @room
/// mention. This is only determined by the `m.mention` object in the event
/// content.
({List<String> userIds, bool room}) get mentions {
final mentionsMap = content.tryGetMap<String, Object?>('m.mentions');
return (
userIds: mentionsMap?.tryGetList<String>('user_ids') ?? [],
room: mentionsMap?.tryGet<bool>('room') ?? false,
);
}
} }
enum FileSendingStatus { enum FileSendingStatus {

View File

@ -2951,5 +2951,28 @@ void main() async {
Timeline(room: room, chunk: TimelineChunk(events: [targetEvent])); Timeline(room: room, chunk: TimelineChunk(events: [targetEvent]));
expect(await event.getReplyEvent(timeline), targetEvent); expect(await event.getReplyEvent(timeline), targetEvent);
}); });
test('getMentions', () {
final event = Event.fromJson(
{
'content': {
'msgtype': 'text',
'body': 'Hello world @alice:matrix.org',
'm.mentions': {
'user_ids': ['@alice:matrix.org'],
'room': false,
}
},
'event_id': '\$143273582443PhrSn:example.org',
'origin_server_ts': 1432735824653,
'room_id': room.id,
'sender': '@example:example.org',
'type': 'm.room.message',
'unsigned': {'age': 1234},
},
room,
);
expect(event.mentions.userIds, ['@alice:matrix.org']);
expect(event.mentions.room, false);
});
}); });
} }