Delete collections deleted on remote

This commit is contained in:
Vishnu Mohandas 2020-10-31 18:18:41 +05:30
parent 4dd4eed10c
commit 7d52d3c411
4 changed files with 51 additions and 20 deletions

View file

@ -20,7 +20,7 @@ class CollectionsDB {
static final columnType = 'type';
static final columnEncryptedPath = 'encrypted_path';
static final columnPathDecryptionNonce = 'path_decryption_nonce';
static final columnCreationTime = 'creation_time';
static final columnUpdationTime = 'updation_time';
CollectionsDB._privateConstructor();
static final CollectionsDB instance = CollectionsDB._privateConstructor();
@ -54,7 +54,7 @@ class CollectionsDB {
$columnType TEXT NOT NULL,
$columnEncryptedPath TEXT,
$columnPathDecryptionNonce TEXT,
$columnCreationTime TEXT NOT NULL
$columnUpdationTime TEXT NOT NULL
)
''');
}
@ -79,20 +79,29 @@ class CollectionsDB {
return collections;
}
Future<int> getLastCollectionCreationTime() async {
Future<int> getLastCollectionUpdationTime() async {
final db = await instance.database;
final rows = await db.query(
collectionsTable,
orderBy: '$columnCreationTime DESC',
orderBy: '$columnUpdationTime DESC',
limit: 1,
);
if (rows.isNotEmpty) {
return int.parse(rows[0][columnCreationTime]);
return int.parse(rows[0][columnUpdationTime]);
} else {
return null;
}
}
Future<int> deleteCollection(int collectionID) async {
final db = await instance.database;
return db.delete(
collectionsTable,
where: '$columnID = ?',
whereArgs: [collectionID],
);
}
Map<String, dynamic> _getRowForCollection(Collection collection) {
var row = new Map<String, dynamic>();
row[columnID] = collection.id;
@ -104,7 +113,7 @@ class CollectionsDB {
row[columnType] = Collection.typeToString(collection.type);
row[columnEncryptedPath] = collection.attributes.encryptedPath;
row[columnPathDecryptionNonce] = collection.attributes.pathDecryptionNonce;
row[columnCreationTime] = collection.creationTime;
row[columnUpdationTime] = collection.updationTime;
return row;
}
@ -120,7 +129,7 @@ class CollectionsDB {
CollectionAttributes(
encryptedPath: row[columnEncryptedPath],
pathDecryptionNonce: row[columnPathDecryptionNonce]),
int.parse(row[columnCreationTime]),
int.parse(row[columnUpdationTime]),
);
}
}

View file

@ -380,6 +380,15 @@ class FilesDB {
);
}
Future<int> deleteCollection(int collectionID) async {
final db = await instance.database;
return db.delete(
table,
where: '$columnCollectionID = ?',
whereArgs: [collectionID],
);
}
Future<int> removeFromCollection(int collectionID, List<int> fileIDs) async {
final db = await instance.database;
return db.delete(

View file

@ -9,7 +9,8 @@ class Collection {
final String name;
final CollectionType type;
final CollectionAttributes attributes;
final int creationTime;
final int updationTime;
final bool isDeleted;
Collection(
this.id,
@ -20,8 +21,9 @@ class Collection {
this.name,
this.type,
this.attributes,
this.creationTime,
);
this.updationTime, {
this.isDeleted = false,
});
static CollectionType typeFromString(String type) {
switch (type) {
@ -53,7 +55,7 @@ class Collection {
'name': name,
'type': typeToString(type),
'attributes': attributes?.toMap(),
'creationTime': creationTime,
'updationTime': updationTime,
};
}
@ -69,7 +71,8 @@ class Collection {
map['name'],
typeFromString(map['type']),
CollectionAttributes.fromMap(map['attributes']),
map['creationTime'],
map['updationTime'],
isDeleted: map['isDeleted'] ?? false,
);
}
@ -80,7 +83,7 @@ class Collection {
@override
String toString() {
return 'Collection(id: $id, ownerID: $ownerID, ownerEmail: $ownerEmail, encryptedKey: $encryptedKey, keyDecryptionNonce: $keyDecryptionNonce, name: $name, type: $type, attributes: $attributes, creationTime: $creationTime)';
return 'Collection(id: $id, ownerID: $ownerID, ownerEmail: $ownerEmail, encryptedKey: $encryptedKey, keyDecryptionNonce: $keyDecryptionNonce, name: $name, type: $type, attributes: $attributes, creationTime: $updationTime)';
}
@override
@ -96,7 +99,7 @@ class Collection {
o.name == name &&
o.type == type &&
o.attributes == attributes &&
o.creationTime == creationTime;
o.updationTime == updationTime;
}
@override
@ -109,7 +112,7 @@ class Collection {
name.hashCode ^
type.hashCode ^
attributes.hashCode ^
creationTime.hashCode;
updationTime.hashCode;
}
}

View file

@ -22,6 +22,7 @@ class CollectionsService {
final _logger = Logger("CollectionsService");
CollectionsDB _db;
FilesDB _filesDB;
Configuration _config;
final _localCollections = Map<String, Collection>();
final _collectionIDToCollections = Map<int, Collection>();
@ -29,6 +30,7 @@ class CollectionsService {
CollectionsService._privateConstructor() {
_db = CollectionsDB.instance;
_filesDB = FilesDB.instance;
_config = Configuration.instance;
}
@ -44,10 +46,19 @@ class CollectionsService {
Future<void> sync() async {
final lastCollectionCreationTime =
await _db.getLastCollectionCreationTime();
await _db.getLastCollectionUpdationTime();
final fetchedCollections =
await _fetchCollections(lastCollectionCreationTime ?? 0);
await _db.insert(fetchedCollections);
final updatedCollections = List<Collection>();
for (final collection in fetchedCollections) {
if (collection.isDeleted) {
await _filesDB.deleteCollection(collection.id);
await _db.deleteCollection(collection.id);
} else {
updatedCollections.add(collection);
}
}
await _db.insert(updatedCollections);
final collections = await _db.getAllCollections();
for (final collection in collections) {
_cacheCollectionAttributes(collection);
@ -212,7 +223,7 @@ class CollectionsService {
Options(headers: {"X-Auth-Token": Configuration.instance.getToken()}),
)
.then((value) async {
await FilesDB.instance.insertMultiple(files);
await _filesDB.insertMultiple(files);
Bus.instance.fire(CollectionUpdatedEvent(collectionID: collectionID));
SyncService.instance.syncWithRemote();
});
@ -233,8 +244,7 @@ class CollectionsService {
options:
Options(headers: {"X-Auth-Token": Configuration.instance.getToken()}),
);
await FilesDB.instance
.removeFromCollection(collectionID, params["fileIDs"]);
await _filesDB.removeFromCollection(collectionID, params["fileIDs"]);
Bus.instance.fire(CollectionUpdatedEvent(collectionID: collectionID));
SyncService.instance.syncWithRemote();
}