Update collection owner structure

This commit is contained in:
Vishnu Mohandas 2020-10-31 21:41:43 +05:30
parent 8fdbefc3d7
commit 46e5993de4
7 changed files with 85 additions and 24 deletions

View file

@ -14,6 +14,7 @@ class CollectionsDB {
static final columnID = 'collection_id';
static final columnOwnerID = 'owner_id';
static final columnOwnerEmail = 'owner_email';
static final columnOwnerName = 'owner_name';
static final columnEncryptedKey = 'encrypted_key';
static final columnKeyDecryptionNonce = 'key_decryption_nonce';
static final columnName = 'name';
@ -48,6 +49,7 @@ class CollectionsDB {
$columnID INTEGER PRIMARY KEY NOT NULL,
$columnOwnerID INTEGER NOT NULL,
$columnOwnerEmail TEXT,
$columnOwnerName TEXT,
$columnEncryptedKey TEXT NOT NULL,
$columnKeyDecryptionNonce TEXT,
$columnName TEXT NOT NULL,
@ -105,8 +107,9 @@ class CollectionsDB {
Map<String, dynamic> _getRowForCollection(Collection collection) {
var row = new Map<String, dynamic>();
row[columnID] = collection.id;
row[columnOwnerID] = collection.ownerID;
row[columnOwnerEmail] = collection.ownerEmail;
row[columnOwnerID] = collection.owner.id;
row[columnOwnerEmail] = collection.owner.email;
row[columnOwnerName] = collection.owner.name;
row[columnEncryptedKey] = collection.encryptedKey;
row[columnKeyDecryptionNonce] = collection.keyDecryptionNonce;
row[columnName] = collection.name;
@ -120,8 +123,11 @@ class CollectionsDB {
Collection _convertToCollection(Map<String, dynamic> row) {
return Collection(
row[columnID],
row[columnOwnerID],
row[columnOwnerEmail],
CollectionOwner(
id: row[columnOwnerID],
email: row[columnOwnerEmail],
name: row[columnOwnerName],
),
row[columnEncryptedKey],
row[columnKeyDecryptionNonce],
row[columnName],

View file

@ -2,8 +2,7 @@ import 'dart:convert';
class Collection {
final int id;
final int ownerID;
final String ownerEmail;
final CollectionOwner owner;
final String encryptedKey;
final String keyDecryptionNonce;
final String name;
@ -14,8 +13,7 @@ class Collection {
Collection(
this.id,
this.ownerID,
this.ownerEmail,
this.owner,
this.encryptedKey,
this.keyDecryptionNonce,
this.name,
@ -49,7 +47,7 @@ class Collection {
Map<String, dynamic> toMap() {
return {
'id': id,
'ownerID': ownerID,
'owner': owner?.toMap(),
'encryptedKey': encryptedKey,
'keyDecryptionNonce': keyDecryptionNonce,
'name': name,
@ -64,8 +62,7 @@ class Collection {
return Collection(
map['id'],
map['ownerID'],
map['ownerEmail'],
CollectionOwner.fromMap(map['owner']),
map['encryptedKey'],
map['keyDecryptionNonce'],
map['name'],
@ -83,7 +80,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: $updationTime)';
return 'Collection(id: $id, owner: ${owner.toString()} encryptedKey: $encryptedKey, keyDecryptionNonce: $keyDecryptionNonce, name: $name, type: $type, attributes: $attributes, creationTime: $updationTime)';
}
@override
@ -92,8 +89,7 @@ class Collection {
return o is Collection &&
o.id == id &&
o.ownerID == ownerID &&
o.ownerEmail == ownerEmail &&
o.owner == owner &&
o.encryptedKey == encryptedKey &&
o.keyDecryptionNonce == keyDecryptionNonce &&
o.name == name &&
@ -105,8 +101,7 @@ class Collection {
@override
int get hashCode {
return id.hashCode ^
ownerID.hashCode ^
ownerEmail.hashCode ^
owner.hashCode ^
encryptedKey.hashCode ^
keyDecryptionNonce.hashCode ^
name.hashCode ^
@ -182,3 +177,66 @@ class CollectionAttributes {
@override
int get hashCode => encryptedPath.hashCode ^ pathDecryptionNonce.hashCode;
}
class CollectionOwner {
int id;
String email;
String name;
CollectionOwner({
this.id,
this.email,
this.name,
});
CollectionOwner copyWith({
int id,
String email,
String name,
}) {
return CollectionOwner(
id: id ?? this.id,
email: email ?? this.email,
name: name ?? this.name,
);
}
Map<String, dynamic> toMap() {
return {
'id': id,
'email': email,
'name': name,
};
}
factory CollectionOwner.fromMap(Map<String, dynamic> map) {
if (map == null) return null;
return CollectionOwner(
id: map['id'],
email: map['email'],
name: map['name'],
);
}
String toJson() => json.encode(toMap());
factory CollectionOwner.fromJson(String source) =>
CollectionOwner.fromMap(json.decode(source));
@override
String toString() => 'CollectionOwner(id: $id, email: $email, name: $name)';
@override
bool operator ==(Object o) {
if (identical(this, o)) return true;
return o is CollectionOwner &&
o.id == id &&
o.email == email &&
o.name == name;
}
@override
int get hashCode => id.hashCode ^ email.hashCode ^ name.hashCode;
}

View file

@ -127,7 +127,7 @@ class CollectionsService {
if (!_cachedKeys.containsKey(collectionID)) {
final collection = _collectionIDToCollections[collectionID];
var key;
if (collection.ownerID == _config.getUserID()) {
if (collection.owner.id == _config.getUserID()) {
final encryptedKey = Sodium.base642bin(collection.encryptedKey);
key = CryptoUtil.decryptSync(encryptedKey, _config.getKey(),
Sodium.base642bin(collection.keyDecryptionNonce));
@ -173,7 +173,6 @@ class CollectionsService {
final key = CryptoUtil.generateKey();
final encryptedKeyData = CryptoUtil.encryptSync(key, _config.getKey());
final collection = await createAndCacheCollection(Collection(
null,
null,
null,
Sodium.bin2base64(encryptedKeyData.encryptedData),
@ -195,7 +194,6 @@ class CollectionsService {
final encryptedPath =
CryptoUtil.encryptSync(utf8.encode(path), _config.getKey());
final collection = await createAndCacheCollection(Collection(
null,
null,
null,
Sodium.bin2base64(encryptedKeyData.encryptedData),

View file

@ -76,7 +76,6 @@ class FavoritesService {
await _collectionsService.createAndCacheCollection(Collection(
null,
null,
null,
Sodium.bin2base64(encryptedKeyData.encryptedData),
Sodium.bin2base64(encryptedKeyData.nonce),
"Favorites",

View file

@ -135,7 +135,7 @@ class _CollectionsGalleryWidgetState extends State<CollectionsGalleryWidget> {
final collections = collectionsService.getCollections();
final ownedCollectionIDs = List<int>();
for (final c in collections) {
if (c.ownerID == userID) {
if (c.owner.id == userID) {
ownedCollectionIDs.add(c.id);
}
}

View file

@ -146,7 +146,7 @@ class _CreateCollectionPageState extends State<CreateCollectionPage> {
final collectionsWithThumbnail = List<CollectionWithThumbnail>();
final collections = CollectionsService.instance.getCollections();
for (final c in collections) {
if (c.ownerID != Configuration.instance.getUserID()) {
if (c.owner.id != Configuration.instance.getUserID()) {
continue;
}
var thumbnail = await FilesDB.instance.getLatestFileInCollection(c.id);

View file

@ -43,7 +43,7 @@ class _SharedCollectionGalleryState extends State<SharedCollectionGallery> {
CollectionsDB.instance.getAllCollections().then((collections) async {
final c = List<CollectionWithThumbnail>();
for (final collection in collections) {
if (collection.ownerID == Configuration.instance.getUserID()) {
if (collection.owner.id == Configuration.instance.getUserID()) {
continue;
}
final thumbnail =
@ -117,7 +117,7 @@ class _SharedCollectionGalleryState extends State<SharedCollectionGallery> {
alignment: Alignment.bottomRight,
child: Container(
child: Text(
c.collection.ownerEmail.substring(0, 1),
c.collection.owner.name.substring(0, 1),
textAlign: TextAlign.center,
),
padding: EdgeInsets.all(8),