|
|
- import 'dart:convert';
-
- import 'package:flutter_dotenv/flutter_dotenv.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<void> sendMessage(Conversation conversation, String data) async {
- MyProfile profile = await MyProfile.getProfile();
-
- var uuid = const Uuid();
- final String messageDataId = uuid.v4();
-
- ConversationUser currentUser = await getConversationUser(conversation, profile.id);
-
- Message message = Message(
- id: messageDataId,
- symmetricKey: '',
- userSymmetricKey: '',
- senderId: profile.id,
- senderUsername: profile.username,
- data: 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.toJson(conversation, messageDataId)
- .then((messageJson) {
- return http.post(
- Uri.parse('${dotenv.env["SERVER_URL"]}api/v1/auth/message'),
- headers: <String, String>{
- '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],
- );
- });
- }
-
- Future<void> updateMessageThread(Conversation conversation, {MyProfile? profile}) async {
- profile ??= await MyProfile.getProfile();
- ConversationUser currentUser = await getConversationUser(conversation, profile.id);
-
- var resp = await http.get(
- Uri.parse('${dotenv.env["SERVER_URL"]}api/v1/auth/messages/${currentUser.associationKey}'),
- headers: {
- 'cookie': await getSessionCookie(),
- }
- );
-
- if (resp.statusCode != 200) {
- throw Exception(resp.body);
- }
-
- List<dynamic> messageThreadJson = jsonDecode(resp.body);
-
- final db = await getDatabaseConnection();
-
- for (var i = 0; i < messageThreadJson.length; i++) {
- Message message = Message.fromJson(
- messageThreadJson[i] as Map<String, dynamic>,
- profile.privateKey!,
- );
-
- ConversationUser messageUser = await getConversationUser(conversation, message.senderId);
- message.senderUsername = messageUser.username;
-
- await db.insert(
- 'messages',
- message.toMap(),
- conflictAlgorithm: ConflictAlgorithm.replace,
- );
- }
- }
-
- Future<void> updateMessageThreads({List<Conversation>? 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;
- // }
- }
|