From e2f7791daae677ef833024b9d57f1ab371d1098b Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Tue, 11 Jun 2019 14:10:50 +0200 Subject: [PATCH] Add User Update class --- lib/src/Connection.dart | 15 +++++++++++++- lib/src/Store.dart | 35 +++++++++++++++++++++------------ lib/src/sync/UserUpdate.dart | 38 ++++++++++++++++++++++++++++++++++++ test/Client_test.dart | 26 ++++++++++++++---------- 4 files changed, 91 insertions(+), 23 deletions(-) create mode 100644 lib/src/sync/UserUpdate.dart diff --git a/lib/src/Connection.dart b/lib/src/Connection.dart index 045f07c..ea82fcd 100644 --- a/lib/src/Connection.dart +++ b/lib/src/Connection.dart @@ -28,6 +28,7 @@ import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'responses/ErrorResponse.dart'; import 'sync/EventUpdate.dart'; +import 'sync/UserUpdate.dart'; import 'sync/RoomUpdate.dart'; import 'Client.dart'; @@ -62,6 +63,10 @@ class Connection { final StreamController onRoomUpdate = new StreamController.broadcast(); + /// Outside of rooms there are account updates like account_data or presences. + final StreamController onUserEvent = + new StreamController.broadcast(); + /// Called when the login state e.g. user gets logged out. final StreamController onLoginStateChanged = new StreamController.broadcast(); @@ -395,7 +400,15 @@ class Connection { void _handleGlobalEvents(List events, String type) { for (int i = 0; i < events.length; i++) - _handleEvent(events[i], type, type); + if (events[i]["type"] is String && events[i]["content"] is dynamic) { + UserUpdate update = UserUpdate( + eventType: events[i]["type"], + type: type, + content: events[i], + ); + client.store?.storeUserEventUpdate(update); + onUserEvent.add(update); + } } void _handleEvent( diff --git a/lib/src/Store.dart b/lib/src/Store.dart index 561939c..232fc64 100644 --- a/lib/src/Store.dart +++ b/lib/src/Store.dart @@ -27,6 +27,7 @@ import 'dart:core'; import 'package:sqflite/sqflite.dart'; import 'package:path/path.dart' as p; import 'sync/EventUpdate.dart'; +import 'sync/UserUpdate.dart'; import 'sync/RoomUpdate.dart'; import 'Client.dart'; import 'User.dart'; @@ -173,8 +174,29 @@ class Store { } } + /// Stores an UserUpdate object in the database. Must be called inside of + /// [transaction]. + Future storeUserEventUpdate(UserUpdate userUpdate) { + dynamic eventContent = userUpdate.content; + String type = userUpdate.type; + + switch (userUpdate.eventType) { + case "m.direct": + if (userUpdate.content["content"] is Map>) { + Map> directMap = userUpdate.content["content"]; + directMap.forEach((String key, List value) { + if (value.length > 0) + txn.rawUpdate( + "UPDATE Rooms SET direct_chat_matrix_id=? WHERE id=?", + [key, value[0]]); + }); + } + break; + } + } + /// Stores an EventUpdate object in the database. Must be called inside of - // /// [transaction]. + /// [transaction]. Future storeEventUpdate(EventUpdate eventUpdate) { dynamic eventContent = eventUpdate.content; String type = eventUpdate.type; @@ -212,17 +234,6 @@ class Store { if (type == "history") return null; switch (eventUpdate.eventType) { - case "m.direct": - if (eventUpdate.content["content"] is Map>) { - Map> directMap = eventUpdate.content["content"]; - directMap.forEach((String key, List value) { - if (value.length > 0) - txn.rawUpdate( - "UPDATE Rooms SET direct_chat_matrix_id=? WHERE id=?", - [key, value[0]]); - }); - } - break; case "m.receipt": if (eventContent["user"] == client.userID) { txn.rawUpdate("UPDATE Rooms SET unread=? WHERE id=?", diff --git a/lib/src/sync/UserUpdate.dart b/lib/src/sync/UserUpdate.dart new file mode 100644 index 0000000..89e101b --- /dev/null +++ b/lib/src/sync/UserUpdate.dart @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2019 Zender & Kurtz GbR. + * + * Authors: + * Christian Pauly + * Marcel Radzio + * + * This file is part of famedlysdk. + * + * famedlysdk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * famedlysdk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Foobar. If not, see . + */ + +/// Represents a new global event like presence or account_data. +class UserUpdate { + + /// Usually 'presence', 'account_data' or whatever. + final String eventType; + + /// See (Matrix Events)[https://matrix.org/docs/spec/client_server/r0.4.0.html] + /// for more informations. + final String type; + + // The json payload of the content of this event. + final dynamic content; + + UserUpdate({this.eventType, this.type, this.content}); +} diff --git a/test/Client_test.dart b/test/Client_test.dart index 6af424f..70c6a14 100644 --- a/test/Client_test.dart +++ b/test/Client_test.dart @@ -26,6 +26,7 @@ import 'package:famedlysdk/src/Client.dart'; import 'package:famedlysdk/src/Connection.dart'; import 'package:famedlysdk/src/sync/EventUpdate.dart'; import 'package:famedlysdk/src/sync/RoomUpdate.dart'; +import 'package:famedlysdk/src/sync/UserUpdate.dart'; import 'package:famedlysdk/src/responses/ErrorResponse.dart'; import 'dart:async'; import 'FakeMatrixApi.dart'; @@ -35,6 +36,7 @@ void main() { Future> roomUpdateListFuture; Future> eventUpdateListFuture; + Future> userUpdateListFuture; /// All Tests related to the Login group("FluffyMatrix", () { @@ -48,6 +50,7 @@ void main() { roomUpdateListFuture = matrix.connection.onRoomUpdate.stream.toList(); eventUpdateListFuture = matrix.connection.onEvent.stream.toList(); + userUpdateListFuture = matrix.connection.onUserEvent.stream.toList(); }; testWidgets('should get created', create); @@ -176,7 +179,7 @@ void main() { List eventUpdateList = await eventUpdateListFuture; - expect(eventUpdateList.length,10); + expect(eventUpdateList.length,7); expect(eventUpdateList[0].eventType=="m.room.member", true); expect(eventUpdateList[0].roomID=="!726s6s6q:example.com", true); @@ -205,20 +208,23 @@ void main() { expect(eventUpdateList[6].eventType=="m.room.member", true); expect(eventUpdateList[6].roomID=="!696r7674:example.com", true); expect(eventUpdateList[6].type=="invite_state", true); + }); - expect(eventUpdateList[7].eventType=="m.presence", true); - expect(eventUpdateList[7].roomID=="presence", true); - expect(eventUpdateList[7].type=="presence", true); + test('User Update Test', () async{ + matrix.connection.onUserEvent.close(); - expect(eventUpdateList[8].eventType=="org.example.custom.config", true); - expect(eventUpdateList[8].roomID=="account_data", true); - expect(eventUpdateList[8].type=="account_data", true); + List eventUpdateList = await userUpdateListFuture; - expect(eventUpdateList[9].eventType=="m.new_device", true); - expect(eventUpdateList[9].roomID=="to_device", true); - expect(eventUpdateList[9].type=="to_device", true); + expect(eventUpdateList.length,3); + expect(eventUpdateList[0].eventType=="m.presence", true); + expect(eventUpdateList[0].type=="presence", true); + expect(eventUpdateList[1].eventType=="org.example.custom.config", true); + expect(eventUpdateList[1].type=="account_data", true); + + expect(eventUpdateList[2].eventType=="m.new_device", true); + expect(eventUpdateList[2].type=="to_device", true); }); testWidgets('should get created', create);