diff --git a/lib/src/Presence.dart b/lib/src/Presence.dart index 124087a7..98b925b3 100644 --- a/lib/src/Presence.dart +++ b/lib/src/Presence.dart @@ -21,23 +21,34 @@ * along with famedlysdk. If not, see . */ -import 'package:famedlysdk/src/AccountData.dart'; -import 'package:famedlysdk/src/RoomState.dart'; +import 'package:famedlysdk/famedlysdk.dart'; -class Presence extends AccountData { - /// The user who has sent this event if it is not a global account data event. +enum PresenceType { online, offline, unavailable } + +/// Informs the client of a user's presence state change. +class Presence { + /// The user who sent this presence. final String sender; - Presence({this.sender, Map content, String typeKey}) - : super(content: content, typeKey: typeKey); + /// The current display name for this user, if any. + final String displayname; - /// Get a State event from a table row or from the event stream. - factory Presence.fromJson(Map jsonPayload) { - final Map content = - RoomState.getMapFromPayload(jsonPayload['content']); - return Presence( - content: content, - typeKey: jsonPayload['type'], - sender: jsonPayload['sender']); - } + /// The current avatar URL for this user, if any. + final MxContent avatarUrl; + final bool currentlyActive; + final int lastActiveAgo; + final PresenceType presence; + final String statusMsg; + + Presence.fromJson(Map json) + : sender = json['sender'], + displayname = json['content']['avatar_url'], + avatarUrl = MxContent(json['content']['avatar_url']), + currentlyActive = json['content']['currently_active'], + lastActiveAgo = json['content']['last_active_ago'], + presence = PresenceType.values.firstWhere( + (e) => + e.toString() == "PresenceType.${json['content']['presence']}", + orElse: () => null), + statusMsg = json['content']['status_msg']; } diff --git a/lib/src/User.dart b/lib/src/User.dart index bf8b6c1f..1ed74c7e 100644 --- a/lib/src/User.dart +++ b/lib/src/User.dart @@ -21,6 +21,7 @@ * along with famedlysdk. If not, see . */ +import 'package:famedlysdk/famedlysdk.dart'; import 'package:famedlysdk/src/Room.dart'; import 'package:famedlysdk/src/RoomState.dart'; import 'package:famedlysdk/src/responses/ErrorResponse.dart'; @@ -166,6 +167,9 @@ class User extends RoomState { return newRoomID; } + /// The newest presence of this user if there is any and null if not. + Presence get presence => room.client.presences[id]; + /// Whether the client is allowed to ban/unban this user. bool get canBan => room.canBan && powerLevel < room.ownPowerLevel; diff --git a/lib/src/utils/MxContent.dart b/lib/src/utils/MxContent.dart index 05f9bc7e..fd2984e4 100644 --- a/lib/src/utils/MxContent.dart +++ b/lib/src/utils/MxContent.dart @@ -32,7 +32,7 @@ class MxContent { MxContent(String mxcUrl) : this._mxc = mxcUrl ?? ""; /// Returns the mxc uri. - get mxc => _mxc; + String get mxc => _mxc; /// Returns a download Link to this content. String getDownloadLink(Client matrix) => matrix.homeserver != null diff --git a/test/Client_test.dart b/test/Client_test.dart index 216a7b0f..fb0be17d 100644 --- a/test/Client_test.dart +++ b/test/Client_test.dart @@ -145,7 +145,7 @@ void main() { expect(contacts.length, 1); expect(contacts[0].senderId, "@alice:example.org"); expect( - matrix.presences["@alice:example.com"].content["presence"], "online"); + matrix.presences["@alice:example.com"].presence, PresenceType.online); expect(presenceCounter, 1); expect(accountDataCounter, 2); diff --git a/test/Presence_test.dart b/test/Presence_test.dart new file mode 100644 index 00000000..e0ace9c9 --- /dev/null +++ b/test/Presence_test.dart @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2019 Zender & Kurtz GbR. + * + * Authors: + * Christian Pauly + * Marcel Radzio + * + * This file is part of famedlysdk. + * + * famedlysdk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * famedlysdk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with famedlysdk. If not, see . + */ + +import 'package:famedlysdk/famedlysdk.dart'; +import 'package:test/test.dart'; + +void main() { + /// All Tests related to the ChatTime + group("Presence", () { + test("fromJson", () async { + Map rawPresence = { + "content": { + "avatar_url": "mxc://localhost:wefuiwegh8742w", + "currently_active": false, + "last_active_ago": 2478593, + "presence": "online", + "status_msg": "Making cupcakes" + }, + "sender": "@example:localhost", + "type": "m.presence" + }; + Presence presence = Presence.fromJson(rawPresence); + expect(presence.sender, "@example:localhost"); + expect(presence.avatarUrl.mxc, "mxc://localhost:wefuiwegh8742w"); + expect(presence.currentlyActive, false); + expect(presence.lastActiveAgo, 2478593); + expect(presence.presence, PresenceType.online); + expect(presence.statusMsg, "Making cupcakes"); + }); + }); +}