[SDK] Fix bugs
This commit is contained in:
parent
ac39be9a1e
commit
898767875a
|
@ -21,6 +21,8 @@
|
|||
* along with famedlysdk. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'package:famedlysdk/src/RawEvent.dart';
|
||||
|
||||
class AccountData {
|
||||
/// The json payload of the content. The content highly depends on the type.
|
||||
final Map<String, dynamic> content;
|
||||
|
@ -29,4 +31,11 @@ class AccountData {
|
|||
final String 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']);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,6 +24,10 @@
|
|||
import 'dart:async';
|
||||
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 'Room.dart';
|
||||
import 'RoomList.dart';
|
||||
|
@ -86,8 +90,38 @@ class Client {
|
|||
/// Returns the current login state.
|
||||
bool isLogged() => accessToken != null;
|
||||
|
||||
/// A list of all rooms the user is participating or invited.
|
||||
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
|
||||
/// login types. Returns false if the server is not compatible with the
|
||||
/// client. Automatically sets [matrixVersions] and [lazyLoadMembers].
|
||||
|
@ -227,7 +261,7 @@ class Client {
|
|||
/// defined by the autojoin room feature in Synapse.
|
||||
Future<List<User>> loadFamedlyContacts() async {
|
||||
List<User> contacts = [];
|
||||
Room contactDiscoveryRoom = await store
|
||||
Room contactDiscoveryRoom = roomList
|
||||
.getRoomByAlias("#famedlyContactDiscovery:${userID.split(":")[1]}");
|
||||
if (contactDiscoveryRoom != null)
|
||||
contacts = await contactDiscoveryRoom.requestParticipants();
|
||||
|
|
|
@ -149,25 +149,33 @@ class Connection {
|
|||
client.lazyLoadMembers = newLazyLoadMembers;
|
||||
client.prevBatch = newPrevBatch;
|
||||
|
||||
client.store?.storeClient();
|
||||
if (client.store != null) {
|
||||
client.store.storeClient();
|
||||
|
||||
List<Room> rooms = await client.store
|
||||
?.getRoomList(onlyLeft: false, onlyGroups: false, onlyDirect: false);
|
||||
client.roomList = RoomList(
|
||||
client: client,
|
||||
onlyLeft: false,
|
||||
onlyDirect: false,
|
||||
onlyGroups: false,
|
||||
onUpdate: null,
|
||||
onInsert: null,
|
||||
onRemove: null,
|
||||
rooms: rooms);
|
||||
List<Room> rooms = await client.store
|
||||
.getRoomList(onlyLeft: false, onlyGroups: false, onlyDirect: false);
|
||||
client.roomList = RoomList(
|
||||
client: client,
|
||||
onlyLeft: false,
|
||||
onlyDirect: false,
|
||||
onlyGroups: false,
|
||||
onUpdate: null,
|
||||
onInsert: null,
|
||||
onRemove: null,
|
||||
rooms: rooms);
|
||||
client.accountData = await client.store.getAccountData();
|
||||
client.presences = await client.store.getPresences();
|
||||
}
|
||||
|
||||
_userEventSub ??= onUserEvent.stream.listen(client.handleUserUpdate);
|
||||
|
||||
onLoginStateChanged.add(LoginState.logged);
|
||||
|
||||
_sync();
|
||||
}
|
||||
|
||||
StreamSubscription _userEventSub;
|
||||
|
||||
/// Resets all settings and stops the synchronisation.
|
||||
void clear() {
|
||||
client.store?.clear();
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
*/
|
||||
|
||||
import 'package:famedlysdk/src/AccountData.dart';
|
||||
import 'package:famedlysdk/src/RawEvent.dart';
|
||||
|
||||
class Presence extends AccountData {
|
||||
/// 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})
|
||||
: 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']);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -363,8 +363,7 @@ class Room {
|
|||
|
||||
/// Sets this room as a direct chat for this user.
|
||||
Future<dynamic> addToDirectChat(String userID) async {
|
||||
Map<String, List<String>> directChats =
|
||||
await client.store.getAccountDataDirectChats();
|
||||
Map<String, List<String>> directChats = client.directChats;
|
||||
if (directChats.containsKey(userID)) if (!directChats[userID].contains(id))
|
||||
directChats[userID].add(id);
|
||||
else
|
||||
|
|
|
@ -73,6 +73,13 @@ class RoomList {
|
|||
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) {
|
||||
// Update the chat list item.
|
||||
// Search the room in the rooms
|
||||
|
|
|
@ -25,6 +25,8 @@ import 'dart:async';
|
|||
import 'dart:convert';
|
||||
import 'dart:core';
|
||||
|
||||
import 'package:famedlysdk/src/AccountData.dart';
|
||||
import 'package:famedlysdk/src/Presence.dart';
|
||||
import 'package:famedlysdk/src/State.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
|
@ -398,6 +400,26 @@ class Store {
|
|||
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 {
|
||||
await db
|
||||
.rawDelete("DELETE FROM NotificationsCache WHERE chat_id=?", [roomID]);
|
||||
|
|
|
@ -89,7 +89,7 @@ class User extends State {
|
|||
/// Returns null on error.
|
||||
Future<String> startDirectChat() async {
|
||||
// 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;
|
||||
|
||||
// Start a new direct chat
|
||||
|
|
Loading…
Reference in a new issue