openfire--好友管理

本文详细解析了Openfire系统中好友关系及分组的管理方式,通过两张关键表ofroster与ofrostergroups实现好友关系的双向记录及好友分组的管理。

主要基于两张table实现:ofroster,ofrostergroups。

    ofroster:用于记录好友关系(一对好友关系用两条记录来实现)

    ofrostergroups:用于记录好友分组

    特别说明:openfire中用户的主键是自然主键,也就是username。没有使用自增ID。

    我们先来看一下官方(http://www.igniterealtime.org/builds/openfire/docs/latest/documentation/database-guide.html)对 两张表的描述:

    

ofRoster (buddy rosters or lists)Column Name Type Length Description

rosterIDNUMBERn/aID of roster (Primary Key)
usernameVARCHAR32User Name
jidTEXTn/aThe address of the roster entry
subNUMBERn/aThe subscription status of the entry
askNUMBERn/aThe ask status of the entry
recvNUMBERn/aFlag indicating the entry is a roster request that was received
nickVARCHAR255The nickname assigned to this roster entry

 




ofRosterGroups (Groups of buddy entries in a roster)Column Name Type Length Description

rosterIDNUMBERn/aRoster ID (Primary Key)
rankNUMBERn/aPosition of the entry (Primary Key)
groupNameVARCHAR255The user defined name for this roster group

 

 

 

看不太明白?不要着急,我们慢慢分析。

假设有用户A,用户B。

当A申请加B为好友时(例如:A将B加入好亲人的分组中)。会在ofroster表中插入两条记录,

rosterID  username     jid        sub  ask  recv  nick

1          A      B@192.168.1.102 0     0   -1     B
2          B      A@192.168.1.102 0    -1    1    null

同时往ofrostergroups表中插入一条记录

rosterID    rank    groupName

1            0        亲人

 

这时B同意将A加为好友,并设置为家人分组中,那么会修改ofroster表中刚插入的两条记录,如下所示:

rosterID  username     jid        sub  ask  recv  nick

1          A      B@192.168.1.102 1     -1    1     B
2          B      A@192.168.1.102 2      0   -1    null

同时往ofrostergroups表中插入一条记录.

rosterID    rank    groupName

2            0        家人

 

到此为止,双方的好友关系便建立起来。

疑问:1.若B不同意呢?则不做任何操作。下一次,若B加A为好友,将等同于执行同意的操作。

 2.如何查询某个人所有好友,和分组?

   在ofroster中根据username便可获得某个用户的所有好友信息。然后根据每条记录的rosterid去ofrostergroups表中查找分组的名称即可。

 3.当用户添加一个空的好友分组时,ofrostergroups表是否插入一条记录?

   不插,测试发现并没有实质的插入一条记录,但用户可以看到这个分组名称,怎么回事?推测可能是存放在session中。测试发现当用户创建一个空的好友分组,然后下线,再上线时,发现该好友分组已消失。充分说明当好友分组为空时,并没有插库。

 

Notice the different status types? Here is a list of all of the different status types, witha brief description, also from the plugin's readme file.

askstatus

  • -1—The roster item has no pending subscription requests.
  • 0—The roster item has been asked for permission to subscribe to its presence but no response has been received.
  • 1—The roster owner has asked the roster item to be unsubscribed from its presence notifications but hasn't yet received confi rmation.

recvstatus

  • -1—There are no subscriptions that have been received but not presented to the user.
  • 1—The server has received a subscribe request, but has not forwarded it to the user.
  • 2—The server has received an unsubscribe request, but has not forwarded it to the user.

substatus

  • -1—Indicates that the roster item should be removed.
  • 0—No subscription is established.
  • 1—The roster owner has a subscription to the roster item’s presence.
  • 2—The roster item has a subscription to the roster owner’s presence.
  • 3—The roster item and the owner have a mutual subscription.

 

代码操作:
 
PacketListener myListener = new PacketListener() {      
             public void processPacket(Packet packet) {  
              if(packet instanceof Message) {
               Message message = (Message) packet;
               String type = message.getType().toString();
               if(type.equals("chat")) {
                //接收消息
                if(message.getBody()!=null){
                 //保存信息到客户端数据库
                                     }
                }  
               }
              } else if(packet instanceof Presence) {
               Presence presence = (Presence) packet;
               String type = presence.getType().toString();
               boolean b= TalkService.this.roster.contains(packet.getFrom());
               Log.i(tag, b+"");
      
               Roster roster = connection.getRoster();
            RosterEntry rosterEntry = roster.getEntry(presence.getFrom());    
             
            String itemType="";
            
            if(rosterEntry!=null){
             Log.e(tag, "PacketListener Presence: rosterEntry.getName()= "+rosterEntry.getName()+
               " - rosterEntry.getUser()="+rosterEntry.getUser()+
               " - rosterEntry.getStatus()="+rosterEntry.getStatus()+
               "  -rosterEntry.getType()= "+rosterEntry.getType());
             itemType=rosterEntry.getType().toString();
            }
            Log.e(tag, "itemType=="+itemType);
               if(type.equals("subscribe")) {//我收到对方的加好友的请求
                //添加好友请求
                FriendRelationParam param = new FriendRelationParam();
                TalkCallback callback = new TalkCallback();
                callback.from = getUId(presence.getFrom());
                callback.to = getUId(presence.getTo());
                try {
        param.callbackStr = callback.toJsonObject().toString();
       } catch (Exception e) {
        e.printStackTrace();
       }
                param.userId = Long.parseLong(callback.to);
                param.friendUid = Long.parseLong(callback.from);
                
                startFriendRelation(param);
                
                if("none".equals(itemType)){//收到对方的加好友的请求
                 Log.i(tag, "type="+type+"  我收到对方的加好友的请求");
                }
                
                if("to".equals(itemType)){//我加对方好友后,对方同意,给我发回的交友请求
                 Log.i(tag, "type="+type+"  我加对方好友后,对方同意,给我发回的交友请求");
                }
                
                
               }else if("subscribed".equals(type)){//对方同意加我为好友 
                
                
                if("both".equals(itemType)){//双方互为好友关系建立
                 Log.i(tag, "type="+type+"  双方互为好友关系建立!");
                }
                
                
                Log.e("TalkService"+Thread.currentThread().getName(), presence.getFrom()+"同意了我["+packet.getTo()+"]的加好友请求 ");
               }else if(type.equals("unsubscribe")) {//对方发起了把我删除了||或者拒绝    //拒绝的时候只会走这A
                
                if("none".equals(itemType)){//拒绝
                 Log.i(tag, "type="+type+"  我被拒绝!!!");
                }
                if("to".equals(itemType)){//我收到被对方删除
                 Log.i(tag, "type="+type+"  我收到被对方删除");
                }
                
                
               }else if(type.equals("unsubscribed")) {//对方把我删除了   //删除的时候 会走A,同时再走一次这
                
                if("none".equals(itemType)){// 我被删除  ,双方关系解除**************************
                 Log.i(tag, "type="+type+"  我被删除的!!!");
                }
                
                
               }else if(type.equals("available")) {//对方告诉我他在线
//                presence = new Presence(Presence.Type.available);
//                presence.setTo(presence.getFrom());
//                connection.sendPacket(presence);
               }
               
               
              }else if(packet instanceof AuthMechanism) {
               
               
              }
              
             }      
         }; 

  

 

