Skip to content

Commit

Permalink
添加获取所有用户的Server API
Browse files Browse the repository at this point in the history
  • Loading branch information
heavyrian2012 committed Jun 20, 2023
1 parent 252204a commit c8ff39c
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* This file is part of the Wildfire Chat package.
* (c) Heavyrain2012 <heavyrain.lee@gmail.com>
*
* 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.InputGetUserInfo;
import cn.wildfirechat.pojos.InputGetUserList;
import cn.wildfirechat.pojos.InputOutputUserInfo;
import cn.wildfirechat.pojos.OutputGetUserList;
import cn.wildfirechat.proto.WFCMessage;
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.List;

@Route(APIPath.User_Get_All)
@HttpMethod("POST")
public class GetAllUserAction extends AdminAction {

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

@Override
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {
InputGetUserList input = getRequestBody(request.getNettyRequest(), InputGetUserList.class);
if (input != null && input.count > 0 && input.offset >= 0) {
List<WFCMessage.User> users = messagesStore.getUserInfoList(input.count, input.offset);
OutputGetUserList outputGetUserList = new OutputGetUserList();
outputGetUserList.userInfoList = new ArrayList<>();
for (WFCMessage.User user : users) {
outputGetUserList.userInfoList.add(InputOutputUserInfo.fromPbUser(user));
}

RestResult result = RestResult.ok(outputGetUserList);
setResponseContent(result, response);
} else {
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER), response);
}

}
return true;
}
}
97 changes: 97 additions & 0 deletions broker/src/main/java/io/moquette/persistence/DatabaseStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -2640,6 +2640,103 @@ String getUserIdByMobile(String mobile) {
return null;
}

