Browse Source

Fetch folder status

Vishnu Mohandas 5 years ago
parent
commit
2949d5858d
2 changed files with 48 additions and 14 deletions
  1. 22 12
      lib/folder_service.dart
  2. 26 2
      lib/models/folder.dart

+ 22 - 12
lib/folder_service.dart

@@ -1,5 +1,8 @@
+import 'dart:developer';
+
 import 'package:dio/dio.dart';
 import 'package:photos/core/configuration.dart';
+import 'package:photos/models/folder.dart';
 
 class FolderSharingService {
   final _dio = Dio();
@@ -9,8 +12,6 @@ class FolderSharingService {
       FolderSharingService._privateConstructor();
 
   Future<Map<String, bool>> getSharingStatus(String path) async {
-    // TODO fetch folderID from path
-    var folderID = 0;
     return _dio
         .get(
       Configuration.instance.getHttpEndpoint() + "/users",
@@ -20,17 +21,28 @@ class FolderSharingService {
         .then((usersResponse) {
       return _dio
           .get(
-        Configuration.instance.getHttpEndpoint() +
-            "/folders/folder/" +
-            folderID.toString(),
+        Configuration.instance.getHttpEndpoint() + "/folders/",
         options: Options(
             headers: {"X-Auth-Token": Configuration.instance.getToken()}),
       )
-          .then((sharedFoldersResponse) {
-        final sharedUsers =
-            (sharedFoldersResponse.data["sharedWith"] as List).toSet();
+          .then((foldersResponse) {
+        var folders = (foldersResponse.data as List)
+            .map((f) => Folder.fromMap(f))
+            .toList();
+        Folder sharedFolder;
+        for (var folder in folders) {
+          if (folder.owner == Configuration.instance.getUsername() &&
+              folder.deviceFolder == path) {
+            sharedFolder = folder;
+            break;
+          }
+        }
+        var sharedUsers = Set<String>();
+        if (sharedFolder != null) {
+          sharedUsers.addAll(sharedFolder.sharedWith);
+        }
         final result = Map<String, bool>();
-        (usersResponse.data as List).forEach((user) {
+        (usersResponse.data["users"] as List).forEach((user) {
           if (user != Configuration.instance.getUsername()) {
             result[user] = sharedUsers.contains(user);
           }
@@ -40,7 +52,5 @@ class FolderSharingService {
     });
   }
 
-  void shareFolder(
-    String path,
-  ) {}
+  void shareFolder(String path) {}
 }

+ 26 - 2
lib/models/folder.dart

@@ -6,6 +6,30 @@ class Folder {
   final List<String> sharedWith;
   final int updateTimestamp;
 
-  Folder(this.folderID, this.name, this.owner, this.deviceFolder,
-      this.sharedWith, this.updateTimestamp);
+  Folder(
+    this.folderID,
+    this.name,
+    this.owner,
+    this.deviceFolder,
+    this.sharedWith,
+    this.updateTimestamp,
+  );
+
+  static Folder fromMap(Map<String, dynamic> map) {
+    if (map == null) return null;
+
+    return Folder(
+      map['folderID'],
+      map['name'],
+      map['owner'],
+      map['deviceFolder'],
+      List<String>.from(map['sharedWith']),
+      map['updateTimestamp'],
+    );
+  }
+
+  @override
+  String toString() {
+    return 'Folder(folderID: $folderID, name: $name, owner: $owner, deviceFolder: $deviceFolder, sharedWith: $sharedWith, updateTimestamp: $updateTimestamp)';
+  }
 }