Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.1 KiB

  1. import 'dart:convert';
  2. import 'package:http/http.dart' as http;
  3. import 'package:flutter_dotenv/flutter_dotenv.dart';
  4. import 'package:pointycastle/export.dart';
  5. import 'package:sqflite/sqflite.dart';
  6. import '/models/conversations.dart';
  7. import '/models/conversation_users.dart';
  8. import '/utils/storage/database.dart';
  9. import '/utils/storage/session_cookie.dart';
  10. import '/utils/storage/encryption_keys.dart';
  11. import '/utils/encryption/aes_helper.dart';
  12. Future<void> updateConversations() async {
  13. RSAPrivateKey privKey = await getPrivateKey();
  14. var resp = await http.get(
  15. Uri.parse('${dotenv.env["SERVER_URL"]}api/v1/auth/conversations'),
  16. headers: {
  17. 'cookie': await getSessionCookie(),
  18. }
  19. );
  20. if (resp.statusCode != 200) {
  21. throw Exception(resp.body);
  22. }
  23. List<Conversation> conversations = [];
  24. List<String> conversationsDetailIds = [];
  25. List<dynamic> conversationsJson = jsonDecode(resp.body);
  26. for (var i = 0; i < conversationsJson.length; i++) {
  27. Conversation conversation = Conversation.fromJson(
  28. conversationsJson[i] as Map<String, dynamic>,
  29. privKey,
  30. );
  31. conversations.add(conversation);
  32. conversationsDetailIds.add(conversation.conversationDetailId);
  33. }
  34. Map<String, String> params = {};
  35. params['conversation_detail_ids'] = conversationsDetailIds.join(',');
  36. var uri = Uri.parse('${dotenv.env["SERVER_URL"]}api/v1/auth/conversation_details');
  37. uri = uri.replace(queryParameters: params);
  38. resp = await http.get(
  39. uri,
  40. headers: {
  41. 'cookie': await getSessionCookie(),
  42. }
  43. );
  44. if (resp.statusCode != 200) {
  45. throw Exception(resp.body);
  46. }
  47. final db = await getDatabaseConnection();
  48. List<dynamic> conversationsDetailsJson = jsonDecode(resp.body);
  49. for (var i = 0; i < conversationsDetailsJson.length; i++) {
  50. var conversationDetailJson = conversationsDetailsJson[i] as Map<String, dynamic>;
  51. var conversation = findConversationByDetailId(conversations, conversationDetailJson['id']);
  52. conversation.name = AesHelper.aesDecrypt(
  53. base64.decode(conversation.symmetricKey),
  54. base64.decode(conversationDetailJson['name']),
  55. );
  56. await db.insert(
  57. 'conversations',
  58. conversation.toMap(),
  59. conflictAlgorithm: ConflictAlgorithm.replace,
  60. );
  61. List<dynamic> usersData = json.decode(
  62. AesHelper.aesDecrypt(
  63. base64.decode(conversation.symmetricKey),
  64. base64.decode(conversationDetailJson['users']),
  65. )
  66. );
  67. for (var i = 0; i < usersData.length; i++) {
  68. ConversationUser conversationUser = ConversationUser.fromJson(
  69. usersData[i] as Map<String, dynamic>,
  70. conversation.id,
  71. );
  72. await db.insert(
  73. 'conversation_users',
  74. conversationUser.toMap(),
  75. conflictAlgorithm: ConflictAlgorithm.replace,
  76. );
  77. }
  78. }
  79. }