Skip to content

Commit

Permalink
支持获取用户群组指定群成员类型
Browse files Browse the repository at this point in the history
  • Loading branch information
heavyrian2012 committed Oct 10, 2023
1 parent 7727fad commit c17a5be
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* This file is part of the Wildfire Chat package.
* (c) Heavyrain2012 <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

package com.xiaoleilu.loServer.action.admin;

import cn.wildfirechat.common.APIPath;
import cn.wildfirechat.common.ErrorCode;
import cn.wildfirechat.pojos.InputGetUserGroupByType;
import cn.wildfirechat.pojos.InputUserId;
import cn.wildfirechat.pojos.OutputGroupIds;
import com.xiaoleilu.loServer.RestResult;
import com.xiaoleilu.loServer.annotation.HttpMethod;
import com.xiaoleilu.loServer.annotation.Route;
import com.xiaoleilu.loServer.handler.Request;
import com.xiaoleilu.loServer.handler.Response;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.util.internal.StringUtil;

import java.util.ArrayList;
import java.util.Set;

@Route(APIPath.Get_User_Groups_By_Type)
@HttpMethod("POST")
public class GetUserGroupsByTypeAction extends AdminAction {

@Override
public boolean isTransactionAction() {
return true;
}

@Override
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {
InputGetUserGroupByType input = getRequestBody(request.getNettyRequest(), InputGetUserGroupByType.class);
if (input != null
&& (!StringUtil.isNullOrEmpty(input.getUserId()))) {

Set<String> groupIds = messagesStore.getUserGroupIds(input.getUserId(), input.getGroupMemberTypes());
OutputGroupIds outputGroupIds = new OutputGroupIds();
outputGroupIds.setGroupIds(new ArrayList<>(groupIds));
RestResult result = RestResult.ok(outputGroupIds);
setResponseContent(result, response);
} else {
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER), response);
}

}
return true;
}
}
39 changes: 39 additions & 0 deletions broker/src/main/java/io/moquette/persistence/DatabaseStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -1845,6 +1845,45 @@ Set<String> getUserGroupIds(String userId) {
return null;
}

Set<String> getUserGroupIds(String userId, List<Integer> memberTypes) {
Connection connection = null;
PreparedStatement statement = null;
ResultSet rs = null;
try {
connection = DBUtil.getConnection();
String sql = "select _gid from t_group_member where _mid = ? ";
if(!memberTypes.isEmpty()) {
sql += " and _type in (";
for (int i = 0; i < memberTypes.size(); i++) {
if(i > 0) {
sql += ",";
}
sql += memberTypes.get(i);
}
sql += ")";
}
statement = connection.prepareStatement(sql);

statement.setString(1, userId);

rs = statement.executeQuery();
Set<String> out = new HashSet<>();
while (rs.next()) {
String uid = rs.getString(1);
out.add(uid);
}
return out;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Utility.printExecption(LOG, e, RDBS_Exception);
} finally {
DBUtil.closeDB(connection, statement, rs);
}
return null;
}


