From fe32526de883b3449a2346d49a9c36ba6aefeb0b Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 23 Jan 2023 20:35:48 +0100 Subject: [PATCH] parse blocking command pushes --- .../android/database/dao/BlockingDao.java | 18 ++++++++++---- .../database/entity/BlockedItemEntity.java | 5 ++-- .../android/xmpp/manager/BlockingManager.java | 24 ++++++++++++++++--- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/main/java/im/conversations/android/database/dao/BlockingDao.java b/src/main/java/im/conversations/android/database/dao/BlockingDao.java index f4c1b1c48..997259791 100644 --- a/src/main/java/im/conversations/android/database/dao/BlockingDao.java +++ b/src/main/java/im/conversations/android/database/dao/BlockingDao.java @@ -2,9 +2,11 @@ package im.conversations.android.database.dao; import androidx.room.Dao; import androidx.room.Insert; +import androidx.room.OnConflictStrategy; import androidx.room.Query; import androidx.room.Transaction; import com.google.common.collect.Collections2; +import eu.siacs.conversations.xmpp.Jid; import im.conversations.android.database.entity.BlockedItemEntity; import im.conversations.android.database.model.Account; import im.conversations.android.xmpp.model.blocking.Item; @@ -13,17 +15,25 @@ import java.util.Collection; @Dao public abstract class BlockingDao { - @Insert + @Insert(onConflict = OnConflictStrategy.REPLACE) abstract void insert(Collection entities); @Query("DELETE FROM blocked WHERE accountId=:account") - abstract void clear(final long account); + public abstract void clear(final long account); @Transaction - public void setBlocklist(final Account account, final Collection blockedItems) { + public void set(final Account account, final Collection blockedItems) { final var entities = - Collections2.transform(blockedItems, i -> BlockedItemEntity.of(account.id, i)); + Collections2.transform( + blockedItems, i -> BlockedItemEntity.of(account.id, i.getJid())); clear(account.id); insert(entities); } + + public void add(Account account, Collection addresses) { + insert(Collections2.transform(addresses, a -> BlockedItemEntity.of(account.id, a))); + } + + @Query("DELETE from blocked WHERE accountId=:account AND address IN(:addresses)") + public abstract void remove(final long account, Collection addresses); } diff --git a/src/main/java/im/conversations/android/database/entity/BlockedItemEntity.java b/src/main/java/im/conversations/android/database/entity/BlockedItemEntity.java index 6cd1d8124..51a71a150 100644 --- a/src/main/java/im/conversations/android/database/entity/BlockedItemEntity.java +++ b/src/main/java/im/conversations/android/database/entity/BlockedItemEntity.java @@ -6,7 +6,6 @@ import androidx.room.ForeignKey; import androidx.room.Index; import androidx.room.PrimaryKey; import eu.siacs.conversations.xmpp.Jid; -import im.conversations.android.xmpp.model.blocking.Item; @Entity( tableName = "blocked", @@ -30,10 +29,10 @@ public class BlockedItemEntity { @NonNull public Jid address; - public static BlockedItemEntity of(final long accountId, final Item item) { + public static BlockedItemEntity of(final long accountId, final Jid address) { final var entity = new BlockedItemEntity(); entity.accountId = accountId; - entity.address = item.getJid(); + entity.address = address; return entity; } } diff --git a/src/main/java/im/conversations/android/xmpp/manager/BlockingManager.java b/src/main/java/im/conversations/android/xmpp/manager/BlockingManager.java index db6ea55cf..8f9d8f5fb 100644 --- a/src/main/java/im/conversations/android/xmpp/manager/BlockingManager.java +++ b/src/main/java/im/conversations/android/xmpp/manager/BlockingManager.java @@ -5,6 +5,7 @@ import com.google.common.collect.Collections2; import im.conversations.android.xmpp.XmppConnection; import im.conversations.android.xmpp.model.blocking.Block; import im.conversations.android.xmpp.model.blocking.Blocklist; +import im.conversations.android.xmpp.model.blocking.Item; import im.conversations.android.xmpp.model.blocking.Unblock; import im.conversations.android.xmpp.model.stanza.IQ; import java.util.Objects; @@ -15,9 +16,26 @@ public class BlockingManager extends AbstractManager { super(context, connection); } - public void handlePush(final Block block) {} + public void handlePush(final Block block) { + final var items = block.getExtensions(Item.class); + final var addresses = + Collections2.transform( + Collections2.filter(items, i -> Objects.nonNull(i.getJid())), Item::getJid); + getDatabase().blockingDao().add(getAccount(), addresses); + } - public void handlePush(final Unblock unblock) {} + public void handlePush(final Unblock unblock) { + final var items = unblock.getExtensions(Item.class); + if (items.isEmpty()) { + getDatabase().blockingDao().clear(getAccount().id); + } else { + final var addresses = + Collections2.transform( + Collections2.filter(items, i -> Objects.nonNull(i.getJid())), + Item::getJid); + getDatabase().blockingDao().remove(getAccount().id, addresses); + } + } public void fetch() { final IQ iqPacket = new IQ(IQ.Type.GET); @@ -37,6 +55,6 @@ public class BlockingManager extends AbstractManager { final var items = blocklist.getExtensions(im.conversations.android.xmpp.model.blocking.Item.class); final var filteredItems = Collections2.filter(items, i -> Objects.nonNull(i.getJid())); - getDatabase().blockingDao().setBlocklist(account, filteredItems); + getDatabase().blockingDao().set(account, filteredItems); } }