From 49bf92f7ca9a40d091770b4efcd65075b71a9ddf Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 14 Jan 2023 07:41:53 +0100 Subject: [PATCH] wire up MessageAckProcessor with DB --- .../database/ConversationsDatabase.java | 3 ++ .../android/database/dao/MessageDao.java | 39 +++++++++++++++++++ .../database/entity/MessageEntity.java | 2 + ...cessor.java => AbstractBaseProcessor.java} | 4 +- .../android/xmpp/processor/BindProcessor.java | 2 +- .../MessageAcknowledgeProcessor.java | 10 +++-- 6 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 src/main/java/im/conversations/android/database/dao/MessageDao.java rename src/main/java/im/conversations/android/xmpp/processor/{BaseProcessor.java => AbstractBaseProcessor.java} (83%) diff --git a/src/main/java/im/conversations/android/database/ConversationsDatabase.java b/src/main/java/im/conversations/android/database/ConversationsDatabase.java index 9547e332b..0d3656900 100644 --- a/src/main/java/im/conversations/android/database/ConversationsDatabase.java +++ b/src/main/java/im/conversations/android/database/ConversationsDatabase.java @@ -6,6 +6,7 @@ import androidx.room.Room; import androidx.room.RoomDatabase; import androidx.room.TypeConverters; import im.conversations.android.database.dao.AccountDao; +import im.conversations.android.database.dao.MessageDao; import im.conversations.android.database.dao.PresenceDao; import im.conversations.android.database.entity.AccountEntity; import im.conversations.android.database.entity.BlockedItemEntity; @@ -68,4 +69,6 @@ public abstract class ConversationsDatabase extends RoomDatabase { public abstract AccountDao accountDao(); public abstract PresenceDao presenceDao(); + + public abstract MessageDao messageDao(); } diff --git a/src/main/java/im/conversations/android/database/dao/MessageDao.java b/src/main/java/im/conversations/android/database/dao/MessageDao.java new file mode 100644 index 000000000..941d62ca2 --- /dev/null +++ b/src/main/java/im/conversations/android/database/dao/MessageDao.java @@ -0,0 +1,39 @@ +package im.conversations.android.database.dao; + +import androidx.annotation.NonNull; +import androidx.room.Dao; +import androidx.room.Query; +import eu.siacs.conversations.xmpp.Jid; +import im.conversations.android.database.model.Account; + +@Dao +public abstract class MessageDao { + + @Query( + "UPDATE message SET acknowledged=1 WHERE messageId=:messageId AND bareTo=:bareTo AND" + + " toResource=NULL AND chatId IN (SELECT id FROM chat WHERE accountId=:account)") + abstract int acknowledge(long account, String messageId, final String bareTo); + + @Query( + "UPDATE message SET acknowledged=1 WHERE messageId=:messageId AND bareTo=:bareTo AND" + + " toResource=:toResource AND chatId IN (SELECT id FROM chat WHERE" + + " accountId=:account)") + abstract int acknowledge( + long account, final String messageId, final String bareTo, final String toResource); + + public boolean acknowledge( + final Account account, @NonNull final String messageId, @NonNull final Jid to) { + return acknowledge(account.id, messageId, to); + } + + public boolean acknowledge( + final long account, @NonNull final String messageId, @NonNull final Jid to) { + if (to.isBareJid()) { + return acknowledge(account, messageId, to.toEscapedString()) > 0; + } else { + return acknowledge( + account, messageId, to.asBareJid().toEscapedString(), to.getResource()) + > 0; + } + } +} diff --git a/src/main/java/im/conversations/android/database/entity/MessageEntity.java b/src/main/java/im/conversations/android/database/entity/MessageEntity.java index 48e037860..69628cfb0 100644 --- a/src/main/java/im/conversations/android/database/entity/MessageEntity.java +++ b/src/main/java/im/conversations/android/database/entity/MessageEntity.java @@ -35,4 +35,6 @@ public class MessageEntity { public String messageId; public String stanzaId; + + public boolean acknowledged = false; } diff --git a/src/main/java/im/conversations/android/xmpp/processor/BaseProcessor.java b/src/main/java/im/conversations/android/xmpp/processor/AbstractBaseProcessor.java similarity index 83% rename from src/main/java/im/conversations/android/xmpp/processor/BaseProcessor.java rename to src/main/java/im/conversations/android/xmpp/processor/AbstractBaseProcessor.java index 7d3b2b3ce..65ed31050 100644 --- a/src/main/java/im/conversations/android/xmpp/processor/BaseProcessor.java +++ b/src/main/java/im/conversations/android/xmpp/processor/AbstractBaseProcessor.java @@ -5,12 +5,12 @@ import im.conversations.android.database.ConversationsDatabase; import im.conversations.android.database.model.Account; import im.conversations.android.xmpp.XmppConnection; -abstract class BaseProcessor { +abstract class AbstractBaseProcessor { protected final Context context; protected final XmppConnection connection; - BaseProcessor(final Context context, final XmppConnection connection) { + AbstractBaseProcessor(final Context context, final XmppConnection connection) { this.context = context; this.connection = connection; } diff --git a/src/main/java/im/conversations/android/xmpp/processor/BindProcessor.java b/src/main/java/im/conversations/android/xmpp/processor/BindProcessor.java index 0f01f3c40..62b5413d8 100644 --- a/src/main/java/im/conversations/android/xmpp/processor/BindProcessor.java +++ b/src/main/java/im/conversations/android/xmpp/processor/BindProcessor.java @@ -10,7 +10,7 @@ import eu.siacs.conversations.xmpp.stanzas.IqPacket; import im.conversations.android.xmpp.XmppConnection; import java.util.function.Consumer; -public class BindProcessor extends BaseProcessor implements Consumer { +public class BindProcessor extends AbstractBaseProcessor implements Consumer { public BindProcessor(final Context context, final XmppConnection connection) { super(context, connection); diff --git a/src/main/java/im/conversations/android/xmpp/processor/MessageAcknowledgeProcessor.java b/src/main/java/im/conversations/android/xmpp/processor/MessageAcknowledgeProcessor.java index e35986fde..b217a2e0a 100644 --- a/src/main/java/im/conversations/android/xmpp/processor/MessageAcknowledgeProcessor.java +++ b/src/main/java/im/conversations/android/xmpp/processor/MessageAcknowledgeProcessor.java @@ -5,12 +5,16 @@ import eu.siacs.conversations.xmpp.Jid; import im.conversations.android.xmpp.XmppConnection; import java.util.function.BiFunction; -public class MessageAcknowledgeProcessor implements BiFunction { +public class MessageAcknowledgeProcessor extends AbstractBaseProcessor + implements BiFunction { - public MessageAcknowledgeProcessor(final Context context, final XmppConnection connection) {} + public MessageAcknowledgeProcessor(final Context context, final XmppConnection connection) { + super(context, connection); + } @Override public Boolean apply(final Jid to, final String id) { - return null; + final var account = getAccount(); + return getDatabase().messageDao().acknowledge(account, id, to); } }