add robot create group function
This commit is contained in:
+104
@@ -0,0 +1,104 @@
|
|||||||
|
/*
|
||||||
|
* 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.robot;
|
||||||
|
|
||||||
|
|
||||||
|
import cn.wildfirechat.common.APIPath;
|
||||||
|
import cn.wildfirechat.common.ErrorCode;
|
||||||
|
import cn.wildfirechat.pojos.InputCreateGroup;
|
||||||
|
import cn.wildfirechat.pojos.OutputCreateGroupResult;
|
||||||
|
import cn.wildfirechat.pojos.PojoGroupInfo;
|
||||||
|
import cn.wildfirechat.proto.WFCMessage;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.xiaoleilu.loServer.RestResult;
|
||||||
|
import com.xiaoleilu.loServer.action.admin.AdminAction;
|
||||||
|
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.moquette.persistence.RPCCenter;
|
||||||
|
import io.moquette.persistence.TargetEntry;
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import io.netty.buffer.Unpooled;
|
||||||
|
import io.netty.handler.codec.http.FullHttpRequest;
|
||||||
|
import io.netty.handler.codec.http.HttpResponseStatus;
|
||||||
|
import win.liyufan.im.IMTopic;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
|
@Route(APIPath.Robot_Create_Group)
|
||||||
|
@HttpMethod("POST")
|
||||||
|
public class CreateGroupAction extends RobotAction {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isTransactionAction() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean action(Request request, Response response) {
|
||||||
|
if (request.getNettyRequest() instanceof FullHttpRequest) {
|
||||||
|
InputCreateGroup inputCreateGroup = getRequestBody(request.getNettyRequest(), InputCreateGroup.class);
|
||||||
|
inputCreateGroup.setOperator(robot.getUid());
|
||||||
|
if (inputCreateGroup.isValide()) {
|
||||||
|
PojoGroupInfo group_info = inputCreateGroup.getGroup().getGroup_info();
|
||||||
|
WFCMessage.CreateGroupRequest createGroupRequest = inputCreateGroup.toProtoGroupRequest();
|
||||||
|
if ((group_info.getHistory_message() > 0 && group_info.getHistory_message() < 128) || group_info.getMax_member_count() > 0) {
|
||||||
|
WFCMessage.GroupInfo.Builder groupInfoBuilder = createGroupRequest.getGroup().getGroupInfo().toBuilder();
|
||||||
|
if (group_info.getHistory_message() > 0 && group_info.getHistory_message() < 128) {
|
||||||
|
groupInfoBuilder.setHistoryMessage(group_info.getHistory_message());
|
||||||
|
}
|
||||||
|
if (group_info.getMax_member_count() > 0) {
|
||||||
|
groupInfoBuilder.setMaxMemberCount(group_info.getMax_member_count());
|
||||||
|
}
|
||||||
|
createGroupRequest = createGroupRequest.toBuilder().setGroup(createGroupRequest.getGroup().toBuilder().setGroupInfo(groupInfoBuilder)).build();
|
||||||
|
}
|
||||||
|
RPCCenter.getInstance().sendRequest(inputCreateGroup.getOperator(), null, IMTopic.CreateGroupTopic, createGroupRequest.toByteArray(), inputCreateGroup.getOperator(), TargetEntry.Type.TARGET_TYPE_USER, new RPCCenter.Callback() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(byte[] result) {
|
||||||
|
ByteBuf byteBuf = Unpooled.buffer();
|
||||||
|
byteBuf.writeBytes(result);
|
||||||
|
ErrorCode errorCode = ErrorCode.fromCode(byteBuf.readByte());
|
||||||
|
if (errorCode == ErrorCode.ERROR_CODE_SUCCESS) {
|
||||||
|
byte[] data = new byte[byteBuf.readableBytes()];
|
||||||
|
byteBuf.readBytes(data);
|
||||||
|
String groupId = new String(data);
|
||||||
|
sendResponse(response, null, new OutputCreateGroupResult(groupId));
|
||||||
|
} else {
|
||||||
|
sendResponse(response, errorCode, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(ErrorCode errorCode) {
|
||||||
|
sendResponse(response, errorCode, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTimeout() {
|
||||||
|
sendResponse(response, ErrorCode.ERROR_CODE_TIMEOUT, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Executor getResponseExecutor() {
|
||||||
|
return command -> {
|
||||||
|
ctx.executor().execute(command);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}, false, 1);
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
response.setStatus(HttpResponseStatus.OK);
|
||||||
|
RestResult result = RestResult.resultOf(ErrorCode.INVALID_PARAMETER);
|
||||||
|
response.setContent(new Gson().toJson(result));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user