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
src/main/java/eu/siacs/conversations

View file

@ -1432,29 +1432,53 @@ public class MessageParser extends AbstractParser implements Consumer<im.convers
final var occupant =
mucOptions.occupantId() ? packet.getExtension(OccupantId.class) : null;
final var occupantId = occupant == null ? null : occupant.getId();
final var message = conversation.findMessageWithServerMsgId(reactingTo);
// TODO use occupant id for isSelf assessment
final boolean isReceived = !mucOptions.isSelf(counterpart);
if (occupantId != null && message != null) {
final var newReactions = new HashSet<>(reactions.getReactions());
newReactions.removeAll(message.getReactions().stream().filter(r -> occupantId.equals(r.occupantId)).map(r -> r.reaction).collect(Collectors.toList()));
final var combinedReactions =
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);
if (occupantId != null) {
// TODO use occupant id for isSelf assessment
final boolean isReceived = !mucOptions.isSelf(counterpart);
final Message message;
final var inMemoryMessage =
conversation.findMessageWithServerMsgId(reactingTo);
if (inMemoryMessage != null) {
message = inMemoryMessage;
} else {
message =
mXmppConnectionService.databaseBackend
.getMessageWithServerMsgId(conversation, reactingTo);
}
if (message != null) {
final var newReactions = new HashSet<>(reactions.getReactions());
newReactions.removeAll(message.getReactions().stream().filter(r -> occupantId.equals(r.occupantId)).map(r -> r.reaction).collect(Collectors.toList()));
final var combinedReactions =
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 {
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) {
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 Jid reactionFrom;
if (packet.fromAccount(account)) {
@ -1478,6 +1502,8 @@ public class MessageParser extends AbstractParser implements Consumer<im.convers
message.setReactions(combinedReactions);
mXmppConnectionService.updateMessage(message, false);
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;
}
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 final UUID uuid;
public final String path;