标签: 杂谈 |
openfire中实现好友添加及分组管理。
主要基于两张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 NamejidTEXTn/aThe address of the roster entrysubNUMBERn/aThe subscription status of the entryaskNUMBERn/aThe ask status of the entryrecvNUMBERn/aFlag indicating the entry is a roster request that was receivednickVARCHAR255The 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@clover 0 0 -1 B
2 B A@clover 0 -1 1 null
同时往ofrostergroups表中插入一条记录
rosterID rank groupName
1 0 Friends
这时B同意将A加为好友,并设置为Friends分组中,那么会修改ofroster表中刚插入的两条记录,如下所示:
rosterID username jid sub ask recv nick
1 A B@clover 1 -1 1 B
2 B A@clover 2 0 -1 A
同时往ofrostergroups表中插入一条记录.
rosterID rank groupName
2 0 Friends
此时,ofrostergroups表中的记录是:
rosterID rank groupName
1 0 Friends
2 0 Friends
到此为止,双方的好友关系便建立起来。
疑问: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, with a 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.