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(); final json = toJson();
json['unsigned'] ??= <String, dynamic>{}; json['unsigned'] ??= <String, dynamic>{};
json['unsigned'][messageSendingStatusKey] = EventStatus.error.intValue; json['unsigned'][messageSendingStatusKey] = EventStatus.error.intValue;
room.client.handleSync(SyncUpdate(nextBatch: '') room.client.handleSync(
..rooms = (RoomsUpdate() SyncUpdate(
..join = (<String, JoinedRoomUpdate>{}..[room.id] = nextBatch: '',
(JoinedRoomUpdate() rooms: RoomsUpdate(
..timeline = (TimelineUpdate() join: {
..events = [MatrixEvent.fromJson(json)]))))); room.id: JoinedRoomUpdate(
timeline: TimelineUpdate(
events: [MatrixEvent.fromJson(json)],
),
)
},
),
),
);
} }
} }
} }

View File

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

View File

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

View File

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

View File

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