fix: Typo and wellknown parsing

This commit is contained in:
Christian Pauly 2021-03-01 11:35:00 +01:00
parent 512abfc476
commit 7d91cdac5e
8 changed files with 47 additions and 41 deletions

View File

@ -19,7 +19,7 @@ code_analyze:
image: cirrusci/flutter image: cirrusci/flutter
dependencies: [] dependencies: []
script: script:
- flutter format lib/ test/ test_driver/ --set-exit-if-changed - flutter format lib/ test/ --set-exit-if-changed
- flutter analyze - flutter analyze
build_api_doc: build_api_doc:

View File

@ -34,4 +34,4 @@ flutter format lib/**/*/*.dart
- Don't repeat yourself! Use local variables, functions, classes. - Don't repeat yourself! Use local variables, functions, classes.
- Write tests for new classes, functions and widgets. - Write tests for new classes, functions and widgets.
- Keep it simple stupid: https://en.wikipedia.org/wiki/KISS_principle - Keep it simple stupid: https://en.wikipedia.org/wiki/KISS_principle
- Describe all of your classes, methods and attributes using **dartdoc** comments. Read this for more informations: https://dart.dev/guides/language/effective-dart/documentation - Describe all of your classes, methods and attributes using **dartdoc** comments. Read this for more information: https://dart.dev/guides/language/effective-dart/documentation

View File

@ -55,7 +55,7 @@ export 'src/model/public_rooms_response.dart';
export 'src/model/push_rule_set.dart'; export 'src/model/push_rule_set.dart';
export 'src/model/pusher.dart'; export 'src/model/pusher.dart';
export 'src/model/request_token_response.dart'; export 'src/model/request_token_response.dart';
export 'src/model/room_alias_informations.dart'; export 'src/model/room_alias_information.dart';
export 'src/model/room_keys_info.dart'; export 'src/model/room_keys_info.dart';
export 'src/model/room_keys_keys.dart'; export 'src/model/room_keys_keys.dart';
export 'src/model/room_summary.dart'; export 'src/model/room_summary.dart';
@ -72,7 +72,7 @@ export 'src/model/timeline_history_response.dart';
export 'src/model/turn_server_credentials.dart'; export 'src/model/turn_server_credentials.dart';
export 'src/model/upload_key_signatures_response.dart'; export 'src/model/upload_key_signatures_response.dart';
export 'src/model/user_search_result.dart'; export 'src/model/user_search_result.dart';
export 'src/model/well_known_informations.dart'; export 'src/model/well_known_information.dart';
export 'src/model/who_is_info.dart'; export 'src/model/who_is_info.dart';
export 'src/model/auth/authentication_data.dart'; export 'src/model/auth/authentication_data.dart';
export 'src/model/auth/authentication_identifier.dart'; export 'src/model/auth/authentication_identifier.dart';

View File

@ -51,7 +51,7 @@ import 'model/public_rooms_response.dart';
import 'model/push_rule_set.dart'; import 'model/push_rule_set.dart';
import 'model/pusher.dart'; import 'model/pusher.dart';
import 'model/request_token_response.dart'; import 'model/request_token_response.dart';
import 'model/room_alias_informations.dart'; import 'model/room_alias_information.dart';
import 'model/room_keys_info.dart'; import 'model/room_keys_info.dart';
import 'model/room_keys_keys.dart'; import 'model/room_keys_keys.dart';
import 'model/server_capabilities.dart'; import 'model/server_capabilities.dart';
@ -66,7 +66,7 @@ import 'model/timeline_history_response.dart';
import 'model/turn_server_credentials.dart'; import 'model/turn_server_credentials.dart';
import 'model/upload_key_signatures_response.dart'; import 'model/upload_key_signatures_response.dart';
import 'model/user_search_result.dart'; import 'model/user_search_result.dart';
import 'model/well_known_informations.dart'; import 'model/well_known_information.dart';
import 'model/who_is_info.dart'; import 'model/who_is_info.dart';
enum RequestType { GET, POST, PUT, DELETE } enum RequestType { GET, POST, PUT, DELETE }
@ -233,14 +233,14 @@ class MatrixApi {
/// Gets discovery information about the domain. The file may include additional keys. /// Gets discovery information about the domain. The file may include additional keys.
/// https://matrix.org/docs/spec/client_server/r0.6.0#get-well-known-matrix-client /// https://matrix.org/docs/spec/client_server/r0.6.0#get-well-known-matrix-client
Future<WellKnownInformations> requestWellKnownInformations() async { Future<WellKnownInformation> requestWellKnownInformation() async {
var baseUrl = homeserver.toString(); var baseUrl = homeserver.toString();
if (baseUrl.endsWith('/')) { if (baseUrl.endsWith('/')) {
baseUrl = baseUrl.substring(0, baseUrl.length - 1); baseUrl = baseUrl.substring(0, baseUrl.length - 1);
} }
final response = await httpClient.get('$baseUrl/.well-known/matrix/client'); final response = await httpClient.get('$baseUrl/.well-known/matrix/client');
final rawJson = json.decode(response.body); final rawJson = json.decode(response.body);
return WellKnownInformations.fromJson(rawJson); return WellKnownInformation.fromJson(rawJson);
} }
Future<LoginTypes> requestLoginTypes() async { Future<LoginTypes> requestLoginTypes() async {
@ -883,13 +883,12 @@ class MatrixApi {
/// Requests that the server resolve a room alias to a room ID. /// Requests that the server resolve a room alias to a room ID.
/// https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-directory-room-roomalias /// https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-directory-room-roomalias
Future<RoomAliasInformations> requestRoomAliasInformations( Future<RoomAliasInformation> requestRoomAliasInformation(String alias) async {
String alias) async {
final response = await request( final response = await request(
RequestType.GET, RequestType.GET,
'/client/r0/directory/room/${Uri.encodeComponent(alias)}', '/client/r0/directory/room/${Uri.encodeComponent(alias)}',
); );
return RoomAliasInformations.fromJson(response); return RoomAliasInformation.fromJson(response);
} }
/// Remove a mapping of room alias to room ID. /// Remove a mapping of room alias to room ID.
@ -1726,7 +1725,7 @@ class MatrixApi {
/// Performs a full text search across different categories. /// Performs a full text search across different categories.
/// https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-search /// https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-search
/// Please note: The specification is not 100% clear what it is expecting and sending here. /// Please note: The specification is not 100% clear what it is expecting and sending here.
/// So we stick with pure json until we have more informations. /// So we stick with pure json until we have more information.
Future<Map<String, dynamic>> globalSearch(Map<String, dynamic> query) async { Future<Map<String, dynamic>> globalSearch(Map<String, dynamic> query) async {
return await request( return await request(
RequestType.POST, RequestType.POST,

View File

@ -21,21 +21,20 @@
* SOFTWARE. * SOFTWARE.
*/ */
import 'well_known_informations.dart'; import 'well_known_information.dart';
class LoginResponse { class LoginResponse {
String userId; String userId;
String accessToken; String accessToken;
String deviceId; String deviceId;
WellKnownInformations wellKnownInformations; WellKnownInformation wellKnownInformation;
LoginResponse.fromJson(Map<String, dynamic> json) { LoginResponse.fromJson(Map<String, dynamic> json) {
userId = json['user_id']; userId = json['user_id'];
accessToken = json['access_token']; accessToken = json['access_token'];
deviceId = json['device_id']; deviceId = json['device_id'];
if (json['well_known'] is Map) { if (json['well_known'] is Map) {
wellKnownInformations = wellKnownInformation = WellKnownInformation.fromJson(json['well_known']);
WellKnownInformations.fromJson(json['well_known']);
} }
} }
@ -44,8 +43,8 @@ class LoginResponse {
if (userId != null) data['user_id'] = userId; if (userId != null) data['user_id'] = userId;
if (accessToken != null) data['access_token'] = accessToken; if (accessToken != null) data['access_token'] = accessToken;
if (deviceId != null) data['device_id'] = deviceId; if (deviceId != null) data['device_id'] = deviceId;
if (wellKnownInformations != null) { if (wellKnownInformation != null) {
data['well_known'] = wellKnownInformations.toJson(); data['well_known'] = wellKnownInformation.toJson();
} }
return data; return data;
} }

View File

@ -21,11 +21,11 @@
* SOFTWARE. * SOFTWARE.
*/ */
class RoomAliasInformations { class RoomAliasInformation {
String roomId; String roomId;
List<String> servers; List<String> servers;
RoomAliasInformations.fromJson(Map<String, dynamic> json) { RoomAliasInformation.fromJson(Map<String, dynamic> json) {
roomId = json['room_id']; roomId = json['room_id'];
servers = json['servers'].cast<String>(); servers = json['servers'].cast<String>();
} }

View File

@ -21,25 +21,34 @@
* SOFTWARE. * SOFTWARE.
*/ */
class WellKnownInformations { import '../utils/try_get_map_extension.dart';
class WellKnownInformation {
MHomeserver mHomeserver; MHomeserver mHomeserver;
MHomeserver mIdentityServer; MHomeserver mIdentityServer;
Map<String, dynamic> content; Map<String, dynamic> content;
WellKnownInformations.fromJson(Map<String, dynamic> json) { WellKnownInformation.fromJson(Map<String, dynamic> json) {
content = json; content = json;
mHomeserver = json['m.homeserver'] != null final mHomeserverMap = json.tryGetMap<String, dynamic>('m.homeserver');
? MHomeserver.fromJson(json['m.homeserver']) if (mHomeserverMap != null) {
: null; mHomeserver = MHomeserver.fromJson(mHomeserverMap);
mIdentityServer = json['m.identity_server'] != null }
? MHomeserver.fromJson(json['m.identity_server']) final mIdentityServerMap =
: null; json.tryGetMap<String, dynamic>('m.identity_server');
if (mIdentityServerMap != null) {
mIdentityServer = MHomeserver.fromJson(mIdentityServerMap);
}
} }
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
final data = content; final data = content;
data['m.homeserver'] = mHomeserver.toJson(); if (mHomeserver != null) {
data['m.identity_server'] = mIdentityServer.toJson(); data['m.homeserver'] = mHomeserver.toJson();
}
if (mIdentityServer != null) {
data['m.identity_server'] = mIdentityServer.toJson();
}
return data; return data;
} }
} }
@ -48,12 +57,12 @@ class MHomeserver {
String baseUrl; String baseUrl;
MHomeserver.fromJson(Map<String, dynamic> json) { MHomeserver.fromJson(Map<String, dynamic> json) {
baseUrl = json['base_url']; baseUrl = json.tryGet<String>('base_url');
} }
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
final data = <String, dynamic>{}; final data = <String, dynamic>{};
data['base_url'] = baseUrl; if (baseUrl != null) data['base_url'] = baseUrl;
return data; return data;
} }
} }

