import 'dart:io'; import '/database/models/conversations.dart'; import '/database/models/image_message.dart'; import '/database/models/messages.dart'; import '/database/models/text_messages.dart'; import '/utils/storage/database.dart'; class MessagesRepository { static Future> getMessagesForThread(Conversation conversation) async { final db = await getDatabaseConnection(); final List> maps = await db.rawQuery( ''' SELECT * FROM messages WHERE association_key IN ( SELECT association_key FROM conversation_users WHERE conversation_id = ? ) ORDER BY created_at DESC; ''', [conversation.id] ); return List.generate(maps.length, (i) { if (maps[i]['data'] == null) { File file = File(maps[i]['file']); return ImageMessage( id: maps[i]['id'], symmetricKey: maps[i]['symmetric_key'], userSymmetricKey: maps[i]['user_symmetric_key'], file: file, senderId: maps[i]['sender_id'], senderUsername: maps[i]['sender_username'], associationKey: maps[i]['association_key'], createdAt: maps[i]['created_at'], failedToSend: maps[i]['failed_to_send'] == 1, ); } return TextMessage( id: maps[i]['id'], symmetricKey: maps[i]['symmetric_key'], userSymmetricKey: maps[i]['user_symmetric_key'], text: maps[i]['data'], senderId: maps[i]['sender_id'], senderUsername: maps[i]['sender_username'], associationKey: maps[i]['association_key'], createdAt: maps[i]['created_at'], failedToSend: maps[i]['failed_to_send'] == 1, ); }); } }