Add User Update class

This commit is contained in:
Christian Pauly 2019-06-11 14:10:50 +02:00
parent 56514d423f
commit e2f7791daa
4 changed files with 91 additions and 23 deletions

View file

@ -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<RoomUpdate> onRoomUpdate =
new StreamController.broadcast();
/// Outside of rooms there are account updates like account_data or presences.
final StreamController<UserUpdate> onUserEvent =
new StreamController.broadcast();
/// Called when the login state e.g. user gets logged out.
final StreamController<LoginState> onLoginStateChanged =
new StreamController.broadcast();
@ -395,7 +400,15 @@ class Connection {
void _handleGlobalEvents(List<dynamic> 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(

View file

@ -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<void> storeUserEventUpdate(UserUpdate userUpdate) {
dynamic eventContent = userUpdate.content;
String type = userUpdate.type;
switch (userUpdate.eventType) {
case "m.direct":
if (userUpdate.content["content"] is Map<String, List<String>>) {
Map<String, List<String>> directMap = userUpdate.content["content"];
directMap.forEach((String key, List<String> 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<void> 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<String, List<String>>) {
Map<String, List<String>> directMap = eventUpdate.content["content"];
directMap.forEach((String key, List<String> 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=?",

View file

@ -0,0 +1,38 @@
/*
* Copyright (c) 2019 Zender & Kurtz GbR.
*
* Authors:
* Christian Pauly <krille@famedly.com>
* Marcel Radzio <mtrnord@famedly.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
/// 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});
}

View file

@ -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<List<RoomUpdate>> roomUpdateListFuture;
Future<List<EventUpdate>> eventUpdateListFuture;
Future<List<UserUpdate>> 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<EventUpdate> 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<UserUpdate> 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);