|
@ -14,7 +14,7 @@ import '/utils/encryption/crypto_utils.dart'; |
|
|
import '/utils/storage/database.dart'; |
|
|
import '/utils/storage/database.dart'; |
|
|
import '/utils/strings.dart'; |
|
|
import '/utils/strings.dart'; |
|
|
|
|
|
|
|
|
Future<Conversation> createConversation(String title, List<Friend> friends) async { |
|
|
|
|
|
|
|
|
Future<Conversation> createConversation(String title, List<Friend> friends, bool twoUser) async { |
|
|
final db = await getDatabaseConnection(); |
|
|
final db = await getDatabaseConnection(); |
|
|
|
|
|
|
|
|
MyProfile profile = await MyProfile.getProfile(); |
|
|
MyProfile profile = await MyProfile.getProfile(); |
|
@ -30,6 +30,7 @@ Future<Conversation> createConversation(String title, List<Friend> friends) asyn |
|
|
symmetricKey: base64.encode(symmetricKey), |
|
|
symmetricKey: base64.encode(symmetricKey), |
|
|
admin: true, |
|
|
admin: true, |
|
|
name: title, |
|
|
name: title, |
|
|
|
|
|
twoUser: twoUser, |
|
|
status: ConversationStatus.pending, |
|
|
status: ConversationStatus.pending, |
|
|
isRead: true, |
|
|
isRead: true, |
|
|
); |
|
|
); |
|
@ -105,7 +106,7 @@ Conversation findConversationByDetailId(List<Conversation> conversations, String |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
// Or return `null`. |
|
|
// Or return `null`. |
|
|
throw ArgumentError.value(id, "id", "No element with that id"); |
|
|
|
|
|
|
|
|
throw ArgumentError.value(id, 'id', 'No element with that id'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
Future<Conversation> getConversationById(String id) async { |
|
|
Future<Conversation> getConversationById(String id) async { |
|
@ -127,6 +128,7 @@ Future<Conversation> getConversationById(String id) async { |
|
|
symmetricKey: maps[0]['symmetric_key'], |
|
|
symmetricKey: maps[0]['symmetric_key'], |
|
|
admin: maps[0]['admin'] == 1, |
|
|
admin: maps[0]['admin'] == 1, |
|
|
name: maps[0]['name'], |
|
|
name: maps[0]['name'], |
|
|
|
|
|
twoUser: maps[0]['two_user'] == 1, |
|
|
status: ConversationStatus.values[maps[0]['status']], |
|
|
status: ConversationStatus.values[maps[0]['status']], |
|
|
isRead: maps[0]['is_read'] == 1, |
|
|
isRead: maps[0]['is_read'] == 1, |
|
|
); |
|
|
); |
|
@ -145,12 +147,48 @@ Future<List<Conversation>> getConversations() async { |
|
|
symmetricKey: maps[i]['symmetric_key'], |
|
|
symmetricKey: maps[i]['symmetric_key'], |
|
|
admin: maps[i]['admin'] == 1, |
|
|
admin: maps[i]['admin'] == 1, |
|
|
name: maps[i]['name'], |
|
|
name: maps[i]['name'], |
|
|
|
|
|
twoUser: maps[i]['two_user'] == 1, |
|
|
status: ConversationStatus.values[maps[i]['status']], |
|
|
status: ConversationStatus.values[maps[i]['status']], |
|
|
isRead: maps[i]['is_read'] == 1, |
|
|
isRead: maps[i]['is_read'] == 1, |
|
|
); |
|
|
); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Future<Conversation?> getTwoUserConversation(String userId) async { |
|
|
|
|
|
final db = await getDatabaseConnection(); |
|
|
|
|
|
|
|
|
|
|
|
MyProfile profile = await MyProfile.getProfile(); |
|
|
|
|
|
|
|
|
|
|
|
print(userId); |
|
|
|
|
|
print(profile.id); |
|
|
|
|
|
|
|
|
|
|
|
final List<Map<String, dynamic>> maps = await db.rawQuery( |
|
|
|
|
|
''' |
|
|
|
|
|
SELECT conversations.* FROM conversations |
|
|
|
|
|
LEFT JOIN conversation_users ON conversation_users.conversation_id = conversations.id |
|
|
|
|
|
WHERE conversation_users.user_id = ? |
|
|
|
|
|
AND conversation_users.user_id != ? |
|
|
|
|
|
AND conversations.two_user = 1 |
|
|
|
|
|
''', |
|
|
|
|
|
[ userId, profile.id ], |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
if (maps.length != 1) { |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return Conversation( |
|
|
|
|
|
id: maps[0]['id'], |
|
|
|
|
|
userId: maps[0]['user_id'], |
|
|
|
|
|
symmetricKey: maps[0]['symmetric_key'], |
|
|
|
|
|
admin: maps[0]['admin'] == 1, |
|
|
|
|
|
name: maps[0]['name'], |
|
|
|
|
|
twoUser: maps[0]['two_user'] == 1, |
|
|
|
|
|
status: ConversationStatus.values[maps[0]['status']], |
|
|
|
|
|
isRead: maps[0]['is_read'] == 1, |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
class Conversation { |
|
|
class Conversation { |
|
|
String id; |
|
|
String id; |
|
@ -158,6 +196,7 @@ class Conversation { |
|
|
String symmetricKey; |
|
|
String symmetricKey; |
|
|
bool admin; |
|
|
bool admin; |
|
|
String name; |
|
|
String name; |
|
|
|
|
|
bool twoUser; |
|
|
ConversationStatus status; |
|
|
ConversationStatus status; |
|
|
bool isRead; |
|
|
bool isRead; |
|
|
|
|
|
|
|
@ -167,6 +206,7 @@ class Conversation { |
|
|
required this.symmetricKey, |
|
|
required this.symmetricKey, |
|
|
required this.admin, |
|
|
required this.admin, |
|
|
required this.name, |
|
|
required this.name, |
|
|
|
|
|
required this.twoUser, |
|
|
required this.status, |
|
|
required this.status, |
|
|
required this.isRead, |
|
|
required this.isRead, |
|
|
}); |
|
|
}); |
|
@ -194,6 +234,7 @@ class Conversation { |
|
|
symmetricKey: base64.encode(symmetricKeyDecrypted), |
|
|
symmetricKey: base64.encode(symmetricKeyDecrypted), |
|
|
admin: admin == 'true', |
|
|
admin: admin == 'true', |
|
|
name: 'Unknown', |
|
|
name: 'Unknown', |
|
|
|
|
|
twoUser: false, |
|
|
status: ConversationStatus.complete, |
|
|
status: ConversationStatus.complete, |
|
|
isRead: true, |
|
|
isRead: true, |
|
|
); |
|
|
); |
|
@ -251,6 +292,7 @@ class Conversation { |
|
|
'symmetric_key': symmetricKey, |
|
|
'symmetric_key': symmetricKey, |
|
|
'admin': admin ? 1 : 0, |
|
|
'admin': admin ? 1 : 0, |
|
|
'name': name, |
|
|
'name': name, |
|
|
|
|
|
'two_user': twoUser ? 1 : 0, |
|
|
'status': status.index, |
|
|
'status': status.index, |
|
|
'is_read': isRead ? 1 : 0, |
|
|
'is_read': isRead ? 1 : 0, |
|
|
}; |
|
|
}; |
|
@ -297,6 +339,28 @@ class Conversation { |
|
|
failedToSend: maps[0]['failed_to_send'] == 1, |
|
|
failedToSend: maps[0]['failed_to_send'] == 1, |
|
|
); |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Future<String> getName() async { |
|
|
|
|
|
if (!twoUser) { |
|
|
|
|
|
return name; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
MyProfile profile = await MyProfile.getProfile(); |
|
|
|
|
|
|
|
|
|
|
|
final db = await getDatabaseConnection(); |
|
|
|
|
|
|
|
|
|
|
|
List<Map<String, dynamic>> maps = await db.query( |
|
|
|
|
|
'conversation_users', |
|
|
|
|
|
where: 'conversation_id = ? AND user_id != ?', |
|
|
|
|
|
whereArgs: [ id, profile.id ], |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
if (maps.length != 1) { |
|
|
|
|
|
throw ArgumentError('Invalid user id'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return maps[0]['username']; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|