100 lines
4.5 KiB
Plaintext
100 lines
4.5 KiB
Plaintext
import 'dart:io';
|
|
import 'dart:convert';
|
|
import 'dart:math';
|
|
import 'package:meta/meta.dart';
|
|
|
|
class Matrix {
|
|
static int txid = 0;
|
|
// TODO Error handeling
|
|
static Future<String> request({@required Uri uri, @required final String method, final String accessToken, final String jsonPayload}) async {
|
|
final client = new HttpClient();
|
|
if (method == 'GET') {
|
|
requestMethod(Uri uri) => client.getUrl(uri);
|
|
return await requestsend(uri: uri, function: requestMethod, accessToken: accessToken, jsonPayload: jsonPayload);
|
|
} else if (method == 'POST') {
|
|
requestMethod(Uri uri) => client.postUrl(uri);
|
|
return await requestsend(uri: uri, function: requestMethod, accessToken: accessToken, jsonPayload: jsonPayload);
|
|
} else if (method == 'PUT') {
|
|
requestMethod(Uri uri) => client.putUrl(uri);
|
|
return await requestsend(uri: uri, function: requestMethod, accessToken: accessToken, jsonPayload: jsonPayload);
|
|
} else if (method == 'DELETE') {
|
|
requestMethod(Uri uri) => client.deleteUrl(uri);
|
|
return await requestsend(uri: uri, function: requestMethod, accessToken: accessToken, jsonPayload: jsonPayload);
|
|
} else {
|
|
throw method + ' aint no method i ever heard off, they speak english in ' + method + '?';
|
|
}
|
|
}
|
|
|
|
static Future<String> requestsend({@required Uri uri, @required final function, final String accessToken, final String jsonPayload}) async {
|
|
return await function(uri).then((req) {
|
|
if (accessToken != null) {
|
|
req.headers.add("Authorization", "Bearer " + accessToken);
|
|
}
|
|
if (jsonPayload != null) {
|
|
req.headers.contentType = new ContentType("application", "json", charset: "utf-8");
|
|
req.write(jsonPayload);
|
|
}
|
|
return req.close();
|
|
}).then((response) {
|
|
return response.transform(utf8.decoder).join();
|
|
});
|
|
}
|
|
|
|
static Future<void> sendMessage({@required Uri uri, @required final String accessToken, @required final String roomid, @required final String message}) async {
|
|
String t_txid = txid.toString();
|
|
txid = txid + 1;
|
|
var json = new JsonCodec();
|
|
String payload = '{"msgtype":"m.text","body":' + json.encode(message) + '}';
|
|
request(uri: uri.replace(path: '/_matrix/client/r0/rooms/' + Uri.encodeComponent(roomid) + '/send/m.room.message/' + t_txid), method: 'PUT', accessToken: accessToken, jsonPayload: payload).then((cont) {
|
|
print(cont);
|
|
});
|
|
}
|
|
|
|
static Future<List<String>> joinedRooms(final String server, final String accessToken) async {
|
|
return await request(uri: Uri.parse(server).replace(path: "/_matrix/client/r0/joined_rooms"), method: 'GET', accessToken: accessToken).then ((cont) {
|
|
var json = new JsonDecoder().convert(cont);
|
|
return json["joined_rooms"].cast<String>();
|
|
});
|
|
}
|
|
|
|
static Future<String> roomAtribute(final String server, final String accessToken, final String roomId, final String mxType, final String jsonType) async {
|
|
return await request(
|
|
uri: Uri.parse(server)
|
|
.replace(path: "/_matrix/client/r0/rooms/" + roomId + "/state/m.room." + mxType),
|
|
method: 'GET',
|
|
accessToken: accessToken).then((cont) {
|
|
var json = new JsonDecoder().convert(cont);
|
|
return json[jsonType];
|
|
});
|
|
}
|
|
|
|
static Future<List<String>> supportedVersions(String server) async {
|
|
return await request(
|
|
uri: Uri.parse(server)
|
|
.replace(path: "/_matrix/client/versions"),
|
|
method: 'GET',
|
|
).then((cont) {
|
|
var json = new JsonDecoder().convert(cont);
|
|
return json["versions"].cast<String>();
|
|
});
|
|
}
|
|
|
|
static Future<void> logout({@required Uri uri, @required final String accessToken}) async {
|
|
request(uri: uri.replace(path:"/_matrix/client/r0/logout"), method: "POST", accessToken: accessToken);
|
|
}
|
|
|
|
static Future<void> sync({@required Uri uri, @required final String accessToken}) async {
|
|
request(uri: uri.replace(path:"/_matrix/client/r0/sync"), method: 'GET', accessToken: accessToken).then((cont) {
|
|
var json = new JsonDecoder().convert(cont);
|
|
print(json["rooms"]["join"]);
|
|
});
|
|
}
|
|
static Future<List<dynamic>> roomsync({@required Uri uri, @required final String accessToken, @required final String roomid, final String paginationToken}) async {
|
|
return await request(uri: uri.replace(path:"/_matrix/client/r0/rooms/" + Uri.encodeComponent(roomid) + "/initialSync"), method: 'GET', accessToken: accessToken).then((cont) {
|
|
var json = new JsonDecoder().convert(cont);
|
|
var john = json["messages"]["chunk"];
|
|
return john;
|
|
});
|
|
}
|
|
}
|