parse blocking command pushes

This commit is contained in:
Daniel Gultsch 2023-01-23 20:35:48 +01:00
parent 164ac450d4
commit fe32526de8
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
3 changed files with 37 additions and 10 deletions

View file

@ -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<BlockedItemEntity> 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<Item> blockedItems) {
public void set(final Account account, final Collection<Item> 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<Jid> 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<Jid> addresses);
}

View file

@ -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;
}
}

View file

@ -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);
}
}