[Client] Add profile getter

This commit is contained in:
Christian Pauly 2019-11-30 10:36:30 +01:00
parent 99a7427cb9
commit c566948be5
4 changed files with 46 additions and 0 deletions

View File

@ -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<Profile> 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,

View File

@ -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<String, dynamic> content;
Profile.fromJson(Map<String, dynamic> json)
: avatarUrl = MxContent(json['avatar_url']),
displayname = json['displayname'],
content = json;
}

View File

@ -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<LoginState> loginStateFuture =
matrix.connection.onLoginStateChanged.stream.first;

View File

@ -234,6 +234,10 @@ class FakeMatrixApi extends MockClient {
static final Map<String, Map<String, dynamic>> 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",