look up older messages in DB for reactions

(cherry picked from commit d84a4796e257db7679e4729680003e93ad75e960)
This commit is contained in:
Daniel Gultsch 2024-10-04 13:28:45 +02:00 committed by Arne
parent 3684c83d90
commit a5c4fb0cf3
2 changed files with 86 additions and 20 deletions

View file

@ -1432,29 +1432,53 @@ public class MessageParser extends AbstractParser implements Consumer<im.convers
final var occupant = final var occupant =
mucOptions.occupantId() ? packet.getExtension(OccupantId.class) : null; mucOptions.occupantId() ? packet.getExtension(OccupantId.class) : null;
final var occupantId = occupant == null ? null : occupant.getId(); final var occupantId = occupant == null ? null : occupant.getId();
final var message = conversation.findMessageWithServerMsgId(reactingTo); if (occupantId != null) {
// TODO use occupant id for isSelf assessment // TODO use occupant id for isSelf assessment
final boolean isReceived = !mucOptions.isSelf(counterpart); final boolean isReceived = !mucOptions.isSelf(counterpart);
if (occupantId != null && message != null) { final Message message;
final var newReactions = new HashSet<>(reactions.getReactions()); final var inMemoryMessage =
newReactions.removeAll(message.getReactions().stream().filter(r -> occupantId.equals(r.occupantId)).map(r -> r.reaction).collect(Collectors.toList())); conversation.findMessageWithServerMsgId(reactingTo);
final var combinedReactions = if (inMemoryMessage != null) {
Reaction.withOccupantId( message = inMemoryMessage;
message.getReactions(), } else {
reactions.getReactions(), message =
isReceived, mXmppConnectionService.databaseBackend
counterpart, .getMessageWithServerMsgId(conversation, reactingTo);
null, }
occupantId, if (message != null) {
message.getRemoteMsgId()); final var newReactions = new HashSet<>(reactions.getReactions());
message.setReactions(combinedReactions); newReactions.removeAll(message.getReactions().stream().filter(r -> occupantId.equals(r.occupantId)).map(r -> r.reaction).collect(Collectors.toList()));
mXmppConnectionService.updateMessage(message, false); final var combinedReactions =
if (!isCarbon && !packet.fromAccount(account)) mXmppConnectionService.getNotificationService().push(message, counterpart, occupantId, newReactions); Reaction.withOccupantId(
message.getReactions(),
reactions.getReactions(),
isReceived,
counterpart,
null,
occupantId,
message.getRemoteMsgId());
message.setReactions(combinedReactions);
mXmppConnectionService.updateMessage(message, false);
if (!isCarbon && !packet.fromAccount(account)) mXmppConnectionService.getNotificationService().push(message, counterpart, occupantId, newReactions);
} else {
Log.d(Config.LOGTAG, "message with id " + reactingTo + " not found");
}
} else { } else {
Log.d(Config.LOGTAG,"not found occupant or message"); Log.d(
Config.LOGTAG,
"received reaction in channel w/o occupant ids. ignoring");
} }
} else if (conversation.getMode() == Conversational.MODE_SINGLE) { } else if (conversation.getMode() == Conversational.MODE_SINGLE) {
final var message = conversation.findMessageWithUuidOrRemoteId(reactingTo); final Message message;
final var inMemoryMessage =
conversation.findMessageWithUuidOrRemoteId(reactingTo);
if (inMemoryMessage != null) {
message = inMemoryMessage;
} else {
message =
mXmppConnectionService.databaseBackend.getMessageWithUuidOrRemoteId(
conversation, reactingTo);
}
final boolean isReceived; final boolean isReceived;
final Jid reactionFrom; final Jid reactionFrom;
if (packet.fromAccount(account)) { if (packet.fromAccount(account)) {
@ -1478,6 +1502,8 @@ public class MessageParser extends AbstractParser implements Consumer<im.convers
message.setReactions(combinedReactions); message.setReactions(combinedReactions);
mXmppConnectionService.updateMessage(message, false); mXmppConnectionService.updateMessage(message, false);
if (!isCarbon && !packet.fromAccount(account)) mXmppConnectionService.getNotificationService().push(message, counterpart, null, newReactions); if (!isCarbon && !packet.fromAccount(account)) mXmppConnectionService.getNotificationService().push(message, counterpart, null, newReactions);
} else {
Log.d(Config.LOGTAG, "message with id " + reactingTo + " not found");
} }
} }
} }

View file

@ -1318,6 +1318,46 @@ public class DatabaseBackend extends SQLiteOpenHelper {
return filesPaths; return filesPaths;
} }
public Message getMessageWithServerMsgId(
final Conversation conversation, final String messageId) {
final var db = this.getReadableDatabase();
final String sql =
"select * from messages where conversationUuid=? and serverMsgId=? LIMIT 1";
final String[] args = {conversation.getUuid(), messageId};
final Cursor cursor = db.rawQuery(sql, args);
if (cursor == null) {
return null;
}
Message message = null;
try {
if (cursor.moveToFirst()) {
message = Message.fromCursor(cursor, conversation);
}
} catch (final IOException e) { }
cursor.close();
return message;
}
public Message getMessageWithUuidOrRemoteId(
final Conversation conversation, final String messageId) {
final var db = this.getReadableDatabase();
final String sql =
"select * from messages where conversationUuid=? and (uuid=? OR remoteMsgId=?) LIMIT 1";
final String[] args = {conversation.getUuid(), messageId, messageId};
final Cursor cursor = db.rawQuery(sql, args);
if (cursor == null) {
return null;
}
Message message = null;
try {
if (cursor.moveToFirst()) {
message = Message.fromCursor(cursor, conversation);
}
} catch (final IOException e) { }
cursor.close();
return message;
}
public static class FilePath { public static class FilePath {
public final UUID uuid; public final UUID uuid;
public final String path; public final String path;