Add User Update class
This commit is contained in:
parent
56514d423f
commit
e2f7791daa
|
@ -28,6 +28,7 @@ import 'package:flutter/material.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'responses/ErrorResponse.dart';
|
import 'responses/ErrorResponse.dart';
|
||||||
import 'sync/EventUpdate.dart';
|
import 'sync/EventUpdate.dart';
|
||||||
|
import 'sync/UserUpdate.dart';
|
||||||
import 'sync/RoomUpdate.dart';
|
import 'sync/RoomUpdate.dart';
|
||||||
import 'Client.dart';
|
import 'Client.dart';
|
||||||
|
|
||||||
|
@ -62,6 +63,10 @@ class Connection {
|
||||||
final StreamController<RoomUpdate> onRoomUpdate =
|
final StreamController<RoomUpdate> onRoomUpdate =
|
||||||
new StreamController.broadcast();
|
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.
|
/// Called when the login state e.g. user gets logged out.
|
||||||
final StreamController<LoginState> onLoginStateChanged =
|
final StreamController<LoginState> onLoginStateChanged =
|
||||||
new StreamController.broadcast();
|
new StreamController.broadcast();
|
||||||
|
@ -395,7 +400,15 @@ class Connection {
|
||||||
|
|
||||||
void _handleGlobalEvents(List<dynamic> events, String type) {
|
void _handleGlobalEvents(List<dynamic> events, String type) {
|
||||||
for (int i = 0; i < events.length; i++)
|
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(
|
void _handleEvent(
|
||||||
|
|
|
@ -27,6 +27,7 @@ import 'dart:core';
|
||||||
import 'package:sqflite/sqflite.dart';
|
import 'package:sqflite/sqflite.dart';
|
||||||
import 'package:path/path.dart' as p;
|
import 'package:path/path.dart' as p;
|
||||||
import 'sync/EventUpdate.dart';
|
import 'sync/EventUpdate.dart';
|
||||||
|
import 'sync/UserUpdate.dart';
|
||||||
import 'sync/RoomUpdate.dart';
|
import 'sync/RoomUpdate.dart';
|
||||||
import 'Client.dart';
|
import 'Client.dart';
|
||||||
import 'User.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
|
/// Stores an EventUpdate object in the database. Must be called inside of
|
||||||
// /// [transaction].
|
/// [transaction].
|
||||||
Future<void> storeEventUpdate(EventUpdate eventUpdate) {
|
Future<void> storeEventUpdate(EventUpdate eventUpdate) {
|
||||||
dynamic eventContent = eventUpdate.content;
|
dynamic eventContent = eventUpdate.content;
|
||||||
String type = eventUpdate.type;
|
String type = eventUpdate.type;
|
||||||
|
@ -212,17 +234,6 @@ class Store {
|
||||||
if (type == "history") return null;
|
if (type == "history") return null;
|
||||||
|
|
||||||
switch (eventUpdate.eventType) {
|
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":
|
case "m.receipt":
|
||||||
if (eventContent["user"] == client.userID) {
|
if (eventContent["user"] == client.userID) {
|
||||||
txn.rawUpdate("UPDATE Rooms SET unread=? WHERE id=?",
|
txn.rawUpdate("UPDATE Rooms SET unread=? WHERE id=?",
|
||||||
|
|
38
lib/src/sync/UserUpdate.dart
Normal file
38
lib/src/sync/UserUpdate.dart
Normal 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});
|
||||||
|
}
|
|
@ -26,6 +26,7 @@ import 'package:famedlysdk/src/Client.dart';
|
||||||
import 'package:famedlysdk/src/Connection.dart';
|
import 'package:famedlysdk/src/Connection.dart';
|
||||||
import 'package:famedlysdk/src/sync/EventUpdate.dart';
|
import 'package:famedlysdk/src/sync/EventUpdate.dart';
|
||||||
import 'package:famedlysdk/src/sync/RoomUpdate.dart';
|
import 'package:famedlysdk/src/sync/RoomUpdate.dart';
|
||||||
|
import 'package:famedlysdk/src/sync/UserUpdate.dart';
|
||||||
import 'package:famedlysdk/src/responses/ErrorResponse.dart';
|
import 'package:famedlysdk/src/responses/ErrorResponse.dart';
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'FakeMatrixApi.dart';
|
import 'FakeMatrixApi.dart';
|
||||||
|
@ -35,6 +36,7 @@ void main() {
|
||||||
|
|
||||||
Future<List<RoomUpdate>> roomUpdateListFuture;
|
Future<List<RoomUpdate>> roomUpdateListFuture;
|
||||||
Future<List<EventUpdate>> eventUpdateListFuture;
|
Future<List<EventUpdate>> eventUpdateListFuture;
|
||||||
|
Future<List<UserUpdate>> userUpdateListFuture;
|
||||||
|
|
||||||
/// All Tests related to the Login
|
/// All Tests related to the Login
|
||||||
group("FluffyMatrix", () {
|
group("FluffyMatrix", () {
|
||||||
|
@ -48,6 +50,7 @@ void main() {
|
||||||
|
|
||||||
roomUpdateListFuture = matrix.connection.onRoomUpdate.stream.toList();
|
roomUpdateListFuture = matrix.connection.onRoomUpdate.stream.toList();
|
||||||
eventUpdateListFuture = matrix.connection.onEvent.stream.toList();
|
eventUpdateListFuture = matrix.connection.onEvent.stream.toList();
|
||||||
|
userUpdateListFuture = matrix.connection.onUserEvent.stream.toList();
|
||||||
};
|
};
|
||||||
testWidgets('should get created', create);
|
testWidgets('should get created', create);
|
||||||
|
|
||||||
|
@ -176,7 +179,7 @@ void main() {
|
||||||
|
|
||||||
List<EventUpdate> eventUpdateList = await eventUpdateListFuture;
|
List<EventUpdate> eventUpdateList = await eventUpdateListFuture;
|
||||||
|
|
||||||
expect(eventUpdateList.length,10);
|
expect(eventUpdateList.length,7);
|
||||||
|
|
||||||
expect(eventUpdateList[0].eventType=="m.room.member", true);
|
expect(eventUpdateList[0].eventType=="m.room.member", true);
|
||||||
expect(eventUpdateList[0].roomID=="!726s6s6q:example.com", 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].eventType=="m.room.member", true);
|
||||||
expect(eventUpdateList[6].roomID=="!696r7674:example.com", true);
|
expect(eventUpdateList[6].roomID=="!696r7674:example.com", true);
|
||||||
expect(eventUpdateList[6].type=="invite_state", true);
|
expect(eventUpdateList[6].type=="invite_state", true);
|
||||||
|
});
|
||||||
|
|
||||||
expect(eventUpdateList[7].eventType=="m.presence", true);
|
test('User Update Test', () async{
|
||||||
expect(eventUpdateList[7].roomID=="presence", true);
|
matrix.connection.onUserEvent.close();
|
||||||
expect(eventUpdateList[7].type=="presence", true);
|
|
||||||
|
|
||||||
expect(eventUpdateList[8].eventType=="org.example.custom.config", true);
|
List<UserUpdate> eventUpdateList = await userUpdateListFuture;
|
||||||
expect(eventUpdateList[8].roomID=="account_data", true);
|
|
||||||
expect(eventUpdateList[8].type=="account_data", true);
|
|
||||||
|
|
||||||
expect(eventUpdateList[9].eventType=="m.new_device", true);
|
expect(eventUpdateList.length,3);
|
||||||
expect(eventUpdateList[9].roomID=="to_device", true);
|
|
||||||
expect(eventUpdateList[9].type=="to_device", true);
|
|
||||||
|
|
||||||
|
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);
|
testWidgets('should get created', create);
|
||||||
|
|
Loading…
Reference in a new issue