From 126b71398479c39c915d2205ec316703a9d2dfbb Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Fri, 30 Nov 2018 21:15:16 +0100 Subject: set access model to open when publishing avatar --- .../messenger/crypto/axolotl/AxolotlService.java | 19 +++-- .../de/pixart/messenger/generator/IqGenerator.java | 8 +-- .../messenger/services/XmppConnectionService.java | 83 ++++++++++++++++------ .../pixart/messenger/xmpp/pep/PublishOptions.java | 9 +++ 4 files changed, 84 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/main/java/de/pixart/messenger/crypto/axolotl/AxolotlService.java b/src/main/java/de/pixart/messenger/crypto/axolotl/AxolotlService.java index cc9d85008..d16aa7afe 100644 --- a/src/main/java/de/pixart/messenger/crypto/axolotl/AxolotlService.java +++ b/src/main/java/de/pixart/messenger/crypto/axolotl/AxolotlService.java @@ -80,10 +80,10 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { private final Map fetchDeviceListStatus = new HashMap<>(); private final HashMap> fetchDeviceIdsMap = new HashMap<>(); private final SerialSingleThreadExecutor executor; - private final HashSet cleanedOwnDeviceIds = new HashSet<>(); private int numPublishTriesOnEmptyPep = 0; private boolean pepBroken = false; private int lastDeviceListNotificationHash = 0; + private final HashSet cleanedOwnDeviceIds = new HashSet<>(); private Set postponedSessions = new HashSet<>(); //sessions stored here will receive after mam catchup treatment private AtomicBoolean changeAccessMode = new AtomicBoolean(false); @@ -145,8 +145,8 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { } private static class AxolotlAddressMap { - protected final Object MAP_LOCK = new Object(); protected Map> map; + protected final Object MAP_LOCK = new Object(); public AxolotlAddressMap() { this.map = new HashMap<>(); @@ -586,7 +586,7 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { @Override public void onIqPacketReceived(Account account, IqPacket packet) { final Element error = packet.getType() == IqPacket.TYPE.ERROR ? packet.findChild("error") : null; - final boolean preConditionNotMet = error != null && error.hasChild("precondition-not-met", Namespace.PUBSUB_ERROR); + final boolean preConditionNotMet = PublishOptions.preconditionNotMet(packet); if (firstAttempt && preConditionNotMet) { Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": precondition wasn't met for device list. pushing node configuration"); mXmppConnectionService.pushNodeConfiguration(account, AxolotlService.PEP_DEVICE_LIST, publishOptions, new XmppConnectionService.OnConfigurationPushed() { @@ -608,7 +608,7 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { } if (packet.getType() == IqPacket.TYPE.ERROR) { if (preConditionNotMet) { - Log.d(Config.LOGTAG,account.getJid().asBareJid()+": device list pre condition still not met on second attempt"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": device list pre condition still not met on second attempt"); } else if (error != null) { pepBroken = true; Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing own device id" + packet.findChild("error")); @@ -802,8 +802,7 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() { @Override public void onIqPacketReceived(final Account account, IqPacket packet) { - final Element error = packet.getType() == IqPacket.TYPE.ERROR ? packet.findChild("error") : null; - final boolean preconditionNotMet = error != null && error.hasChild("precondition-not-met", Namespace.PUBSUB_ERROR); + final boolean preconditionNotMet = PublishOptions.preconditionNotMet(packet); if (firstAttempt && preconditionNotMet) { Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": precondition wasn't met for bundle. pushing node configuration"); final String node = AxolotlService.PEP_BUNDLES + ":" + getOwnDeviceId(); @@ -828,9 +827,9 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { } } else if (packet.getType() == IqPacket.TYPE.ERROR) { if (preconditionNotMet) { - Log.d(Config.LOGTAG,getLogprefix(account) + "bundle precondition still not met after second attempt"); + Log.d(Config.LOGTAG, getLogprefix(account) + "bundle precondition still not met after second attempt"); } else { - Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing bundle: " + error); + Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing bundle: " + packet.toString()); } pepBroken = true; } @@ -1154,7 +1153,7 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { private void removeFromDeviceAnnouncement(Integer id) { HashSet temp = new HashSet<>(getOwnDeviceIds()); if (temp.remove(id)) { - Log.d(Config.LOGTAG,account.getJid().asBareJid()+" remove own device id "+id+" from announcement. devices left:"+temp); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + " remove own device id " + id + " from announcement. devices left:" + temp); publishOwnDeviceId(temp); } } @@ -1311,7 +1310,7 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { return false; } sessions.addAll(findOwnSessions()); - for(XmppAxolotlSession session : sessions) { + for (XmppAxolotlSession session : sessions) { axolotlMessage.addDevice(session); } return true; diff --git a/src/main/java/de/pixart/messenger/generator/IqGenerator.java b/src/main/java/de/pixart/messenger/generator/IqGenerator.java index cb56d66a8..160496273 100644 --- a/src/main/java/de/pixart/messenger/generator/IqGenerator.java +++ b/src/main/java/de/pixart/messenger/generator/IqGenerator.java @@ -137,12 +137,12 @@ public class IqGenerator extends AbstractGenerator { return packet; } - public IqPacket publishAvatar(Avatar avatar) { + public IqPacket publishAvatar(Avatar avatar, Bundle options) { final Element item = new Element("item"); item.setAttribute("id", avatar.sha1sum); final Element data = item.addChild("data", "urn:xmpp:avatar:data"); data.setContent(avatar.image); - return publish("urn:xmpp:avatar:data", item); + return publish("urn:xmpp:avatar:data", item, options); } public IqPacket publishElement(final String namespace, final Element element, final Bundle options) { @@ -152,7 +152,7 @@ public class IqGenerator extends AbstractGenerator { return publish(namespace, item, options); } - public IqPacket publishAvatarMetadata(final Avatar avatar) { + public IqPacket publishAvatarMetadata(final Avatar avatar, final Bundle options) { final Element item = new Element("item"); item.setAttribute("id", avatar.sha1sum); final Element metadata = item @@ -163,7 +163,7 @@ public class IqGenerator extends AbstractGenerator { info.setAttribute("height", avatar.height); info.setAttribute("width", avatar.height); info.setAttribute("type", avatar.type); - return publish("urn:xmpp:avatar:metadata", item); + return publish("urn:xmpp:avatar:metadata", item, options); } public IqPacket retrievePepAvatar(final Avatar avatar) { diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index 708200355..c435c1f35 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -1671,9 +1671,7 @@ public class XmppConnectionService extends Service { if (response.getType() == IqPacket.TYPE.RESULT) { return; } - final Element error = response.getType() == IqPacket.TYPE.ERROR ? response.findChild("error") : null; - final boolean preconditionNotMet = error != null && error.hasChild("precondition-not-met", Namespace.PUBSUB_ERROR); - if (retry && preconditionNotMet) { + if (retry && PublishOptions.preconditionNotMet(response)) { pushNodeConfiguration(account, node, options, new OnConfigurationPushed() { @Override public void onPushSucceeded() { @@ -3290,30 +3288,36 @@ public class XmppConnectionService extends Service { } public void publishAvatar(Account account, final Avatar avatar, final OnAvatarPublication callback) { - IqPacket packet = this.mIqGenerator.publishAvatar(avatar); + final Bundle options; + if (account.getXmppConnection().getFeatures().pepPublishOptions()) { + options = PublishOptions.openAccess(); + } else { + options = null; + } + publishAvatar(account, avatar, options, true, callback); + } + + public void publishAvatar(Account account, final Avatar avatar, final Bundle options, final boolean retry, final OnAvatarPublication callback) { + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": publishing avatar. options=" + options); + IqPacket packet = this.mIqGenerator.publishAvatar(avatar, options); this.sendIqPacket(account, packet, new OnIqPacketReceived() { @Override public void onIqPacketReceived(Account account, IqPacket result) { if (result.getType() == IqPacket.TYPE.RESULT) { - final IqPacket packet = XmppConnectionService.this.mIqGenerator.publishAvatarMetadata(avatar); - sendIqPacket(account, packet, new OnIqPacketReceived() { + publishAvatarMetadata(account, avatar, options, true, callback); + } else if (retry && PublishOptions.preconditionNotMet(result)) { + pushNodeConfiguration(account, "urn:xmpp:avatar:data", options, new OnConfigurationPushed() { @Override - public void onIqPacketReceived(Account account, IqPacket result) { - if (result.getType() == IqPacket.TYPE.RESULT) { - if (account.setAvatar(avatar.getFilename())) { - getAvatarService().clear(account); - databaseBackend.updateAccount(account); - } - Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": published avatar " + (avatar.size / 1024) + "KiB"); - if (callback != null) { - callback.onAvatarPublicationSucceeded(); - } - } else { - if (callback != null) { - callback.onAvatarPublicationFailed(R.string.error_publish_avatar_server_reject); - } - } + public void onPushSucceeded() { + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": changed node configuration for avatar node"); + publishAvatar(account, avatar, options, false, callback); + } + + @Override + public void onPushFailed() { + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to change node configuration for avatar node"); + publishAvatar(account, avatar, null, false, callback); } }); } else { @@ -3327,6 +3331,43 @@ public class XmppConnectionService extends Service { }); } + public void publishAvatarMetadata(Account account, final Avatar avatar, final Bundle options, final boolean retry, final OnAvatarPublication callback) { + final IqPacket packet = XmppConnectionService.this.mIqGenerator.publishAvatarMetadata(avatar, options); + sendIqPacket(account, packet, new OnIqPacketReceived() { + @Override + public void onIqPacketReceived(Account account, IqPacket result) { + if (result.getType() == IqPacket.TYPE.RESULT) { + if (account.setAvatar(avatar.getFilename())) { + getAvatarService().clear(account); + databaseBackend.updateAccount(account); + } + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": published avatar " + (avatar.size / 1024) + "KiB"); + if (callback != null) { + callback.onAvatarPublicationSucceeded(); + } + } else if (retry && PublishOptions.preconditionNotMet(result)) { + pushNodeConfiguration(account, "urn:xmpp:avatar:metadata", options, new OnConfigurationPushed() { + @Override + public void onPushSucceeded() { + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": changed node configuration for avatar meta data node"); + publishAvatarMetadata(account, avatar, options, false, callback); + } + + @Override + public void onPushFailed() { + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to change node configuration for avatar meta data node"); + publishAvatarMetadata(account, avatar, null, false, callback); + } + }); + } else { + if (callback != null) { + callback.onAvatarPublicationFailed(R.string.error_publish_avatar_server_reject); + } + } + } + }); + } + public void republishAvatarIfNeeded(Account account) { if (account.getAxolotlService().isPepBroken()) { Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": skipping republication of avatar because pep is broken"); diff --git a/src/main/java/de/pixart/messenger/xmpp/pep/PublishOptions.java b/src/main/java/de/pixart/messenger/xmpp/pep/PublishOptions.java index 56b85169c..53b12d239 100644 --- a/src/main/java/de/pixart/messenger/xmpp/pep/PublishOptions.java +++ b/src/main/java/de/pixart/messenger/xmpp/pep/PublishOptions.java @@ -2,6 +2,10 @@ package de.pixart.messenger.xmpp.pep; import android.os.Bundle; +import de.pixart.messenger.utils.Namespace; +import de.pixart.messenger.xml.Element; +import de.pixart.messenger.xmpp.stanzas.IqPacket; + public class PublishOptions { private PublishOptions() { @@ -21,4 +25,9 @@ public class PublishOptions { return options; } + public static boolean preconditionNotMet(IqPacket response) { + final Element error = response.getType() == IqPacket.TYPE.ERROR ? response.findChild("error") : null; + return error != null && error.hasChild("precondition-not-met", Namespace.PUBSUB_ERROR); + } + } \ No newline at end of file -- cgit v1.2.3