<commit_before>package org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.get;

import java.util.concurrent.BlockingQueue;

import org.apache.log4j.Logger;
import org.buddycloud.channelserver.Configuration;
import org.buddycloud.channelserver.channel.ChannelManager;
import org.buddycloud.channelserver.db.exception.NodeStoreException;
import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.JabberPubsub;
import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.PubSubElementProcessorAbstract;
import org.buddycloud.channelserver.pubsub.model.NodeMembership;
import org.buddycloud.channelserver.utils.XMLConstants;
import org.buddycloud.channelserver.utils.node.item.payload.Buddycloud;
import org.dom4j.Element;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;
import org.xmpp.resultsetmanagement.ResultSet;

public class AffiliationsGet extends PubSubElementProcessorAbstract {

    private final BlockingQueue<Packet> outQueue;
    private final ChannelManager channelManager;

    private IQ requestIq;
    private String node;
    private JID actorJid;
    private IQ result;
    private String firstItem;

    private static final Logger LOGGER = Logger.getLogger(AffiliationsGet.class);


    public AffiliationsGet(BlockingQueue<Packet> outQueue, ChannelManager channelManager) {
        this.outQueue = outQueue;
        this.channelManager = channelManager;
    }

    @Override
    public void process(Element elm, JID actorJID, IQ reqIQ, Element rsm) throws Exception {
        result = IQ.createResultIQ(reqIQ);
        requestIq = reqIQ;
        actorJid = actorJID;

        node = elm.attributeValue(XMLConstants.NODE_ATTR);
        if (!Configuration.getInstance().isLocalJID(requestIq.getFrom())) {
            result.getElement().addAttribute(XMLConstants.REMOTE_SERVER_DISCOVER_ATTR, Boolean.FALSE.toString());

        }
        String namespace = JabberPubsub.NS_PUBSUB_OWNER;
        if (node == null) {
            namespace = JabberPubsub.NAMESPACE_URI;
        }


        Element pubsub = result.setChildElement(XMLConstants.PUBSUB_ELEM, namespace);
        Element affiliations = pubsub.addElement(XMLConstants.AFFILIATION_ELEM);

        if (actorJid == null) {
            actorJid = requestIq.getFrom();
        }

        boolean isProcessedLocally = true;
        if (node == null) {
            isProcessedLocally = getUserMemberships(affiliations);
        } else {
            isProcessedLocally = getNodeAffiliations(affiliations);
        }
        if (!isProcessedLocally) {
            return;
        }

        outQueue.put(result);
    }

    private boolean getNodeAffiliations(Element affiliations) throws NodeStoreException, InterruptedException {
        if (!Configuration.getInstance().isLocalNode(node) && (!channelManager.isCachedNode(node))) {

            makeRemoteRequest(node.split("/")[2]);
            return false;
        }
        ResultSet<NodeMembership> nodeMemberships;
        nodeMemberships = channelManager.getNodeMemberships(node);

        if ((!nodeMemberships.isEmpty()) && (!Configuration.getInstance().isLocalNode(node))) {
            makeRemoteRequest(node.split("/")[2]);
            return false;
        }


        for (NodeMembership nodeMembership : nodeMemberships) {

            if (actorJid.toBareJID().equals(nodeMembership.getUser().toBareJID())) {
                if (null == firstItem) {
                    firstItem = nodeMembership.getUser().toString();
                }

                affiliations.addElement(XMLConstants.AFFILIATION_ELEM).addAttribute(XMLConstants.NODE_ATTR, nodeMembership.getNodeId())
                        .addAttribute(XMLConstants.AFFILIATION_ELEM, nodeMembership.getAffiliation().toString())
                        .addAttribute(XMLConstants.JID_ATTR, nodeMembership.getUser().toString());
            }
        }

        return true;
    }

    private boolean isOwnerModerator() throws NodeStoreException {
        return channelManager.getNodeMembership(node, actorJid).getAffiliation().canAuthorize();
    }

