|
|
@ -13,63 +13,64 @@ import '/utils/encryption/aes_helper.dart'; |
|
|
|
Future<void> updateConversations() async { |
|
|
|
RSAPrivateKey privKey = await getPrivateKey(); |
|
|
|
|
|
|
|
var resp = await http.get( |
|
|
|
Uri.parse('${dotenv.env["SERVER_URL"]}api/v1/auth/conversations'), |
|
|
|
headers: { |
|
|
|
'cookie': await getSessionCookie(), |
|
|
|
} |
|
|
|
); |
|
|
|
|
|
|
|
if (resp.statusCode != 200) { |
|
|
|
try { |
|
|
|
var resp = await http.get( |
|
|
|
Uri.parse('${dotenv.env["SERVER_URL"]}api/v1/auth/conversations'), |
|
|
|
headers: { |
|
|
|
'cookie': await getSessionCookie(), |
|
|
|
} |
|
|
|
); |
|
|
|
|
|
|
|
if (resp.statusCode != 200) { |
|
|
|
throw Exception(resp.body); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
List<Conversation> conversations = []; |
|
|
|
List<String> conversationsDetailIds = []; |
|
|
|
List<Conversation> conversations = []; |
|
|
|
List<String> conversationsDetailIds = []; |
|
|
|
|
|
|
|
List<dynamic> conversationsJson = jsonDecode(resp.body); |
|
|
|
List<dynamic> conversationsJson = jsonDecode(resp.body); |
|
|
|
|
|
|
|
for (var i = 0; i < conversationsJson.length; i++) { |
|
|
|
for (var i = 0; i < conversationsJson.length; i++) { |
|
|
|
Conversation conversation = Conversation.fromJson( |
|
|
|
conversationsJson[i] as Map<String, dynamic>, |
|
|
|
privKey, |
|
|
|
conversationsJson[i] as Map<String, dynamic>, |
|
|
|
privKey, |
|
|
|
); |
|
|
|
conversations.add(conversation); |
|
|
|
conversationsDetailIds.add(conversation.conversationDetailId); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Map<String, String> params = {}; |
|
|
|
params['conversation_detail_ids'] = conversationsDetailIds.join(','); |
|
|
|
var uri = Uri.parse('${dotenv.env["SERVER_URL"]}api/v1/auth/conversation_details'); |
|
|
|
uri = uri.replace(queryParameters: params); |
|
|
|
Map<String, String> params = {}; |
|
|
|
params['conversation_detail_ids'] = conversationsDetailIds.join(','); |
|
|
|
var uri = Uri.parse('${dotenv.env["SERVER_URL"]}api/v1/auth/conversation_details'); |
|
|
|
uri = uri.replace(queryParameters: params); |
|
|
|
|
|
|
|
resp = await http.get( |
|
|
|
uri, |
|
|
|
headers: { |
|
|
|
'cookie': await getSessionCookie(), |
|
|
|
} |
|
|
|
); |
|
|
|
resp = await http.get( |
|
|
|
uri, |
|
|
|
headers: { |
|
|
|
'cookie': await getSessionCookie(), |
|
|
|
} |
|
|
|
); |
|
|
|
|
|
|
|
if (resp.statusCode != 200) { |
|
|
|
if (resp.statusCode != 200) { |
|
|
|
throw Exception(resp.body); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
final db = await getDatabaseConnection(); |
|
|
|
final db = await getDatabaseConnection(); |
|
|
|
|
|
|
|
List<dynamic> conversationsDetailsJson = jsonDecode(resp.body); |
|
|
|
for (var i = 0; i < conversationsDetailsJson.length; i++) { |
|
|
|
List<dynamic> conversationsDetailsJson = jsonDecode(resp.body); |
|
|
|
for (var i = 0; i < conversationsDetailsJson.length; i++) { |
|
|
|
var conversationDetailJson = conversationsDetailsJson[i] as Map<String, dynamic>; |
|
|
|
var conversation = findConversationByDetailId(conversations, conversationDetailJson['id']); |
|
|
|
|
|
|
|
conversation.name = AesHelper.aesDecrypt( |
|
|
|
base64.decode(conversation.symmetricKey), |
|
|
|
base64.decode(conversationDetailJson['name']), |
|
|
|
base64.decode(conversation.symmetricKey), |
|
|
|
base64.decode(conversationDetailJson['name']), |
|
|
|
); |
|
|
|
|
|
|
|
await db.insert( |
|
|
|
'conversations', |
|
|
|
conversation.toMap(), |
|
|
|
conflictAlgorithm: ConflictAlgorithm.replace, |
|
|
|
'conversations', |
|
|
|
conversation.toMap(), |
|
|
|
conflictAlgorithm: ConflictAlgorithm.replace, |
|
|
|
); |
|
|
|
|
|
|
|
List<dynamic> usersData = json.decode( |
|
|
@ -80,16 +81,19 @@ Future<void> updateConversations() async { |
|
|
|
); |
|
|
|
|
|
|
|
for (var i = 0; i < usersData.length; i++) { |
|
|
|
ConversationUser conversationUser = ConversationUser.fromJson( |
|
|
|
usersData[i] as Map<String, dynamic>, |
|
|
|
conversation.id, |
|
|
|
); |
|
|
|
|
|
|
|
await db.insert( |
|
|
|
'conversation_users', |
|
|
|
conversationUser.toMap(), |
|
|
|
conflictAlgorithm: ConflictAlgorithm.replace, |
|
|
|
); |
|
|
|
ConversationUser conversationUser = ConversationUser.fromJson( |
|
|
|
usersData[i] as Map<String, dynamic>, |
|
|
|
conversation.id, |
|
|
|
); |
|
|
|
|
|
|
|
await db.insert( |
|
|
|
'conversation_users', |
|
|
|
conversationUser.toMap(), |
|
|
|
conflictAlgorithm: ConflictAlgorithm.replace, |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (SocketException) { |
|
|
|
return; |
|
|
|
} |
|
|
|
} |