You can directly operate on database data to add friends to Openfire users.
[Size = large] Add friends and manage groups in openfire.
It is mainly implemented based on two tables: ofroster and ofrostergroups.
Ofroster: used to record friend relationships (two records are used for a friend relationship)
Ofrostergroups: used to record friend groups
Note: In openfire, a user's primary key is a natural primary key, that is, username. No auto-increment ID is used.
Let's take a look at the official (http://www.igniterealtime.org/builds/openfire/docs/latest/documentation/database-guide.html) description of the two tables:
OfRoster (friend list)
[Table]
| Column name | type | length | description |
| RosterID | NUMBER | n/a | NUMBER register (primary key) |
| Username | VARCHAR | 32 | user name |
| Jid | TEXT | n/a | address register entry |
| Sub | NUMBER | n/a | entry of the subscription status |
| Ask | NUMBER | n/a | sales position entry |
| Recv | NUMBER | n/a | The report indicates that a request is received on the roster. |
| Nick | VARCHAR | 255 | the nickname is assigned to this roster for entry |
[/Table]
OfRosterGroups (entries in the group's friends list)
[Table]
| Column name | type | length | description |
| RosterID | NUMBER | n/a | roster NUMBER (primary key) |
| Rank | NUMBER | n/a | position item (primary key) |
| GroupName | VARCHAR | 255 | User-Defined name. This roster group |
[/Table]
Not quite clear? Don't worry. Let's analyze it slowly.
Assume there is user A and user B.
When A applies to add B as A friend (for example, A adds B to A Group of Friends ). Two records will be inserted in the ofroster table,
RosterID username jid sub ask recv nick
1 A 0 0-1 B
2 B 0-1 1 null
Insert a record to the ofrostergroups table
RosterID rank groupName
1 0 loved ones
In this case, B agrees to add a as a friend and set it to a family group. Then, the two records just inserted in the ofroster table will be modified, as shown below:
RosterID username jid sub ask recv nick
1 A 1-1 1 B
2 B 2 0-1 null
Insert a record to the ofrostergroups table.
RosterID rank groupName
2 0 families
So far, mutual friends are established.
Question: 1. What if B does not agree? Then, no operation is performed. Next time, if B adds A as A friend, it is equivalent to performing the agreed operation.
2. How to query all friends and groups of a person?
In ofroster, you can obtain all the friends of a user based on username. Then, find the group name in the ofrostergroups table based on the rosterid of each record.
3. When a user adds an empty friend group, does the ofrostergroups table insert a record?
No insert. The test shows that there is no substantive insert of a record, but the user can see the group name. What is going on? It may be stored in the session. The test shows that when a user creates an empty friend group and goes offline and goes online again, the Group disappears. It fully indicates that no library is inserted when the friend group is empty.
The following describes the meaning of each field:
Askstatus
-1-no pending friend adding requests.
The roster item has no pending sub‑requests.
0-a pending friend adding request exists.
The roster item has been asked for permission to subscribe to its presence but no response has been already ed.
1-is there any reply to the deletion request?
The roster owner has asked the roster item to be unsubscribed from its presence comprehensions but hasn' t yet already ed confi rmation.
Recvstatus
-1-A friend adding request has been replied.
There are no subscriptions that have been already ed but not presented to the user.
1-received a friend request but did not reply to the friend
The server has received ed a subscribe request, but has not forwarded it to the user.
2-I guess I didn't reply to the deletion request.
The server has received ed an unsubscribe request, but has not forwarded it to the user.
Substatus
-1-you should delete this friend.
Indicates that the roster item shocould be removed.
0-no friend relationship is established
No subcategory is established.
1-The user has sent a friend request
The roster owner has a subscribe to the roster item's presence.
2-receive a friend request and add another friend
The roster item has a subscribe to the roster owner's presence.
3-Mutual friends have been added
The roster item and the owner have a mutual subscribe.
After knowing this, we will understand the process of adding friends. The following is the operation of our code [/size].
[Code = "java"] public static void main (String [] args ){
ResultSet rs = null;
Statement stmt = null;
Connection conn = null;
Try {
Class. forName ("oracle. jdbc. driver. OracleDriver ");
// New oracle. jdbc. driver. OracleDriver ();
Conn = DriverManager. getConnection ("jdbc: oracle: thin: @ 192.168.1.85: 1521: ORCL", "test", "test ");
Stmt = conn. createStatement ();
// Aaa and eee Add friends to each other
String SQL = "insert into OFROSTER values ('30', 'Eee ', 'aaa @ ppt03-20141024i', '3', '-1','-1 ', 'aaa ')";
Stmt.exe cute (SQL );
String sql2 = "insert into OFROSTER values ('31', 'aaa', 'Eee @ ppt03-20141024i ', '3','-1', '-1 ', 'Eee ')";
Stmt.exe cute (sql2 );
// Put aaa and eee in the Friend group
String sql3 = "insert into OFROSTERGROUPS values ('30', '0', 'friend ')";
Stmt.exe cute (sql3 );
String sql4 = "insert into OFROSTERGROUPS values ('31', '0', 'friend ')";
Stmt.exe cute (sql4 );
} Catch (ClassNotFoundException e ){
E. printStackTrace ();
} Catch (SQLException e ){
E. printStackTrace ();
} Finally {
Try {
If (rs! = Null ){
Rs. close ();
Rs = null;
}
If (stmt! = Null ){
Stmt. close ();
Stmt = null;
}
If (conn! = Null ){
Conn. close ();
Conn = null;
}
} Catch (SQLException e ){
E. printStackTrace ();
}
}
} [/Code]
[Size = large] This is the operation that I use JDBC to directly operate the database. At first, it seems that it is not working. Later, I added a plug-in officially provided by openfire: SubscriptionPlugin, this plug-in automatically adds friends. After adding, you can.
If you have any questions, please leave a message [/size].