View File

@ -129,13 +129,13 @@ void main() {
supportedVersions.toJson()); supportedVersions.toJson());
matrixApi.homeserver = null; matrixApi.homeserver = null;
}); });
test('getWellKnownInformations', () async { test('getWellKnownInformation', () async {
matrixApi.homeserver = Uri.parse('https://fakeserver.notexisting'); matrixApi.homeserver = Uri.parse('https://fakeserver.notexisting');
final wellKnownInformations = final wellKnownInformation =
await matrixApi.requestWellKnownInformations(); await matrixApi.requestWellKnownInformation();
expect(wellKnownInformations.mHomeserver.baseUrl, expect(wellKnownInformation.mHomeserver.baseUrl,
'https://fakeserver.notexisting'); 'https://fakeserver.notexisting');
expect(wellKnownInformations.toJson(), { expect(wellKnownInformation.toJson(), {
'm.homeserver': {'base_url': 'https://fakeserver.notexisting'}, 'm.homeserver': {'base_url': 'https://fakeserver.notexisting'},
'm.identity_server': { 'm.identity_server': {
'base_url': 'https://identity.fakeserver.notexisting' 'base_url': 'https://identity.fakeserver.notexisting'
@ -654,19 +654,18 @@ void main() {
matrixApi.homeserver = matrixApi.accessToken = null; matrixApi.homeserver = matrixApi.accessToken = null;
}); });
test('requestRoomAliasInformations', () async { test('requestRoomAliasInformation', () async {
matrixApi.homeserver = Uri.parse('https://fakeserver.notexisting'); matrixApi.homeserver = Uri.parse('https://fakeserver.notexisting');
matrixApi.accessToken = '1234'; matrixApi.accessToken = '1234';
final roomAliasInformations = final roomAliasInformation = await matrixApi.requestRoomAliasInformation(
await matrixApi.requestRoomAliasInformations(
'#testalias:example.com', '#testalias:example.com',
); );
expect( expect(
FakeMatrixApi.api['GET'] FakeMatrixApi.api['GET']
['/client/r0/directory/room/%23testalias%3Aexample.com']({}), ['/client/r0/directory/room/%23testalias%3Aexample.com']({}),
roomAliasInformations.toJson()); roomAliasInformation.toJson());
matrixApi.homeserver = matrixApi.accessToken = null; matrixApi.homeserver = matrixApi.accessToken = null;
}); });