2019-06-09 11:57:33 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2019-06-09 10:16:48 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:core';
|
|
|
|
import 'responses/ErrorResponse.dart';
|
|
|
|
import 'Connection.dart';
|
|
|
|
import 'Store.dart';
|
2019-06-11 11:09:26 +00:00
|
|
|
import 'User.dart';
|
2019-06-09 10:16:48 +00:00
|
|
|
|
2019-06-09 12:33:25 +00:00
|
|
|
/// Represents a Matrix client to communicate with a
|
2019-06-09 10:16:48 +00:00
|
|
|
/// [Matrix](https://matrix.org) homeserver and is the entry point for this
|
|
|
|
/// SDK.
|
|
|
|
class Client {
|
|
|
|
/// Handles the connection for this client.
|
|
|
|
Connection connection;
|
|
|
|
|
|
|
|
/// Optional persistent store for all data.
|
|
|
|
Store store;
|
|
|
|
|
2019-06-12 11:43:14 +00:00
|
|
|
Client(this.clientName, {this.debug = false}) {
|
2019-06-09 10:16:48 +00:00
|
|
|
connection = Connection(this);
|
|
|
|
|
2019-06-11 11:09:26 +00:00
|
|
|
if (this.clientName != "testclient") store = Store(this);
|
2019-06-09 10:16:48 +00:00
|
|
|
connection.onLoginStateChanged.stream.listen((loginState) {
|
|
|
|
print("LoginState: ${loginState.toString()}");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-12 11:43:14 +00:00
|
|
|
/// Whether debug prints should be displayed.
|
|
|
|
final bool debug;
|
|
|
|
|
2019-06-09 10:16:48 +00:00
|
|
|
/// The required name for this client.
|
|
|
|
final String clientName;
|
|
|
|
|
|
|
|
/// The homeserver this client is communicating with.
|
|
|
|
String homeserver;
|
|
|
|
|
|
|
|
/// The Matrix ID of the current logged user.
|
|
|
|
String userID;
|
|
|
|
|
|
|
|
/// This is the access token for the matrix client. When it is undefined, then
|
|
|
|
/// the user needs to sign in first.
|
|
|
|
String accessToken;
|
|
|
|
|
|
|
|
/// This points to the position in the synchronization history.
|
|
|
|
String prevBatch;
|
|
|
|
|
|
|
|
/// The device ID is an unique identifier for this device.
|
|
|
|
String deviceID;
|
|
|
|
|
|
|
|
/// The device name is a human readable identifier for this device.
|
|
|
|
String deviceName;
|
|
|
|
|
|
|
|
/// Which version of the matrix specification does this server support?
|
|
|
|
List<String> matrixVersions;
|
|
|
|
|
|
|
|
/// Wheither the server supports lazy load members.
|
|
|
|
bool lazyLoadMembers = false;
|
|
|
|
|
|
|
|
/// Returns the current login state.
|
|
|
|
bool isLogged() => accessToken != 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].
|
|
|
|
Future<bool> checkServer(serverUrl) async {
|
|
|
|
homeserver = serverUrl;
|
|
|
|
|
|
|
|
final versionResp =
|
2019-06-11 11:09:26 +00:00
|
|
|
await connection.jsonRequest(type: "GET", action: "/client/versions");
|
2019-06-09 10:16:48 +00:00
|
|
|
if (versionResp is ErrorResponse) {
|
|
|
|
connection.onError.add(ErrorResponse(errcode: "NO_RESPONSE", error: ""));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
final List<String> versions = List<String>.from(versionResp["versions"]);
|
|
|
|
|
|
|
|
if (versions == null) {
|
|
|
|
connection.onError.add(ErrorResponse(errcode: "NO_RESPONSE", error: ""));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < versions.length; i++) {
|
|
|
|
if (versions[i] == "r0.4.0")
|
|
|
|
break;
|
|
|
|
else if (i == versions.length - 1) {
|
|
|
|
connection.onError.add(ErrorResponse(errcode: "NO_SUPPORT", error: ""));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
matrixVersions = versions;
|
|
|
|
|
|
|
|
if (versionResp.containsKey("unstable_features") &&
|
|
|
|
versionResp["unstable_features"].containsKey("m.lazy_load_members")) {
|
|
|
|
lazyLoadMembers = versionResp["unstable_features"]["m.lazy_load_members"]
|
|
|
|
? true
|
|
|
|
: false;
|
|
|
|
}
|
|
|
|
|
|
|
|
final loginResp =
|
2019-06-11 11:09:26 +00:00
|
|
|
await connection.jsonRequest(type: "GET", action: "/client/r0/login");
|
2019-06-09 10:16:48 +00:00
|
|
|
if (loginResp is ErrorResponse) {
|
|
|
|
connection.onError.add(loginResp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
final List<dynamic> flows = loginResp["flows"];
|
|
|
|
|
|
|
|
for (int i = 0; i < flows.length; i++) {
|
|
|
|
if (flows[i].containsKey("type") &&
|
|
|
|
flows[i]["type"] == "m.login.password")
|
|
|
|
break;
|
|
|
|
else if (i == flows.length - 1) {
|
|
|
|
connection.onError.add(ErrorResponse(errcode: "NO_SUPPORT", error: ""));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handles the login and allows the client to call all APIs which require
|
|
|
|
/// authentication. Returns false if the login was not successful.
|
|
|
|
Future<bool> login(String username, String password) async {
|
2019-06-11 11:09:26 +00:00
|
|
|
final loginResp = await connection
|
|
|
|
.jsonRequest(type: "POST", action: "/client/r0/login", data: {
|
2019-06-09 10:16:48 +00:00
|
|
|
"type": "m.login.password",
|
|
|
|
"user": username,
|
|
|
|
"identifier": {
|
|
|
|
"type": "m.id.user",
|
|
|
|
"user": username,
|
|
|
|
},
|
|
|
|
"password": password,
|
|
|
|
"initial_device_display_name": "Famedly Talk"
|
|
|
|
});
|
|
|
|
|
|
|
|
if (loginResp is ErrorResponse) {
|
|
|
|
connection.onError.add(loginResp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
final userID = loginResp["user_id"];
|
|
|
|
final accessToken = loginResp["access_token"];
|
|
|
|
if (userID == null || accessToken == null) {
|
|
|
|
connection.onError.add(ErrorResponse(errcode: "NO_SUPPORT", error: ""));
|
|
|
|
}
|
|
|
|
|
|
|
|
await connection.connect(
|
|
|
|
newToken: accessToken,
|
|
|
|
newUserID: userID,
|
|
|
|
newHomeserver: homeserver,
|
|
|
|
newDeviceName: "",
|
|
|
|
newDeviceID: "",
|
|
|
|
newMatrixVersions: matrixVersions,
|
|
|
|
newLazyLoadMembers: lazyLoadMembers);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sends a logout command to the homeserver and clears all local data,
|
|
|
|
/// including all persistent data from the store.
|
|
|
|
Future<void> logout() async {
|
2019-06-11 11:09:26 +00:00
|
|
|
final dynamic resp = await connection.jsonRequest(
|
|
|
|
type: "POST", action: "/client/r0/logout/all");
|
2019-06-14 06:09:37 +00:00
|
|
|
if (resp is ErrorResponse) connection.onError.add(resp);
|
2019-06-09 10:16:48 +00:00
|
|
|
|
|
|
|
await connection.clear();
|
|
|
|
}
|
|
|
|
|
2019-06-11 11:09:26 +00:00
|
|
|
/// Creates a new group chat and invites the given Users and returns the new
|
|
|
|
/// created room ID.
|
|
|
|
Future<String> createGroup(List<User> users) async {
|
|
|
|
List<String> inviteIDs = [];
|
|
|
|
for (int i = 0; i < users.length; i++) inviteIDs.add(users[i].id);
|
|
|
|
|
2019-06-14 06:09:37 +00:00
|
|
|
final dynamic resp = await connection.jsonRequest(
|
2019-06-11 11:09:26 +00:00
|
|
|
type: "POST",
|
|
|
|
action: "/client/r0/createRoom",
|
|
|
|
data: {"invite": inviteIDs, "preset": "private_chat"});
|
|
|
|
|
2019-06-14 06:09:37 +00:00
|
|
|
if (resp is ErrorResponse) {
|
|
|
|
connection.onError.add(resp);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-06-11 11:09:26 +00:00
|
|
|
return resp["room_id"];
|
|
|
|
}
|
2019-06-09 10:16:48 +00:00
|
|
|
}
|