From a6fd11a243191deace00d5fe756091321a62e0cb Mon Sep 17 00:00:00 2001 From: "heavyrain.lee" Date: Thu, 8 Aug 2019 08:27:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=83=A8=E5=88=86=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../action/admin/CreateChannelAction.java | 94 +++++++++++++ .../imhandler/RecallMessageHandler.java | 2 +- .../persistence/MemoryMessagesStore.java | 2 +- .../java/io/moquette/spi/IMessagesStore.java | 2 +- .../InputCreateChannel.java | 131 ++++++++++++++++++ .../OutputCreateChannel.java | 28 ++++ .../java/cn/wildfirechat/common/APIPath.java | 2 + .../cn/wildfirechat/sdk/GeneralAdmin.java | 9 +- .../main/java/cn/wildfirechat/sdk/Main.java | 12 +- 9 files changed, 275 insertions(+), 7 deletions(-) create mode 100755 broker/src/main/java/com/xiaoleilu/loServer/action/admin/CreateChannelAction.java create mode 100755 common/src/main/java/cn.wildfirechat.pojos/InputCreateChannel.java create mode 100755 common/src/main/java/cn.wildfirechat.pojos/OutputCreateChannel.java diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/admin/CreateChannelAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/CreateChannelAction.java new file mode 100755 index 00000000..f9065848 --- /dev/null +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/CreateChannelAction.java @@ -0,0 +1,94 @@ +/* + * 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.*; +import cn.wildfirechat.proto.WFCMessage; +import com.google.gson.Gson; +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.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 io.netty.util.internal.StringUtil; +import win.liyufan.im.IMTopic; +import win.liyufan.im.UUIDGenerator; + +import java.util.concurrent.Executor; + +@Route(APIPath.Create_Channel) +@HttpMethod("POST") +public class CreateChannelAction extends AdminAction { + + @Override + public boolean isTransactionAction() { + return true; + } + + @Override + public boolean action(Request request, Response response) { + if (request.getNettyRequest() instanceof FullHttpRequest) { + InputCreateChannel inputCreateChannel = getRequestBody(request.getNettyRequest(), InputCreateChannel.class); + if (inputCreateChannel != null + && !StringUtil.isNullOrEmpty(inputCreateChannel.getName())) { + + + if(StringUtil.isNullOrEmpty(inputCreateChannel.getTargetId())) { + inputCreateChannel.setTargetId(messagesStore.getShortUUID()); + } + + RPCCenter.getInstance().sendRequest(inputCreateChannel.getOwner(), null, IMTopic.CreateChannelTopic, inputCreateChannel.toProtoChannelInfo().toByteArray(), inputCreateChannel.getOwner(), 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) { + sendResponse(response, null, new OutputCreateChannel(inputCreateChannel.getTargetId())); + } 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); + }; + } + }, true); + return false; + } else { + response.setStatus(HttpResponseStatus.OK); + RestResult result = RestResult.resultOf(ErrorCode.INVALID_PARAMETER); + response.setContent(new Gson().toJson(result)); + } + + } + return true; + } +} diff --git a/broker/src/main/java/io/moquette/imhandler/RecallMessageHandler.java b/broker/src/main/java/io/moquette/imhandler/RecallMessageHandler.java index c0df8d06..50126178 100644 --- a/broker/src/main/java/io/moquette/imhandler/RecallMessageHandler.java +++ b/broker/src/main/java/io/moquette/imhandler/RecallMessageHandler.java @@ -21,7 +21,7 @@ import java.util.Set; public class RecallMessageHandler extends IMHandler { @Override public ErrorCode action(ByteBuf ackPayload, String clientID, String fromUser, boolean isAdmin, WFCMessage.INT64Buf int64Buf, Qos1PublishHandler.IMCallback callback) { - ErrorCode errorCode = m_messagesStore.recallMessage(int64Buf.getId(), fromUser); + ErrorCode errorCode = m_messagesStore.recallMessage(int64Buf.getId(), fromUser, isAdmin); if(errorCode != ErrorCode.ERROR_CODE_SUCCESS) { return errorCode; diff --git a/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java b/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java index 811b5568..c34be91a 100755 --- a/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java +++ b/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java @@ -1134,7 +1134,7 @@ public class MemoryMessagesStore implements IMessagesStore { } @Override - public ErrorCode recallMessage(long messageUid, String operatorId) { + public ErrorCode recallMessage(long messageUid, String operatorId, boolean isAdmin) { HazelcastInstance hzInstance = m_Server.getHazelcastInstance(); IMap mIMap = hzInstance.getMap(MESSAGES_MAP); diff --git a/broker/src/main/java/io/moquette/spi/IMessagesStore.java b/broker/src/main/java/io/moquette/spi/IMessagesStore.java index 7e2341db..70cfbd19 100755 --- a/broker/src/main/java/io/moquette/spi/IMessagesStore.java +++ b/broker/src/main/java/io/moquette/spi/IMessagesStore.java @@ -119,7 +119,7 @@ public interface IMessagesStore { boolean isMemberInGroup(String member, String groupId); ErrorCode canSendMessageInGroup(String member, String groupId); - ErrorCode recallMessage(long messageUid, String operatorId); + ErrorCode recallMessage(long messageUid, String operatorId, boolean isAdmin); WFCMessage.Robot getRobot(String robotId); void addRobot(WFCMessage.Robot robot); diff --git a/common/src/main/java/cn.wildfirechat.pojos/InputCreateChannel.java b/common/src/main/java/cn.wildfirechat.pojos/InputCreateChannel.java new file mode 100755 index 00000000..8501dce6 --- /dev/null +++ b/common/src/main/java/cn.wildfirechat.pojos/InputCreateChannel.java @@ -0,0 +1,131 @@ +/* + * 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 cn.wildfirechat.pojos; + + +import cn.wildfirechat.proto.ProtoConstants; +import cn.wildfirechat.proto.WFCMessage; +import io.netty.util.internal.StringUtil; + +public class InputCreateChannel { + private String owner; + private String name; + private String targetId; + private String callback; + private String portrait; + private int auto; + private String secret; + private String desc; + private int status; + private String extra; + private long updateDt; + + public WFCMessage.ChannelInfo toProtoChannelInfo() { + return WFCMessage.ChannelInfo.newBuilder().setOwner(owner) + .setName(name) + .setTargetId(targetId) + .setCallback(callback) + .setPortrait(portrait) + .setAutomatic(auto) + .setSecret(secret) + .setDesc(desc) + .setStatus(status) + .setExtra(extra) + .setUpdateDt(updateDt) + .build(); + } + + public String getOwner() { + return owner; + } + + public void setOwner(String owner) { + this.owner = owner; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getTargetId() { + return targetId; + } + + public void setTargetId(String targetId) { + this.targetId = targetId; + } + + public String getCallback() { + return callback; + } + + public void setCallback(String callback) { + this.callback = callback; + } + + public String getPortrait() { + return portrait; + } + + public void setPortrait(String portrait) { + this.portrait = portrait; + } + + public int getAuto() { + return auto; + } + + public void setAuto(int auto) { + this.auto = auto; + } + + public String getSecret() { + return secret; + } + + public void setSecret(String secret) { + this.secret = secret; + } + + public String getDesc() { + return desc; + } + + public void setDesc(String desc) { + this.desc = desc; + } + + public int getStatus() { + return status; + } + + public void setStatus(int status) { + this.status = status; + } + + public String getExtra() { + return extra; + } + + public void setExtra(String extra) { + this.extra = extra; + } + + public long getUpdateDt() { + return updateDt; + } + + public void setUpdateDt(long updateDt) { + this.updateDt = updateDt; + } +} diff --git a/common/src/main/java/cn.wildfirechat.pojos/OutputCreateChannel.java b/common/src/main/java/cn.wildfirechat.pojos/OutputCreateChannel.java new file mode 100755 index 00000000..00dd6b29 --- /dev/null +++ b/common/src/main/java/cn.wildfirechat.pojos/OutputCreateChannel.java @@ -0,0 +1,28 @@ +/* + * 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 cn.wildfirechat.pojos; + + +import cn.wildfirechat.proto.WFCMessage; + +public class OutputCreateChannel { + private String targetId; + + public OutputCreateChannel(String targetId) { + this.targetId = targetId; + } + + public String getTargetId() { + return targetId; + } + + public void setTargetId(String targetId) { + this.targetId = targetId; + } +} diff --git a/common/src/main/java/cn/wildfirechat/common/APIPath.java b/common/src/main/java/cn/wildfirechat/common/APIPath.java index 86098f67..0a7912b5 100644 --- a/common/src/main/java/cn/wildfirechat/common/APIPath.java +++ b/common/src/main/java/cn/wildfirechat/common/APIPath.java @@ -36,6 +36,8 @@ public interface APIPath { String Group_Member_Add = "/admin/group/member/add"; String Group_Member_Kickoff = "/admin/group/member/del"; + + String Create_Channel = "/admin/channel/create"; String Get_System_Setting = "/admin/system/get_setting"; String Put_System_Setting = "/admin/system/put_setting"; diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/GeneralAdmin.java b/sdk/src/main/java/cn/wildfirechat/sdk/GeneralAdmin.java index cff264ca..7c955bc5 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/GeneralAdmin.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/GeneralAdmin.java @@ -1,9 +1,7 @@ package cn.wildfirechat.sdk; import cn.wildfirechat.common.APIPath; -import cn.wildfirechat.pojos.InputFriendRequest; -import cn.wildfirechat.pojos.InputGetFriendList; -import cn.wildfirechat.pojos.SystemSettingPojo; +import cn.wildfirechat.pojos.*; import cn.wildfirechat.sdk.model.IMResult; import cn.wildfirechat.sdk.utilities.AdminHttpUtils; @@ -24,4 +22,9 @@ public class GeneralAdmin { return AdminHttpUtils.httpJsonPost(path, input, Void.class); } + public static IMResult createChannel(InputCreateChannel inputCreateChannel) throws Exception { + String path = APIPath.Create_Channel; + return AdminHttpUtils.httpJsonPost(path, inputCreateChannel, OutputCreateChannel.class); + } + } diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/Main.java b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java index 32a86ade..269de2d0 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/Main.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java @@ -251,7 +251,7 @@ public class Main { System.exit(-1); } - voidIMResult = GroupAdmin.kickoffGroupMembers("user1", groupInfo.getTarget_id(), Arrays.asList("use2", "user3"), null, null); + voidIMResult = GroupAdmin.kickoffGroupMembers("user1", groupInfo.getTarget_id(), Arrays.asList("user3"), null, null); if (voidIMResult != null && voidIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("kickoff group member success"); } else { @@ -328,6 +328,16 @@ public class Main { System.exit(-1); } + InputCreateChannel inputCreateChannel = new InputCreateChannel(); + inputCreateChannel.setName("MyChannel"); + IMResult resultCreateChannel = GeneralAdmin.createChannel(inputCreateChannel); + if (resultCreateChannel != null && resultCreateChannel.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { + System.out.println("success"); + } else { + System.out.println("create channel failure"); + System.exit(-1); + } + //初始化机器人API RobotHttpUtils.init("http://localhost", "robot1", "123456"); //***********************************************