This repository has been archived on 2020-05-24. You can view files and clone it, but cannot push or open issues or pull requests.
hermes/lib/matrix/cs-r0.5.0

100 lines
4.5 KiB
Plaintext
Raw Permalink Normal View History

2019-01-30 18:36:10 +00:00
import 'dart:io';
import 'dart:convert';
import 'dart:math';
2019-04-24 12:57:24 +00:00
import 'package:meta/meta.dart';
2019-01-30 18:36:10 +00:00
class Matrix {
2019-08-01 12:43:43 +00:00
static int txid = 0;
2019-01-30 18:36:10 +00:00
// TODO Error handeling
2019-04-24 12:57:24 +00:00
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') {
2019-04-24 15:46:35 +00:00
requestMethod(Uri uri) => client.getUrl(uri);
2019-04-24 12:57:24 +00:00
return await requestsend(uri: uri, function: requestMethod, accessToken: accessToken, jsonPayload: jsonPayload);
2019-04-24 15:46:35 +00:00
} 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);
2019-04-24 12:57:24 +00:00
} else {
2019-07-30 15:01:51 +00:00
throw method + ' aint no method i ever heard off, they speak english in ' + method + '?';
2019-04-24 12:57:24 +00:00
}
2019-04-24 15:46:35 +00:00
}
2019-04-24 12:57:24 +00:00
2019-07-30 12:53:18 +00:00
static Future<String> requestsend({@required Uri uri, @required final function, final String accessToken, final String jsonPayload}) async {
2019-04-24 12:57:24 +00:00
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 {
2019-08-01 12:43:43 +00:00
String t_txid = txid.toString();
txid = txid + 1;
var json = new JsonCodec();
String payload = '{"msgtype":"m.text","body":' + json.encode(message) + '}';
2019-08-01 12:43:43 +00:00
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);
});
}
2019-04-24 12:57:24 +00:00
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 {
2019-04-24 15:46:35 +00:00
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];
2019-04-16 02:47:19 +00:00
});
}
2019-04-24 15:46:35 +00:00
2019-04-24 12:57:24 +00:00
static Future<List<String>> supportedVersions(String server) async {
2019-04-24 15:46:35 +00:00
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>();
2019-01-30 18:36:10 +00:00
});
}
2019-07-30 12:53:18 +00:00
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);
}
2019-07-30 15:01:51 +00:00
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);
2019-11-04 11:32:40 +00:00
print(json["rooms"]["join"]);
2019-07-30 15:01:51 +00:00
});
}
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) {
2019-07-30 15:01:51 +00:00
var json = new JsonDecoder().convert(cont);
var john = json["messages"]["chunk"];
return john;
2019-07-30 15:01:51 +00:00
});
}
2019-01-30 18:36:10 +00:00
}