reset affiliation when inviting someone not currently in group. fixes #4146

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/services/XmppConnectionService.java | 42 
1 file changed, 18 insertions(+), 24 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/services/XmppConnectionService.java 🔗

@@ -471,7 +471,6 @@ public class XmppConnectionService extends Service {
     private OpenPgpServiceConnection pgpServiceConnection;
     private PgpEngine mPgpEngine = null;
     private WakeLock wakeLock;
-    private PowerManager pm;
     private LruCache<String, Bitmap> mBitmapCache;
     private final BroadcastReceiver mInternalEventReceiver = new InternalEventReceiver();
     private final BroadcastReceiver mInternalScreenEventReceiver = new InternalEventReceiver();
@@ -1172,7 +1171,7 @@ public class XmppConnectionService extends Service {
             this.pgpServiceConnection.bindToService();
         }
 
-        this.pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
+        final PowerManager pm = ContextCompat.getSystemService(this, PowerManager.class);
         this.wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Conversations:Service");
 
         toggleForegroundService();
@@ -3337,35 +3336,26 @@ public class XmppConnectionService extends Service {
 
     public void changeAffiliationInConference(final Conversation conference, Jid user, final MucOptions.Affiliation affiliation, final OnAffiliationChanged callback) {
         final Jid jid = user.asBareJid();
-        IqPacket request = this.mIqGenerator.changeAffiliation(conference, jid, affiliation.toString());
-        sendIqPacket(conference.getAccount(), request, new OnIqPacketReceived() {
-            @Override
-            public void onIqPacketReceived(Account account, IqPacket packet) {
-                if (packet.getType() == IqPacket.TYPE.RESULT) {
-                    conference.getMucOptions().changeAffiliation(jid, affiliation);
-                    getAvatarService().clear(conference);
+        final IqPacket request = this.mIqGenerator.changeAffiliation(conference, jid, affiliation.toString());
+        sendIqPacket(conference.getAccount(), request, (account, response) -> {
+            if (response.getType() == IqPacket.TYPE.RESULT) {
+                conference.getMucOptions().changeAffiliation(jid, affiliation);
+                getAvatarService().clear(conference);
+                if (callback != null) {
                     callback.onAffiliationChangedSuccessful(jid);
                 } else {
-                    callback.onAffiliationChangeFailed(jid, R.string.could_not_change_affiliation);
+                    Log.d(Config.LOGTAG, "changed affiliation of " + user + " to " + affiliation);
                 }
+            } else if (callback != null) {
+                callback.onAffiliationChangeFailed(jid, R.string.could_not_change_affiliation);
+            } else {
+                Log.d(Config.LOGTAG, "unable to change affiliation");
             }
         });
     }
 
-    public void changeAffiliationsInConference(final Conversation conference, MucOptions.Affiliation before, MucOptions.Affiliation after) {
-        List<Jid> jids = new ArrayList<>();
-        for (MucOptions.User user : conference.getMucOptions().getUsers()) {
-            if (user.getAffiliation() == before && user.getRealJid() != null) {
-                jids.add(user.getRealJid());
-            }
-        }
-        IqPacket request = this.mIqGenerator.changeAffiliation(conference, jids, after.toString());
-        sendIqPacket(conference.getAccount(), request, mDefaultIqHandler);
-    }
-
     public void changeRoleInConference(final Conversation conference, final String nick, MucOptions.Role role) {
         IqPacket request = this.mIqGenerator.changeRole(conference, nick, role.toString());
-        Log.d(Config.LOGTAG, request.toString());
         sendIqPacket(conference.getAccount(), request, (account, packet) -> {
             if (packet.getType() != IqPacket.TYPE.RESULT) {
                 Log.d(Config.LOGTAG, account.getJid().asBareJid() + " unable to change role of " + nick);
@@ -3928,9 +3918,13 @@ public class XmppConnectionService extends Service {
         new Thread(() -> reconnectAccount(account, false, true)).start();
     }
 
-    public void invite(Conversation conversation, Jid contact) {
+    public void invite(final Conversation conversation, final Jid contact) {
         Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": inviting " + contact + " to " + conversation.getJid().asBareJid());
-        MessagePacket packet = mMessageGenerator.invite(conversation, contact);
+        final MucOptions.User user = conversation.getMucOptions().findUserByRealJid(contact.asBareJid());
+        if (user == null || user.getAffiliation() == MucOptions.Affiliation.OUTCAST) {
+            changeAffiliationInConference(conversation, contact, MucOptions.Affiliation.NONE, null);
+        }
+        final MessagePacket packet = mMessageGenerator.invite(conversation, contact);
         sendMessagePacket(conversation.getAccount(), packet);
     }