    private boolean getUserMemberships(Element affiliations) throws NodeStoreException, InterruptedException {

        if (!Configuration.getInstance().isLocalJID(actorJid) && (!channelManager.isCachedJID(requestIq.getFrom()))) {
            makeRemoteRequest(actorJid.getDomain());
            return false;
        }

        ResultSet<NodeMembership> memberships = channelManager.getUserMemberships(actorJid);

        for (NodeMembership membership : memberships) {

            if (actorJid.toBareJID().equals(membership.getUser().toBareJID())) {
                LOGGER.trace("Adding affiliation for " + membership.getUser() + " affiliation " + membership.getAffiliation() + " (no node provided)");

                if (null == firstItem) {
                    firstItem = membership.getNodeId();
                }

                affiliations.addElement(XMLConstants.AFFILIATION_ELEM).addAttribute(XMLConstants.NODE_ATTR, membership.getNodeId())
                        .addAttribute(XMLConstants.AFFILIATION_ELEM, membership.getAffiliation().toString())
                        .addAttribute(XMLConstants.JID_ATTR, membership.getUser().toBareJID());
            }

        }
        return true;
    }

    private void makeRemoteRequest(String node) throws InterruptedException {
        LOGGER.info("Going federated for <affiliations />");
        requestIq.setTo(new JID(node).getDomain());
        if (null == requestIq.getElement().element("pubsub").element("actor")) {
            Element actor = requestIq.getElement().element("pubsub").addElement("actor", Buddycloud.NS);
            actor.addText(requestIq.getFrom().toBareJID());
        }
        outQueue.put(requestIq);
    }

    @Override
    public boolean accept(Element elm) {
        return XMLConstants.AFFILIATION_ELEM.equals(elm.getName());
    }
}<commit_msg>Fix issues around affiliations gathering<commit_after>package org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.get;

import java.util.concurrent.BlockingQueue;

import org.apache.log4j.Logger;
import org.buddycloud.channelserver.Configuration;
import org.buddycloud.channelserver.channel.ChannelManager;
import org.buddycloud.channelserver.db.exception.NodeStoreException;
import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.JabberPubsub;
import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.PubSubElementProcessorAbstract;
import org.buddycloud.channelserver.pubsub.model.NodeMembership;
import org.buddycloud.channelserver.utils.XMLConstants;
import org.buddycloud.channelserver.utils.node.item.payload.Buddycloud;
import org.dom4j.Element;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;
import org.xmpp.resultsetmanagement.ResultSet;

public class AffiliationsGet extends PubSubElementProcessorAbstract {

    private final BlockingQueue<Packet> outQueue;
    private final ChannelManager channelManager;

    private IQ requestIq;
    private String node;
    private JID actorJid;
    private IQ result;
    private String firstItem;

    private static final Logger LOGGER = Logger.getLogger(AffiliationsGet.class);


    public AffiliationsGet(BlockingQueue<Packet> outQueue, ChannelManager channelManager) {
        this.outQueue = outQueue;
        this.channelManager = channelManager;
    }

    @Override
    public void process(Element elm, JID actorJID, IQ reqIQ, Element rsm) throws Exception {
        result = IQ.createResultIQ(reqIQ);
        requestIq = reqIQ;
        actorJid = actorJID;

        node = elm.attributeValue(XMLConstants.NODE_ATTR);
        if (!Configuration.getInstance().isLocalJID(requestIq.getFrom())) {
            result.getElement().addAttribute(XMLConstants.REMOTE_SERVER_DISCOVER_ATTR, Boolean.FALSE.toString());

        }
        String namespace = JabberPubsub.NS_PUBSUB_OWNER;
        if (node == null) {
            namespace = JabberPubsub.NAMESPACE_URI;
        }


        Element pubsub = result.setChildElement(XMLConstants.PUBSUB_ELEM, namespace);
        Element affiliations = pubsub.addElement(XMLConstants.AFFILIATIONS_ELEM);

        if (actorJid == null) {
            actorJid = requestIq.getFrom();
        }

        boolean isProcessedLocally = true;
        if (node == null) {
            isProcessedLocally = getUserMemberships(affiliations);
        } else {
            isProcessedLocally = getNodeAffiliations(affiliations);
        }
        if (!isProcessedLocally) {
            return;
        }

        outQueue.put(result);
    }