[root@yfw openfire-restAPI-plugin]# wget --spider https://github.com/igniterealtime/openfire-restAPI-plugin/releases/download/v1.5.0/restapi.jar Spider mode enabled. Check if remote file exists. --2025-10-02 17:02:47-- https://github.com/igniterealtime/openfire-restAPI-plugin/releases/download/v1.5.0/restapi.jar Resolving github.com (github.com)... 20.205.243.166 Connecting to github.com (github.com)|20.205.243.166|:443... connected. HTTP request sent, awaiting response... 404 Not Found Remote file does not exist -- broken link!!! [root@yfw openfire-restAPI-plugin]# 插件 描述 版本 作者 重启 删除 Plugin Client Control README changelog Controls clients allowed to connect and available features 2.1.10 Jive Software 刷新 Client Control 删除 Client Control Plugin Content Filter README changelog Scans message packets for defined patterns 1.9.0 Conor Hayes 刷新 Content Filter 删除 Content Filter Plugin Fastpath Service README changelog Support for managed queued chat requests, such as a support team might use. 4.5.1 Ignite Realtime 刷新 Fastpath Service 删除 Fastpath Service Plugin Jabber Browsing README changelog This plugin implements the (obsolete!) XEP-0011 'Jabber Browsing' specification for service discovery using the jabber:iq:browse namespace. 1.0.1 Guus der Kinderen 刷新 Jabber Browsing 删除 Jabber Browsing Plugin Just married README changelog Allows admins to rename or copy users 1.3.0 Holger Bergunde 刷新 Just married 删除 Just married Plugin MUC Service Discovery Extensions README changelog Allows an admin to configure Extended Service Discovery information to Multi User Chat entities. 1.0.0 Guus der Kinderen 刷新 MUC Service Discovery Extensions 删除 MUC Service Discovery Extensions Plugin Pade README changelog Web-based chat, groupchat, telephones, audio and video conferencing solution using ConverseJS, Jitsi and FreeSWITCH 1.8.4 Ignite Realtime 刷新 Pade 删除 Pade Plugin Presence Service README changelog Exposes presence information through HTTP. 1.7.4 Jive Software 刷新 Presence Service 删除 Presence Service Plugin Push Notification README changelog Adds Push Notification (XEP-0357) support to Openfire. 1.0.1 Guus der Kinderen 刷新 Push Notification 删除 Push Notification Plugin REST API README changelog Allows administration over a RESTful API. 1.8.0 Roman Soldatow 刷新 REST API 删除 REST API Plugin Search README changelog Provides support for Jabber Search (XEP-0055) 1.7.5 Ryan Graham 刷新 Search 删除 Search Plugin User Creation changelog Creates users and populates rosters. 1.4.1 Jive Software 刷新 User Creation 删除 User Creation Plugin User Import Export README changelog Enables import and export of user data 2.8.0 Ryan Graham 刷新 User Import Export 删除 User Import Export Plugin User Service README changelog (Deprecated) Please use the REST API Plugin. Allows administration of users via HTTP requests. 2.1.3 Roman Soldatow, Justin Hunt 刷新 User Service 删除 User Service Plugin User Status Plugin README changelog Openfire plugin to save the user status to the database. 1.3.0 Stefan Reuter 刷新 User Status Plugin 删除 User Status Plugin Plugin Openfire WebSocket README changelog Provides WebSocket support for Openfire. 1.2.1 Tom Evans 删除 Openfire WebSocket Plugin xmppweb README changelog Adds the (third-party) xmpp-web client to Openfire. 0.10.3 Release 1 Guus der Kinderen 刷新 xmppweb 删除 xmppweb
最新发布
10-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值