Merge branch 'client-feature-device-management' into 'master'

[Client] Implement device management

See merge request famedly/famedlysdk!210
This commit is contained in:
Christian Pauly 2020-02-19 13:31:42 +00:00
commit 189ea6f50a
4 changed files with 110 additions and 0 deletions

View file

@ -36,6 +36,7 @@ import 'package:famedlysdk/src/utils/open_id_credentials.dart';
import 'package:famedlysdk/src/utils/session_key.dart';
import 'package:famedlysdk/src/utils/to_device_event.dart';
import 'package:famedlysdk/src/utils/turn_server_credentials.dart';
import 'package:famedlysdk/src/utils/user_device.dart';
import 'package:olm/olm.dart' as olm;
import 'package:pedantic/pedantic.dart';
import 'room.dart';
@ -1745,4 +1746,38 @@ class Client {
}
}
}
/// Gets information about all devices for the current user.
Future<List<UserDevice>> requestUserDevices() async {
final Map<String, dynamic> response =
await jsonRequest(type: HTTPType.GET, action: "/client/r0/devices");
List<UserDevice> userDevices = [];
for (final rawDevice in response["devices"]) {
userDevices.add(
UserDevice.fromJson(rawDevice, this),
);
}
return userDevices;
}
/// Gets information about all devices for the current user.
Future<UserDevice> requestUserDevice(String deviceId) async {
final Map<String, dynamic> response = await jsonRequest(
type: HTTPType.GET, action: "/client/r0/devices/$deviceId");
return UserDevice.fromJson(response, this);
}
/// Deletes the given devices, and invalidates any access token associated with them.
Future<void> deleteDevices(List<String> deviceIds,
{Map<String, dynamic> auth}) async {
await jsonRequest(
type: HTTPType.POST,
action: "/client/r0/delete_devices",
data: {
"devices": deviceIds,
if (auth != null) "auth": auth,
},
);
return;
}
}

View file

@ -0,0 +1,54 @@
import '../client.dart';
/// Registered device for this user.
class UserDevice {
/// Identifier of this device.
final String deviceId;
/// Display name set by the user for this device. Absent if no name has been set.
final String displayName;
/// The IP address where this device was last seen. (May be a few minutes out of date, for efficiency reasons).
final String lastSeenIp;
/// The time when this devices was last seen. (May be a few minutes out of date, for efficiency reasons).
final DateTime lastSeenTs;
final Client _client;
/// Updates the metadata on the given device.
Future<void> updateMetaData(String newName) async {
await _client.jsonRequest(
type: HTTPType.PUT,
action: "/client/r0/devices/$deviceId",
data: {"display_name": newName},
);
return;
}
/// Deletes the given device, and invalidates any access token associated with it.
Future<void> deleteDevice(Map<String, dynamic> auth) async {
await _client.jsonRequest(
type: HTTPType.DELETE,
action: "/client/r0/devices/$deviceId",
data: auth != null ? {"auth": auth} : null,
);
return;
}
UserDevice(
this._client, {
this.deviceId,
this.displayName,
this.lastSeenIp,
this.lastSeenTs,
});
UserDevice.fromJson(Map<String, dynamic> json, Client client)
: deviceId = json['device_id'],
displayName = json['display_name'],
lastSeenIp = json['last_seen_ip'],
lastSeenTs =
DateTime.fromMillisecondsSinceEpoch(json['last_seen_ts'] ?? 0),
_client = client;
}

View file

@ -36,6 +36,7 @@ import 'package:famedlysdk/src/sync/user_update.dart';
import 'package:famedlysdk/src/utils/matrix_exception.dart';
import 'package:famedlysdk/src/utils/matrix_file.dart';
import 'package:famedlysdk/src/utils/profile.dart';
import 'package:famedlysdk/src/utils/user_device.dart';
import 'package:olm/olm.dart' as olm;
import 'package:test/test.dart';
@ -448,6 +449,16 @@ void main() {
expect(resp["room_id"], roomID);
});
test('requestUserDevices', () async {
final List<UserDevice> userDevices = await matrix.requestUserDevices();
expect(userDevices.length, 1);
expect(userDevices.first.deviceId, "QBUAZIFURK");
expect(userDevices.first.displayName, "android");
expect(userDevices.first.lastSeenIp, "1.2.3.4");
expect(
userDevices.first.lastSeenTs.millisecondsSinceEpoch, 1474491775024);
});
test('get archive', () async {
List<Room> archive = await matrix.archive;

View file

@ -548,6 +548,16 @@ class FakeMatrixApi extends MockClient {
static final Map<String, Map<String, dynamic>> api = {
"GET": {
"/client/r0/devices": (var req) => {
"devices": [
{
"device_id": "QBUAZIFURK",
"display_name": "android",
"last_seen_ip": "1.2.3.4",
"last_seen_ts": 1474491775024
}
]
},
"/client/r0/rooms/1/state/m.room.member/@alice:example.com": (var req) =>
{"displayname": "Alice"},
"/client/r0/profile/@getme:example.com": (var req) => {