refactor: Update matrix_api_lite and use SyncUpdate constructors

This commit is contained in:
Krille Fear 2021-11-10 10:04:03 +01:00
parent db7d3b652a
commit 58b36a67a3
5 changed files with 116 additions and 71 deletions

View File

@ -122,12 +122,20 @@ class Event extends MatrixEvent {
final json = toJson();
json['unsigned'] ??= <String, dynamic>{};
json['unsigned'][messageSendingStatusKey] = EventStatus.error.intValue;
room.client.handleSync(SyncUpdate(nextBatch: '')
..rooms = (RoomsUpdate()
..join = (<String, JoinedRoomUpdate>{}..[room.id] =
(JoinedRoomUpdate()
..timeline = (TimelineUpdate()
..events = [MatrixEvent.fromJson(json)])))));
room.client.handleSync(
SyncUpdate(
nextBatch: '',
rooms: RoomsUpdate(
join: {
room.id: JoinedRoomUpdate(
timeline: TimelineUpdate(
events: [MatrixEvent.fromJson(json)],
),
)
},
),
),
);
}
}
}

View File

@ -515,16 +515,24 @@ class Room {
/// set a read marker!
Future<void> markUnread(bool unread) async {
final content = MarkedUnread(unread).toJson();
await _handleFakeSync(SyncUpdate(nextBatch: '')
..rooms = (RoomsUpdate()
..join = (({}..[id] = (JoinedRoomUpdate()
..accountData = [
await _handleFakeSync(
SyncUpdate(
nextBatch: '',
rooms: RoomsUpdate(
join: {
id: JoinedRoomUpdate(
accountData: [
BasicRoomEvent(
content: content,
roomId: id,
type: EventType.markedUnread,
),
],
)
])))));
},
),
),
);
await client.setAccountDataPerRoom(
client.userID!,
id,
@ -793,11 +801,13 @@ class Room {
}
}
final sentDate = DateTime.now();
final syncUpdate = SyncUpdate(nextBatch: '')
..rooms = (RoomsUpdate()
..join = (<String, JoinedRoomUpdate>{}..[id] = (JoinedRoomUpdate()
..timeline = (TimelineUpdate()
..events = [
final syncUpdate = SyncUpdate(
nextBatch: '',
rooms: RoomsUpdate(
join: {
id: JoinedRoomUpdate(
timeline: TimelineUpdate(
events: [
MatrixEvent(
content: content,
type: type,
@ -808,8 +818,13 @@ class Room {
messageSendingStatusKey: EventStatus.sending.intValue,
'transaction_id': messageID,
},
)
]))));
),
],
),
),
},
),
);
await _handleFakeSync(syncUpdate);
// Send the text and on success, store and display a *sent* event.
@ -879,11 +894,14 @@ class Room {
if ([MatrixError.M_NOT_FOUND, MatrixError.M_UNKNOWN]
.contains(exception.error)) {
await _handleFakeSync(
SyncUpdate(nextBatch: '')
..rooms = (RoomsUpdate()
..leave = {
'$id': (LeftRoomUpdate()),
}),
SyncUpdate(
nextBatch: '',
rooms: RoomsUpdate(
leave: {
id: LeftRoomUpdate(),
},
),
),
);
}
rethrow;
@ -953,24 +971,34 @@ class Room {
if (!((resp.chunk?.isNotEmpty ?? false) && resp.end != null)) return;
await client.handleSync(
SyncUpdate(nextBatch: '')
..rooms = (RoomsUpdate()
..join = membership == Membership.join
? ({}..[id] = ((JoinedRoomUpdate()
..state = resp.state
..timeline = (TimelineUpdate()
..limited = false
..events = resp.chunk
..prevBatch = resp.end))))
: null
..leave = membership != Membership.join
? ({}..[id] = ((LeftRoomUpdate()
..state = resp.state
..timeline = (TimelineUpdate()
..limited = false
..events = resp.chunk
..prevBatch = resp.end))))
SyncUpdate(
nextBatch: '',
rooms: RoomsUpdate(
join: membership == Membership.join
? {
id: JoinedRoomUpdate(
state: resp.state,
timeline: TimelineUpdate(
limited: false,
events: resp.chunk,
prevBatch: resp.end,
),
)
}
: null,
leave: membership != Membership.join
? {
id: LeftRoomUpdate(
state: resp.state,
timeline: TimelineUpdate(
limited: false,
events: resp.chunk,
prevBatch: resp.end,
),
),
}
: null),
),
sortAtTheEnd: true);
};

View File

@ -16,7 +16,7 @@ dependencies:
crypto: ^3.0.0
base58check: ^2.0.0
olm: ^2.0.0
matrix_api_lite: ^0.4.3
matrix_api_lite: ^0.5.1
hive: ^2.0.4
ffi: ^1.0.0
js: ^0.6.3

View File

@ -122,10 +122,12 @@ class FakeMatrixApi extends MockClient {
action.contains('/account_data/') &&
!action.contains('/room/')) {
final type = Uri.decodeComponent(action.split('/').last);
final syncUpdate = sdk.SyncUpdate(nextBatch: '')
..accountData = [
final syncUpdate = sdk.SyncUpdate(
nextBatch: '',
accountData: [
sdk.BasicEvent(content: decodeJson(data), type: type)
];
],
);
if (client?.database != null) {
await client?.database?.transaction(() async {
await client?.handleSync(syncUpdate);

View File

@ -306,18 +306,25 @@ void main() {
});
test('Clear cache on limited timeline', () async {
client.onSync.add(SyncUpdate(nextBatch: '1234')
..rooms = (RoomsUpdate()
..join = {
roomID: (JoinedRoomUpdate()
..timeline = (TimelineUpdate()
..limited = true
..prevBatch = 'blah')
..unreadNotifications = UnreadNotificationCounts.fromJson({
'highlight_count': 0,
'notification_count': 0,
}))
}));
client.onSync.add(
SyncUpdate(
nextBatch: '1234',
rooms: RoomsUpdate(
join: {
roomID: JoinedRoomUpdate(
timeline: TimelineUpdate(
limited: true,
prevBatch: 'blah',
),
unreadNotifications: UnreadNotificationCounts(
highlightCount: 0,
notificationCount: 0,
),
),
},
),
),
);
await Future.delayed(Duration(milliseconds: 50));
expect(timeline.events.isEmpty, true);
});