From 5fbeceb2171cad18ef91df8e02c4babdcdf384b1 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Tue, 14 Jan 2020 11:27:26 +0000 Subject: [PATCH] [SDK] Refactoring --- lib/src/client.dart | 8 +++++++- lib/src/event.dart | 17 ++++------------- lib/src/room.dart | 6 +++--- lib/src/utils/matrix_exception.dart | 10 ++++++++-- test/client_test.dart | 5 ----- test/event_test.dart | 4 ++-- test/fake_matrix_api.dart | 6 +++++- test/timeline_test.dart | 2 +- 8 files changed, 30 insertions(+), 28 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index c4bb8ed..82669a4 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -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 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 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; // 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) { diff --git a/lib/src/event.dart b/lib/src/event.dart index 94cde99..4c7d13e 100644 --- a/lib/src/event.dart +++ b/lib/src/event.dart @@ -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; diff --git a/lib/src/room.dart b/lib/src/room.dart index da4854c..0ad6120 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -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 ""; } diff --git a/lib/src/utils/matrix_exception.dart b/lib/src/utils/matrix_exception.dart index f542148..44f447e 100644 --- a/lib/src/utils/matrix_exception.dart +++ b/lib/src/utils/matrix_exception.dart @@ -47,10 +47,16 @@ class MatrixException implements Exception { final Map 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; diff --git a/test/client_test.dart b/test/client_test.dart index 562f32a..ebbe3de 100644 --- a/test/client_test.dart +++ b/test/client_test.dart @@ -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", diff --git a/test/event_test.dart b/test/event_test.dart index d7e50d8..f46bc81 100644 --- a/test/event_test.dart +++ b/test/event_test.dart @@ -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; diff --git a/test/fake_matrix_api.dart b/test/fake_matrix_api.dart index 43314b3..3eb58ff 100644 --- a/test/fake_matrix_api.dart +++ b/test/fake_matrix_api.dart @@ -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 messagesResponse = { diff --git a/test/timeline_test.dart b/test/timeline_test.dart index 495e2c8..92ac2f8 100644 --- a/test/timeline_test.dart +++ b/test/timeline_test.dart @@ -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,