diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/admin/DestroyChannelAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/DestroyChannelAction.java new file mode 100755 index 00000000..fa0eba7d --- /dev/null +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/DestroyChannelAction.java @@ -0,0 +1,62 @@ +/* + * This file is part of the Wildfire Chat package. + * (c) Heavyrain2012 + * + * 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; + } +} diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/admin/GetChannelAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/GetChannelAction.java new file mode 100755 index 00000000..f766d99a --- /dev/null +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/GetChannelAction.java @@ -0,0 +1,51 @@ +/* + * This file is part of the Wildfire Chat package. + * (c) Heavyrain2012 + * + * 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; + } +} diff --git a/broker/src/main/java/io/moquette/imhandler/DestroyUserHandler.java b/broker/src/main/java/io/moquette/imhandler/DestroyUserHandler.java index 39b55bea..07469f47 100644 --- a/broker/src/main/java/io/moquette/imhandler/DestroyUserHandler.java +++ b/broker/src/main/java/io/moquette/imhandler/DestroyUserHandler.java @@ -29,6 +29,8 @@ public class DestroyUserHandler extends IMHandler { m_messagesStore.clearUserGroups(fromUser); m_messagesStore.clearUserChannels(fromUser); m_messagesStore.destoryUser(fromUser); + + m_messagesStore.destroyRobot(fromUser); }); return ErrorCode.ERROR_CODE_SUCCESS; } else { diff --git a/broker/src/main/java/io/moquette/imhandler/DistoryChannelHandler.java b/broker/src/main/java/io/moquette/imhandler/DistroyChannelHandler.java similarity index 91% rename from broker/src/main/java/io/moquette/imhandler/DistoryChannelHandler.java rename to broker/src/main/java/io/moquette/imhandler/DistroyChannelHandler.java index 6f370d24..38f48990 100644 --- a/broker/src/main/java/io/moquette/imhandler/DistoryChannelHandler.java +++ b/broker/src/main/java/io/moquette/imhandler/DistroyChannelHandler.java @@ -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 { +@Handler(value = IMTopic.DestroyChannelInfoTopic) +public class DistroyChannelHandler extends GroupHandler { @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()); diff --git a/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java b/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java index b921d39f..20ccdd7a 100755 --- a/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java +++ b/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java @@ -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 mUserMap = hzInstance.getMap(ROBOTS); + mUserMap.remove(robotId); + } @Override public ErrorCode getUserInfo(String fromUser, List requestList, WFCMessage.PullUserResult.Builder builder) { diff --git a/broker/src/main/java/io/moquette/spi/IMessagesStore.java b/broker/src/main/java/io/moquette/spi/IMessagesStore.java index a7637a35..dafb089e 100755 --- a/broker/src/main/java/io/moquette/spi/IMessagesStore.java +++ b/broker/src/main/java/io/moquette/spi/IMessagesStore.java @@ -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 requestList, WFCMessage.PullUserResult.Builder builder); diff --git a/broker/src/main/java/win/liyufan/im/IMTopic.java b/broker/src/main/java/win/liyufan/im/IMTopic.java index 6d1cf98b..69ac7d4d 100755 --- a/broker/src/main/java/win/liyufan/im/IMTopic.java +++ b/broker/src/main/java/win/liyufan/im/IMTopic.java @@ -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"; diff --git a/common/src/main/java/cn/wildfirechat/common/APIPath.java b/common/src/main/java/cn/wildfirechat/common/APIPath.java index b99a5d2f..2bd43229 100644 --- a/common/src/main/java/cn/wildfirechat/common/APIPath.java +++ b/common/src/main/java/cn/wildfirechat/common/APIPath.java @@ -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"; diff --git a/common/src/main/java/cn/wildfirechat/pojos/InputChannelId.java b/common/src/main/java/cn/wildfirechat/pojos/InputChannelId.java new file mode 100644 index 00000000..5ed02076 --- /dev/null +++ b/common/src/main/java/cn/wildfirechat/pojos/InputChannelId.java @@ -0,0 +1,12 @@ +package cn.wildfirechat.pojos; + +public class InputChannelId { + public String channelId; + + public InputChannelId() { + } + + public InputChannelId(String channelId) { + this.channelId = channelId; + } +} diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/GeneralAdmin.java b/sdk/src/main/java/cn/wildfirechat/sdk/GeneralAdmin.java index 6077c38c..c2b995c5 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/GeneralAdmin.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/GeneralAdmin.java @@ -27,6 +27,18 @@ public class GeneralAdmin { return AdminHttpUtils.httpJsonPost(path, inputCreateChannel, OutputCreateChannel.class); } + public static IMResult 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 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 setConversationTop(String userId, int conversationType, String target, int line, boolean isTop) throws Exception { String key = conversationType + "-" + line + "-" + target; diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/Main.java b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java index de93efee..7fda6423 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/Main.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java @@ -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 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 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 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 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 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 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 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); diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/UserAdmin.java b/sdk/src/main/java/cn/wildfirechat/sdk/UserAdmin.java index e041b81e..4892342e 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/UserAdmin.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/UserAdmin.java @@ -42,6 +42,16 @@ public class UserAdmin { return AdminHttpUtils.httpJsonPost(path, robot, OutputCreateRobot.class); } + /* + 销毁机器人和销毁用户是同一个接口 + */ + public static IMResult 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 getUserToken(String userId, String clientId, int platform) throws Exception { String path = APIPath.User_Get_Token; InputGetToken getToken = new InputGetToken(userId, clientId, platform);