Add logic to update sortOrder on remote

Signed-off-by: Neeraj Gupta <254676+ua741@users.noreply.github.com>
This commit is contained in:
Neeraj Gupta 2023-05-26 08:03:58 +05:30
parent 690fb935ae
commit 4f99062662
4 changed files with 85 additions and 1 deletions

View file

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

View file

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

View file

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

View file

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