aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/services/muc/listener/ConferenceConfigurationFetched.java
blob: a386db8861714e779e1086dd53e072fea5be0d5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package de.thedevstack.conversationsplus.services.muc.listener;

import android.util.Log;

import de.thedevstack.conversationsplus.Config;
import de.thedevstack.conversationsplus.entities.Account;
import de.thedevstack.conversationsplus.entities.Conversation;
import de.thedevstack.conversationsplus.entities.MucOptions;
import de.thedevstack.conversationsplus.generator.PresenceGenerator;
import de.thedevstack.conversationsplus.persistance.DatabaseBackend;
import de.thedevstack.conversationsplus.services.muc.OnConferenceConfigurationFetched;
import de.thedevstack.conversationsplus.services.muc.OnConferenceJoined;
import de.thedevstack.conversationsplus.utils.XmppConnectionServiceAccessor;
import de.thedevstack.conversationsplus.utils.XmppSendUtil;
import de.thedevstack.conversationsplus.xml.Element;
import de.thedevstack.conversationsplus.xmpp.OnIqPacketReceived;
import de.thedevstack.conversationsplus.xmpp.jid.Jid;
import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket;
import de.thedevstack.conversationsplus.xmpp.stanzas.PresencePacket;

/**
 */
public class ConferenceConfigurationFetched implements OnConferenceConfigurationFetched {
    private OnConferenceJoined onConferenceJoined;

    public ConferenceConfigurationFetched(OnConferenceJoined onConferenceJoined) {
        this.onConferenceJoined = onConferenceJoined;
    }

    private void join(Conversation conversation) {
        Account account = conversation.getAccount();
        final MucOptions mucOptions = conversation.getMucOptions();
        final Jid joinJid = mucOptions.getSelf().getFullJid();
        Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": joining conversation " + joinJid.toString());
        long lastMessageTransmitted = conversation.getLastMessageTransmitted();
        PresencePacket packet = PresenceGenerator.generateMucJoin(account, joinJid, mucOptions, lastMessageTransmitted);
        XmppSendUtil.sendPresencePacket(account, packet);
        if (onConferenceJoined != null) {
            onConferenceJoined.onConferenceJoined(conversation);
        }
        if (!joinJid.equals(conversation.getJid())) {
            conversation.setContactJid(joinJid);
            DatabaseBackend.getInstance().updateConversation(conversation);
        }

        if (mucOptions.mamSupport()) {
            XmppConnectionServiceAccessor.xmppConnectionService.getMessageArchiveService().catchupMUC(conversation);
        }
        if (mucOptions.membersOnly() && mucOptions.nonanonymous()) {
            fetchConferenceMembers(conversation);
        }
        XmppConnectionServiceAccessor.xmppConnectionService.sendUnsentMessages(conversation);
    }

    @Override
    public void onConferenceConfigurationFetched(Conversation conversation) {
        join(conversation);
    }

    @Override
    public void onFetchFailed(final Conversation conversation, Element error) {
        join(conversation);
        XmppConnectionServiceAccessor.xmppConnectionService.fetchConferenceConfiguration(conversation);
    }

    private void fetchConferenceMembers(final Conversation conversation) {
        final Account account = conversation.getAccount();
        final String[] affiliations = {"member","admin","owner"};
        OnIqPacketReceived callback = new OnIqPacketReceived() {

            private int i = 0;

            @Override
            public void onIqPacketReceived(Account account, IqPacket packet) {
                Element query = packet.query("http://jabber.org/protocol/muc#admin");
                if (packet.getType() == IqPacket.TYPE.RESULT && query != null) {
                    for(Element child : query.getChildren()) {
                        if ("item".equals(child.getName())) {
                            conversation.getMucOptions().putMember(child.getAttributeAsJid("jid"));
                        }
                    }
                } else {
                    Log.d(Config.LOGTAG,account.getJid().toBareJid()+": could not request affiliation "+affiliations[i]+" in "+conversation.getJid().toBareJid());
                }
                ++i;
                if (i >= affiliations.length) {
                    Log.d(Config.LOGTAG,account.getJid().toBareJid()+": retrieved members for "+conversation.getJid().toBareJid()+": "+conversation.getMucOptions().getMembers());
                }
            }
        };
        for(String affiliation : affiliations) {
            XmppSendUtil.sendIqPacket(account, XmppConnectionServiceAccessor.xmppConnectionService.getIqGenerator().queryAffiliation(conversation, affiliation), callback);
        }
        Log.d(Config.LOGTAG,account.getJid().toBareJid()+": fetching members for "+conversation.getName());
    }
}