From c566948be56139e79473a92e01181632e0e9b5fc Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Sat, 30 Nov 2019 10:36:30 +0100 Subject: [PATCH] [Client] Add profile getter --- lib/src/Client.dart | 14 ++++++++++++++ lib/src/utils/Profile.dart | 18 ++++++++++++++++++ test/Client_test.dart | 10 ++++++++++ test/FakeMatrixApi.dart | 4 ++++ 4 files changed, 46 insertions(+) create mode 100644 lib/src/utils/Profile.dart diff --git a/lib/src/Client.dart b/lib/src/Client.dart index 344eb80..20d5df2 100644 --- a/lib/src/Client.dart +++ b/lib/src/Client.dart @@ -38,6 +38,7 @@ import 'User.dart'; import 'requests/SetPushersRequest.dart'; import 'responses/ErrorResponse.dart'; import 'responses/PushrulesResponse.dart'; +import 'utils/Profile.dart'; typedef AccountDataEventCB = void Function(AccountData accountData); typedef PresenceCB = void Function(Presence presence); @@ -257,6 +258,19 @@ class Client { await connection.clear(); } + /// Get the combined profile information for this user. This API may be used to + /// fetch the user's own profile information or other users; either locally + /// or on remote homeservers. + Future getProfileFromUserId(String userId) async { + final dynamic resp = await connection.jsonRequest( + type: HTTPType.GET, action: "/client/r0/profile/${userId}"); + if (resp is ErrorResponse) { + connection.onError.add(resp); + return null; + } + return Profile.fromJson(resp); + } + /// Creates a new [RoomList] object. RoomList getRoomList( {onRoomListUpdateCallback onUpdate, diff --git a/lib/src/utils/Profile.dart b/lib/src/utils/Profile.dart new file mode 100644 index 0000000..c17af43 --- /dev/null +++ b/lib/src/utils/Profile.dart @@ -0,0 +1,18 @@ +import 'package:famedlysdk/src/utils/MxContent.dart'; + +/// Represents a user profile returned by a /profile request. +class Profile { + /// The user's avatar URL if they have set one, otherwise null. + final MxContent avatarUrl; + + /// The user's display name if they have set one, otherwise null. + final String displayname; + + /// This API may return keys which are not limited to displayname or avatar_url. + final Map content; + + Profile.fromJson(Map json) + : avatarUrl = MxContent(json['avatar_url']), + displayname = json['displayname'], + content = json; +} diff --git a/test/Client_test.dart b/test/Client_test.dart index 13fbb66..216a7b0 100644 --- a/test/Client_test.dart +++ b/test/Client_test.dart @@ -36,6 +36,7 @@ import 'package:famedlysdk/src/sync/EventUpdate.dart'; import 'package:famedlysdk/src/sync/RoomUpdate.dart'; import 'package:famedlysdk/src/sync/UserUpdate.dart'; import 'package:famedlysdk/src/utils/MatrixFile.dart'; +import 'package:famedlysdk/src/utils/Profile.dart'; import 'package:test/test.dart'; import 'FakeMatrixApi.dart'; @@ -367,6 +368,15 @@ void main() { expect(archive.rooms[0].membership, Membership.leave); }); + test('getProfileFromUserId', () async { + final Profile profile = + await matrix.getProfileFromUserId("@getme:example.com"); + expect(profile.avatarUrl.mxc, "mxc://test"); + expect(profile.displayname, "You got me"); + expect(profile.content["avatar_url"], profile.avatarUrl.mxc); + expect(profile.content["displayname"], profile.displayname); + }); + test('Logout when token is unknown', () async { Future loginStateFuture = matrix.connection.onLoginStateChanged.stream.first; diff --git a/test/FakeMatrixApi.dart b/test/FakeMatrixApi.dart index c0d1a78..982f4cf 100644 --- a/test/FakeMatrixApi.dart +++ b/test/FakeMatrixApi.dart @@ -234,6 +234,10 @@ class FakeMatrixApi extends MockClient { static final Map> api = { "GET": { + "/client/r0/profile/@getme:example.com": (var req) => { + "avatar_url": "mxc://test", + "displayname": "You got me", + }, "/client/r0/rooms/!localpart:server.abc/state/m.room.member/@getme:example.com": (var req) => { "avatar_url": "mxc://test",