[SDK] Fix bugs

This commit is contained in:
Christian Pauly 2019-08-07 12:06:28 +02:00
parent ac39be9a1e
commit 898767875a
8 changed files with 106 additions and 16 deletions

View file

@ -21,6 +21,8 @@
* along with famedlysdk. If not, see <http://www.gnu.org/licenses/>. * along with famedlysdk. If not, see <http://www.gnu.org/licenses/>.
*/ */
import 'package:famedlysdk/src/RawEvent.dart';
class AccountData { class AccountData {
/// The json payload of the content. The content highly depends on the type. /// The json payload of the content. The content highly depends on the type.
final Map<String, dynamic> content; final Map<String, dynamic> content;
@ -29,4 +31,11 @@ class AccountData {
final String typeKey; final String typeKey;
AccountData({this.content, this.typeKey}); AccountData({this.content, this.typeKey});
/// Get a State event from a table row or from the event stream.
factory AccountData.fromJson(Map<String, dynamic> jsonPayload) {
final Map<String, dynamic> content =
RawEvent.getMapFromPayload(jsonPayload['content']);
return AccountData(content: content, typeKey: jsonPayload['type']);
}
} }

View file

@ -24,6 +24,10 @@
import 'dart:async'; import 'dart:async';
import 'dart:core'; import 'dart:core';
import 'package:famedlysdk/src/AccountData.dart';
import 'package:famedlysdk/src/Presence.dart';
import 'package:famedlysdk/src/sync/UserUpdate.dart';
import 'Connection.dart'; import 'Connection.dart';
import 'Room.dart'; import 'Room.dart';
import 'RoomList.dart'; import 'RoomList.dart';
@ -86,8 +90,38 @@ class Client {
/// Returns the current login state. /// Returns the current login state.
bool isLogged() => accessToken != null; bool isLogged() => accessToken != null;
/// A list of all rooms the user is participating or invited.
RoomList roomList; RoomList roomList;
/// Key/Value store of account data.
Map<String, AccountData> accountData = {};
/// Presences of users by a given matrix ID
Map<String, Presence> presences = {};
void handleUserUpdate(UserUpdate userUpdate) {
if (userUpdate.type == "account_data") {
AccountData newAccountData = AccountData.fromJson(userUpdate.content);
accountData[newAccountData.typeKey] = newAccountData;
}
if (userUpdate.type == "presence") {
Presence newPresence = Presence.fromJson(userUpdate.content);
presences[newPresence.typeKey] = newPresence;
}
}
Map<String, List<String>> get directChats =>
accountData["m.direct"] != null ? accountData["m.direct"].content : {};
/// Returns the (first) room ID from the store which is a private chat with the user [userId].
/// Returns null if there is none.
String getDirectChatFromUserId(String userId) =>
accountData["m.direct"] != null &&
accountData["m.direct"].content[userId] is List<String> &&
accountData["m.direct"].content[userId].length > 0
? accountData["m.direct"].content[userId][0]
: null;
/// Checks the supported versions of the Matrix protocol and the supported /// Checks the supported versions of the Matrix protocol and the supported
/// login types. Returns false if the server is not compatible with the /// login types. Returns false if the server is not compatible with the
/// client. Automatically sets [matrixVersions] and [lazyLoadMembers]. /// client. Automatically sets [matrixVersions] and [lazyLoadMembers].
@ -227,7 +261,7 @@ class Client {
/// defined by the autojoin room feature in Synapse. /// defined by the autojoin room feature in Synapse.
Future<List<User>> loadFamedlyContacts() async { Future<List<User>> loadFamedlyContacts() async {
List<User> contacts = []; List<User> contacts = [];
Room contactDiscoveryRoom = await store Room contactDiscoveryRoom = roomList
.getRoomByAlias("#famedlyContactDiscovery:${userID.split(":")[1]}"); .getRoomByAlias("#famedlyContactDiscovery:${userID.split(":")[1]}");
if (contactDiscoveryRoom != null) if (contactDiscoveryRoom != null)
contacts = await contactDiscoveryRoom.requestParticipants(); contacts = await contactDiscoveryRoom.requestParticipants();

View file

@ -149,10 +149,11 @@ class Connection {
client.lazyLoadMembers = newLazyLoadMembers; client.lazyLoadMembers = newLazyLoadMembers;
client.prevBatch = newPrevBatch; client.prevBatch = newPrevBatch;
client.store?.storeClient(); if (client.store != null) {
client.store.storeClient();
List<Room> rooms = await client.store List<Room> rooms = await client.store
?.getRoomList(onlyLeft: false, onlyGroups: false, onlyDirect: false); .getRoomList(onlyLeft: false, onlyGroups: false, onlyDirect: false);
client.roomList = RoomList( client.roomList = RoomList(
client: client, client: client,
onlyLeft: false, onlyLeft: false,
@ -162,12 +163,19 @@ class Connection {
onInsert: null, onInsert: null,
onRemove: null, onRemove: null,
rooms: rooms); rooms: rooms);
client.accountData = await client.store.getAccountData();
client.presences = await client.store.getPresences();
}
_userEventSub ??= onUserEvent.stream.listen(client.handleUserUpdate);
onLoginStateChanged.add(LoginState.logged); onLoginStateChanged.add(LoginState.logged);
_sync(); _sync();
} }
StreamSubscription _userEventSub;
/// Resets all settings and stops the synchronisation. /// Resets all settings and stops the synchronisation.
void clear() { void clear() {
client.store?.clear(); client.store?.clear();

View file

@ -22,6 +22,7 @@
*/ */
import 'package:famedlysdk/src/AccountData.dart'; import 'package:famedlysdk/src/AccountData.dart';
import 'package:famedlysdk/src/RawEvent.dart';
class Presence extends AccountData { class Presence extends AccountData {
/// The user who has sent this event if it is not a global account data event. /// The user who has sent this event if it is not a global account data event.
@ -29,4 +30,14 @@ class Presence extends AccountData {
Presence({this.sender, Map<String, dynamic> content, String typeKey}) Presence({this.sender, Map<String, dynamic> content, String typeKey})
: super(content: content, typeKey: typeKey); : super(content: content, typeKey: typeKey);
/// Get a State event from a table row or from the event stream.
factory Presence.fromJson(Map<String, dynamic> jsonPayload) {
final Map<String, dynamic> content =
RawEvent.getMapFromPayload(jsonPayload['content']);
return Presence(
content: content,
typeKey: jsonPayload['type'],
sender: jsonPayload['sender']);
}
} }

View file

@ -363,8 +363,7 @@ class Room {
/// Sets this room as a direct chat for this user. /// Sets this room as a direct chat for this user.
Future<dynamic> addToDirectChat(String userID) async { Future<dynamic> addToDirectChat(String userID) async {
Map<String, List<String>> directChats = Map<String, List<String>> directChats = client.directChats;
await client.store.getAccountDataDirectChats();
if (directChats.containsKey(userID)) if (!directChats[userID].contains(id)) if (directChats.containsKey(userID)) if (!directChats[userID].contains(id))
directChats[userID].add(id); directChats[userID].add(id);
else else

View file

@ -73,6 +73,13 @@ class RoomList {
roomSub ??= client.connection.onRoomUpdate.stream.listen(_handleRoomUpdate); roomSub ??= client.connection.onRoomUpdate.stream.listen(_handleRoomUpdate);
} }
Room getRoomByAlias(String alias) {
for (int i = 0; i < rooms.length; i++) {
if (rooms[i].canonicalAlias == alias) return rooms[i];
}
return null;
}
void _handleRoomUpdate(RoomUpdate chatUpdate) { void _handleRoomUpdate(RoomUpdate chatUpdate) {
// Update the chat list item. // Update the chat list item.
// Search the room in the rooms // Search the room in the rooms

View file

@ -25,6 +25,8 @@ import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:core'; import 'dart:core';
import 'package:famedlysdk/src/AccountData.dart';
import 'package:famedlysdk/src/Presence.dart';
import 'package:famedlysdk/src/State.dart'; import 'package:famedlysdk/src/State.dart';
import 'package:path/path.dart' as p; import 'package:path/path.dart' as p;
import 'package:sqflite/sqflite.dart'; import 'package:sqflite/sqflite.dart';
@ -398,6 +400,26 @@ class Store {
return Event.fromJson(res[0], room); return Event.fromJson(res[0], room);
} }
Future<Map<String, AccountData>> getAccountData() async {
Map<String, AccountData> newAccountData = {};
List<Map<String, dynamic>> rawAccountData =
await db.rawQuery("SELECT * FROM AccountData");
for (int i = 0; i < rawAccountData.length; i++)
newAccountData[rawAccountData[i]["type"]] =
AccountData.fromJson(rawAccountData[i]);
return newAccountData;
}
Future<Map<String, Presence>> getPresences() async {
Map<String, Presence> newPresences = {};
List<Map<String, dynamic>> rawPresences =
await db.rawQuery("SELECT * FROM Presences");
for (int i = 0; i < rawPresences.length; i++)
newPresences[rawPresences[i]["type"]] =
Presence.fromJson(rawPresences[i]);
return newPresences;
}
Future forgetNotification(String roomID) async { Future forgetNotification(String roomID) async {
await db await db
.rawDelete("DELETE FROM NotificationsCache WHERE chat_id=?", [roomID]); .rawDelete("DELETE FROM NotificationsCache WHERE chat_id=?", [roomID]);

View file

@ -89,7 +89,7 @@ class User extends State {
/// Returns null on error. /// Returns null on error.
Future<String> startDirectChat() async { Future<String> startDirectChat() async {
// Try to find an existing direct chat // Try to find an existing direct chat
String roomID = await room.client?.rooms.getDirectChatRoomID(id); String roomID = await room.client?.getDirectChatFromUserId(id);
if (roomID != null) return roomID; if (roomID != null) return roomID;
// Start a new direct chat // Start a new direct chat