From e4da3780f57f83fd3c2a26a6fcc5c24ff9192775 Mon Sep 17 00:00:00 2001 From: "heavyrain.lee" Date: Sat, 4 Jan 2020 17:04:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=A2=91=E9=81=93sdk?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../action/admin/CreateChannelAction.java | 5 +- .../imhandler/CreateChannelHandler.java | 2 +- .../java/cn/wildfirechat/common/APIPath.java | 6 +- .../pojos/InputChannelSubscribe.java | 36 ++++++++ .../pojos/InputModifyChannelInfo.java | 36 ++++++++ .../pojos/OutputCreateChannel.java | 12 ++- .../pojos/SendChannelMessageData.java | 66 ++++++++++++++ .../wildfirechat/pojos/UserOnlineStatus.java | 1 + .../wildfirechat/sdk/ChannelServiceApi.java | 65 +++++++++++++ .../main/java/cn/wildfirechat/sdk/Main.java | 91 ++++++++++++++++++- .../sdk/utilities/ChannelHttpUtils.java | 22 +++-- 11 files changed, 327 insertions(+), 15 deletions(-) create mode 100644 common/src/main/java/cn/wildfirechat/pojos/InputChannelSubscribe.java create mode 100755 common/src/main/java/cn/wildfirechat/pojos/InputModifyChannelInfo.java create mode 100644 common/src/main/java/cn/wildfirechat/pojos/SendChannelMessageData.java create mode 100644 sdk/src/main/java/cn/wildfirechat/sdk/ChannelServiceApi.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 index 798bbad5..364e6ea8 100755 --- a/broker/src/main/java/com/xiaoleilu/loServer/action/admin/CreateChannelAction.java +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/CreateChannelAction.java @@ -59,7 +59,10 @@ public class CreateChannelAction extends AdminAction { byteBuf.writeBytes(result); ErrorCode errorCode = ErrorCode.fromCode(byteBuf.readByte()); if (errorCode == ErrorCode.ERROR_CODE_SUCCESS) { - sendResponse(response, null, new OutputCreateChannel(inputCreateChannel.getTargetId())); + byte[] data = new byte[byteBuf.readableBytes()]; + byteBuf.readBytes(data); + String secret = new String(data); + sendResponse(response, null, new OutputCreateChannel(inputCreateChannel.getTargetId(), secret)); } else { sendResponse(response, errorCode, null); } diff --git a/broker/src/main/java/io/moquette/imhandler/CreateChannelHandler.java b/broker/src/main/java/io/moquette/imhandler/CreateChannelHandler.java index bcc7d937..f619a40f 100644 --- a/broker/src/main/java/io/moquette/imhandler/CreateChannelHandler.java +++ b/broker/src/main/java/io/moquette/imhandler/CreateChannelHandler.java @@ -41,7 +41,7 @@ public class CreateChannelHandler extends GroupHandler { if (errorCode == ErrorCode.ERROR_CODE_SUCCESS) { WFCMessage.ModifyUserSettingReq modifyUserSettingReq = WFCMessage.ModifyUserSettingReq.newBuilder().setScope(kUserSettingMyChannels).setKey(request.getTargetId()).setValue("1").build(); mServer.internalRpcMsg(fromUser, null, modifyUserSettingReq.toByteArray(), 0, fromUser, PutUserSettingTopic, false); - byte[] data = request.getTargetId().getBytes(); + byte[] data = request.getSecret().getBytes(); ackPayload.ensureWritable(data.length).writeBytes(data); return ErrorCode.ERROR_CODE_SUCCESS; } else { diff --git a/common/src/main/java/cn/wildfirechat/common/APIPath.java b/common/src/main/java/cn/wildfirechat/common/APIPath.java index 346b2ee2..80795d66 100644 --- a/common/src/main/java/cn/wildfirechat/common/APIPath.java +++ b/common/src/main/java/cn/wildfirechat/common/APIPath.java @@ -48,8 +48,12 @@ public interface APIPath { String Get_System_Setting = "/admin/system/get_setting"; String Put_System_Setting = "/admin/system/put_setting"; - + String Channel_User_Info = "/channel/user_info"; + String Channel_Update_Profile = "/channel/update_profile"; String Channel_Message_Send = "/channel/message/send"; + String Channel_Subscribe = "/channel/subscribe"; + String Channel_Unsubscribe = "/channel/unsubscribe"; + String Channel_Subscriber_List = "/channel/subscriber_list"; String Robot_User_Info = "/robot/user_info"; String Robot_Message_Send = "/robot/message/send"; diff --git a/common/src/main/java/cn/wildfirechat/pojos/InputChannelSubscribe.java b/common/src/main/java/cn/wildfirechat/pojos/InputChannelSubscribe.java new file mode 100644 index 00000000..40d4c9bf --- /dev/null +++ b/common/src/main/java/cn/wildfirechat/pojos/InputChannelSubscribe.java @@ -0,0 +1,36 @@ +/* + * 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 java.util.ArrayList; +import java.util.List; + +public class InputChannelSubscribe { + private List targets; + private int subscribe; //1,订阅;0,取消订阅 + + public List getTargets() { + return targets; + } + + public void setTargets(List targets) { + this.targets = targets; + } + + public int getSubscribe() { + return subscribe; + } + + public void setSubscribe(int subscribe) { + this.subscribe = subscribe; + } +} diff --git a/common/src/main/java/cn/wildfirechat/pojos/InputModifyChannelInfo.java b/common/src/main/java/cn/wildfirechat/pojos/InputModifyChannelInfo.java new file mode 100755 index 00000000..3ae11f92 --- /dev/null +++ b/common/src/main/java/cn/wildfirechat/pojos/InputModifyChannelInfo.java @@ -0,0 +1,36 @@ +/* + * 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; +import io.netty.util.internal.StringUtil; + +import java.util.Arrays; + +public class InputModifyChannelInfo { + private int type; + private String value; + + public int getType() { + return type; + } + + public void setType(int type) { + this.type = type; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/common/src/main/java/cn/wildfirechat/pojos/OutputCreateChannel.java b/common/src/main/java/cn/wildfirechat/pojos/OutputCreateChannel.java index 00dd6b29..598788a9 100755 --- a/common/src/main/java/cn/wildfirechat/pojos/OutputCreateChannel.java +++ b/common/src/main/java/cn/wildfirechat/pojos/OutputCreateChannel.java @@ -13,9 +13,11 @@ import cn.wildfirechat.proto.WFCMessage; public class OutputCreateChannel { private String targetId; + private String secret; - public OutputCreateChannel(String targetId) { + public OutputCreateChannel(String targetId, String secret) { this.targetId = targetId; + this.secret = secret; } public String getTargetId() { @@ -25,4 +27,12 @@ public class OutputCreateChannel { public void setTargetId(String targetId) { this.targetId = targetId; } + + public String getSecret() { + return secret; + } + + public void setSecret(String secret) { + this.secret = secret; + } } diff --git a/common/src/main/java/cn/wildfirechat/pojos/SendChannelMessageData.java b/common/src/main/java/cn/wildfirechat/pojos/SendChannelMessageData.java new file mode 100644 index 00000000..b0f1d569 --- /dev/null +++ b/common/src/main/java/cn/wildfirechat/pojos/SendChannelMessageData.java @@ -0,0 +1,66 @@ +/* + * 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; + +import java.util.ArrayList; +import java.util.List; + +public class SendChannelMessageData { + private List targets; + private int line; + private MessagePayload payload; + + public List getTargets() { + return targets; + } + + public void setTargets(List targets) { + this.targets = targets; + } + + public int getLine() { + return line; + } + + public void setLine(int line) { + this.line = line; + } + + public MessagePayload getPayload() { + return payload; + } + + public void setPayload(MessagePayload payload) { + this.payload = payload; + } + + public static boolean isValide(SendChannelMessageData sendMessageData) { + if(sendMessageData == null || + sendMessageData.getPayload() == null) { + return false; + } + return true; + } + + public WFCMessage.Message toProtoMessage(String channelId, String channelOwner) { + if (targets == null) { + targets = new ArrayList<>(); + } + + return WFCMessage.Message.newBuilder().setFromUser(channelOwner) + .setConversation(WFCMessage.Conversation.newBuilder().setType(ProtoConstants.ConversationType.ConversationType_Channel).setTarget(channelId).setLine(line)) + .addAllTo(targets) + .setContent(payload.toProtoMessageContent()) + .build(); + } +} diff --git a/common/src/main/java/cn/wildfirechat/pojos/UserOnlineStatus.java b/common/src/main/java/cn/wildfirechat/pojos/UserOnlineStatus.java index edfbc000..8ae14bd4 100644 --- a/common/src/main/java/cn/wildfirechat/pojos/UserOnlineStatus.java +++ b/common/src/main/java/cn/wildfirechat/pojos/UserOnlineStatus.java @@ -4,6 +4,7 @@ public class UserOnlineStatus { public static final int ONLINE = 0; public static final int OFFLINE = 1; public static final int LOGOUT = -1; + public String userId; public String clientId; public int platform; diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/ChannelServiceApi.java b/sdk/src/main/java/cn/wildfirechat/sdk/ChannelServiceApi.java new file mode 100644 index 00000000..af27e6c8 --- /dev/null +++ b/sdk/src/main/java/cn/wildfirechat/sdk/ChannelServiceApi.java @@ -0,0 +1,65 @@ +package cn.wildfirechat.sdk; + +import cn.wildfirechat.common.APIPath; +import cn.wildfirechat.pojos.*; +import cn.wildfirechat.proto.ProtoConstants; +import cn.wildfirechat.sdk.model.IMResult; +import cn.wildfirechat.sdk.utilities.AdminHttpUtils; +import cn.wildfirechat.sdk.utilities.ChannelHttpUtils; +import cn.wildfirechat.sdk.utilities.RobotHttpUtils; + +import java.util.List; + +//仅专业版支持,社区版不支持 +public class ChannelServiceApi { + private ChannelHttpUtils httpUtils; + + public ChannelServiceApi(String imurl, String channelId, String secret) { + httpUtils = new ChannelHttpUtils(imurl, channelId, secret); + } + + public IMResult getUserInfo(String userId) throws Exception { + String path = APIPath.Channel_User_Info; + InputGetUserInfo getUserInfo = new InputGetUserInfo(userId, null, null); + return httpUtils.httpJsonPost(path, getUserInfo, InputOutputUserInfo.class); + } + + public IMResult modifyChannelInfo(/*ProtoConstants.ModifyChannelInfoType*/int type, String value) throws Exception { + String path = APIPath.Channel_Update_Profile; + InputModifyChannelInfo modifyChannelInfo = new InputModifyChannelInfo(); + modifyChannelInfo.setType(type); + modifyChannelInfo.setValue(value); + return httpUtils.httpJsonPost(path, modifyChannelInfo, Void.class); + } + + public IMResult sendMessage(int line, List targets, MessagePayload payload) throws Exception { + String path = APIPath.Channel_Message_Send; + SendChannelMessageData messageData = new SendChannelMessageData(); + messageData.setLine(0); + messageData.setTargets(targets); + messageData.setPayload(payload); + return httpUtils.httpJsonPost(path, messageData, SendMessageResult.class); + } + + public IMResult subscribe(List targets) throws Exception { + String path = APIPath.Channel_Subscribe; + InputChannelSubscribe input = new InputChannelSubscribe(); + input.setTargets(targets); + input.setSubscribe(1); + return httpUtils.httpJsonPost(path, input, Void.class); + } + + public IMResult unsubscribe(List targets) throws Exception { + String path = APIPath.Channel_Unsubscribe; + InputChannelSubscribe input = new InputChannelSubscribe(); + input.setTargets(targets); + input.setSubscribe(0); + return httpUtils.httpJsonPost(path, input, Void.class); + } + + public IMResult getSubscriberList() throws Exception { + String path = APIPath.Channel_Subscriber_List; + return httpUtils.httpJsonPost(path, null, OutputStringList.class); + } + +} diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/Main.java b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java index 7e09fa19..2dce8a79 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/Main.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java @@ -10,6 +10,7 @@ import cn.wildfirechat.sdk.utilities.RobotHttpUtils; import java.util.ArrayList; import java.util.Arrays; +import java.util.Date; import java.util.List; import static cn.wildfirechat.proto.ProtoConstants.SystemSettingType.Group_Max_Member_Count; @@ -455,6 +456,15 @@ public class Main { } + voidIMResult = UserAdmin.destroyUser("user11"); + if (voidIMResult != null && voidIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { + System.out.println("destroy user success"); + } else { + System.out.println("destroy user failure"); + System.exit(-1); + } + + //初始化机器人API RobotHttpUtils.init("http://localhost", "robot1", "123456"); //*********************************************** @@ -483,8 +493,87 @@ public class Main { System.exit(-1); } + //测试频道API功能,仅专业版支持 + //1. 先创建频道 + inputCreateChannel = new InputCreateChannel(); + inputCreateChannel.setName("testChannel"); + inputCreateChannel.setOwner("user1"); + 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); + } - voidIMResult = UserAdmin.destroyUser("user1"); + //2. 初始化api + ChannelServiceApi channelServiceApi = new ChannelServiceApi("http://localhost", inputCreateChannel.getTargetId(), inputCreateChannel.getSecret()); + + //3. 测试channel api功能 + resultVoid = channelServiceApi.subscribe(Arrays.asList("user2","user3")); + if (resultVoid != null && resultVoid.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { + System.out.println("block user done"); + } else { + System.out.println("block user failure"); + System.exit(-1); + } + + IMResult resultStringList = channelServiceApi.getSubscriberList(); + if (resultStringList != null && resultStringList.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS && resultStringList.getResult().getList().contains("user2") && resultStringList.getResult().getList().contains("user3")) { + System.out.println("block user done"); + } else { + System.out.println("block user failure"); + System.exit(-1); + } + + resultVoid = channelServiceApi.unsubscribe(Arrays.asList("user2")); + if (resultVoid != null && resultVoid.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { + System.out.println("block user done"); + } else { + System.out.println("block user failure"); + System.exit(-1); + } + + resultStringList = channelServiceApi.getSubscriberList(); + if (resultStringList != null && resultStringList.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS && resultStringList.getResult().getList().contains("user2") && !resultStringList.getResult().getList().contains("user3")) { + System.out.println("block user done"); + } else { + System.out.println("block user failure"); + System.exit(-1); + } + + resultGetUserInfo1 = channelServiceApi.getUserInfo("user2"); + if (resultGetUserInfo1 != null && resultGetUserInfo1.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { + System.out.println("get user info success"); + } else { + System.out.println("get user info by name failure"); + System.exit(-1); + } + + + payload = new MessagePayload(); + payload.setType(1); + payload.setSearchableContent("hello world"); + + resultSendMessage = channelServiceApi.sendMessage(0, null,payload); + if (resultSendMessage != null && resultSendMessage.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { + System.out.println("send message success"); + } else { + System.out.println("send message failure"); + System.exit(-1); + } + + payload.setSearchableContent("hello to user2"); + + resultSendMessage = channelServiceApi.sendMessage(0, Arrays.asList("user2"),payload); + if (resultSendMessage != null && resultSendMessage.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { + System.out.println("send message success"); + } else { + System.out.println("send message failure"); + System.exit(-1); + } + + voidIMResult = channelServiceApi.modifyChannelInfo(ProtoConstants.ModifyChannelInfoType.Modify_Channel_Desc, "this is a test channel, update at:" + new Date().toString()); if (voidIMResult != null && voidIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("destroy user success"); } else { diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/utilities/ChannelHttpUtils.java b/sdk/src/main/java/cn/wildfirechat/sdk/utilities/ChannelHttpUtils.java index ba43730f..76124766 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/utilities/ChannelHttpUtils.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/utilities/ChannelHttpUtils.java @@ -22,23 +22,23 @@ import java.nio.charset.Charset; public class ChannelHttpUtils { private static final Logger LOG = LoggerFactory.getLogger(ChannelHttpUtils.class); - private static String url; - private static String channelId; - private static String channelSecret; + private String imurl; + private String channelId; + private String channelSecret; - public static void init(String url, String channelId, String secret) { - ChannelHttpUtils.url = url; - ChannelHttpUtils.channelId = channelId; - ChannelHttpUtils.channelSecret = secret; + public ChannelHttpUtils(String imurl, String channelId, String secret) { + this.imurl = imurl; + this.channelId = channelId; + this.channelSecret = secret; } - public static IMResult httpJsonPost(String path, Object object, Class clazz) throws Exception{ - if (isNullOrEmpty(url) || isNullOrEmpty(path)) { + public IMResult httpJsonPost(String path, Object object, Class clazz) throws Exception{ + if (isNullOrEmpty(imurl) || isNullOrEmpty(path)) { LOG.error("Not init IM SDK correctly. Do you forget init it?"); throw new Exception("SDK url or secret lack!"); } - String url = ChannelHttpUtils.url + path; + String url = imurl + path; HttpPost post = null; try { HttpClient httpClient = HttpClientBuilder.create().build(); @@ -54,8 +54,10 @@ public class ChannelHttpUtils { post.setHeader("Connection", "Keep-Alive"); post.setHeader("nonce", nonce + ""); post.setHeader("timestamp", "" + timestamp); + post.setHeader("cid", channelId); post.setHeader("sign", sign); + String jsonStr = null; if (object != null) { jsonStr = new Gson().toJson(object);