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.

203 lines
5.9 KiB

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