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.

319 lines
8.7 KiB

  1. import 'dart:convert';
  2. import 'package:Envelope/utils/storage/get_file.dart';
  3. import 'package:http/http.dart' as http;
  4. import 'package:pointycastle/export.dart';
  5. import 'package:sqflite/sqflite.dart';
  6. import 'package:uuid/uuid.dart';
  7. import '/database/models/friends.dart';
  8. import '/database/models/conversation_users.dart';
  9. import '/database/models/conversations.dart';
  10. import '/database/models/my_profile.dart';
  11. import '/exceptions/update_data_exception.dart';
  12. import '/utils/encryption/aes_helper.dart';
  13. import '/utils/storage/database.dart';
  14. import '/utils/storage/session_cookie.dart';
  15. class _BaseConversationsResult {
  16. List<Conversation> conversations;
  17. List<String> detailIds;
  18. _BaseConversationsResult({
  19. required this.conversations,
  20. required this.detailIds,
  21. });
  22. }
  23. class ConversationsService {
  24. static Future<void> saveConversation(Conversation conversation) async {
  25. final db = await getDatabaseConnection();
  26. db.update(
  27. 'conversations',
  28. conversation.toMap(),
  29. where: 'id = ?',
  30. whereArgs: [conversation.id],
  31. );
  32. }
  33. static Future<Conversation> addUsersToConversation(Conversation conversation, List<Friend> friends) async {
  34. final db = await getDatabaseConnection();
  35. var uuid = const Uuid();
  36. for (Friend friend in friends) {
  37. await db.insert(
  38. 'conversation_users',
  39. ConversationUser(
  40. id: uuid.v4(),
  41. userId: friend.friendId,
  42. conversationId: conversation.id,
  43. username: friend.username,
  44. associationKey: uuid.v4(),
  45. publicKey: friend.publicKey,
  46. admin: false,
  47. ).toMap(),
  48. conflictAlgorithm: ConflictAlgorithm.replace,
  49. );
  50. }
  51. return conversation;
  52. }
  53. static Future<void> updateConversation(
  54. Conversation conversation,
  55. {
  56. includeUsers = false,
  57. updatedImage = false,
  58. saveConversation = true,
  59. } ) async {
  60. if (saveConversation) {
  61. await saveConversation(conversation);
  62. }
  63. String sessionCookie = await getSessionCookie();
  64. Map<String, dynamic> conversationJson = await conversation.payloadJson(includeUsers: includeUsers);
  65. var resp = await http.put(
  66. await MyProfile.getServerUrl('api/v1/auth/conversations'),
  67. headers: <String, String>{
  68. 'Content-Type': 'application/json; charset=UTF-8',
  69. 'cookie': sessionCookie,
  70. },
  71. body: jsonEncode(conversationJson),
  72. );
  73. if (resp.statusCode != 204) {
  74. throw UpdateDataException('Unable to update conversation, please try again later.');
  75. }
  76. if (!updatedImage) {
  77. return;
  78. }
  79. Map<String, dynamic> attachmentJson = conversation.payloadImageJson();
  80. resp = await http.post(
  81. await MyProfile.getServerUrl('api/v1/auth/conversations/${conversation.id}/image'),
  82. headers: <String, String>{
  83. 'Content-Type': 'application/json; charset=UTF-8',
  84. 'cookie': sessionCookie,
  85. },
  86. body: jsonEncode(attachmentJson),
  87. );
  88. if (resp.statusCode != 204) {
  89. throw UpdateDataException('Unable to update conversation image, please try again later.');
  90. }
  91. }
  92. static Future<_BaseConversationsResult> _getBaseConversations({
  93. int page = 0
  94. }) async {
  95. RSAPrivateKey privKey = await MyProfile.getPrivateKey();
  96. Map<String, String> params = {};
  97. if (page != 0) {
  98. params['page'] = page.toString();
  99. }
  100. var uri = await MyProfile.getServerUrl('api/v1/auth/conversations');
  101. uri = uri.replace(queryParameters: params);
  102. http.Response resp = await http.get(
  103. uri,
  104. headers: {
  105. 'cookie': await getSessionCookie(),
  106. }
  107. );
  108. if (resp.statusCode != 200) {
  109. throw Exception(resp.body);
  110. }
  111. _BaseConversationsResult result = _BaseConversationsResult(
  112. conversations: [],
  113. detailIds: []
  114. );
  115. List<dynamic> conversationsJson = jsonDecode(resp.body);
  116. if (conversationsJson.isEmpty) {
  117. return result;
  118. }
  119. for (var i = 0; i < conversationsJson.length; i++) {
  120. Conversation conversation = Conversation.fromJson(
  121. conversationsJson[i] as Map<String, dynamic>,
  122. privKey,
  123. );
  124. result.conversations.add(conversation);
  125. result.detailIds.add(conversation.id);
  126. }
  127. return result;
  128. }
  129. static Conversation _findConversationByDetailId(List<Conversation> conversations, String id) {
  130. for (var conversation in conversations) {
  131. if (conversation.id == id) {
  132. return conversation;
  133. }
  134. }
  135. // Or return `null`.
  136. throw ArgumentError.value(id, 'id', 'No element with that id');
  137. }
  138. static Future<void> _storeConversations(Database db, Conversation conversation, Map<String, dynamic> detailsJson) async {
  139. conversation.createdAt = DateTime.parse(detailsJson['created_at']);
  140. conversation.updatedAt = DateTime.parse(detailsJson['updated_at']);
  141. conversation.messageExpiryDefault = detailsJson['message_expiry'];
  142. conversation.twoUser = AesHelper.aesDecrypt(
  143. base64.decode(conversation.symmetricKey),
  144. base64.decode(detailsJson['two_user']),
  145. ) == 'true';
  146. if (conversation.twoUser) {
  147. MyProfile profile = await MyProfile.getProfile();
  148. final db = await getDatabaseConnection();
  149. List<Map<String, dynamic>> maps = await db.query(
  150. 'conversation_users',
  151. where: 'conversation_id = ? AND user_id != ?',
  152. whereArgs: [ conversation.id, profile.id ],
  153. );
  154. if (maps.length != 1) {
  155. conversation.name = 'TODO: Fix this';
  156. } else {
  157. conversation.name = maps[0]['username'];
  158. }
  159. } else {
  160. conversation.name = AesHelper.aesDecrypt(
  161. base64.decode(conversation.symmetricKey),
  162. base64.decode(detailsJson['name']),
  163. );
  164. }
  165. if (detailsJson['attachment_id'] != null) {
  166. conversation.icon = await getFile(
  167. '$defaultServerUrl/files/${detailsJson['attachment']['image_link']}',
  168. conversation.id,
  169. conversation.symmetricKey,
  170. ).catchError((dynamic) async {});
  171. }
  172. await db.insert(
  173. 'conversations',
  174. conversation.toMap(),
  175. conflictAlgorithm: ConflictAlgorithm.replace,
  176. );
  177. List<dynamic> usersData = detailsJson['users'];
  178. for (var i = 0; i < usersData.length; i++) {
  179. ConversationUser conversationUser = ConversationUser.fromJson(
  180. usersData[i] as Map<String, dynamic>,
  181. base64.decode(conversation.symmetricKey),
  182. );
  183. await db.insert(
  184. 'conversation_users',
  185. conversationUser.toMap(),
  186. conflictAlgorithm: ConflictAlgorithm.replace,
  187. );
  188. }
  189. }
  190. static Future<void> updateConversations({
  191. int page = 0,
  192. DateTime? updatedAt
  193. }) async {
  194. _BaseConversationsResult baseConvs = await _getBaseConversations(page: page);
  195. if (baseConvs.detailIds.isEmpty) {
  196. return;
  197. }
  198. Map<String, String> params = {};
  199. if (updatedAt != null) {
  200. params['updated_at'] = updatedAt.toIso8601String();
  201. }
  202. params['conversation_detail_ids'] = baseConvs.detailIds.join(',');
  203. var uri = await MyProfile.getServerUrl('api/v1/auth/conversation_details');
  204. uri = uri.replace(queryParameters: params);
  205. http.Response resp = await http.get(
  206. uri,
  207. headers: {
  208. 'cookie': await getSessionCookie(),
  209. }
  210. );
  211. if (resp.statusCode != 200) {
  212. throw Exception(resp.body);
  213. }
  214. final db = await getDatabaseConnection();
  215. List<dynamic> conversationsDetailsJson = jsonDecode(resp.body);
  216. for (var i = 0; i < conversationsDetailsJson.length; i++) {
  217. Map<String, dynamic> detailsJson = conversationsDetailsJson[i] as Map<String, dynamic>;
  218. Conversation conversation = _findConversationByDetailId(baseConvs.conversations, detailsJson['id']);
  219. await _storeConversations(db, conversation, detailsJson);
  220. }
  221. }
  222. static Future<void> uploadConversation(Conversation conversation) async {
  223. String sessionCookie = await getSessionCookie();
  224. Map<String, dynamic> conversationJson = await conversation.payloadJson();
  225. var resp = await http.post(
  226. await MyProfile.getServerUrl('api/v1/auth/conversations'),
  227. headers: <String, String>{
  228. 'Content-Type': 'application/json; charset=UTF-8',
  229. 'cookie': sessionCookie,
  230. },
  231. body: jsonEncode(conversationJson),
  232. );
  233. if (resp.statusCode != 204) {
  234. throw Exception('Failed to create conversation');
  235. }
  236. }
  237. static Future<void> updateMessageExpiry(String id, String messageExpiry) async {
  238. http.Response resp = await http.post(
  239. await MyProfile.getServerUrl(
  240. 'api/v1/auth/conversations/$id/message_expiry'
  241. ),
  242. headers: {
  243. 'cookie': await getSessionCookie(),
  244. },
  245. body: jsonEncode({
  246. 'message_expiry': messageExpiry,
  247. }),
  248. );
  249. if (resp.statusCode == 204) {
  250. return;
  251. }
  252. throw Exception('Cannot set message expiry for conversation');
  253. }
  254. }