本次Spark二次开发是为了客服模块的开发, 能让用户一旦点击该客服则直接自己主动加入好友。而客服放则需自己主动加入好友,不同弹出对话框进行允许,这方便的广大客服。
如今废话不多说,直接上代码。
package org.jivesoftware.spark.ui; import java.util.ArrayList; import org.jivesoftware.spark.util.log.Log; import org.jivesoftware.spark.util.ModelUtil; import org.jivesoftware.resource.Res; import org.jivesoftware.smack.Roster; import org.jivesoftware.smack.RosterEntry; import org.jivesoftware.smack.RosterGroup; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.packet.Presence; import org.jivesoftware.smack.packet.RosterPacket; import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.spark.SparkManager; import org.jivesoftware.spark.UserManager; /** * 处理全部订阅请求 * @author jian * */ public class MySubscription { private String jid; //构造函数 public MySubscription(){ } /** * 调用 * @param jid 主键ID */ public void invoke(final String jid){ this.jid=jid; final Roster roster=SparkManager.getConnection().getRoster();//返回好友冊 //假设用户已经在好友其中,则不显示 RosterEntry entry=roster.getEntry(jid);//返回相应的用户 if(entry!=null&&entry.getType()==RosterPacket.ItemType.to){ Presence response=new Presence(Presence.Type.subscribed); response.setTo(jid); SparkManager.getConnection().sendPacket(response); return; } UserManager userManager=SparkManager.getUserManager(); String username=userManager.getNickname(userManager.getFullJID(jid)); username=username==null?StringUtils.parseName(UserManager.unescapeJID(jid)):username;//给usernam赋值 boolean addEntry=addEntry(username); GetGroupName(); if(addEntry){ Presence response=new Presence(Presence.Type.subscribed); response.setTo(jid); SparkManager.getConnection().sendPacket(response); } } /** * 允许加入好友 */ private boolean addEntry(String username){ String errorMessage=Res.getString("title.error"); String groupName="Friends"; //获取该用户组是否存在 ContactGroup contactGroup=SparkManager.getWorkspace().getContactList().getContactGroup(groupName); boolean isSharedGroup = contactGroup!=null&&contactGroup.isSharedGroup(); System.out.println("用户组存在?"+isSharedGroup); if(isSharedGroup){ errorMessage=Res.getString("message.cannot.add.contact.to.shared.group"); }else if(!ModelUtil.hasLength(username)){ errorMessage=Res.getString("message.specify.contact.jid"); }else if(!ModelUtil.hasLength(groupName)){ errorMessage=Res.getString("message.specify.group"); }else if(ModelUtil.hasLength(username)&&ModelUtil.hasLength(groupName)&&!isSharedGroup){ System.out.println("加入好友"); System.out.println("直接输出Group组:"+groupName); addEntity(jid, username, groupName); return true; } return false; } /** * 把新用户加入到好友名单 * @param jid 账号 * @param username 昵称 * @param groupName 组名 */ private RosterEntry addEntity(String jid,String username,String groupName){ String[] groups={groupName}; System.out.println("開始加入了哦"); for(int i=0;i<groups.length;i++){ System.out.println(groups[i]); } Roster roster=SparkManager.getConnection().getRoster(); RosterEntry userEntry=roster.getEntry(jid); boolean isSubscribed=true; if(userEntry!=null){ isSubscribed=userEntry.getGroups().size()==0; } if(isSubscribed){ try { roster.createEntry(jid, username, new String[]{groupName}); } catch (XMPPException e) { Log.error("不能加入新的好友"+jid,e); } return roster.getEntry(jid); } try { RosterGroup rosterGroup=roster.getGroup(groupName); if(rosterGroup==null){ rosterGroup=roster.createGroup(groupName); } if(userEntry==null){ roster.createEntry(jid, username, groups); userEntry=roster.getEntry(jid); }else { userEntry.setName(username); rosterGroup.addEntry(userEntry); } userEntry=roster.getEntry(jid); } catch (XMPPException ex) { Log.error(ex); } return userEntry; } private void GetGroupName(){ ArrayList<String> list=new ArrayList<String>(); for(ContactGroup group:SparkManager.getWorkspace().getContactList().getContactGroups()){ if(!group.isOfflineGroup()&&!"Unfiled".equalsIgnoreCase(group.getGroupName())&&!group.isSharedGroup()){ list.add(group.getGroupName()); System.out.println(group.getGroupName()); } } } }
而在加入好友所用的类:org.jivesoftware.spark.ui.SubscriptionDialog.java
而监听并调用的类为:org.jivesoftware.spark.ui.ContactList.java 第2134行
版权声明:本文博客原创文章。博客,未经同意,不得转载。