Merge branch 'presence-enhance-features' into 'master'

[Presence] Enhance presences

See merge request famedly/famedlysdk!130
This commit is contained in:
Christian Pauly 2019-12-04 09:39:07 +00:00
commit 579570a19e
5 changed files with 83 additions and 17 deletions

View File

@ -21,23 +21,34 @@
* along with famedlysdk. If not, see <http://www.gnu.org/licenses/>.
*/
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<String, dynamic> 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<String, dynamic> jsonPayload) {
final Map<String, dynamic> 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<String, dynamic> 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'];
}

View File

@ -21,6 +21,7 @@
* along with famedlysdk. If not, see <http://www.gnu.org/licenses/>.
*/
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;

View File

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

View File

@ -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);

51
test/Presence_test.dart Normal file
View File

@ -0,0 +1,51 @@
/*
* Copyright (c) 2019 Zender & Kurtz GbR.
*
* Authors:
* Christian Pauly <krille@famedly.com>
* Marcel Radzio <mtrnord@famedly.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
import 'package:famedlysdk/famedlysdk.dart';
import 'package:test/test.dart';
void main() {
/// All Tests related to the ChatTime
group("Presence", () {
test("fromJson", () async {
Map<String, dynamic> 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");
});
});
}