From 537221b0b4980e7d510631091b5fe3700e52ac61 Mon Sep 17 00:00:00 2001 From: heavyrian2012 Date: Mon, 2 May 2022 22:07:54 +0800 Subject: [PATCH] add application token --- broker/config/wildfirechat.conf | 2 + .../VerifyApplicationUserInfoAction.java | 58 ++++++++ .../java/io/moquette/BrokerConstants.java | 2 + .../imhandler/GetApplicationTokenHandler.java | 29 ++++ .../persistence/MemoryMessagesStore.java | 43 ++++++ .../java/io/moquette/spi/IMessagesStore.java | 4 + .../src/main/java/win/liyufan/im/IMTopic.java | 2 + .../java/cn/wildfirechat/common/APIPath.java | 6 +- .../cn/wildfirechat/common/ErrorCode.java | 1 + ...va => InputVerifyApplicationUserInfo.java} | 2 +- .../pojos/OutputVerifyApplicationUser.java | 21 --- .../OutputVerifyApplicationUserInfo.java | 39 ++++++ .../src/main/resources/wildfirechat.conf | 2 + .../wildfirechat/sdk/ChannelServiceApi.java | 30 +++-- .../main/java/cn/wildfirechat/sdk/Main.java | 124 +++++++++--------- .../cn/wildfirechat/sdk/RobotService.java | 10 +- 16 files changed, 272 insertions(+), 103 deletions(-) create mode 100755 broker/src/main/java/com/xiaoleilu/loServer/action/channel/VerifyApplicationUserInfoAction.java create mode 100644 broker/src/main/java/io/moquette/imhandler/GetApplicationTokenHandler.java rename common/src/main/java/cn/wildfirechat/pojos/{InputVerifyApplicationUser.java => InputVerifyApplicationUserInfo.java} (90%) delete mode 100755 common/src/main/java/cn/wildfirechat/pojos/OutputVerifyApplicationUser.java create mode 100755 common/src/main/java/cn/wildfirechat/pojos/OutputVerifyApplicationUserInfo.java diff --git a/broker/config/wildfirechat.conf b/broker/config/wildfirechat.conf index a28620a0..6dc271ad 100755 --- a/broker/config/wildfirechat.conf +++ b/broker/config/wildfirechat.conf @@ -108,6 +108,8 @@ message.global_mute_exception_types 401,402,403,404,405,407,408,410,411 ## 当为true时,不转发server api发送的消息;当为false时,转发server api发送的消息。 message.no_forward_admin_message false +## 应用密钥 +application.secret_key 123456 ##禁止搜索用户 friend.disable_search false diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/channel/VerifyApplicationUserInfoAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/channel/VerifyApplicationUserInfoAction.java new file mode 100755 index 00000000..f1e815d9 --- /dev/null +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/channel/VerifyApplicationUserInfoAction.java @@ -0,0 +1,58 @@ +/* + * 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.channel; + +import cn.wildfirechat.common.APIPath; +import cn.wildfirechat.common.ErrorCode; +import cn.wildfirechat.pojos.InputVerifyApplicationUserInfo; +import cn.wildfirechat.pojos.OutputVerifyApplicationUserInfo; +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.netty.handler.codec.http.FullHttpRequest; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.netty.util.internal.StringUtil; + +@Route(APIPath.Channel_Verify_Application_UserInfo) +@HttpMethod("POST") +public class VerifyApplicationUserInfoAction extends ChannelAction { + + @Override + public boolean action(Request request, Response response) { + if (request.getNettyRequest() instanceof FullHttpRequest) { + InputVerifyApplicationUserInfo inputUserToken = getRequestBody(request.getNettyRequest(), InputVerifyApplicationUserInfo.class); + RestResult result; + if (inputUserToken == null || StringUtil.isNullOrEmpty(inputUserToken.getToken())) { + result = RestResult.resultOf(ErrorCode.INVALID_PARAMETER); + } else { + String userId = messagesStore.verifyApplicationToken(inputUserToken.getToken(), channelInfo.getTargetId()); + if(userId != null) { + OutputVerifyApplicationUserInfo outputVerifyApplicationUser = new OutputVerifyApplicationUserInfo(); + outputVerifyApplicationUser.setUserId(userId); + WFCMessage.User user = messagesStore.getUserInfo(userId); + if(user != null) { + outputVerifyApplicationUser.setDisplayName(user.getDisplayName()); + outputVerifyApplicationUser.setPortraitUrl(user.getPortrait()); + } + result = RestResult.ok(outputVerifyApplicationUser); + } else { + result = RestResult.resultOf(ErrorCode.ERROR_CODE_APPLICATION_TOKEN_ERROR_OR_TIMEOUT); + } + } + + response.setStatus(HttpResponseStatus.OK); + response.setContent(new Gson().toJson(result)); + } + return true; + } +} diff --git a/broker/src/main/java/io/moquette/BrokerConstants.java b/broker/src/main/java/io/moquette/BrokerConstants.java index 2ee3c554..11d9c206 100755 --- a/broker/src/main/java/io/moquette/BrokerConstants.java +++ b/broker/src/main/java/io/moquette/BrokerConstants.java @@ -176,6 +176,8 @@ public final class BrokerConstants { public static final String MESSAGES_RECALL_TIME_LIMIT = "message.recall_time_limit"; public static final String MESSAGES_DISABLE_GROUP_MANAGER_RECALL = "message.disable_group_manager_recall"; + public static final String APPLICATION_SECRET_KEY = "application.secret_key"; + public static final String HTTP_ADMIN_RATE_LIMIT = "http.admin.rate_limit"; public static final String HTTP_ROBOT_RATE_LIMIT = "http.robot.rate_limit"; public static final String HTTP_CHANNEL_RATE_LIMIT = "http.channel.rate_limit"; diff --git a/broker/src/main/java/io/moquette/imhandler/GetApplicationTokenHandler.java b/broker/src/main/java/io/moquette/imhandler/GetApplicationTokenHandler.java new file mode 100644 index 00000000..fd62d910 --- /dev/null +++ b/broker/src/main/java/io/moquette/imhandler/GetApplicationTokenHandler.java @@ -0,0 +1,29 @@ +/* + * 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 io.moquette.imhandler; + +import cn.wildfirechat.common.ErrorCode; +import cn.wildfirechat.proto.ProtoConstants; +import cn.wildfirechat.proto.WFCMessage; +import io.moquette.spi.impl.Qos1PublishHandler; +import io.netty.buffer.ByteBuf; +import win.liyufan.im.IMTopic; + +import static cn.wildfirechat.common.ErrorCode.ERROR_CODE_SUCCESS; + +@Handler(IMTopic.GetApplicationTokenRequestTopic) +public class GetApplicationTokenHandler extends IMHandler { + @Override + public ErrorCode action(ByteBuf ackPayload, String clientID, String fromUser, ProtoConstants.RequestSourceType requestSourceType, WFCMessage.IDBuf request, Qos1PublishHandler.IMCallback callback) { + String token = m_messagesStore.getApplicationToken(fromUser, request.getId()); + byte[] data = WFCMessage.IDBuf.newBuilder().setId(token).build().toByteArray(); + ackPayload.ensureWritable(data.length).writeBytes(data); + return ERROR_CODE_SUCCESS; + } +} diff --git a/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java b/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java index 6a5c0f88..bc43440f 100755 --- a/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java +++ b/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java @@ -31,6 +31,7 @@ import io.moquette.server.Constants; import io.moquette.server.Server; import io.moquette.spi.IMatchingCondition; import io.moquette.spi.IMessagesStore; +import io.moquette.spi.impl.security.AES; import io.moquette.spi.security.Tokenor; import io.moquette.spi.impl.subscriptions.Topic; import io.netty.handler.codec.mqtt.MqttVersion; @@ -61,6 +62,7 @@ import static cn.wildfirechat.proto.ProtoConstants.PersistFlag.Transparent; import static cn.wildfirechat.proto.ProtoConstants.Platform.*; import static cn.wildfirechat.proto.ProtoConstants.UpdateUserInfoMask.*; import static io.moquette.BrokerConstants.*; +import static io.moquette.BrokerConstants.APPLICATION_SECRET_KEY; import static io.moquette.server.Constants.MAX_CHATROOM_MESSAGE_QUEUE; import static io.moquette.server.Constants.MAX_MESSAGE_QUEUE; import static cn.wildfirechat.pojos.MyInfoType.*; @@ -164,6 +166,8 @@ public class MemoryMessagesStore implements IMessagesStore { private Set mUserHideProperties = new HashSet<>(); + private String mApplicationSecretKey; + MemoryMessagesStore(Server server, DatabaseStore databaseStore) { m_Server = server; this.databaseStore = databaseStore; @@ -442,6 +446,15 @@ public class MemoryMessagesStore implements IMessagesStore { e.printStackTrace(); } + try { + mApplicationSecretKey = server.getConfig().getProperty(APPLICATION_SECRET_KEY, "wfcapp1290secretkey"); + } catch (Exception e) { + e.printStackTrace(); + } + while (StringUtil.isNullOrEmpty(mApplicationSecretKey) || mApplicationSecretKey.length() < 16) { + mApplicationSecretKey += "w"; + mApplicationSecretKey += mApplicationSecretKey; + } } @@ -3301,6 +3314,36 @@ public class MemoryMessagesStore implements IMessagesStore { return true; } + @Override + public String getApplicationToken(String fromUser, String applicationId) { + byte[] token = AES.AESEncrypt(fromUser + "?|?" + System.currentTimeMillis() + "?|?" + applicationId, mApplicationSecretKey); + return new String(Base64.getEncoder().encode(token)); + } + + @Override + public String verifyApplicationToken(String token, String applicationId) { + byte[] data = Base64.getDecoder().decode(token); + data = AES.AESDecrypt(data, mApplicationSecretKey, true); + + if (data == null || data.length == 0) { + return null; + } else { + String str = new String(data); + String[] strArr = str.split("\\?\\|\\?"); + if (strArr.length == 3) { + if (applicationId.equals(strArr[2])) { + long timestamp= Long.parseLong(strArr[1]); + if(System.currentTimeMillis() - timestamp > 60*1000) { + return null; + } + return strArr[0]; + } + } + } + return null; + } + + @Override public boolean isNewFriendWelcomeMessage() { return mFriendNewWelcomeMessage; diff --git a/broker/src/main/java/io/moquette/spi/IMessagesStore.java b/broker/src/main/java/io/moquette/spi/IMessagesStore.java index 73b6cb85..f6f4b470 100755 --- a/broker/src/main/java/io/moquette/spi/IMessagesStore.java +++ b/broker/src/main/java/io/moquette/spi/IMessagesStore.java @@ -193,6 +193,10 @@ public interface IMessagesStore { boolean checkChatroomParticipantIdelTime(MemorySessionStore.Session session); + + String getApplicationToken(String fromUser, String applicationId); + String verifyApplicationToken(String token, String applicationId); + ErrorCode getUserSettings(String userId, long version, WFCMessage.GetUserSettingResult.Builder builder); WFCMessage.UserSettingEntry getUserSetting(String userId, int scope, String key); List getUserSetting(String userId, int scope); diff --git a/broker/src/main/java/win/liyufan/im/IMTopic.java b/broker/src/main/java/win/liyufan/im/IMTopic.java index d64316a9..3adf2740 100755 --- a/broker/src/main/java/win/liyufan/im/IMTopic.java +++ b/broker/src/main/java/win/liyufan/im/IMTopic.java @@ -83,4 +83,6 @@ public interface IMTopic { String KickoffPCClientTopic = "KPCC"; String ClearSessionTopic = "CST"; + + String GetApplicationTokenRequestTopic = "ATR"; } diff --git a/common/src/main/java/cn/wildfirechat/common/APIPath.java b/common/src/main/java/cn/wildfirechat/common/APIPath.java index 9411c435..cabd6f83 100644 --- a/common/src/main/java/cn/wildfirechat/common/APIPath.java +++ b/common/src/main/java/cn/wildfirechat/common/APIPath.java @@ -91,17 +91,15 @@ public interface APIPath { String Channel_Message_Send = "/channel/message/send"; String Channel_Subscribe = "/channel/subscribe"; String Channel_Subscriber_List = "/channel/subscriber_list"; + String Channel_Verify_Application_UserInfo = "/channel/user/verify"; String Robot_User_Info = "/robot/user_info"; String Robot_Message_Send = "/robot/message/send"; - String Robot_Set_Callback = "/robot/set_callback"; String Robot_Get_Callback = "/robot/get_callback"; String Robot_Delete_Callback = "/robot/delete_callback"; String Robot_Update_Profile = "/robot/update_profile"; - - String Robot_Verify_Application_User = "/robot/user/verify"; - + String Robot_Verify_Application_UserInfo = "/robot/user/verify"; String Robot_Group_Member_Add = "/robot/group/member/add"; String Robot_Group_Allow_Member = "/robot/group/manager/allow"; String Robot_Create_Group = "/robot/group/create"; diff --git a/common/src/main/java/cn/wildfirechat/common/ErrorCode.java b/common/src/main/java/cn/wildfirechat/common/ErrorCode.java index ea50a1cf..d8c29137 100755 --- a/common/src/main/java/cn/wildfirechat/common/ErrorCode.java +++ b/common/src/main/java/cn/wildfirechat/common/ErrorCode.java @@ -50,6 +50,7 @@ public enum ErrorCode { ERROR_CODE_CONFERENCE_ROOM_NOT_EXIST(60, "conference room not exist"), ERROR_CODE_CONFERENCE_SERVICE_NOT_AVAILABLE(61, "conference service not available"), + ERROR_CODE_APPLICATION_TOKEN_ERROR_OR_TIMEOUT(70, "application token error or timeout"), ERROR_CODE_SECRET_CHAT_ACCEPTED_BY_OTHER_CLIENT(81, "secret chat accepted by other client"), ERROR_CODE_SECRET_CHAT_SESSION_NOT_EXIST(82, "secret chat session not exist"), diff --git a/common/src/main/java/cn/wildfirechat/pojos/InputVerifyApplicationUser.java b/common/src/main/java/cn/wildfirechat/pojos/InputVerifyApplicationUserInfo.java similarity index 90% rename from common/src/main/java/cn/wildfirechat/pojos/InputVerifyApplicationUser.java rename to common/src/main/java/cn/wildfirechat/pojos/InputVerifyApplicationUserInfo.java index c6cd7e79..8d983cec 100755 --- a/common/src/main/java/cn/wildfirechat/pojos/InputVerifyApplicationUser.java +++ b/common/src/main/java/cn/wildfirechat/pojos/InputVerifyApplicationUserInfo.java @@ -8,7 +8,7 @@ package cn.wildfirechat.pojos; -public class InputVerifyApplicationUser { +public class InputVerifyApplicationUserInfo { private String token; public String getToken() { diff --git a/common/src/main/java/cn/wildfirechat/pojos/OutputVerifyApplicationUser.java b/common/src/main/java/cn/wildfirechat/pojos/OutputVerifyApplicationUser.java deleted file mode 100755 index 17d9740d..00000000 --- a/common/src/main/java/cn/wildfirechat/pojos/OutputVerifyApplicationUser.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * 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; - -public class OutputVerifyApplicationUser { - private String userId; - - public String getUserId() { - return userId; - } - - public void setUserId(String userId) { - this.userId = userId; - } -} diff --git a/common/src/main/java/cn/wildfirechat/pojos/OutputVerifyApplicationUserInfo.java b/common/src/main/java/cn/wildfirechat/pojos/OutputVerifyApplicationUserInfo.java new file mode 100755 index 00000000..032390a6 --- /dev/null +++ b/common/src/main/java/cn/wildfirechat/pojos/OutputVerifyApplicationUserInfo.java @@ -0,0 +1,39 @@ +/* + * 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; + +public class OutputVerifyApplicationUserInfo { + private String userId; + private String displayName; + private String portraitUrl; + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public String getDisplayName() { + return displayName; + } + + public void setDisplayName(String displayName) { + this.displayName = displayName; + } + + public String getPortraitUrl() { + return portraitUrl; + } + + public void setPortraitUrl(String portraitUrl) { + this.portraitUrl = portraitUrl; + } +} diff --git a/distribution/src/main/resources/wildfirechat.conf b/distribution/src/main/resources/wildfirechat.conf index 2a3ffef9..e3b4620f 100755 --- a/distribution/src/main/resources/wildfirechat.conf +++ b/distribution/src/main/resources/wildfirechat.conf @@ -103,6 +103,8 @@ message.global_mute_exception_types 401,402,403,404,405,407,408,410,411 ## 当为true时,不转发server api发送的消息;当为false时,转发server api发送的消息。 message.no_forward_admin_message false +## 应用密钥 +application.secret_key 123456 ##禁止搜索用户 friend.disable_search false diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/ChannelServiceApi.java b/sdk/src/main/java/cn/wildfirechat/sdk/ChannelServiceApi.java index 37aeb5b5..2e53f057 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/ChannelServiceApi.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/ChannelServiceApi.java @@ -9,28 +9,28 @@ import java.util.List; //仅专业版支持,社区版不支持 public class ChannelServiceApi { - private final ChannelHttpUtils httpUtils; + private final ChannelHttpUtils channelHttpUtils; public ChannelServiceApi(String imurl, String channelId, String secret) { - httpUtils = new ChannelHttpUtils(imurl, channelId, secret); + channelHttpUtils = 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); + return channelHttpUtils.httpJsonPost(path, getUserInfo, InputOutputUserInfo.class); } public IMResult getUserInfoByName(String userName) throws Exception { String path = APIPath.Channel_User_Info; InputGetUserInfo getUserInfo = new InputGetUserInfo(null, userName, null); - return httpUtils.httpJsonPost(path, getUserInfo, InputOutputUserInfo.class); + return channelHttpUtils.httpJsonPost(path, getUserInfo, InputOutputUserInfo.class); } public IMResult getUserInfoByMobile(String mobile) throws Exception { String path = APIPath.Channel_User_Info; InputGetUserInfo getUserInfo = new InputGetUserInfo(null, null, mobile); - return httpUtils.httpJsonPost(path, getUserInfo, InputOutputUserInfo.class); + return channelHttpUtils.httpJsonPost(path, getUserInfo, InputOutputUserInfo.class); } public IMResult modifyChannelInfo(/*ProtoConstants.ModifyChannelInfoType*/int type, String value) throws Exception { @@ -38,12 +38,12 @@ public class ChannelServiceApi { InputModifyChannelInfo modifyChannelInfo = new InputModifyChannelInfo(); modifyChannelInfo.setType(type); modifyChannelInfo.setValue(value); - return httpUtils.httpJsonPost(path, modifyChannelInfo, Void.class); + return channelHttpUtils.httpJsonPost(path, modifyChannelInfo, Void.class); } public IMResult getChannelInfo() throws Exception { String path = APIPath.Channel_Get_Profile; - return httpUtils.httpJsonPost(path, null, OutputGetChannelInfo.class); + return channelHttpUtils.httpJsonPost(path, null, OutputGetChannelInfo.class); } public IMResult sendMessage(int line, List targets, MessagePayload payload) throws Exception { @@ -52,7 +52,7 @@ public class ChannelServiceApi { messageData.setLine(0); messageData.setTargets(targets); messageData.setPayload(payload); - return httpUtils.httpJsonPost(path, messageData, SendMessageResult.class); + return channelHttpUtils.httpJsonPost(path, messageData, SendMessageResult.class); } public IMResult subscribe(String userId) throws Exception { @@ -60,7 +60,7 @@ public class ChannelServiceApi { InputChannelSubscribe input = new InputChannelSubscribe(); input.setTarget(userId); input.setSubscribe(1); - return httpUtils.httpJsonPost(path, input, Void.class); + return channelHttpUtils.httpJsonPost(path, input, Void.class); } public IMResult unsubscribe(String userId) throws Exception { @@ -68,12 +68,20 @@ public class ChannelServiceApi { InputChannelSubscribe input = new InputChannelSubscribe(); input.setTarget(userId); input.setSubscribe(0); - return httpUtils.httpJsonPost(path, input, Void.class); + return channelHttpUtils.httpJsonPost(path, input, Void.class); } public IMResult getSubscriberList() throws Exception { String path = APIPath.Channel_Subscriber_List; - return httpUtils.httpJsonPost(path, null, OutputStringList.class); + return channelHttpUtils.httpJsonPost(path, null, OutputStringList.class); + } + + + public IMResult verifyApplicationUserInfo(String token) throws Exception { + String path = APIPath.Channel_Verify_Application_UserInfo; + InputVerifyApplicationUserInfo input = new InputVerifyApplicationUserInfo(); + input.setToken(token); + return channelHttpUtils.httpJsonPost(path, input, OutputVerifyApplicationUserInfo.class); } } diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/Main.java b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java index e2ad1ce0..125abc42 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/Main.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java @@ -1328,58 +1328,54 @@ public class Main { ChannelServiceApi channelServiceApi = new ChannelServiceApi(IMUrl, resultCreateChannel.getResult().getTargetId(), secret); - - if (commercialServer) { - //3. 测试channel api功能 - IMResult resultVoid = channelServiceApi.subscribe("userId2"); - if (resultVoid != null && resultVoid.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { - System.out.println("subscribe success"); - } else { - System.out.println("subscribe failure"); - System.exit(-1); - } - - resultVoid = channelServiceApi.subscribe("userId3"); - if (resultVoid != null && resultVoid.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { - System.out.println("subscribe done"); - } else { - System.out.println("subscribe failure"); - System.exit(-1); - } - - IMResult resultStringList = channelServiceApi.getSubscriberList(); - if (resultStringList != null && resultStringList.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS && resultStringList.getResult().getList().contains("userId2") && resultStringList.getResult().getList().contains("userId3")) { - System.out.println("get subscriber done"); - } else { - System.out.println("get subscriber failure"); - System.exit(-1); - } - - resultVoid = channelServiceApi.unsubscribe("userId2"); - if (resultVoid != null && resultVoid.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { - System.out.println("unsubscriber done"); - } else { - System.out.println("unsubscriber failure"); - System.exit(-1); - } - - resultStringList = channelServiceApi.getSubscriberList(); - if (resultStringList != null && resultStringList.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS && resultStringList.getResult().getList().contains("userId3") && !resultStringList.getResult().getList().contains("userId2")) { - System.out.println("get subscriber done"); - } else { - System.out.println("get subscriber failure"); - System.exit(-1); - } - - IMResult resultGetUserInfo1 = channelServiceApi.getUserInfo("userId3"); - if (resultGetUserInfo1 != null && resultGetUserInfo1.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { - System.out.println("get user info success"); - } else { - System.out.println("get user info failure"); - System.exit(-1); - } + //3. 测试channel api功能 + IMResult resultVoid = channelServiceApi.subscribe("userId2"); + if (resultVoid != null && resultVoid.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { + System.out.println("subscribe success"); + } else { + System.out.println("subscribe failure"); + System.exit(-1); } + resultVoid = channelServiceApi.subscribe("userId3"); + if (resultVoid != null && resultVoid.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { + System.out.println("subscribe done"); + } else { + System.out.println("subscribe failure"); + System.exit(-1); + } + + IMResult resultStringList = channelServiceApi.getSubscriberList(); + if (resultStringList != null && resultStringList.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS && resultStringList.getResult().getList().contains("userId2") && resultStringList.getResult().getList().contains("userId3")) { + System.out.println("get subscriber done"); + } else { + System.out.println("get subscriber failure"); + System.exit(-1); + } + + resultVoid = channelServiceApi.unsubscribe("userId2"); + if (resultVoid != null && resultVoid.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { + System.out.println("unsubscriber done"); + } else { + System.out.println("unsubscriber failure"); + System.exit(-1); + } + + resultStringList = channelServiceApi.getSubscriberList(); + if (resultStringList != null && resultStringList.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS && resultStringList.getResult().getList().contains("userId3") && !resultStringList.getResult().getList().contains("userId2")) { + System.out.println("get subscriber done"); + } else { + System.out.println("get subscriber failure"); + System.exit(-1); + } + + IMResult resultGetUserInfo1 = channelServiceApi.getUserInfo("userId3"); + if (resultGetUserInfo1 != null && resultGetUserInfo1.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { + System.out.println("get user info success"); + } else { + System.out.println("get user info failure"); + System.exit(-1); + } MessagePayload payload = new MessagePayload(); payload.setType(1); @@ -1403,22 +1399,20 @@ public class Main { System.exit(-1); } - if (commercialServer) { - IMResult 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("modify channel profile success"); - } else { - System.out.println("modify channel profile failure"); - System.exit(-1); - } + IMResult 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("modify channel profile success"); + } else { + System.out.println("modify channel profile failure"); + System.exit(-1); + } - IMResult outputGetChannelInfoIMResult = channelServiceApi.getChannelInfo(); - if (outputGetChannelInfoIMResult != null && outputGetChannelInfoIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { - System.out.println("get channel info success"); - } else { - System.out.println("get channel info failure"); - System.exit(-1); - } + IMResult outputGetChannelInfoIMResult = channelServiceApi.getChannelInfo(); + if (outputGetChannelInfoIMResult != null && outputGetChannelInfoIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { + System.out.println("get channel info success"); + } else { + System.out.println("get channel info failure"); + System.exit(-1); } } static void testSensitiveApi() throws Exception { diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/RobotService.java b/sdk/src/main/java/cn/wildfirechat/sdk/RobotService.java index 259abbf5..67db481c 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/RobotService.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/RobotService.java @@ -4,7 +4,6 @@ import cn.wildfirechat.common.APIPath; import cn.wildfirechat.pojos.*; import cn.wildfirechat.sdk.model.IMResult; import cn.wildfirechat.sdk.utilities.RobotHttpUtils; -import cn.wildfirechat.sdk.utilities.RobotHttpUtils; import java.util.List; @@ -214,4 +213,13 @@ public class RobotService { input.setNotify_message(notify_message); return robotHttpUtils.httpJsonPost(path, input, Void.class); } + + public IMResult verifyApplicationUserInfo(String token) throws Exception { + String path = APIPath.Robot_Verify_Application_UserInfo; + InputVerifyApplicationUserInfo input = new InputVerifyApplicationUserInfo(); + input.setToken(token); + return robotHttpUtils.httpJsonPost(path, input, OutputVerifyApplicationUserInfo.class); + } + + }