Implement pinned events

This commit is contained in:
Christian Pauly 2020-06-24 08:41:52 +00:00
parent ed262c0d51
commit 2afd0bb3a8
5 changed files with 64 additions and 14 deletions

View file

@ -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';

View file

@ -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<String> get pinnedEventIds => states[EventTypes.RoomPinnedEvents] != null
? (states[EventTypes.RoomPinnedEvents].content['pinned'] is List<String>
? states[EventTypes.RoomPinnedEvents].content['pinned']
: <String>[])
: <String>[];
/// 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<String> setPinnedEvents(List<String> pinnedEventIds) =>
client.api.sendState(
id,
EventTypes.RoomPinnedEvents,
{'pinned': pinnedEventIds},
);
/// return all current emote packs for this room
Map<String, Map<String, String>> get emotePacks {
final packs = <String, Map<String, String>>{};

View file

@ -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 {

View file

@ -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',

View file

@ -150,6 +150,19 @@ void main() {
content: {'url': 'mxc://testurl'},
stateKey: '');
expect(room.avatar.toString(), 'mxc://testurl');
expect(room.pinnedEventIds, <String>[]);
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',