Fix request history

This commit is contained in:
Christian Pauly 2020-07-21 07:34:30 +00:00
parent 845d0cf791
commit 4f2a8febf9
4 changed files with 57 additions and 56 deletions

View file

@ -33,6 +33,8 @@ class SyncUpdate {
DeviceListsUpdate deviceLists; DeviceListsUpdate deviceLists;
Map<String, int> deviceOneTimeKeysCount; Map<String, int> deviceOneTimeKeysCount;
SyncUpdate();
SyncUpdate.fromJson(Map<String, dynamic> json) { SyncUpdate.fromJson(Map<String, dynamic> json) {
nextBatch = json['next_batch']; nextBatch = json['next_batch'];
rooms = json['rooms'] != null ? RoomsUpdate.fromJson(json['rooms']) : null; rooms = json['rooms'] != null ? RoomsUpdate.fromJson(json['rooms']) : null;
@ -97,6 +99,8 @@ class RoomsUpdate {
Map<String, InvitedRoomUpdate> invite; Map<String, InvitedRoomUpdate> invite;
Map<String, LeftRoomUpdate> leave; Map<String, LeftRoomUpdate> leave;
RoomsUpdate();
RoomsUpdate.fromJson(Map<String, dynamic> json) { RoomsUpdate.fromJson(Map<String, dynamic> json) {
join = json['join'] != null join = json['join'] != null
? (json['join'] as Map) ? (json['join'] as Map)
@ -136,6 +140,8 @@ class JoinedRoomUpdate extends SyncRoomUpdate {
List<BasicRoomEvent> accountData; List<BasicRoomEvent> accountData;
UnreadNotificationCounts unreadNotifications; UnreadNotificationCounts unreadNotifications;
JoinedRoomUpdate();
JoinedRoomUpdate.fromJson(Map<String, dynamic> json) { JoinedRoomUpdate.fromJson(Map<String, dynamic> json) {
summary = summary =
json['summary'] != null ? RoomSummary.fromJson(json['summary']) : null; json['summary'] != null ? RoomSummary.fromJson(json['summary']) : null;
@ -260,6 +266,9 @@ class TimelineUpdate {
List<MatrixEvent> events; List<MatrixEvent> events;
bool limited; bool limited;
String prevBatch; String prevBatch;
TimelineUpdate();
TimelineUpdate.fromJson(Map<String, dynamic> json) { TimelineUpdate.fromJson(Map<String, dynamic> json) {
events = json['events'] != null events = json['events'] != null
? (json['events'] as List).map((i) => MatrixEvent.fromJson(i)).toList() ? (json['events'] as List).map((i) => MatrixEvent.fromJson(i)).toList()
@ -267,6 +276,7 @@ class TimelineUpdate {
limited = json['limited']; limited = json['limited'];
prevBatch = json['prev_batch']; prevBatch = json['prev_batch'];
} }
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
final data = <String, dynamic>{}; final data = <String, dynamic>{};
if (events != null) { if (events != null) {

View file

@ -737,19 +737,22 @@ class Client {
} }
/// Use this method only for testing utilities! /// Use this method only for testing utilities!
Future<void> handleSync(SyncUpdate sync) async { Future<void> handleSync(SyncUpdate sync, {bool sortAtTheEnd = false}) async {
if (sync.toDevice != null) { if (sync.toDevice != null) {
await _handleToDeviceEvents(sync.toDevice); await _handleToDeviceEvents(sync.toDevice);
} }
if (sync.rooms != null) { if (sync.rooms != null) {
if (sync.rooms.join != null) { if (sync.rooms.join != null) {
await _handleRooms(sync.rooms.join, Membership.join); await _handleRooms(sync.rooms.join, Membership.join,
sortAtTheEnd: sortAtTheEnd);
} }
if (sync.rooms.invite != null) { if (sync.rooms.invite != null) {
await _handleRooms(sync.rooms.invite, Membership.invite); await _handleRooms(sync.rooms.invite, Membership.invite,
sortAtTheEnd: sortAtTheEnd);
} }
if (sync.rooms.leave != null) { if (sync.rooms.leave != null) {
await _handleRooms(sync.rooms.leave, Membership.leave); await _handleRooms(sync.rooms.leave, Membership.leave,
sortAtTheEnd: sortAtTheEnd);
} }
} }
if (sync.presence != null) { if (sync.presence != null) {
@ -827,7 +830,8 @@ class Client {
} }
Future<void> _handleRooms( Future<void> _handleRooms(
Map<String, SyncRoomUpdate> rooms, Membership membership) async { Map<String, SyncRoomUpdate> rooms, Membership membership,
{bool sortAtTheEnd = false}) async {
for (final entry in rooms.entries) { for (final entry in rooms.entries) {
final id = entry.key; final id = entry.key;
final room = entry.value; final room = entry.value;
@ -853,8 +857,11 @@ class Client {
handledEvents = true; handledEvents = true;
} }
if (room.timeline?.events?.isNotEmpty ?? false) { if (room.timeline?.events?.isNotEmpty ?? false) {
await _handleRoomEvents(id, await _handleRoomEvents(
room.timeline.events.map((i) => i.toJson()).toList(), 'timeline'); id,
room.timeline.events.map((i) => i.toJson()).toList(),
sortAtTheEnd ? 'history' : 'timeline',
sortAtTheEnd: sortAtTheEnd);
handledEvents = true; handledEvents = true;
} }
if (room.ephemeral?.isNotEmpty ?? false) { if (room.ephemeral?.isNotEmpty ?? false) {
@ -938,14 +945,16 @@ class Client {
} }
Future<void> _handleRoomEvents( Future<void> _handleRoomEvents(
String chat_id, List<dynamic> events, String type) async { String chat_id, List<dynamic> events, String type,
{bool sortAtTheEnd = false}) async {
for (num i = 0; i < events.length; i++) { for (num i = 0; i < events.length; i++) {
await _handleEvent(events[i], chat_id, type); await _handleEvent(events[i], chat_id, type, sortAtTheEnd: sortAtTheEnd);
} }
} }
Future<void> _handleEvent( Future<void> _handleEvent(
Map<String, dynamic> event, String roomID, String type) async { Map<String, dynamic> event, String roomID, String type,
{bool sortAtTheEnd = false}) async {
if (event['type'] is String && event['content'] is Map<String, dynamic>) { if (event['type'] is String && event['content'] is Map<String, dynamic>) {
// The client must ignore any new m.room.encryption event to prevent // The client must ignore any new m.room.encryption event to prevent
// man-in-the-middle attacks! // man-in-the-middle attacks!
@ -960,7 +969,9 @@ class Client {
// ephemeral events aren't persisted and don't need a sort order - they are // ephemeral events aren't persisted and don't need a sort order - they are
// expected to be processed as soon as they come in // expected to be processed as soon as they come in
final sortOrder = type != 'ephemeral' ? room.newSortOrder : 0.0; final sortOrder = type != 'ephemeral'
? (sortAtTheEnd ? room.oldSortOrder : room.newSortOrder)
: 0.0;
var update = EventUpdate( var update = EventUpdate(
eventType: event['type'], eventType: event['type'],
roomID: roomID, roomID: roomID,

View file

@ -791,28 +791,17 @@ class Room {
final loadFn = () async { final loadFn = () async {
if (!((resp.chunk?.isNotEmpty ?? false) && resp.end != null)) return; if (!((resp.chunk?.isNotEmpty ?? false) && resp.end != null)) return;
if (resp.state != null) { await client.handleSync(
for (final state in resp.state) { SyncUpdate()
await EventUpdate( ..rooms = (RoomsUpdate()
type: 'state', ..join = {
roomID: id, '$id': (JoinedRoomUpdate()
eventType: state.type, ..state = resp.state
content: state.toJson(), ..timeline = (TimelineUpdate()
sortOrder: oldSortOrder, ..events = resp.chunk
).decrypt(this, store: true); ..prevBatch = resp.end)),
} }),
} sortAtTheEnd: true);
for (final hist in resp.chunk) {
final eventUpdate = await EventUpdate(
type: 'history',
roomID: id,
eventType: hist.type,
content: hist.toJson(),
sortOrder: oldSortOrder,
).decrypt(this, store: true);
client.onEvent.add(eventUpdate);
}
}; };
if (client.database != null) { if (client.database != null) {
@ -824,15 +813,6 @@ class Room {
} else { } else {
await loadFn(); await loadFn();
} }
client.onRoomUpdate.add(
RoomUpdate(
id: id,
membership: membership,
prev_batch: resp.end,
notification_count: notificationCount,
highlight_count: highlightCount,
),
);
} }
/// Sets this room as a direct chat for this user if not already. /// Sets this room as a direct chat for this user if not already.

View file

@ -49,19 +49,6 @@ void main() {
test('Create', () async { test('Create', () async {
await client.checkServer('https://fakeServer.notExisting'); await client.checkServer('https://fakeServer.notExisting');
client.onEvent.add(EventUpdate(
type: 'timeline',
roomID: roomID,
eventType: 'm.room.message',
content: {
'type': 'm.room.message',
'content': {'msgtype': 'm.text', 'body': 'Testcase'},
'sender': '@alice:example.com',
'status': 2,
'event_id': '1',
'origin_server_ts': testTimeStamp
},
sortOrder: room.newSortOrder));
client.onEvent.add(EventUpdate( client.onEvent.add(EventUpdate(
type: 'timeline', type: 'timeline',
@ -75,7 +62,20 @@ void main() {
'event_id': '2', 'event_id': '2',
'origin_server_ts': testTimeStamp - 1000 'origin_server_ts': testTimeStamp - 1000
}, },
sortOrder: room.oldSortOrder)); sortOrder: room.newSortOrder));
client.onEvent.add(EventUpdate(
type: 'timeline',
roomID: roomID,
eventType: 'm.room.message',
content: {
'type': 'm.room.message',
'content': {'msgtype': 'm.text', 'body': 'Testcase'},
'sender': '@alice:example.com',
'status': 2,
'event_id': '1',
'origin_server_ts': testTimeStamp
},
sortOrder: room.newSortOrder));
expect(timeline.sub != null, true); expect(timeline.sub != null, true);