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

View file

@ -737,19 +737,22 @@ class Client {
}
/// 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) {
await _handleToDeviceEvents(sync.toDevice);
}
if (sync.rooms != 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) {
await _handleRooms(sync.rooms.invite, Membership.invite);
await _handleRooms(sync.rooms.invite, Membership.invite,
sortAtTheEnd: sortAtTheEnd);
}
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) {
@ -827,7 +830,8 @@ class Client {
}
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) {
final id = entry.key;
final room = entry.value;
@ -853,8 +857,11 @@ class Client {
handledEvents = true;
}
if (room.timeline?.events?.isNotEmpty ?? false) {
await _handleRoomEvents(id,
room.timeline.events.map((i) => i.toJson()).toList(), 'timeline');
await _handleRoomEvents(
id,
room.timeline.events.map((i) => i.toJson()).toList(),
sortAtTheEnd ? 'history' : 'timeline',
sortAtTheEnd: sortAtTheEnd);
handledEvents = true;
}
if (room.ephemeral?.isNotEmpty ?? false) {
@ -938,14 +945,16 @@ class Client {
}
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++) {
await _handleEvent(events[i], chat_id, type);
await _handleEvent(events[i], chat_id, type, sortAtTheEnd: sortAtTheEnd);
}
}
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>) {
// The client must ignore any new m.room.encryption event to prevent
// 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
// 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(
eventType: event['type'],
roomID: roomID,

View file

@ -791,28 +791,17 @@ class Room {
final loadFn = () async {
if (!((resp.chunk?.isNotEmpty ?? false) && resp.end != null)) return;
if (resp.state != null) {
for (final state in resp.state) {
await EventUpdate(
type: 'state',
roomID: id,
eventType: state.type,
content: state.toJson(),
sortOrder: oldSortOrder,
).decrypt(this, store: 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);
}
await client.handleSync(
SyncUpdate()
..rooms = (RoomsUpdate()
..join = {
'$id': (JoinedRoomUpdate()
..state = resp.state
..timeline = (TimelineUpdate()
..events = resp.chunk
..prevBatch = resp.end)),
}),
sortAtTheEnd: true);
};
if (client.database != null) {
@ -824,15 +813,6 @@ class Room {
} else {
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.

View file

@ -49,19 +49,6 @@ void main() {
test('Create', () async {
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(
type: 'timeline',
@ -75,7 +62,20 @@ void main() {
'event_id': '2',
'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);