    private boolean getNodeAffiliations(Element affiliations) throws NodeStoreException, InterruptedException {
        if (!Configuration.getInstance().isLocalNode(node) && (!channelManager.isCachedNode(node))) {

            makeRemoteRequest(node.split("/")[2]);
            return false;
        }
        ResultSet<NodeMembership> nodeMemberships;
        nodeMemberships = channelManager.getNodeMemberships(node);

        if ((!nodeMemberships.isEmpty()) && (!Configuration.getInstance().isLocalNode(node))) {
            makeRemoteRequest(node.split("/")[2]);
            return false;
        }


        for (NodeMembership nodeMembership : nodeMemberships) {

            if (actorJid.toBareJID().equals(nodeMembership.getUser().toBareJID())) {
                if (null == firstItem) {
                    firstItem = nodeMembership.getUser().toString();
                }

                affiliations.addElement(XMLConstants.AFFILIATION_ELEM).addAttribute(XMLConstants.NODE_ATTR, nodeMembership.getNodeId())
                        .addAttribute(XMLConstants.AFFILIATION_ELEM, nodeMembership.getAffiliation().toString())
                        .addAttribute(XMLConstants.JID_ATTR, nodeMembership.getUser().toString());
            }
        }

        return true;
    }

    private boolean isOwnerModerator() throws NodeStoreException {
        return channelManager.getNodeMembership(node, actorJid).getAffiliation().canAuthorize();
    }

    private boolean getUserMemberships(Element affiliations) throws NodeStoreException, InterruptedException {

        if (!Configuration.getInstance().isLocalJID(actorJid) && (!channelManager.isCachedJID(requestIq.getFrom()))) {
            makeRemoteRequest(actorJid.getDomain());
            return false;
        }

        ResultSet<NodeMembership> memberships = channelManager.getUserMemberships(actorJid);

        for (NodeMembership membership : memberships) {

            if (actorJid.toBareJID().equals(membership.getUser().toBareJID())) {
                LOGGER.trace("Adding affiliation for " + membership.getUser() + " affiliation " + membership.getAffiliation() + " (no node provided)");

                if (null == firstItem) {
                    firstItem = membership.getNodeId();
                }

                affiliations.addElement(XMLConstants.AFFILIATION_ELEM).addAttribute(XMLConstants.NODE_ATTR, membership.getNodeId())
                        .addAttribute(XMLConstants.AFFILIATION_ELEM, membership.getAffiliation().toString())
                        .addAttribute(XMLConstants.JID_ATTR, membership.getUser().toBareJID());
            }

        }
        return true;
    }

    private void makeRemoteRequest(String node) throws InterruptedException {
        LOGGER.info("Going federated for <affiliations />");
        requestIq.setTo(new JID(node).getDomain());
        if (null == requestIq.getElement().element("pubsub").element("actor")) {
            Element actor = requestIq.getElement().element("pubsub").addElement("actor", Buddycloud.NS);
            actor.addText(requestIq.getFrom().toBareJID());
        }
        outQueue.put(requestIq);
    }

    @Override
    public boolean accept(Element elm) {
        return XMLConstants.AFFILIATIONS_ELEM.equals(elm.getName());
    }
}
{'fraction_non_alphanumeric': 0.10221664575491426, 'fraction_numerical': 0.0006691760769552489, 'mean_word_length': 3.0847283908438676, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1924783', 'n_tokens_mistral': 3386, 'n_tokens_neox': 3245, 'n_words': 518}