[SDK] Refactoring
This commit is contained in:
parent
4fe18fe1e3
commit
5fbeceb217
|
@ -413,6 +413,7 @@ class Client {
|
||||||
|
|
||||||
/// Fetches the pushrules for the logged in user.
|
/// Fetches the pushrules for the logged in user.
|
||||||
/// These are needed for notifications on Android
|
/// These are needed for notifications on Android
|
||||||
|
@Deprecated("Use [pushRules] instead.")
|
||||||
Future<PushRules> getPushrules() async {
|
Future<PushRules> getPushrules() async {
|
||||||
final dynamic resp = await this.jsonRequest(
|
final dynamic resp = await this.jsonRequest(
|
||||||
type: HTTPType.GET,
|
type: HTTPType.GET,
|
||||||
|
@ -422,6 +423,11 @@ class Client {
|
||||||
return PushRules.fromJson(resp);
|
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.
|
/// This endpoint allows the creation, modification and deletion of pushers for this user ID.
|
||||||
Future<void> setPushers(String pushKey, String kind, String appId,
|
Future<void> setPushers(String pushKey, String kind, String appId,
|
||||||
String appDisplayName, String deviceDisplayName, String lang, String url,
|
String appDisplayName, String deviceDisplayName, String lang, String url,
|
||||||
|
@ -661,7 +667,7 @@ class Client {
|
||||||
jsonResp = jsonDecode(String.fromCharCodes(resp.body.runes))
|
jsonResp = jsonDecode(String.fromCharCodes(resp.body.runes))
|
||||||
as Map<String, dynamic>; // May throw FormatException
|
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.
|
// The server has responsed with an matrix related error.
|
||||||
MatrixException exception = MatrixException(resp);
|
MatrixException exception = MatrixException(resp);
|
||||||
if (exception.error == MatrixError.M_UNKNOWN_TOKEN) {
|
if (exception.error == MatrixError.M_UNKNOWN_TOKEN) {
|
||||||
|
|
|
@ -160,18 +160,6 @@ class Event {
|
||||||
return data;
|
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
|
/// 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.
|
/// stateKey. Otherwise it will be the [type] as a string.
|
||||||
@deprecated
|
@deprecated
|
||||||
|
@ -318,8 +306,11 @@ class Event {
|
||||||
/// Returns the formatted boy of this event if it has a formatted body.
|
/// Returns the formatted boy of this event if it has a formatted body.
|
||||||
String get formattedText => content["formatted_body"] ?? "";
|
String get formattedText => content["formatted_body"] ?? "";
|
||||||
|
|
||||||
|
@Deprecated("Use [body] instead.")
|
||||||
|
String getBody() => body;
|
||||||
|
|
||||||
/// Use this to get the body.
|
/// Use this to get the body.
|
||||||
String getBody() {
|
String get body {
|
||||||
if (redacted) return "Redacted";
|
if (redacted) return "Redacted";
|
||||||
if (text != "") return text;
|
if (text != "") return text;
|
||||||
if (formattedText != "") return formattedText;
|
if (formattedText != "") return formattedText;
|
||||||
|
|
|
@ -152,7 +152,7 @@ class Room {
|
||||||
|
|
||||||
Event get lastEvent {
|
Event get lastEvent {
|
||||||
DateTime lastTime = DateTime.fromMillisecondsSinceEpoch(0);
|
DateTime lastTime = DateTime.fromMillisecondsSinceEpoch(0);
|
||||||
Event lastEvent = getState("m.room.message")?.timelineEvent;
|
Event lastEvent = getState("m.room.message");
|
||||||
if (lastEvent == null) {
|
if (lastEvent == null) {
|
||||||
states.forEach((final String key, final entry) {
|
states.forEach((final String key, final entry) {
|
||||||
if (!entry.containsKey("")) return;
|
if (!entry.containsKey("")) return;
|
||||||
|
@ -161,7 +161,7 @@ class Room {
|
||||||
state.time.millisecondsSinceEpoch >
|
state.time.millisecondsSinceEpoch >
|
||||||
lastTime.millisecondsSinceEpoch) {
|
lastTime.millisecondsSinceEpoch) {
|
||||||
lastTime = state.time;
|
lastTime = state.time;
|
||||||
lastEvent = state.timelineEvent;
|
lastEvent = state;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -241,7 +241,7 @@ class Room {
|
||||||
/// The last message sent to this room.
|
/// The last message sent to this room.
|
||||||
String get lastMessage {
|
String get lastMessage {
|
||||||
if (lastEvent != null) {
|
if (lastEvent != null) {
|
||||||
return lastEvent.getBody();
|
return lastEvent.body;
|
||||||
} else {
|
} else {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,10 +47,16 @@ class MatrixException implements Exception {
|
||||||
final Map<String, dynamic> raw;
|
final Map<String, dynamic> raw;
|
||||||
|
|
||||||
/// The unique identifier for this error.
|
/// 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.
|
/// 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
|
/// The frozen request which triggered this Error
|
||||||
http.Response response;
|
http.Response response;
|
||||||
|
|
|
@ -347,11 +347,6 @@ void main() {
|
||||||
await matrix.setAvatar(testFile);
|
await matrix.setAvatar(testFile);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('getPushrules', () async {
|
|
||||||
final pushrules = await matrix.getPushrules();
|
|
||||||
expect(pushrules != null, true);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('setPushers', () async {
|
test('setPushers', () async {
|
||||||
await matrix.setPushers("abcdefg", "http", "com.famedly.famedlysdk",
|
await matrix.setPushers("abcdefg", "http", "com.famedly.famedlysdk",
|
||||||
"famedlySDK", "GitLabCi", "en", "https://examplepushserver.com",
|
"famedlySDK", "GitLabCi", "en", "https://examplepushserver.com",
|
||||||
|
|
|
@ -65,13 +65,13 @@ void main() {
|
||||||
expect(event.status, 2);
|
expect(event.status, 2);
|
||||||
expect(event.text, body);
|
expect(event.text, body);
|
||||||
expect(event.formattedText, formatted_body);
|
expect(event.formattedText, formatted_body);
|
||||||
expect(event.getBody(), body);
|
expect(event.body, body);
|
||||||
expect(event.type, EventTypes.Message);
|
expect(event.type, EventTypes.Message);
|
||||||
jsonObj["state_key"] = "";
|
jsonObj["state_key"] = "";
|
||||||
Event state = Event.fromJson(jsonObj, null);
|
Event state = Event.fromJson(jsonObj, null);
|
||||||
expect(state.eventId, id);
|
expect(state.eventId, id);
|
||||||
expect(state.stateKey, "");
|
expect(state.stateKey, "");
|
||||||
expect(state.timelineEvent.status, 1);
|
expect(state.status, 2);
|
||||||
});
|
});
|
||||||
test("Test all EventTypes", () async {
|
test("Test all EventTypes", () async {
|
||||||
Event event;
|
Event event;
|
||||||
|
|
|
@ -54,6 +54,9 @@ class FakeMatrixApi extends MockClient {
|
||||||
// Call API
|
// Call API
|
||||||
if (api.containsKey(method) && api[method].containsKey(action)) {
|
if (api.containsKey(method) && api[method].containsKey(action)) {
|
||||||
res = api[method][action](data);
|
res = api[method][action](data);
|
||||||
|
if (res.containsKey("errcode")) {
|
||||||
|
return Response(json.encode(res), 405);
|
||||||
|
}
|
||||||
} else if (method == "GET" &&
|
} else if (method == "GET" &&
|
||||||
action.contains("/client/r0/rooms/") &&
|
action.contains("/client/r0/rooms/") &&
|
||||||
action.contains("/state/m.room.member/")) {
|
action.contains("/state/m.room.member/")) {
|
||||||
|
@ -64,9 +67,10 @@ class FakeMatrixApi extends MockClient {
|
||||||
"errcode": "M_UNRECOGNIZED",
|
"errcode": "M_UNRECOGNIZED",
|
||||||
"error": "Unrecognized request"
|
"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 = {
|
static Map<String, dynamic> messagesResponse = {
|
||||||
|
|
|
@ -91,7 +91,7 @@ void main() {
|
||||||
expect(timeline.events[0].eventId, "1");
|
expect(timeline.events[0].eventId, "1");
|
||||||
expect(timeline.events[0].sender.id, "@alice:example.com");
|
expect(timeline.events[0].sender.id, "@alice:example.com");
|
||||||
expect(timeline.events[0].time.millisecondsSinceEpoch, testTimeStamp);
|
expect(timeline.events[0].time.millisecondsSinceEpoch, testTimeStamp);
|
||||||
expect(timeline.events[0].getBody(), "Testcase");
|
expect(timeline.events[0].body, "Testcase");
|
||||||
expect(
|
expect(
|
||||||
timeline.events[0].time.millisecondsSinceEpoch >
|
timeline.events[0].time.millisecondsSinceEpoch >
|
||||||
timeline.events[1].time.millisecondsSinceEpoch,
|
timeline.events[1].time.millisecondsSinceEpoch,
|
||||||
|
|
Loading…
Reference in a new issue