浏览代码

Add logic to update sortOrder on remote

Signed-off-by: Neeraj Gupta <254676+ua741@users.noreply.github.com>
Neeraj Gupta 2 年之前
父节点
当前提交
4f99062662

+ 9 - 0
lib/models/collection.dart

@@ -22,14 +22,23 @@ class Collection {
   final int updationTime;
   final bool isDeleted;
   String? mMdEncodedJson;
+  String? mMdPubEncodedJson;
   int mMdVersion = 0;
+  int mMbPubVersion = 0;
   CollectionMagicMetadata? _mmd;
+  CollectionPubMagicMetadata? _pubMmd;
 
   CollectionMagicMetadata get magicMetadata =>
       _mmd ?? CollectionMagicMetadata.fromEncodedJson(mMdEncodedJson ?? '{}');
 
+  CollectionPubMagicMetadata get pubMagicMetadata =>
+      _pubMmd ??
+      CollectionPubMagicMetadata.fromEncodedJson(mMdPubEncodedJson ?? '{}');
+
   set magicMetadata(val) => _mmd = val;
 
+  set pubMagicMetadata(val) => _pubMmd = val;
+
   Collection(
     this.id,
     this.owner,

+ 59 - 0
lib/services/collections_service.dart

@@ -610,6 +610,65 @@ class CollectionsService {
     }
   }
 
+  Future<void> updatePublicMagicMetadata(
+    Collection collection,
+    Map<String, dynamic> newMetadataUpdate,
+  ) async {
+    final int ownerID = Configuration.instance.getUserID()!;
+    try {
+      if (collection.owner?.id != ownerID) {
+        throw AssertionError("cannot modify albums not owned by you");
+      }
+      // read the existing magic metadata and apply new updates to existing data
+      // current update is simple replace. This will be enhanced in the future,
+      // as required.
+      final Map<String, dynamic> jsonToUpdate =
+          jsonDecode(collection.mMdPubEncodedJson ?? '{}');
+      newMetadataUpdate.forEach((key, value) {
+        jsonToUpdate[key] = value;
+      });
+
+      final key = getCollectionKey(collection.id);
+      final encryptedMMd = await CryptoUtil.encryptChaCha(
+        utf8.encode(jsonEncode(jsonToUpdate)) as Uint8List,
+        key,
+      );
+      // for required field, the json validator on golang doesn't treat 0 as valid
+      // value. Instead of changing version to ptr, decided to start version with 1.
+      final int currentVersion = max(collection.mMbPubVersion, 1);
+      final params = UpdateMagicMetadataRequest(
+        id: collection.id,
+        magicMetadata: MetadataRequest(
+          version: currentVersion,
+          count: jsonToUpdate.length,
+          data: CryptoUtil.bin2base64(encryptedMMd.encryptedData!),
+          header: CryptoUtil.bin2base64(encryptedMMd.header!),
+        ),
+      );
+      await _enteDio.put(
+        "/collections/public-magic-metadata",
+        data: params,
+      );
+      // update the local information so that it's reflected on UI
+      collection.mMdPubEncodedJson = jsonEncode(jsonToUpdate);
+      collection.pubMagicMetadata =
+          CollectionPubMagicMetadata.fromJson(jsonToUpdate);
+      collection.mMbPubVersion = currentVersion + 1;
+      _cacheCollectionAttributes(collection);
+      // trigger sync to fetch the latest collection state from server
+      sync().ignore();
+    } on DioError catch (e) {
+      if (e.response != null && e.response?.statusCode == 409) {
+        _logger.severe('collection magic data out of sync');
+        sync().ignore();
+      }
+      rethrow;
+    } catch (e, s) {
+      _logger.severe("failed to sync magic metadata", e, s);
+      rethrow;
+    }
+  }
+
   Future<void> createShareUrl(
     Collection collection, {
     bool enableCollect = false,

+ 1 - 1
lib/ui/viewer/gallery/gallery_app_bar_widget.dart

@@ -425,7 +425,7 @@ class _GalleryAppBarWidgetState extends State<GalleryAppBarWidget> {
       ],
     );
     if (sortByAsc != null) {
-      showShortToast(context, "coming soon");
+      changeSortOrder(bContext, widget.collection!, sortByAsc);
     }
   }
 

+ 16 - 0
lib/utils/magic_util.dart

@@ -77,6 +77,22 @@ Future<void> changeCollectionVisibility(
   }
 }
 
+Future<void> changeSortOrder(
+  BuildContext context,
+  Collection collection,
+  bool sortedInAscOrder,
+) async {
+  try {
+    final Map<String, dynamic> update = {"asc": sortedInAscOrder};
+    await CollectionsService.instance
+        .updatePublicMagicMetadata(collection, update);
+  } catch (e, s) {
+    _logger.severe("failed to update collection visibility", e, s);
+    showShortToast(context, S.of(context).somethingWentWrong);
+    rethrow;
+  }
+}
+
 Future<bool> editTime(
   BuildContext context,
   List<File> files,