Merge branch 'sdk-refactor-api' into 'master'

[SDK] Refactoring

See merge request famedly/famedlysdk!163
This commit is contained in:
Christian Pauly 2020-01-14 11:27:27 +00:00
commit bb6b5dfe3f
8 changed files with 30 additions and 28 deletions

View file

@ -413,6 +413,7 @@ class Client {
/// Fetches the pushrules for the logged in user.
/// These are needed for notifications on Android
@Deprecated("Use [pushRules] instead.")
Future<PushRules> getPushrules() async {
final dynamic resp = await this.jsonRequest(
type: HTTPType.GET,
@ -422,6 +423,11 @@ class Client {
return PushRules.fromJson(resp);
}
/// Returns the push rules for the logged in user.
PushRules get pushRules => accountData.containsKey("m.push_rules")
? PushRules.fromJson(accountData["m.push_rules"].content)
: null;
/// This endpoint allows the creation, modification and deletion of pushers for this user ID.
Future<void> setPushers(String pushKey, String kind, String appId,
String appDisplayName, String deviceDisplayName, String lang, String url,
@ -661,7 +667,7 @@ class Client {
jsonResp = jsonDecode(String.fromCharCodes(resp.body.runes))
as Map<String, dynamic>; // May throw FormatException
if (jsonResp.containsKey("errcode") && jsonResp["errcode"] is String) {
if (resp.statusCode >= 400 && resp.statusCode < 500) {
// The server has responsed with an matrix related error.
MatrixException exception = MatrixException(resp);
if (exception.error == MatrixError.M_UNKNOWN_TOKEN) {

View file

@ -160,18 +160,6 @@ class Event {
return data;
}
Event get timelineEvent => Event(
content: content,
typeKey: typeKey,
eventId: eventId,
room: room,
roomId: roomId,
senderId: senderId,
time: time,
unsigned: unsigned,
status: 1,
);
/// The unique key of this event. For events with a [stateKey], it will be the
/// stateKey. Otherwise it will be the [type] as a string.
@deprecated
@ -318,8 +306,11 @@ class Event {
/// Returns the formatted boy of this event if it has a formatted body.
String get formattedText => content["formatted_body"] ?? "";
@Deprecated("Use [body] instead.")
String getBody() => body;
/// Use this to get the body.
String getBody() {
String get body {
if (redacted) return "Redacted";
if (text != "") return text;
if (formattedText != "") return formattedText;

View file

@ -152,7 +152,7 @@ class Room {
Event get lastEvent {
DateTime lastTime = DateTime.fromMillisecondsSinceEpoch(0);
Event lastEvent = getState("m.room.message")?.timelineEvent;
Event lastEvent = getState("m.room.message");
if (lastEvent == null) {
states.forEach((final String key, final entry) {
if (!entry.containsKey("")) return;
@ -161,7 +161,7 @@ class Room {
state.time.millisecondsSinceEpoch >
lastTime.millisecondsSinceEpoch) {
lastTime = state.time;
lastEvent = state.timelineEvent;
lastEvent = state;
}
});
}
@ -241,7 +241,7 @@ class Room {
/// The last message sent to this room.
String get lastMessage {
if (lastEvent != null) {
return lastEvent.getBody();
return lastEvent.body;
} else {
return "";
}

View file

@ -47,10 +47,16 @@ class MatrixException implements Exception {
final Map<String, dynamic> raw;
/// The unique identifier for this error.
String get errcode => raw["errcode"];
String get errcode =>
raw["errcode"] ??
(requireAdditionalAuthentication ? "M_FORBIDDEN" : "M_UNKNOWN");
/// A human readable error description.
String get errorMessage => raw["error"];
String get errorMessage =>
raw["error"] ??
(requireAdditionalAuthentication
? "Require additional authentication"
: "Unknown error");
/// The frozen request which triggered this Error
http.Response response;

View file

@ -347,11 +347,6 @@ void main() {
await matrix.setAvatar(testFile);
});
test('getPushrules', () async {
final pushrules = await matrix.getPushrules();
expect(pushrules != null, true);
});
test('setPushers', () async {
await matrix.setPushers("abcdefg", "http", "com.famedly.famedlysdk",
"famedlySDK", "GitLabCi", "en", "https://examplepushserver.com",

View file

@ -65,13 +65,13 @@ void main() {
expect(event.status, 2);
expect(event.text, body);
expect(event.formattedText, formatted_body);
expect(event.getBody(), body);
expect(event.body, body);
expect(event.type, EventTypes.Message);
jsonObj["state_key"] = "";
Event state = Event.fromJson(jsonObj, null);
expect(state.eventId, id);
expect(state.stateKey, "");
expect(state.timelineEvent.status, 1);
expect(state.status, 2);
});
test("Test all EventTypes", () async {
Event event;

View file

@ -54,6 +54,9 @@ class FakeMatrixApi extends MockClient {
// Call API
if (api.containsKey(method) && api[method].containsKey(action)) {
res = api[method][action](data);
if (res.containsKey("errcode")) {
return Response(json.encode(res), 405);
}
} else if (method == "GET" &&
action.contains("/client/r0/rooms/") &&
action.contains("/state/m.room.member/")) {
@ -64,9 +67,10 @@ class FakeMatrixApi extends MockClient {
"errcode": "M_UNRECOGNIZED",
"error": "Unrecognized request"
};
return Response(json.encode(res), 405);
}
return Response(json.encode(res), 100);
return Response(json.encode(res), 200);
});
static Map<String, dynamic> messagesResponse = {

View file

@ -91,7 +91,7 @@ void main() {
expect(timeline.events[0].eventId, "1");
expect(timeline.events[0].sender.id, "@alice:example.com");
expect(timeline.events[0].time.millisecondsSinceEpoch, testTimeStamp);
expect(timeline.events[0].getBody(), "Testcase");
expect(timeline.events[0].body, "Testcase");
expect(
timeline.events[0].time.millisecondsSinceEpoch >
timeline.events[1].time.millisecondsSinceEpoch,