From acb297ac96603c15064480790a662939b24a638e Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 13 Feb 2023 15:36:10 +0100 Subject: [PATCH] store roster groups in DB --- .../android/database/dao/RosterDao.java | 13 +++++++++++-- .../database/entity/RosterItemGroupEntity.java | 7 +++++++ .../android/xmpp/model/roster/Group.java | 10 ++++++++++ .../android/xmpp/model/roster/Item.java | 10 ++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 src/main/java/im/conversations/android/xmpp/model/roster/Group.java diff --git a/src/main/java/im/conversations/android/database/dao/RosterDao.java b/src/main/java/im/conversations/android/database/dao/RosterDao.java index 62dbcf6a6..0f05a5db4 100644 --- a/src/main/java/im/conversations/android/database/dao/RosterDao.java +++ b/src/main/java/im/conversations/android/database/dao/RosterDao.java @@ -6,8 +6,10 @@ import androidx.room.Dao; import androidx.room.Insert; 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.RosterItemEntity; +import im.conversations.android.database.entity.RosterItemGroupEntity; import im.conversations.android.database.model.Account; import im.conversations.android.xmpp.model.roster.Item; import java.util.Collection; @@ -33,7 +35,9 @@ public abstract class RosterDao { clear(account.id); for (final Item item : rosterItems) { final long id = insert(RosterItemEntity.of(account.id, item)); - // TODO insert groups + insertRosterGroups( + Collections2.transform( + item.getGroups(), name -> RosterItemGroupEntity.of(id, name))); } setRosterVersion(account.id, version); } @@ -50,8 +54,13 @@ public abstract class RosterDao { } final RosterItemEntity entity = RosterItemEntity.of(account.id, item); final long id = insert(entity); - // TODO groups + insertRosterGroups( + Collections2.transform( + item.getGroups(), name -> RosterItemGroupEntity.of(id, name))); } setRosterVersion(account.id, version); } + + @Insert + protected abstract void insertRosterGroups(Collection entities); } diff --git a/src/main/java/im/conversations/android/database/entity/RosterItemGroupEntity.java b/src/main/java/im/conversations/android/database/entity/RosterItemGroupEntity.java index e9caa6912..b73c70b4a 100644 --- a/src/main/java/im/conversations/android/database/entity/RosterItemGroupEntity.java +++ b/src/main/java/im/conversations/android/database/entity/RosterItemGroupEntity.java @@ -23,4 +23,11 @@ public class RosterItemGroupEntity { @NonNull public Long rosterItemId; public String name; + + public static RosterItemGroupEntity of(long rosterItemId, final String name) { + final var entity = new RosterItemGroupEntity(); + entity.rosterItemId = rosterItemId; + entity.name = name; + return entity; + } } diff --git a/src/main/java/im/conversations/android/xmpp/model/roster/Group.java b/src/main/java/im/conversations/android/xmpp/model/roster/Group.java new file mode 100644 index 000000000..ec0d3a717 --- /dev/null +++ b/src/main/java/im/conversations/android/xmpp/model/roster/Group.java @@ -0,0 +1,10 @@ +package im.conversations.android.xmpp.model.roster; + +import im.conversations.android.xmpp.model.Extension; + +public class Group extends Extension { + + public Group() { + super(Group.class); + } +} diff --git a/src/main/java/im/conversations/android/xmpp/model/roster/Item.java b/src/main/java/im/conversations/android/xmpp/model/roster/Item.java index 4c867f81d..7ce705171 100644 --- a/src/main/java/im/conversations/android/xmpp/model/roster/Item.java +++ b/src/main/java/im/conversations/android/xmpp/model/roster/Item.java @@ -1,11 +1,15 @@ package im.conversations.android.xmpp.model.roster; +import com.google.common.collect.Collections2; +import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xmpp.Jid; import im.conversations.android.annotation.XmlElement; import im.conversations.android.xmpp.model.Extension; import java.util.Arrays; +import java.util.Collection; import java.util.List; import java.util.Locale; +import java.util.Objects; @XmlElement public class Item extends Extension { @@ -38,6 +42,12 @@ public class Item extends Extension { } } + public Collection getGroups() { + return Collections2.filter( + Collections2.transform(getExtensions(Group.class), Element::getContent), + Objects::nonNull); + } + public enum Subscription { NONE, TO,