|
|
@ -14,7 +14,7 @@ import '/utils/encryption/crypto_utils.dart'; |
|
|
|
import '/utils/storage/database.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(); |
|
|
|
|
|
|
|
MyProfile profile = await MyProfile.getProfile(); |
|
|
@ -30,6 +30,7 @@ Future<Conversation> createConversation(String title, List<Friend> friends) asyn |
|
|
|
symmetricKey: base64.encode(symmetricKey), |
|
|
|
admin: true, |
|
|
|
name: title, |
|
|
|
twoUser: twoUser, |
|
|
|
status: ConversationStatus.pending, |
|
|
|
isRead: true, |
|
|
|
); |
|
|
@ -105,7 +106,7 @@ Conversation findConversationByDetailId(List<Conversation> conversations, String |
|
|
|
} |
|
|
|
} |
|
|
|
// 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 { |
|
|
@ -127,6 +128,7 @@ Future<Conversation> getConversationById(String id) async { |
|
|
|
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, |
|
|
|
); |
|
|
@ -145,12 +147,48 @@ Future<List<Conversation>> getConversations() async { |
|
|
|
symmetricKey: maps[i]['symmetric_key'], |
|
|
|
admin: maps[i]['admin'] == 1, |
|
|
|
name: maps[i]['name'], |
|
|
|
twoUser: maps[i]['two_user'] == 1, |
|
|
|
status: ConversationStatus.values[maps[i]['status']], |
|
|
|
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 { |
|
|
|
String id; |
|
|
@ -158,6 +196,7 @@ class Conversation { |
|
|
|
String symmetricKey; |
|
|
|
bool admin; |
|
|
|
String name; |
|
|
|
bool twoUser; |
|
|
|
ConversationStatus status; |
|
|
|
bool isRead; |
|
|
|
|
|
|
@ -167,6 +206,7 @@ class Conversation { |
|
|
|
required this.symmetricKey, |
|
|
|
required this.admin, |
|
|
|
required this.name, |
|
|
|
required this.twoUser, |
|
|
|
required this.status, |
|
|
|
required this.isRead, |
|
|
|
}); |
|
|
@ -194,6 +234,7 @@ class Conversation { |
|
|
|
symmetricKey: base64.encode(symmetricKeyDecrypted), |
|
|
|
admin: admin == 'true', |
|
|
|
name: 'Unknown', |
|
|
|
twoUser: false, |
|
|
|
status: ConversationStatus.complete, |
|
|
|
isRead: true, |
|
|
|
); |
|
|
@ -251,6 +292,7 @@ class Conversation { |
|
|
|
'symmetric_key': symmetricKey, |
|
|
|
'admin': admin ? 1 : 0, |
|
|
|
'name': name, |
|
|
|
'two_user': twoUser ? 1 : 0, |
|
|
|
'status': status.index, |
|
|
|
'is_read': isRead ? 1 : 0, |
|
|
|
}; |
|
|
@ -297,6 +339,28 @@ class Conversation { |
|
|
|
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']; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|