Set<String> getCommonGroupIds(String userId1, String userId2) {
Connection connection = null;
PreparedStatement statement = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1944,6 +1944,11 @@ public Set<String> getUserGroupIds(String userId) {
return databaseStore.getUserGroupIds(userId);
}

@Override
public Set<String> getUserGroupIds(String userId, List<Integer> memberTypes) {
return databaseStore.getUserGroupIds(userId, memberTypes);
}

@Override
public Set<String> getCommonGroupIds(String userId1, String userId2) {
return databaseStore.getCommonGroupIds(userId1, userId2);
Expand Down
1 change: 1 addition & 0 deletions broker/src/main/java/io/moquette/spi/IMessagesStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public String toString() {
List<WFCMessage.GroupInfo> getGroupInfos(List<WFCMessage.UserRequest> requests, String fromUser, boolean isAdmin);
WFCMessage.GroupInfo getGroupInfo(String groupId);
Set<String> getUserGroupIds(String userId);
Set<String> getUserGroupIds(String userId, List<Integer> types);
Set<String> getCommonGroupIds(String userId1, String userId2);
ErrorCode getGroupMembers(String fromUser, String groupId, long maxDt, List<WFCMessage.GroupMember> members);
WFCMessage.GroupMember getGroupMember(String groupId, String memberId);
Expand Down
1 change: 1 addition & 0 deletions common/src/main/java/cn/wildfirechat/common/APIPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public interface APIPath {
String Group_Mute_Member = "/admin/group/manager/mute";
String Group_Allow_Member = "/admin/group/manager/allow";
String Get_User_Groups = "/admin/group/of_user";
String Get_User_Groups_By_Type = "/admin/group/of_user_by_type";
String Group_Set_Member_Alias = "/admin/group/member/set_alias";
String Group_Set_Member_Extra = "/admin/group/member/set_extra";
String Get_Common_Groups = "/admin/group/common_group";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This file is part of the Wildfire Chat package.
* (c) Heavyrain2012 <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

package cn.wildfirechat.pojos;

import java.util.List;

public class InputGetUserGroupByType {
private String userId;
private List<Integer> groupMemberTypes;

public InputGetUserGroupByType() {
}

public InputGetUserGroupByType(String userId, List<Integer> groupMemberTypes) {
this.userId = userId;
this.groupMemberTypes = groupMemberTypes;
}

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public List<Integer> getGroupMemberTypes() {
return groupMemberTypes;
}

public void setGroupMemberTypes(List<Integer> groupMemberTypes) {
this.groupMemberTypes = groupMemberTypes;
}
}
7 changes: 7 additions & 0 deletions sdk/src/main/java/cn/wildfirechat/sdk/GroupAdmin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cn.wildfirechat.common.APIPath;
import cn.wildfirechat.pojos.*;
import cn.wildfirechat.proto.ProtoConstants;
import cn.wildfirechat.sdk.model.IMResult;
import cn.wildfirechat.sdk.utilities.AdminHttpUtils;

Expand Down Expand Up @@ -178,6 +179,12 @@ public static IMResult<OutputGroupIds> getUserGroups(String user) throws Excepti
return AdminHttpUtils.httpJsonPost(path, inputUserId, OutputGroupIds.class);
}

public static IMResult<OutputGroupIds> getUserGroupsByType(String user, List</*ProtoConstants.GroupMemberType*/Integer> groupMemberType) throws Exception {
String path = APIPath.Get_User_Groups_By_Type;
InputGetUserGroupByType input = new InputGetUserGroupByType(user, groupMemberType);
return AdminHttpUtils.httpJsonPost(path, input, OutputGroupIds.class);
}

public static IMResult<OutputGroupIds> getCommonGroups(String user1, String user2) throws Exception {
String path = APIPath.Get_Common_Groups;
StringPairPojo intput = new StringPairPojo(user1, user2);
Expand Down
9 changes: 8 additions & 1 deletion sdk/src/main/java/cn/wildfirechat/sdk/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,6 @@ static void testUserRelation() throws Exception {
System.out.println("success");
} else {
System.out.println("send friend request success");
System.exit(-1);
}

IMResult<Void> updateFriendStatusResult = RelationAdmin.setUserFriend("ff1", "ff2", true, "{\"from\":1}");
Expand Down Expand Up @@ -657,6 +656,14 @@ static void testGroup() throws Exception {
System.exit(-1);
}

groupIdsIMResult = GroupAdmin.getUserGroupsByType("user2", Arrays.asList(ProtoConstants.GroupMemberType.GroupMemberType_Manager, ProtoConstants.GroupMemberType.GroupMemberType_Owner));
if (groupIdsIMResult != null && groupIdsIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("get user groups by type success");
} else {
System.out.println("get user groups by type failure");
System.exit(-1);
}

groupIdsIMResult = GroupAdmin.getCommonGroups("user1", "user2");
if (groupIdsIMResult != null && groupIdsIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("get user common groups success");
Expand Down

0 comments on commit c17a5be

Please sign in to comment.