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.

200 lines
5.8 KiB

  1. import 'dart:convert';
  2. import 'package:Envelope/exceptions/update_data_exception.dart';
  3. import 'package:Envelope/utils/storage/get_file.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:http/http.dart' as http;
  6. import 'package:pointycastle/export.dart';
  7. import 'package:sqflite/sqflite.dart';
  8. import '/components/flash_message.dart';
  9. import '/models/conversation_users.dart';
  10. import '/models/conversations.dart';
  11. import '/models/my_profile.dart';
  12. import '/utils/encryption/aes_helper.dart';
  13. import '/utils/storage/database.dart';
  14. import '/utils/storage/session_cookie.dart';
  15. Future<void> updateConversation(
  16. Conversation conversation,
  17. {
  18. includeUsers = false,
  19. updatedImage = false,
  20. } ) async {
  21. String sessionCookie = await getSessionCookie();
  22. Map<String, dynamic> conversationJson = await conversation.payloadJson(includeUsers: includeUsers);
  23. var resp = await http.put(
  24. await MyProfile.getServerUrl('api/v1/auth/conversations'),
  25. headers: <String, String>{
  26. 'Content-Type': 'application/json; charset=UTF-8',
  27. 'cookie': sessionCookie,
  28. },
  29. body: jsonEncode(conversationJson),
  30. );
  31. if (resp.statusCode != 204) {
  32. throw UpdateDataException('Unable to update conversation, please try again later.');
  33. }
  34. if (!updatedImage) {
  35. return;
  36. }
  37. Map<String, dynamic> attachmentJson = conversation.payloadImageJson();
  38. resp = await http.post(
  39. await MyProfile.getServerUrl('api/v1/auth/conversations/${conversation.id}/image'),
  40. headers: <String, String>{
  41. 'Content-Type': 'application/json; charset=UTF-8',
  42. 'cookie': sessionCookie,
  43. },
  44. body: jsonEncode(attachmentJson),
  45. );
  46. if (resp.statusCode != 204) {
  47. throw UpdateDataException('Unable to update conversation image, please try again later.');
  48. }
  49. }
  50. // TODO: Refactor this function
  51. Future<void> updateConversations() async {
  52. RSAPrivateKey privKey = await MyProfile.getPrivateKey();
  53. // try {
  54. var resp = await http.get(
  55. await MyProfile.getServerUrl('api/v1/auth/conversations'),
  56. headers: {
  57. 'cookie': await getSessionCookie(),
  58. }
  59. );
  60. if (resp.statusCode != 200) {
  61. throw Exception(resp.body);
  62. }
  63. List<Conversation> conversations = [];
  64. List<String> conversationsDetailIds = [];
  65. List<dynamic> conversationsJson = jsonDecode(resp.body);
  66. if (conversationsJson.isEmpty) {
  67. return;
  68. }
  69. for (var i = 0; i < conversationsJson.length; i++) {
  70. Conversation conversation = Conversation.fromJson(
  71. conversationsJson[i] as Map<String, dynamic>,
  72. privKey,
  73. );
  74. conversations.add(conversation);
  75. conversationsDetailIds.add(conversation.id);
  76. }
  77. Map<String, String> params = {};
  78. params['conversation_detail_ids'] = conversationsDetailIds.join(',');
  79. var uri = await MyProfile.getServerUrl('api/v1/auth/conversation_details');
  80. uri = uri.replace(queryParameters: params);
  81. resp = await http.get(
  82. uri,
  83. headers: {
  84. 'cookie': await getSessionCookie(),
  85. }
  86. );
  87. if (resp.statusCode != 200) {
  88. throw Exception(resp.body);
  89. }
  90. final db = await getDatabaseConnection();
  91. List<dynamic> conversationsDetailsJson = jsonDecode(resp.body);
  92. for (var i = 0; i < conversationsDetailsJson.length; i++) {
  93. var conversationDetailJson = conversationsDetailsJson[i] as Map<String, dynamic>;
  94. var conversation = findConversationByDetailId(conversations, conversationDetailJson['id']);
  95. conversation.twoUser = AesHelper.aesDecrypt(
  96. base64.decode(conversation.symmetricKey),
  97. base64.decode(conversationDetailJson['two_user']),
  98. ) == 'true';
  99. if (conversation.twoUser) {
  100. MyProfile profile = await MyProfile.getProfile();
  101. final db = await getDatabaseConnection();
  102. List<Map<String, dynamic>> maps = await db.query(
  103. 'conversation_users',
  104. where: 'conversation_id = ? AND user_id != ?',
  105. whereArgs: [ conversation.id, profile.id ],
  106. );
  107. if (maps.length != 1) {
  108. throw ArgumentError('Invalid user id');
  109. }
  110. conversation.name = maps[0]['username'];
  111. } else {
  112. conversation.name = AesHelper.aesDecrypt(
  113. base64.decode(conversation.symmetricKey),
  114. base64.decode(conversationDetailJson['name']),
  115. );
  116. }
  117. // TODO: Handle exception here
  118. if (conversationDetailJson['attachment_id'] != null) {
  119. conversation.icon = await getFile(
  120. conversationDetailJson['attachment']['image_link'],
  121. conversation.id,
  122. conversation.symmetricKey,
  123. );
  124. }
  125. await db.insert(
  126. 'conversations',
  127. conversation.toMap(),
  128. conflictAlgorithm: ConflictAlgorithm.replace,
  129. );
  130. List<dynamic> usersData = conversationDetailJson['users'];
  131. for (var i = 0; i < usersData.length; i++) {
  132. ConversationUser conversationUser = ConversationUser.fromJson(
  133. usersData[i] as Map<String, dynamic>,
  134. base64.decode(conversation.symmetricKey),
  135. );
  136. await db.insert(
  137. 'conversation_users',
  138. conversationUser.toMap(),
  139. conflictAlgorithm: ConflictAlgorithm.replace,
  140. );
  141. }
  142. }
  143. // } catch (SocketException) {
  144. // return;
  145. // }
  146. }
  147. Future<void> uploadConversation(Conversation conversation, BuildContext context) async {
  148. String sessionCookie = await getSessionCookie();
  149. Map<String, dynamic> conversationJson = await conversation.payloadJson();
  150. var resp = await http.post(
  151. await MyProfile.getServerUrl('api/v1/auth/conversations'),
  152. headers: <String, String>{
  153. 'Content-Type': 'application/json; charset=UTF-8',
  154. 'cookie': sessionCookie,
  155. },
  156. body: jsonEncode(conversationJson),
  157. );
  158. if (resp.statusCode != 200) {
  159. showMessage('Failed to create conversation', context);
  160. }
  161. }