List<WFCMessage.User> getAllUsers(int count, int offset) {
Connection connection = null;
PreparedStatement statement = null;
ResultSet rs = null;
List<WFCMessage.User> outList = new ArrayList<>();
try {
connection = DBUtil.getConnection();
String sql = "select `_uid`, `_name`" +
", `_display_name`" +
", `_portrait`" +
", `_mobile`" +
", `_gender`" +
", `_email`" +
", `_address`" +
", `_company`" +
", `_social`" +
", `_extra`" +
", `_type`" +
", `_deleted`" +
", `_dt` from t_user where `_deleted` = 0 order by _createTime limit ? offset ?";
statement = connection.prepareStatement(sql);

int index = 1;
statement.setInt(1, count);
statement.setInt(2, offset);


rs = statement.executeQuery();
while (rs.next()) {
WFCMessage.User.Builder builder = WFCMessage.User.newBuilder();
index = 1;
String value = rs.getString(index++);
value = (value == null ? "" : value);
builder.setUid(value);

value = rs.getString(index++);
value = (value == null ? "" : value);
builder.setName(value);

value = rs.getString(index++);
value = (value == null ? "" : value);
builder.setDisplayName(value);

value = rs.getString(index++);
value = (value == null ? "" : value);
builder.setPortrait(value);

value = rs.getString(index++);
value = (value == null ? "" : value);
builder.setMobile(value);

int gender = rs.getInt(index++);
builder.setGender(gender);

value = rs.getString(index++);
value = (value == null ? "" : value);
builder.setEmail(value);

value = rs.getString(index++);
value = (value == null ? "" : value);
builder.setAddress(value);

value = rs.getString(index++);
value = (value == null ? "" : value);
builder.setCompany(value);

value = rs.getString(index++);
value = (value == null ? "" : value);
builder.setSocial(value);

value = rs.getString(index++);
value = (value == null ? "" : value);
builder.setExtra(value);

int type = rs.getInt(index++);
builder.setType(type);

int deleted = rs.getInt(index++);
builder.setDeleted(deleted);

long longValue = rs.getLong(index++);
if(longValue <= 0)
longValue = 1;
builder.setUpdateDt(longValue);

outList.add(builder.build());
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Utility.printExecption(LOG, e, RDBS_Exception);
} finally {
DBUtil.closeDB(connection, statement, rs);
}
return outList;
}

Set<String> getAllEnds() {
Connection connection = null;
PreparedStatement statement = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2764,6 +2764,12 @@ public WFCMessage.User getUserInfoByMobile(String mobile) {
String userId = databaseStore.getUserIdByMobile(mobile);
return getUserInfo(userId);
}

@Override
public List<WFCMessage.User> getUserInfoList(int count, int offset) {
return databaseStore.getAllUsers(count, offset);
}

@Override
public List<WFCMessage.User> searchUser(String keyword, int searchType, int page) {
if (mDisableSearch) {
Expand Down
2 changes: 2 additions & 0 deletions broker/src/main/java/io/moquette/spi/IMessagesStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ public String toString() {
WFCMessage.User getUserInfoByMobile(String mobile);
List<WFCMessage.User> searchUser(String keyword, int searchType, int page);

List<WFCMessage.User> getUserInfoList(int count, int offset);

boolean updateSystemSetting(int id, String value, String desc);
SystemSettingPojo getSystemSetting(int id);
void createChatroom(String chatroomId, WFCMessage.ChatroomInfo chatroomInfo);
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 @@ -26,6 +26,7 @@ public interface APIPath {
String User_Get_Token = "/admin/user/get_token";
String User_Update_Block_Status = "/admin/user/update_block_status";
String User_Get_Info = "/admin/user/get_info";
String User_Get_All = "/admin/user/all";
String User_Get_Robot_Info = "/admin/user/get_robot_info";
String User_Get_Blocked_List = "/admin/user/get_blocked_list";
String User_Check_Block_Status = "/admin/user/check_block_status";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cn.wildfirechat.pojos;

public class InputGetUserList {
public int count;
public int offset;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cn.wildfirechat.pojos;

import java.util.List;

public class OutputGetUserList {
public List<InputOutputUserInfo> userInfoList;
}
2 changes: 1 addition & 1 deletion sdk/sdk_readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
<version>1.15</version>
</dependency>

<dependency>
Expand Down
14 changes: 14 additions & 0 deletions sdk/src/main/java/cn/wildfirechat/sdk/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,20 @@ static void testUser() throws Exception {
System.exit(-1);
}

int limit = 5;
for (int i = 0; i < 100; i++) {
IMResult<OutputGetUserList> getUserListIMResult = UserAdmin.getAllUsers(limit, i*limit);
if (getUserListIMResult != null && getUserListIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("getUserListIMResult success");
if(getUserListIMResult.getResult().userInfoList.size() < limit) {
break;
}
} else {
System.out.println("getUserListIMResult failure");
System.exit(-1);
}
}

if (commercialServer) {
IMResult<GetOnlineUserCountResult> getOnlineUserCountResultIMResult = UserAdmin.getOnlineUserCount();
if (getOnlineUserCountResultIMResult != null && getOnlineUserCountResultIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
Expand Down
8 changes: 8 additions & 0 deletions sdk/src/main/java/cn/wildfirechat/sdk/UserAdmin.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public static IMResult<InputOutputUserInfo> getUserByMobile(String mobile) throw
return AdminHttpUtils.httpJsonPost(path, getUserInfo, InputOutputUserInfo.class);
}

public static IMResult<OutputGetUserList> getAllUsers(int count, int offset) throws Exception {
String path = APIPath.User_Get_All;
InputGetUserList input = new InputGetUserList();
input.count = count;
input.offset = offset;
return AdminHttpUtils.httpJsonPost(path, input, OutputGetUserList.class);
}

public static IMResult<OutputCreateUser> createUser(InputOutputUserInfo user) throws Exception {
String path = APIPath.Create_User;
return AdminHttpUtils.httpJsonPost(path, user, OutputCreateUser.class);
Expand Down

0 comments on commit c8ff39c

Please sign in to comment.