nc-photos/app/lib/entity/sharee/data_source.dart

74 lines
2.2 KiB
Dart
Raw Normal View History

2021-08-07 22:34:44 +02:00
import 'dart:convert';
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/api/entity_converter.dart';
2021-08-07 22:34:44 +02:00
import 'package:nc_photos/entity/sharee.dart';
import 'package:nc_photos/exception.dart';
import 'package:nc_photos/np_api_util.dart';
import 'package:np_api/np_api.dart' as api;
2022-12-16 16:01:04 +01:00
import 'package:np_codegen/np_codegen.dart';
import 'package:np_common/ci_string.dart';
import 'package:np_common/type.dart';
2021-08-07 22:34:44 +02:00
2022-12-16 16:01:04 +01:00
part 'data_source.g.dart';
@npLog
2021-08-07 22:34:44 +02:00
class ShareeRemoteDataSource implements ShareeDataSource {
@override
list(Account account) async {
_log.info("[list]");
final response =
await ApiUtil.fromAccount(account).ocs().filesSharing().sharees().get(
itemType: "file",
lookup: false,
);
2021-08-07 22:34:44 +02:00
if (!response.isGood) {
_log.severe("[list] Failed requesting server: $response");
throw ApiException(
response: response,
2022-07-21 07:45:49 +02:00
message:
"Server responed with an error: HTTP ${response.statusCode}");
2021-08-07 22:34:44 +02:00
}
final apiShares = await api.ShareeParser().parse(response.body);
return apiShares.map(ApiShareeConverter.fromApi).toList();
2021-08-07 22:34:44 +02:00
}
}
2022-12-16 16:01:04 +01:00
@npLog
2021-08-07 22:34:44 +02:00
class _ShareeParser {
List<Sharee> call(JsonObj json) {
final JsonObj dataJson = json["ocs"]["data"];
final product = <Sharee>[];
for (final kt in _keyTypes.entries) {
for (final u in dataJson[kt.key] ?? []) {
try {
product.add(Sharee(
type: kt.value,
label: u["label"],
shareType: u["value"]["shareType"],
2021-11-12 22:13:02 +01:00
shareWith: CiString(u["value"]["shareWith"]),
2021-08-07 22:34:44 +02:00
shareWithDisplayNameUnique: u["shareWithDisplayNameUnique"],
));
} catch (e) {
_log.severe("[list] Failed parsing json: ${jsonEncode(u)}", e);
}
}
}
return product;
}
static const _keyTypes = {
"users": ShareeType.user,
"groups": ShareeType.group,
"remotes": ShareeType.remote,
"remote_groups": ShareeType.remoteGroup,
"emails": ShareeType.email,
"circles": ShareeType.circle,
"rooms": ShareeType.room,
"deck": ShareeType.deck,
"lookup": ShareeType.lookup,
};
}