Issue in blocking user in chatlist using smack and open fire server
Asked Answered
H

4

8

I want to block a particular friend from my chat list with XMPP. code works fine. There is no Exception, but I am not able to block a user. I'm using open fire server. what changes should i made on server?

Can u guys have any idea?

My code:

public void XMPPAddNewPrivacyList(Connection connection, String userName) {

    String listName = "newList";

    // Create the list of PrivacyItem that will allow or
    // deny some privacy aspect

    List<PrivacyItem> privacyItems = new Vector<PrivacyItem>();

    PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid.toString(),
            false, 1);
    item.setValue(userName);
    privacyItems.add(item);

    // Create the new list.

    try {
        PrivacyListManager privacyManager = new PrivacyListManager(connection);
        privacyManager = PrivacyListManager
                .getInstanceFor(connection);
        privacyManager.createPrivacyList(listName, privacyItems);

    } catch (XMPPException e) {
        System.out.println("PRIVACY_ERROR: " + e);
    }
}
Hoppe answered 4/9, 2013 at 9:58 Comment(2)
hi I am getting PrivacyListManager.getInstanceFor(connection) as null. Please help me, I am not able to understand why it is getting as null.Episcopate
Hi, it is working fine in java, but I am getting exception in asmack. I know where the issue is, can you give me the providermanager for privacy list code for me.Episcopate
N
5

try this ...

public boolean blockFriend(String friendName) {

    PrivacyItem item=new PrivacyItem(PrivacyItem.Type.jid,friendName, false, 7);
    PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(connection);
    List<PrivacyItem> list=new ArrayList<PrivacyItem>();
    list.add(item);

    try {
        privacyManager.updatePrivacyList(NEWLIST, list);
        privacyManager.setActiveListName(NEWLIST);
        return true;
    } catch (SmackException.NoResponseException |XMPPException.XMPPErrorException | SmackException.NotConnectedException e) {
        e.printStackTrace();
        return false;
    }


}

and for unblock just replace false with true in object of privacyitem `

Nonintervention answered 11/5, 2015 at 6:15 Comment(0)
P
0

I think that the problem should be one of the following:

  • userName isn't correct, for example "[email protected]".
  • You are not listening for privacy changes I mean, you didn't implement the PrivacyListListener interface.
  • In the PrivacyItem constructor don't you should use PrivacyRule.JID instead PrivacyItem.Type.jid.toString()?.
  • If you wanna block a friend don't you should use updatePrivacyList instead createPrivacyList.

I reccomend you take a closer look to the documentation Smack documentation

Puebla answered 22/9, 2013 at 3:59 Comment(2)
Hi astinx, please post a workable code for blocking user. I am facing problem with blocking user. I am getting PrivacyListManager .getInstanceFor(connection) returning null.Please help meEpiscopate
Sorry, we already buried long ago the XMPP tech. We are now using a node is a much better approach, much more faster and reliable, we had a chat with almost 2000 users, XMMP couldn't handle this requirement.Puebla
H
0
    // Here function for block user on xmpp

    public boolean blockUser(String userName) {

    String jid = userName@localhost
    String listName = "public";

    // Create the list of PrivacyItem that will allow or
    // deny some privacy aspect

    //ArrayList privacyItems = new ArrayList();

    List<PrivacyItem> privacyItems = new Vector<PrivacyItem>();


    PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid, jid, false, 1);
    // item.setValue(userName);
    item.setFilterIQ(false);
    item.setFilterMessage(false);
    item.setFilterPresenceIn(false);
    item.setFilterPresenceOut(false);

    privacyItems.add(item);

    // Get the privacy manager for the current connection.

    // Create the new list.
    PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(XMPPUtils.INSTANCE.connection);


    try {
        privacyManager.updatePrivacyList(listName, privacyItems);
        privacyManager.setActiveListName(listName);

        return true;
    } catch (Exception e) {
        Log.e("PRIVACY_ERROR: ", " " + e.toString());
        e.printStackTrace();
    }

    return false;
}
Heroism answered 27/8, 2015 at 10:39 Comment(0)
A
0

Privacy is a method for users to block communications from particular other users. In XMPP this is done by managing one's privacy lists.

1 - In order to add a new list in the server, the client MAY implement something like:

 

    // Create a privacy manager for the current connection._
    PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(myConnection);
    // Retrieve server privacy lists_
    PrivacyList[] lists = privacyManager.getPrivacyLists();

2 - In order to add a new list in the server, the client MAY implement something like:

 

// Set the name of the list_
String listName = "newList";

// Create the list of PrivacyItem that will allow or deny some privacy aspect_
String user = "[email protected]";
String groupName = "enemies";
ArrayList privacyItems = new ArrayList();

PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid, user, true, 1);
privacyItems.add(item);

item = new PrivacyItem(PrivacyItem.Type.subscription, PrivacyItem.SUBSCRIPTION_BOTH, true, 2);
privacyItems.add(item);

item = new PrivacyItem(PrivacyItem.Type.group, groupName, false, 3);
item.setFilterMessage(true);
privacyItems.add(item);

// Get the privacy manager for the current connection._
PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(myConnection);
// Create the new list._
privacyManager.createPrivacyList(listName, privacyItems);

Affectionate answered 11/9, 2015 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.