server api支持获取频道信息,销毁频道信息,销毁机器人功能
This commit is contained in:
+62
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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.InputChannelId;
|
||||
import cn.wildfirechat.pojos.OutputCreateChannel;
|
||||
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.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.handler.codec.http.FullHttpRequest;
|
||||
import io.netty.util.internal.StringUtil;
|
||||
import win.liyufan.im.IMTopic;
|
||||
|
||||
@Route(APIPath.Destroy_Channel)
|
||||
@HttpMethod("POST")
|
||||
public class DestroyChannelAction extends AdminAction {
|
||||
|
||||
@Override
|
||||
public boolean isTransactionAction() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean action(Request request, Response response) {
|
||||
if (request.getNettyRequest() instanceof FullHttpRequest) {
|
||||
InputChannelId inputCreateChannel = getRequestBody(request.getNettyRequest(), InputChannelId.class);
|
||||
if (inputCreateChannel != null
|
||||
&& !StringUtil.isNullOrEmpty(inputCreateChannel.channelId)) {
|
||||
WFCMessage.ChannelInfo channelInfo = messagesStore.getChannelInfo(inputCreateChannel.channelId);
|
||||
if(channelInfo == null) {
|
||||
setResponseContent(RestResult.resultOf(ErrorCode.ERROR_CODE_NOT_EXIST), response);
|
||||
return true;
|
||||
}
|
||||
|
||||
WFCMessage.IDBuf.Builder builder = WFCMessage.IDBuf.newBuilder().setId(inputCreateChannel.channelId);
|
||||
sendApiMessage(response, channelInfo.getOwner(), IMTopic.DestroyChannelInfoTopic, builder.build().toByteArray(), result -> {
|
||||
ByteBuf byteBuf = Unpooled.buffer();
|
||||
byteBuf.writeBytes(result);
|
||||
ErrorCode errorCode = ErrorCode.fromCode(byteBuf.readByte());
|
||||
return new Result(errorCode);
|
||||
});
|
||||
return false;
|
||||
} else {
|
||||
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER), response);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.InputChannelId;
|
||||
import cn.wildfirechat.pojos.OutputGetChannelInfo;
|
||||
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;
|
||||
|
||||
@Route(APIPath.Get_Channel_Info)
|
||||
@HttpMethod("POST")
|
||||
public class GetChannelAction extends AdminAction {
|
||||
|
||||
@Override
|
||||
public boolean isTransactionAction() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean action(Request request, Response response) {
|
||||
if (request.getNettyRequest() instanceof FullHttpRequest) {
|
||||
InputChannelId inputCreateChannel = getRequestBody(request.getNettyRequest(), InputChannelId.class);
|
||||
if (inputCreateChannel != null
|
||||
&& !StringUtil.isNullOrEmpty(inputCreateChannel.channelId)) {
|
||||
WFCMessage.ChannelInfo channelInfo = messagesStore.getChannelInfo(inputCreateChannel.channelId);
|
||||
if (channelInfo == null) {
|
||||
setResponseContent(RestResult.resultOf(ErrorCode.ERROR_CODE_NOT_EXIST), response);
|
||||
} else {
|
||||
setResponseContent(RestResult.ok(OutputGetChannelInfo.fromPbInfo(channelInfo)), response);
|
||||
}
|
||||
} else {
|
||||
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER), response);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,8 @@ public class DestroyUserHandler extends IMHandler<WFCMessage.IDBuf> {
|
||||
m_messagesStore.clearUserGroups(fromUser);
|
||||
m_messagesStore.clearUserChannels(fromUser);
|
||||
m_messagesStore.destoryUser(fromUser);
|
||||
|
||||
m_messagesStore.destroyRobot(fromUser);
|
||||
});
|
||||
return ErrorCode.ERROR_CODE_SUCCESS;
|
||||
} else {
|
||||
|
||||
+2
-2
@@ -17,8 +17,8 @@ import win.liyufan.im.IMTopic;
|
||||
import static win.liyufan.im.IMTopic.PutUserSettingTopic;
|
||||
import static win.liyufan.im.UserSettingScope.kUserSettingMyChannels;
|
||||
|
||||
@Handler(value = IMTopic.DestoryChannelInfoTopic)
|
||||
public class DistoryChannelHandler extends GroupHandler<WFCMessage.IDBuf> {
|
||||
@Handler(value = IMTopic.DestroyChannelInfoTopic)
|
||||
public class DistroyChannelHandler extends GroupHandler<WFCMessage.IDBuf> {
|
||||
@Override
|
||||
public ErrorCode action(ByteBuf ackPayload, String clientID, String fromUser, boolean isAdmin, WFCMessage.IDBuf request, Qos1PublishHandler.IMCallback callback) {
|
||||
ErrorCode errorCode = m_messagesStore.distoryChannel(fromUser, request.getId());
|
||||
@@ -2134,6 +2134,12 @@ public class MemoryMessagesStore implements IMessagesStore {
|
||||
mUserMap.put(robot.getUid(), robot);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroyRobot(String robotId) {
|
||||
HazelcastInstance hzInstance = m_Server.getHazelcastInstance();
|
||||
IMap<String, WFCMessage.Robot> mUserMap = hzInstance.getMap(ROBOTS);
|
||||
mUserMap.remove(robotId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ErrorCode getUserInfo(String fromUser, List<WFCMessage.UserRequest> requestList, WFCMessage.PullUserResult.Builder builder) {
|
||||
|
||||
@@ -133,6 +133,7 @@ public interface IMessagesStore {
|
||||
|
||||
WFCMessage.Robot getRobot(String robotId);
|
||||
void addRobot(WFCMessage.Robot robot);
|
||||
void destroyRobot(String robotId);
|
||||
|
||||
ErrorCode getUserInfo(String fromUser, List<WFCMessage.UserRequest> requestList, WFCMessage.PullUserResult.Builder builder);
|
||||
|
||||
|
||||
@@ -70,7 +70,7 @@ public interface IMTopic {
|
||||
String CreateChannelTopic = "CHC";
|
||||
String ModifyChannelInfoTopic = "CHMI";
|
||||
String TransferChannelInfoTopic = "CHT";
|
||||
String DestoryChannelInfoTopic = "CHD";
|
||||
String DestroyChannelInfoTopic = "CHD";
|
||||
String ChannelSearchTopic = "CHS";
|
||||
String ChannelListenTopic = "CHL";
|
||||
String ChannelPullTopic = "CHP";
|
||||
|
||||
@@ -69,6 +69,8 @@ public interface APIPath {
|
||||
String Group_Set_Member_Extra = "/admin/group/member/set_extra";
|
||||
|
||||
String Create_Channel = "/admin/channel/create";
|
||||
String Destroy_Channel = "/admin/channel/destroy";
|
||||
String Get_Channel_Info = "/admin/channel/get";
|
||||
String Get_System_Setting = "/admin/system/get_setting";
|
||||
String Put_System_Setting = "/admin/system/put_setting";
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package cn.wildfirechat.pojos;
|
||||
|
||||
public class InputChannelId {
|
||||
public String channelId;
|
||||
|
||||
public InputChannelId() {
|
||||
}
|
||||
|
||||
public InputChannelId(String channelId) {
|
||||
this.channelId = channelId;
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,18 @@ public class GeneralAdmin {
|
||||
return AdminHttpUtils.httpJsonPost(path, inputCreateChannel, OutputCreateChannel.class);
|
||||
}
|
||||
|
||||
public static IMResult<Void> destroyChannel(String channelId) throws Exception {
|
||||
String path = APIPath.Destroy_Channel;
|
||||
InputChannelId inputChannelId = new InputChannelId(channelId);
|
||||
return AdminHttpUtils.httpJsonPost(path, inputChannelId, Void.class);
|
||||
}
|
||||
|
||||
public static IMResult<OutputGetChannelInfo> getChannelInfo(String channelId) throws Exception {
|
||||
String path = APIPath.Get_Channel_Info;
|
||||
InputChannelId inputChannelId = new InputChannelId(channelId);
|
||||
return AdminHttpUtils.httpJsonPost(path, inputChannelId, OutputGetChannelInfo.class);
|
||||
}
|
||||
|
||||
//以下仅专业版支持
|
||||
public static IMResult<Void> setConversationTop(String userId, int conversationType, String target, int line, boolean isTop) throws Exception {
|
||||
String key = conversationType + "-" + line + "-" + target;
|
||||
|
||||
@@ -19,6 +19,13 @@ import static cn.wildfirechat.proto.ProtoConstants.SystemSettingType.Group_Max_M
|
||||
|
||||
public class Main {
|
||||
private static boolean commercialServer = false;
|
||||
//管理端口是8080
|
||||
private static String AdminUrl = "http://localhost:18080";
|
||||
private static String AdminSecret = "123456";
|
||||
|
||||
//机器人和频道使用IM服务的公开端口80,注意不是18080
|
||||
private static String IMUrl = "http://localhost";
|
||||
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
//admin使用的是18080端口,超级管理接口,理论上不能对外开放端口,也不能让非内部服务知悉密钥。
|
||||
@@ -32,7 +39,7 @@ public class Main {
|
||||
|
||||
static void testAdmin() throws Exception {
|
||||
//初始化服务API
|
||||
AdminConfig.initAdmin("http://localhost:18080", "123456");
|
||||
AdminConfig.initAdmin(AdminUrl, AdminSecret);
|
||||
|
||||
testUser();
|
||||
testUserRelation();
|
||||
@@ -84,7 +91,13 @@ public class Main {
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
|
||||
IMResult<Void> destroyResult = UserAdmin.destroyRobot("robot1");
|
||||
if(destroyResult != null && destroyResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
|
||||
System.out.println("success");
|
||||
} else {
|
||||
System.out.println("destroy user failure");
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
IMResult<InputOutputUserInfo> resultGetUserInfo1 = UserAdmin.getUserByName(userInfo.getName());
|
||||
if (resultGetUserInfo1 != null && resultGetUserInfo1.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
|
||||
@@ -711,17 +724,46 @@ public class Main {
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
String channelName = "MyChannel";
|
||||
String channelOwner = "user1";
|
||||
InputCreateChannel inputCreateChannel = new InputCreateChannel();
|
||||
inputCreateChannel.setName("MyChannel");
|
||||
inputCreateChannel.setOwner("user1");
|
||||
inputCreateChannel.setName(channelName);
|
||||
inputCreateChannel.setOwner(channelOwner);
|
||||
IMResult<OutputCreateChannel> resultCreateChannel = GeneralAdmin.createChannel(inputCreateChannel);
|
||||
if (resultCreateChannel != null && resultCreateChannel.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
|
||||
System.out.println("success");
|
||||
inputCreateChannel.setTargetId(resultCreateChannel.result.getTargetId());
|
||||
} else {
|
||||
System.out.println("create channel failure");
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
IMResult<OutputGetChannelInfo> resultGetChannel = GeneralAdmin.getChannelInfo(inputCreateChannel.getTargetId());
|
||||
if(resultGetChannel != null && resultGetChannel.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS
|
||||
&& resultGetChannel.getResult().getName().equals(channelName)
|
||||
&& resultGetChannel.getResult().getOwner().equals(channelOwner)) {
|
||||
System.out.println("success");
|
||||
} else {
|
||||
System.out.println("get channel failure");
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
IMResult<Void> voidIMResult = GeneralAdmin.destroyChannel(inputCreateChannel.getTargetId());
|
||||
if(voidIMResult != null && voidIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
|
||||
System.out.println("success");
|
||||
} else {
|
||||
System.out.println("destroy channel failure");
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
resultGetChannel = GeneralAdmin.getChannelInfo(inputCreateChannel.getTargetId());
|
||||
if(resultGetChannel != null && resultGetChannel.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS && (resultGetChannel.getResult().getState() & ProtoConstants.ChannelState.Channel_State_Mask_Deleted) > 0) {
|
||||
System.out.println("success");
|
||||
} else {
|
||||
System.out.println("get channel failure");
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
IMResult<HealthCheckResult> health = GeneralAdmin.healthCheck();
|
||||
if(health != null && health.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
|
||||
System.out.println(health.result);
|
||||
@@ -887,10 +929,27 @@ public class Main {
|
||||
}
|
||||
|
||||
static void testRobot() throws Exception {
|
||||
//初始化机器人API,注意端口是80,不是18080
|
||||
String robotId = "robot1";
|
||||
String robotSecret = "123456";
|
||||
//初始化服务API
|
||||
AdminConfig.initAdmin(AdminUrl, AdminSecret);
|
||||
//创建机器人
|
||||
InputCreateRobot createRobot = new InputCreateRobot();
|
||||
createRobot.setUserId(robotId);
|
||||
createRobot.setName(robotId);
|
||||
createRobot.setDisplayName("机器人");
|
||||
createRobot.setOwner("userId1");
|
||||
createRobot.setSecret(robotSecret);
|
||||
createRobot.setCallback("http://127.0.0.1:8883/robot/recvmsg");
|
||||
IMResult<OutputCreateRobot> resultCreateRobot = UserAdmin.createRobot(createRobot);
|
||||
if (resultCreateRobot != null && resultCreateRobot.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
|
||||
System.out.println("Create robot " + resultCreateRobot.getResult().getUserId() + " success");
|
||||
} else {
|
||||
System.out.println("Create robot failure");
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
RobotService robotService = new RobotService("http://localhost", robotId, "123456");
|
||||
RobotService robotService = new RobotService(IMUrl, robotId, robotSecret);
|
||||
|
||||
//***********************************************
|
||||
//**** 机器人API
|
||||
@@ -1156,7 +1215,7 @@ public class Main {
|
||||
//***测试频道API功能,仅专业版支持***
|
||||
static void testChannel() throws Exception {
|
||||
//初始化服务API
|
||||
AdminConfig.initAdmin("http://localhost:18080", "123456");
|
||||
AdminConfig.initAdmin(AdminUrl, AdminSecret);
|
||||
|
||||
//先创建3个用户
|
||||
InputOutputUserInfo userInfo = new InputOutputUserInfo();
|
||||
@@ -1218,7 +1277,7 @@ public class Main {
|
||||
|
||||
|
||||
//2. 初始化api,注意端口是80,不是18080
|
||||
ChannelServiceApi channelServiceApi = new ChannelServiceApi("http://localhost", resultCreateChannel.getResult().getTargetId(), secret);
|
||||
ChannelServiceApi channelServiceApi = new ChannelServiceApi(IMUrl, resultCreateChannel.getResult().getTargetId(), secret);
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -42,6 +42,16 @@ public class UserAdmin {
|
||||
return AdminHttpUtils.httpJsonPost(path, robot, OutputCreateRobot.class);
|
||||
}
|
||||
|
||||
/*
|
||||
销毁机器人和销毁用户是同一个接口
|
||||
*/
|
||||
public static IMResult<Void> destroyRobot(String userId) throws Exception {
|
||||
String path = APIPath.Destroy_User;
|
||||
InputDestroyUser inputDestroyUser = new InputDestroyUser();
|
||||
inputDestroyUser.setUserId(userId);
|
||||
return AdminHttpUtils.httpJsonPost(path, inputDestroyUser, Void.class);
|
||||
}
|
||||
|
||||
public static IMResult<OutputGetIMTokenData> getUserToken(String userId, String clientId, int platform) throws Exception {
|
||||
String path = APIPath.User_Get_Token;
|
||||
InputGetToken getToken = new InputGetToken(userId, clientId, platform);
|
||||
|
||||
Reference in New Issue
Block a user