From 2afd0bb3a88b07c9620b86e3fd2ebaad0480eaf7 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Wed, 24 Jun 2020 08:41:52 +0000 Subject: [PATCH] Implement pinned events --- lib/matrix_api/model/event_types.dart | 1 + lib/src/room.dart | 16 ++++++++++++++ test/client_test.dart | 32 +++++++++++++++------------ test/fake_matrix_api.dart | 16 ++++++++++++++ test/room_test.dart | 13 +++++++++++ 5 files changed, 64 insertions(+), 14 deletions(-) diff --git a/lib/matrix_api/model/event_types.dart b/lib/matrix_api/model/event_types.dart index 4582b30..4de7608 100644 --- a/lib/matrix_api/model/event_types.dart +++ b/lib/matrix_api/model/event_types.dart @@ -27,6 +27,7 @@ abstract class EventTypes { static const String RoomMember = 'm.room.member'; static const String RoomPowerLevels = 'm.room.power_levels'; static const String RoomName = 'm.room.name'; + static const String RoomPinnedEvents = 'm.room.pinned_events'; static const String RoomTopic = 'm.room.topic'; static const String RoomAvatar = 'm.room.avatar'; static const String RoomTombstone = 'm.room.tombsone'; diff --git a/lib/src/room.dart b/lib/src/room.dart index 6d812fd..9450f22 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -152,6 +152,14 @@ class Room { ? states[EventTypes.RoomName].content['name'] : ''; + /// The pinned events for this room. If there are no this returns an empty + /// list. + List get pinnedEventIds => states[EventTypes.RoomPinnedEvents] != null + ? (states[EventTypes.RoomPinnedEvents].content['pinned'] is List + ? states[EventTypes.RoomPinnedEvents].content['pinned'] + : []) + : []; + /// Returns a localized displayname for this server. If the room is a groupchat /// without a name, then it will return the localized version of 'Group with Alice' instead /// of just 'Alice' to make it different to a direct chat. @@ -353,6 +361,14 @@ class Room { {'topic': newName}, ); + /// Call the Matrix API to change the pinned events of this room. + Future setPinnedEvents(List pinnedEventIds) => + client.api.sendState( + id, + EventTypes.RoomPinnedEvents, + {'pinned': pinnedEventIds}, + ); + /// return all current emote packs for this room Map> get emotePacks { final packs = >{}; diff --git a/test/client_test.dart b/test/client_test.dart index 3963400..39292b6 100644 --- a/test/client_test.dart +++ b/test/client_test.dart @@ -258,7 +258,7 @@ void main() { var eventUpdateList = await eventUpdateListFuture; - expect(eventUpdateList.length, 13); + expect(eventUpdateList.length, 14); expect(eventUpdateList[0].eventType, 'm.room.member'); expect(eventUpdateList[0].roomID, '!726s6s6q:example.com'); @@ -272,41 +272,45 @@ void main() { expect(eventUpdateList[2].roomID, '!726s6s6q:example.com'); expect(eventUpdateList[2].type, 'state'); - expect(eventUpdateList[3].eventType, 'm.room.member'); + expect(eventUpdateList[3].eventType, 'm.room.pinned_events'); expect(eventUpdateList[3].roomID, '!726s6s6q:example.com'); - expect(eventUpdateList[3].type, 'timeline'); + expect(eventUpdateList[3].type, 'state'); - expect(eventUpdateList[4].eventType, 'm.room.message'); + expect(eventUpdateList[4].eventType, 'm.room.member'); expect(eventUpdateList[4].roomID, '!726s6s6q:example.com'); expect(eventUpdateList[4].type, 'timeline'); - expect(eventUpdateList[5].eventType, 'm.typing'); + expect(eventUpdateList[5].eventType, 'm.room.message'); expect(eventUpdateList[5].roomID, '!726s6s6q:example.com'); - expect(eventUpdateList[5].type, 'ephemeral'); + expect(eventUpdateList[5].type, 'timeline'); - expect(eventUpdateList[6].eventType, 'm.receipt'); + expect(eventUpdateList[6].eventType, 'm.typing'); expect(eventUpdateList[6].roomID, '!726s6s6q:example.com'); expect(eventUpdateList[6].type, 'ephemeral'); expect(eventUpdateList[7].eventType, 'm.receipt'); expect(eventUpdateList[7].roomID, '!726s6s6q:example.com'); - expect(eventUpdateList[7].type, 'account_data'); + expect(eventUpdateList[7].type, 'ephemeral'); - expect(eventUpdateList[8].eventType, 'm.tag'); + expect(eventUpdateList[8].eventType, 'm.receipt'); expect(eventUpdateList[8].roomID, '!726s6s6q:example.com'); expect(eventUpdateList[8].type, 'account_data'); - expect(eventUpdateList[9].eventType, 'org.example.custom.room.config'); + expect(eventUpdateList[9].eventType, 'm.tag'); expect(eventUpdateList[9].roomID, '!726s6s6q:example.com'); expect(eventUpdateList[9].type, 'account_data'); - expect(eventUpdateList[10].eventType, 'm.room.name'); - expect(eventUpdateList[10].roomID, '!696r7674:example.com'); - expect(eventUpdateList[10].type, 'invite_state'); + expect(eventUpdateList[10].eventType, 'org.example.custom.room.config'); + expect(eventUpdateList[10].roomID, '!726s6s6q:example.com'); + expect(eventUpdateList[10].type, 'account_data'); - expect(eventUpdateList[11].eventType, 'm.room.member'); + expect(eventUpdateList[11].eventType, 'm.room.name'); expect(eventUpdateList[11].roomID, '!696r7674:example.com'); expect(eventUpdateList[11].type, 'invite_state'); + + expect(eventUpdateList[12].eventType, 'm.room.member'); + expect(eventUpdateList[12].roomID, '!696r7674:example.com'); + expect(eventUpdateList[12].type, 'invite_state'); }); test('To Device Update Test', () async { diff --git a/test/fake_matrix_api.dart b/test/fake_matrix_api.dart index 4e3eb08..4bde4c5 100644 --- a/test/fake_matrix_api.dart +++ b/test/fake_matrix_api.dart @@ -194,6 +194,18 @@ class FakeMatrixApi extends MockClient { 'origin_server_ts': 1417731086795, 'event_id': '666972737430353:example.com' }, + { + 'content': { + 'pinned': ['1234:bla'] + }, + 'type': 'm.room.pinned_events', + 'event_id': '21432735824443PhrSn:example.org', + 'room_id': '!1234:example.com', + 'sender': '@example:example.org', + 'origin_server_ts': 1432735824653, + 'unsigned': {'age': 1234}, + 'state_key': '' + }, ] }, 'timeline': { @@ -1805,6 +1817,10 @@ class FakeMatrixApi extends MockClient { (var reqI) => { 'event_id': '42', }, + '/client/r0/rooms/%21localpart%3Aserver.abc/state/m.room.pinned_events': + (var reqI) => { + 'event_id': '42', + }, '/client/r0/rooms/%21localpart%3Aserver.abc/state/m.room.power_levels': (var reqI) => { 'event_id': '42', diff --git a/test/room_test.dart b/test/room_test.dart index 5e67d7f..3fc0398 100644 --- a/test/room_test.dart +++ b/test/room_test.dart @@ -150,6 +150,19 @@ void main() { content: {'url': 'mxc://testurl'}, stateKey: ''); expect(room.avatar.toString(), 'mxc://testurl'); + + expect(room.pinnedEventIds, []); + room.states['m.room.pinned_events'] = Event( + senderId: '@test:example.com', + type: 'm.room.pinned_events', + roomId: room.id, + room: room, + eventId: '123', + content: { + 'pinned': ['1234'] + }, + stateKey: ''); + expect(room.pinnedEventIds.first, '1234'); room.states['m.room.message'] = Event( senderId: '@test:example.com', type: 'm.room.message',