From 95bf66ca7d4f7acd295e21d0a38500a03f4f46fb Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 28 Sep 2019 02:23:15 +0200 Subject: [PATCH] Bookmarks2: support retraction --- .../siacs/conversations/entities/Account.java | 55 ++++++++++++------- .../conversations/entities/Bookmark.java | 18 +++--- .../conversations/generator/IqGenerator.java | 1 + .../conversations/parser/MessageParser.java | 24 +++++++- .../services/XmppConnectionService.java | 16 +++--- .../ui/ChannelDiscoveryActivity.java | 1 - .../ui/ConferenceDetailsActivity.java | 5 +- .../ui/StartConversationActivity.java | 4 +- 8 files changed, 78 insertions(+), 46 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Account.java b/src/main/java/eu/siacs/conversations/entities/Account.java index 91a08a0cd..ef180ded9 100644 --- a/src/main/java/eu/siacs/conversations/entities/Account.java +++ b/src/main/java/eu/siacs/conversations/entities/Account.java @@ -11,8 +11,10 @@ import org.json.JSONObject; import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArraySet; @@ -84,7 +86,7 @@ public class Account extends AbstractEntity implements AvatarService.Avatarable private PgpDecryptionService pgpDecryptionService = null; private XmppConnection xmppConnection = null; private long mEndGracePeriod = 0L; - private List bookmarks = new CopyOnWriteArrayList<>(); + private final Map bookmarks = new HashMap<>(); private Presence.Status presenceStatus = Presence.Status.ONLINE; private String presenceStatusMessage = null; @@ -469,36 +471,51 @@ public class Account extends AbstractEntity implements AvatarService.Avatarable return this.roster; } - public List getBookmarks() { - return this.bookmarks; + public Collection getBookmarks() { + return this.bookmarks.values(); } - public void setBookmarks(final CopyOnWriteArrayList bookmarks) { - this.bookmarks = bookmarks; + public void setBookmarks(Map bookmarks) { + synchronized (this.bookmarks) { + this.bookmarks.clear(); + this.bookmarks.putAll(bookmarks); + } + } + + public void putBookmark(Bookmark bookmark) { + synchronized (this.bookmarks) { + this.bookmarks.put(bookmark.getJid(), bookmark); + } + } + + public void removeBookmark(Bookmark bookmark) { + synchronized (this.bookmarks) { + this.bookmarks.remove(bookmark.getJid()); + } + } + + public void removeBookmark(Jid jid) { + synchronized (this.bookmarks) { + this.bookmarks.remove(jid); + } } public Set getBookmarkedJids() { - final Set jids = new HashSet<>(); - for(final Bookmark bookmark : this.bookmarks) { - final Jid jid = bookmark.getJid(); - if (jid != null) { - jids.add(jid.asBareJid()); - } + synchronized (this.bookmarks) { + return new HashSet<>(this.bookmarks.keySet()); } - return jids; } - public boolean hasBookmarkFor(final Jid conferenceJid) { - return getBookmark(conferenceJid) != null; + public boolean hasBookmarkFor(final Jid jid) { + synchronized (this.bookmarks) { + return this.bookmarks.containsKey(jid.asBareJid()); + } } Bookmark getBookmark(final Jid jid) { - for (final Bookmark bookmark : this.bookmarks) { - if (bookmark.getJid() != null && jid.asBareJid().equals(bookmark.getJid().asBareJid())) { - return bookmark; - } + synchronized (this.bookmarks) { + return this.bookmarks.get(jid.asBareJid()); } - return null; } public boolean setAvatar(final String filename) { diff --git a/src/main/java/eu/siacs/conversations/entities/Bookmark.java b/src/main/java/eu/siacs/conversations/entities/Bookmark.java index 4b20b1cd9..8f71c46b9 100644 --- a/src/main/java/eu/siacs/conversations/entities/Bookmark.java +++ b/src/main/java/eu/siacs/conversations/entities/Bookmark.java @@ -41,43 +41,43 @@ public class Bookmark extends Element implements ListItem { this.account = account; } - public static Collection parseFromStorage(Element storage, Account account) { + public static Map parseFromStorage(Element storage, Account account) { if (storage == null) { - return Collections.emptyList(); + return Collections.emptyMap(); } final HashMap bookmarks = new HashMap<>(); for (final Element item : storage.getChildren()) { if (item.getName().equals("conference")) { final Bookmark bookmark = Bookmark.parse(item, account); if (bookmark != null) { - final Bookmark old = bookmarks.put(bookmark.getJid(), bookmark); + final Bookmark old = bookmarks.put(bookmark.jid, bookmark); if (old != null && old.getBookmarkName() != null && bookmark.getBookmarkName() == null) { bookmark.setBookmarkName(old.getBookmarkName()); } } } } - return bookmarks.values(); + return bookmarks; } - public static Collection parseFromPubsub(Element pubsub, Account account) { + public static Map parseFromPubsub(Element pubsub, Account account) { if (pubsub == null) { - return Collections.emptyList(); + return Collections.emptyMap(); } final Element items = pubsub.findChild("items"); if (items != null && Namespace.BOOKMARK.equals(items.getAttribute("node"))) { - final List bookmarks = new ArrayList<>(); + final Map bookmarks = new HashMap<>(); for(Element item : items.getChildren()) { if (item.getName().equals("item")) { final Bookmark bookmark = Bookmark.parseFromItem(item, account); if (bookmark != null) { - bookmarks.add(bookmark); + bookmarks.put(bookmark.jid, bookmark); } } } return bookmarks; } - return Collections.emptyList(); + return Collections.emptyMap(); } public static Bookmark parse(Element element, Account account) { diff --git a/src/main/java/eu/siacs/conversations/generator/IqGenerator.java b/src/main/java/eu/siacs/conversations/generator/IqGenerator.java index bb27c7a93..54085d638 100644 --- a/src/main/java/eu/siacs/conversations/generator/IqGenerator.java +++ b/src/main/java/eu/siacs/conversations/generator/IqGenerator.java @@ -147,6 +147,7 @@ public class IqGenerator extends AbstractGenerator { final Element pubsub = packet.addChild("pubsub", Namespace.PUBSUB); final Element retract = pubsub.addChild("retract"); retract.setAttribute("node", node); + retract.setAttribute("notify","true"); retract.addChild("item").setAttribute("id", id); return packet; } diff --git a/src/main/java/eu/siacs/conversations/parser/MessageParser.java b/src/main/java/eu/siacs/conversations/parser/MessageParser.java index cb272e47a..532474b6c 100644 --- a/src/main/java/eu/siacs/conversations/parser/MessageParser.java +++ b/src/main/java/eu/siacs/conversations/parser/MessageParser.java @@ -227,11 +227,29 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece if (account.getXmppConnection().getFeatures().bookmarksConversion()) { final Element i = items.findChild("item"); final Element storage = i == null ? null : i.findChild("storage", Namespace.BOOKMARKS); - Collection bookmarks = Bookmark.parseFromStorage(storage, account); + Map bookmarks = Bookmark.parseFromStorage(storage, account); mXmppConnectionService.processBookmarksInitial(account, bookmarks, true); - Log.d(Config.LOGTAG,account.getJid().asBareJid()+": processing bookmark PEP event"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": processing bookmark PEP event"); } else { - Log.d(Config.LOGTAG,account.getJid().asBareJid()+": ignoring bookmark PEP event because bookmark conversion was not detected"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ignoring bookmark PEP event because bookmark conversion was not detected"); + } + } else if (Namespace.BOOKMARK.equals(node) && account.getJid().asBareJid().equals(from)) { + final Element item = items.findChild("item"); + final Element retract = items.findChild("retract"); + if (item != null) { + final Bookmark bookmark = Bookmark.parseFromItem(item, account); + if (bookmark != null) { + //TODO find conversation + account.putBookmark(bookmark); + //TODO handle autojoin + } + } + if (retract != null) { + final Jid id = InvalidJid.getNullForInvalid(retract.getAttributeAsJid("id")); + if (id != null) { + account.removeBookmark(id); + Log.d(Config.LOGTAG,account.getJid().asBareJid()+": deleted bookmark for "+id); + } } } else { Log.d(Config.LOGTAG,account.getJid().asBareJid()+" received pubsub notification for node="+node); diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index df8f431e0..931b2d317 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -1564,7 +1564,7 @@ public class XmppConnectionService extends Service { if (response.getType() == IqPacket.TYPE.RESULT) { final Element query1 = response.query(); final Element storage = query1.findChild("storage", "storage:bookmarks"); - Collection bookmarks = Bookmark.parseFromStorage(storage, account); + Map bookmarks = Bookmark.parseFromStorage(storage, account); processBookmarksInitial(a, bookmarks, false); } else { Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": could not fetch bookmarks"); @@ -1580,17 +1580,17 @@ public class XmppConnectionService extends Service { public void onIqPacketReceived(final Account account, final IqPacket response) { if (response.getType() == IqPacket.TYPE.RESULT) { final Element pubsub = response.findChild("pubsub", Namespace.PUBSUB); - final Collection bookmarks = Bookmark.parseFromPubsub(pubsub, account); + final Map bookmarks = Bookmark.parseFromPubsub(pubsub, account); processBookmarksInitial(account, bookmarks, true); } } }); } - public void processBookmarksInitial(Account account, Collection bookmarks, final boolean pep) { + public void processBookmarksInitial(Account account, Map bookmarks, final boolean pep) { final Set previousBookmarks = account.getBookmarkedJids(); final boolean synchronizeWithBookmarks = synchronizeWithBookmarks(); - for (Bookmark bookmark : bookmarks) { + for (Bookmark bookmark : bookmarks.values()) { previousBookmarks.remove(bookmark.getJid().asBareJid()); Conversation conversation = find(bookmark); if (conversation != null) { @@ -1617,10 +1617,11 @@ public class XmppConnectionService extends Service { } } } - account.setBookmarks(new CopyOnWriteArrayList<>(bookmarks)); + account.setBookmarks(bookmarks); } public void createBookmark(final Account account, final Bookmark bookmark) { + account.putBookmark(bookmark); final XmppConnection connection = account.getXmppConnection(); if (connection.getFeatures().bookmarks2()) { final Element item = mIqGenerator.publishBookmarkItem(bookmark); @@ -1633,6 +1634,7 @@ public class XmppConnectionService extends Service { } public void deleteBookmark(final Account account, final Bookmark bookmark) { + account.removeBookmark(bookmark); final XmppConnection connection = account.getXmppConnection(); if (connection.getFeatures().bookmarksConversion()) { IqPacket request = mIqGenerator.deleteItem(Namespace.BOOKMARK, bookmark.getJid().asBareJid().toEscapedString()); @@ -2068,12 +2070,11 @@ public class XmppConnectionService extends Service { getMessageArchiveService().kill(conversation); if (conversation.getMode() == Conversation.MODE_MULTI) { if (conversation.getAccount().getStatus() == Account.State.ONLINE) { - Bookmark bookmark = conversation.getBookmark(); + final Bookmark bookmark = conversation.getBookmark(); if (maySynchronizeWithBookmarks && bookmark != null && synchronizeWithBookmarks()) { if (conversation.getMucOptions().getError() == MucOptions.Error.DESTROYED) { Account account = bookmark.getAccount(); bookmark.setConversation(null); - account.getBookmarks().remove(bookmark); deleteBookmark(account, bookmark); } else if (bookmark.autojoin()) { bookmark.setAutojoin(false); @@ -4473,7 +4474,6 @@ public class XmppConnectionService extends Service { bookmark.setBookmarkName(name); } bookmark.setAutojoin(getPreferences().getBoolean("autojoin", getResources().getBoolean(R.bool.autojoin))); - account.getBookmarks().add(bookmark); createBookmark(account, bookmark); bookmark.setConversation(conversation); } diff --git a/src/main/java/eu/siacs/conversations/ui/ChannelDiscoveryActivity.java b/src/main/java/eu/siacs/conversations/ui/ChannelDiscoveryActivity.java index 648f5c26e..c4eea1d72 100644 --- a/src/main/java/eu/siacs/conversations/ui/ChannelDiscoveryActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ChannelDiscoveryActivity.java @@ -232,7 +232,6 @@ public class ChannelDiscoveryActivity extends XmppActivity implements MenuItem.O } else { bookmark = new Bookmark(account, conversation.getJid().asBareJid()); bookmark.setAutojoin(syncAutojoin); - account.getBookmarks().add(bookmark); xmppConnectionService.createBookmark(account, bookmark); } switchToConversation(conversation); diff --git a/src/main/java/eu/siacs/conversations/ui/ConferenceDetailsActivity.java b/src/main/java/eu/siacs/conversations/ui/ConferenceDetailsActivity.java index ee0334b67..755a6b045 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConferenceDetailsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConferenceDetailsActivity.java @@ -386,9 +386,8 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers } protected void deleteBookmark() { - Account account = mConversation.getAccount(); - Bookmark bookmark = mConversation.getBookmark(); - account.getBookmarks().remove(bookmark); + final Account account = mConversation.getAccount(); + final Bookmark bookmark = mConversation.getBookmark(); bookmark.setConversation(null); xmppConnectionService.deleteBookmark(account, bookmark); updateView(); diff --git a/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java b/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java index e5c8a0619..55c619356 100644 --- a/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java @@ -478,8 +478,7 @@ public class StartConversationActivity extends XmppActivity implements XmppConne builder.setMessage(JidDialog.style(this, R.string.remove_bookmark_text, bookmark.getJid().toEscapedString())); builder.setPositiveButton(R.string.delete, (dialog, which) -> { bookmark.setConversation(null); - Account account = bookmark.getAccount(); - account.getBookmarks().remove(bookmark); + final Account account = bookmark.getAccount(); xmppConnectionService.deleteBookmark(account, bookmark); filter(mSearchEditText.getText().toString()); }); @@ -1041,7 +1040,6 @@ public class StartConversationActivity extends XmppActivity implements XmppConne if (nick != null && !nick.isEmpty() && !nick.equals(MucOptions.defaultNick(account))) { bookmark.setNick(nick); } - account.getBookmarks().add(bookmark); xmppConnectionService.createBookmark(account, bookmark); final Conversation conversation = xmppConnectionService .findOrCreateConversation(account, conferenceJid, true, true, true);