fix(service): Fix loading the wrong quote from the database

This commit is contained in:
PapaTutuWawa 2022-11-12 14:12:15 +01:00
parent 9b16bf6e6f
commit 09ba2122e7
2 changed files with 9 additions and 8 deletions

View File

@ -110,7 +110,7 @@ class DatabaseService {
final rawQuote = (await _db.query( final rawQuote = (await _db.query(
'Messages', 'Messages',
where: 'conversationJid = ? AND id = ?', where: 'conversationJid = ? AND id = ?',
whereArgs: [jid, m['id']! as int], whereArgs: [jid, m['quote_id']! as int],
)).first; )).first;
quotes = Message.fromDatabaseJson(rawQuote, null); quotes = Message.fromDatabaseJson(rawQuote, null);
} }
@ -268,7 +268,7 @@ class DatabaseService {
int? mediaSize, int? mediaSize,
} }
) async { ) async {
final m = Message( var m = Message(
sender, sender,
body, body,
timestamp, timestamp,
@ -301,17 +301,17 @@ class DatabaseService {
mediaSize: mediaSize, mediaSize: mediaSize,
); );
Message? quotes;
if (quoteId != null) { if (quoteId != null) {
quotes = await getMessageByXmppId(quoteId, conversationJid); final quotes = await getMessageByXmppId(quoteId, conversationJid);
if (quotes == null) { if (quotes == null) {
_log.warning('Failed to add quote for message with id $quoteId'); _log.warning('Failed to add quote for message with id $quoteId');
} else {
m = m.copyWith(quotes: quotes);
} }
} }
return m.copyWith( return m.copyWith(
id: await _db.insert('Messages', m.toDatabaseJson(quotes?.id)), id: await _db.insert('Messages', m.toDatabaseJson()),
quotes: quotes,
); );
} }

View File

@ -83,7 +83,7 @@ class Message with _$Message {
}).copyWith(quotes: quotes); }).copyWith(quotes: quotes);
} }
Map<String, dynamic> toDatabaseJson(int? quoteId) { Map<String, dynamic> toDatabaseJson() {
final map = toJson() final map = toJson()
..remove('id') ..remove('id')
..remove('quotes'); ..remove('quotes');
@ -96,7 +96,8 @@ class Message with _$Message {
'displayed': boolToInt(displayed), 'displayed': boolToInt(displayed),
'acked': boolToInt(acked), 'acked': boolToInt(acked),
'encrypted': boolToInt(encrypted), 'encrypted': boolToInt(encrypted),
'quote_id': quoteId, // NOTE: Message.quote_id is a foreign-key
'quote_id': quotes?.id,
'plaintextHashes': _optionalJsonEncode(plaintextHashes), 'plaintextHashes': _optionalJsonEncode(plaintextHashes),
'ciphertextHashes': _optionalJsonEncode(ciphertextHashes), 'ciphertextHashes': _optionalJsonEncode(ciphertextHashes),
'isDownloading': boolToInt(isDownloading), 'isDownloading': boolToInt(isDownloading),