/*
* Famedly Matrix SDK
* Copyright (C) 2019, 2020 Famedly GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
import 'dart:async';
import 'dart:convert';
import 'dart:core';
import 'package:famedlysdk/famedlysdk.dart';
import 'package:famedlysdk/matrix_api.dart';
import 'package:famedlysdk/encryption.dart';
import 'package:famedlysdk/src/room.dart';
import 'package:famedlysdk/src/utils/device_keys_list.dart';
import 'package:famedlysdk/src/utils/matrix_file.dart';
import 'package:famedlysdk/src/utils/to_device_event.dart';
import 'package:http/http.dart' as http;
import 'package:pedantic/pedantic.dart';
import 'event.dart';
import 'room.dart';
import 'utils/event_update.dart';
import 'utils/room_update.dart';
import 'user.dart';
import 'database/database.dart' show Database;
typedef RoomSorter = int Function(Room a, Room b);
enum LoginState { logged, loggedOut }
/// Represents a Matrix client to communicate with a
/// [Matrix](https://matrix.org) homeserver and is the entry point for this
/// SDK.
class Client {
int _id;
int get id => _id;
Database database;
bool enableE2eeRecovery;
MatrixApi api;
Encryption encryption;
/// Create a client
/// clientName = unique identifier of this client
/// debug: Print debug output?
/// database: The database instance to use
/// enableE2eeRecovery: Enable additional logic to try to recover from bad e2ee sessions
Client(this.clientName,
{this.debug = false,
this.database,
this.enableE2eeRecovery = false,
http.Client httpClient}) {
api = MatrixApi(debug: debug, httpClient: httpClient);
onLoginStateChanged.stream.listen((loginState) {
if (debug) {
print('[LoginState]: ${loginState.toString()}');
}
});
}
/// Whether debug prints should be displayed.
final bool debug;
/// The required name for this client.
final String clientName;
/// The Matrix ID of the current logged user.
String get userID => _userID;
String _userID;
/// This points to the position in the synchronization history.
String prevBatch;
/// The device ID is an unique identifier for this device.
String get deviceID => _deviceID;
String _deviceID;
/// The device name is a human readable identifier for this device.
String get deviceName => _deviceName;
String _deviceName;
/// Returns the current login state.
bool isLogged() => api.accessToken != null;
/// A list of all rooms the user is participating or invited.
List get rooms => _rooms;
List _rooms = [];
/// Whether this client supports end-to-end encryption using olm.
bool get encryptionEnabled => encryption != null && encryption.enabled;
/// Whether this client is able to encrypt and decrypt files.
bool get fileEncryptionEnabled => encryptionEnabled && true;
String get identityKey => encryption?.identityKey ?? '';
String get fingerprintKey => encryption?.fingerprintKey ?? '';
/// Warning! This endpoint is for testing only!
set rooms(List newList) {
print('Warning! This endpoint is for testing only!');
_rooms = newList;
}
/// Key/Value store of account data.
Map accountData = {};
/// Presences of users by a given matrix ID
Map presences = {};
int _transactionCounter = 0;
@Deprecated('Use [api.request()] instead')
Future