import 'dart:convert'; import 'dart:io'; import 'package:Envelope/models/text_messages.dart'; import 'package:http/http.dart' as http; import 'package:sqflite/sqflite.dart'; import 'package:uuid/uuid.dart'; import '/models/conversation_users.dart'; import '/models/conversations.dart'; import '/models/messages.dart'; import '/models/my_profile.dart'; import '/utils/storage/database.dart'; import '/utils/storage/session_cookie.dart'; Future sendMessage(Conversation conversation, { String? data, List? files }) async { MyProfile profile = await MyProfile.getProfile(); var uuid = const Uuid(); final String messageId = uuid.v4(); ConversationUser currentUser = await getConversationUser(conversation, profile.id); List> messagesToAdd = []; if (data != null) { messagesToAdd.add({ 'text': data }); } if (files != null && files.isNotEmpty) { for (File file in files) { messagesToAdd.add({ 'file': file }); } } var message = TextMessage( id: messageId, symmetricKey: '', userSymmetricKey: '', senderId: currentUser.userId, senderUsername: profile.username, text: data!, associationKey: currentUser.associationKey, createdAt: DateTime.now().toIso8601String(), failedToSend: false, ); final db = await getDatabaseConnection(); await db.insert( 'messages', message.toMap(), conflictAlgorithm: ConflictAlgorithm.replace, ); String sessionCookie = await getSessionCookie(); message.payloadJson(conversation, messageId) .then((messageJson) async { return http.post( await MyProfile.getServerUrl('api/v1/auth/message'), headers: { 'Content-Type': 'application/json; charset=UTF-8', 'cookie': sessionCookie, }, body: messageJson, ); }) .then((resp) { if (resp.statusCode != 200) { throw Exception('Unable to send message'); } }) .catchError((exception) { message.failedToSend = true; db.update( 'messages', message.toMap(), where: 'id = ?', whereArgs: [message.id], ); throw exception; }); } Future updateMessageThread(Conversation conversation, {MyProfile? profile}) async { profile ??= await MyProfile.getProfile(); ConversationUser currentUser = await getConversationUser(conversation, profile.id); var resp = await http.get( await MyProfile.getServerUrl('api/v1/auth/messages/${currentUser.associationKey}'), headers: { 'cookie': await getSessionCookie(), } ); if (resp.statusCode != 200) { throw Exception(resp.body); } List messageThreadJson = jsonDecode(resp.body); final db = await getDatabaseConnection(); for (var i = 0; i < messageThreadJson.length; i++) { var message = TextMessage.fromJson( messageThreadJson[i] as Map, profile.privateKey!, ); ConversationUser messageUser = await getConversationUser(conversation, message.senderId); message.senderUsername = messageUser.username; await db.insert( 'messages', message.toMap(), conflictAlgorithm: ConflictAlgorithm.replace, ); } } Future updateMessageThreads({List? conversations}) async { // try { MyProfile profile = await MyProfile.getProfile(); conversations ??= await getConversations(); for (var i = 0; i < conversations.length; i++) { await updateMessageThread(conversations[i], profile: profile); } // } catch(SocketException) { // return; // } }