set access model to open when publishing avatar. fixes #3291

This commit is contained in:
Daniel Gultsch 2018-11-25 20:58:48 +01:00
parent 3906643d44
commit 1de385dcb9
4 changed files with 82 additions and 33 deletions

View File

@ -586,7 +586,7 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
@Override @Override
public void onIqPacketReceived(Account account, IqPacket packet) { public void onIqPacketReceived(Account account, IqPacket packet) {
final Element error = packet.getType() == IqPacket.TYPE.ERROR ? packet.findChild("error") : null; 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) { if (firstAttempt && preConditionNotMet) {
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": precondition wasn't met for device list. pushing node configuration"); 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() { mXmppConnectionService.pushNodeConfiguration(account, AxolotlService.PEP_DEVICE_LIST, publishOptions, new XmppConnectionService.OnConfigurationPushed() {
@ -802,8 +802,7 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() { mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
@Override @Override
public void onIqPacketReceived(final Account account, IqPacket packet) { public void onIqPacketReceived(final Account account, IqPacket packet) {
final Element error = packet.getType() == IqPacket.TYPE.ERROR ? packet.findChild("error") : null; final boolean preconditionNotMet = PublishOptions.preconditionNotMet(packet);
final boolean preconditionNotMet = error != null && error.hasChild("precondition-not-met", Namespace.PUBSUB_ERROR);
if (firstAttempt && preconditionNotMet) { if (firstAttempt && preconditionNotMet) {
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": precondition wasn't met for bundle. pushing node configuration"); Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": precondition wasn't met for bundle. pushing node configuration");
final String node = AxolotlService.PEP_BUNDLES + ":" + getOwnDeviceId(); final String node = AxolotlService.PEP_BUNDLES + ":" + getOwnDeviceId();
@ -830,7 +829,7 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
if (preconditionNotMet) { 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 { } 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; pepBroken = true;
} }

View File

@ -138,12 +138,12 @@ public class IqGenerator extends AbstractGenerator {
return packet; return packet;
} }
public IqPacket publishAvatar(Avatar avatar) { public IqPacket publishAvatar(Avatar avatar, Bundle options) {
final Element item = new Element("item"); final Element item = new Element("item");
item.setAttribute("id", avatar.sha1sum); item.setAttribute("id", avatar.sha1sum);
final Element data = item.addChild("data", "urn:xmpp:avatar:data"); final Element data = item.addChild("data", "urn:xmpp:avatar:data");
data.setContent(avatar.image); 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) { public IqPacket publishElement(final String namespace,final Element element, final Bundle options) {
@ -153,7 +153,7 @@ public class IqGenerator extends AbstractGenerator {
return publish(namespace, item, options); 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"); final Element item = new Element("item");
item.setAttribute("id", avatar.sha1sum); item.setAttribute("id", avatar.sha1sum);
final Element metadata = item final Element metadata = item
@ -164,7 +164,7 @@ public class IqGenerator extends AbstractGenerator {
info.setAttribute("height", avatar.height); info.setAttribute("height", avatar.height);
info.setAttribute("width", avatar.height); info.setAttribute("width", avatar.height);
info.setAttribute("type", avatar.type); 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) { public IqPacket retrievePepAvatar(final Avatar avatar) {

View File

@ -1473,9 +1473,7 @@ public class XmppConnectionService extends Service {
if (response.getType() == IqPacket.TYPE.RESULT) { if (response.getType() == IqPacket.TYPE.RESULT) {
return; return;
} }
final Element error = response.getType() == IqPacket.TYPE.ERROR ? response.findChild("error") : null; if (retry && PublishOptions.preconditionNotMet(response)) {
final boolean preconditionNotMet = error != null && error.hasChild("precondition-not-met", Namespace.PUBSUB_ERROR);
if (retry && preconditionNotMet) {
pushNodeConfiguration(account, node, options, new OnConfigurationPushed() { pushNodeConfiguration(account, node, options, new OnConfigurationPushed() {
@Override @Override
public void onPushSucceeded() { public void onPushSucceeded() {
@ -2940,33 +2938,39 @@ public class XmppConnectionService extends Service {
}); });
} }
public void publishAvatar(Account account, final Avatar avatar, final OnAvatarPublication callback) { 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() { this.sendIqPacket(account, packet, new OnIqPacketReceived() {
@Override @Override
public void onIqPacketReceived(Account account, IqPacket result) { public void onIqPacketReceived(Account account, IqPacket result) {
if (result.getType() == IqPacket.TYPE.RESULT) { if (result.getType() == IqPacket.TYPE.RESULT) {
final IqPacket packet = XmppConnectionService.this.mIqGenerator.publishAvatarMetadata(avatar); publishAvatarMetadata(account, avatar, options,true, callback);
sendIqPacket(account, packet, new OnIqPacketReceived() { } else if (retry && PublishOptions.preconditionNotMet(result)) {
@Override pushNodeConfiguration(account, "urn:xmpp:avatar:data", options, new OnConfigurationPushed() {
public void onIqPacketReceived(Account account, IqPacket result) { @Override
if (result.getType() == IqPacket.TYPE.RESULT) { public void onPushSucceeded() {
if (account.setAvatar(avatar.getFilename())) { Log.d(Config.LOGTAG,account.getJid().asBareJid()+": changed node configuration for avatar node");
getAvatarService().clear(account); publishAvatar(account, avatar, options, false, callback);
databaseBackend.updateAccount(account); }
}
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": published avatar " + (avatar.size / 1024) + "KiB"); @Override
if (callback != null) { public void onPushFailed() {
callback.onAvatarPublicationSucceeded(); Log.d(Config.LOGTAG,account.getJid().asBareJid()+": unable to change node configuration for avatar node");
} publishAvatar(account, avatar, null, false, callback);
} else { }
if (callback != null) { });
callback.onAvatarPublicationFailed(R.string.error_publish_avatar_server_reject);
}
}
}
});
} else { } else {
Element error = result.findChild("error"); Element error = result.findChild("error");
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server rejected avatar " + (avatar.size / 1024) + "KiB " + (error != null ? error.toString() : "")); Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server rejected avatar " + (avatar.size / 1024) + "KiB " + (error != null ? error.toString() : ""));
@ -2978,6 +2982,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) { public void republishAvatarIfNeeded(Account account) {
if (account.getAxolotlService().isPepBroken()) { if (account.getAxolotlService().isPepBroken()) {
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": skipping republication of avatar because pep is broken"); Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": skipping republication of avatar because pep is broken");

View File

@ -2,6 +2,10 @@ package eu.siacs.conversations.xmpp.pep;
import android.os.Bundle; import android.os.Bundle;
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xml.Namespace;
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
public class PublishOptions { public class PublishOptions {
private PublishOptions() { private PublishOptions() {
@ -21,4 +25,9 @@ public class PublishOptions {
return options; 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);
}
} }