diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/robot/DeleteCallbackAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/robot/DeleteCallbackAction.java new file mode 100755 index 00000000..e00533fe --- /dev/null +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/robot/DeleteCallbackAction.java @@ -0,0 +1,41 @@ +/* + * 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.robot; + +import cn.wildfirechat.common.APIPath; +import cn.wildfirechat.common.ErrorCode; +import cn.wildfirechat.pojos.RobotCallbackPojo; +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.Robot_Delete_Callback) +@HttpMethod("POST") +public class DeleteCallbackAction extends RobotAction { + + @Override + public boolean action(Request request, Response response) { + if (request.getNettyRequest() instanceof FullHttpRequest) { + if(!StringUtil.isNullOrEmpty(robot.getCallback())) { + robot = robot.toBuilder().clearCallback().setState(0).build(); + messagesStore.addRobot(robot); + } + response.setStatus(HttpResponseStatus.OK); + RestResult result = RestResult.ok(); + response.setContent(new Gson().toJson(result)); + } + return true; + } +} diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/robot/GetCallbackAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/robot/GetCallbackAction.java new file mode 100755 index 00000000..04117436 --- /dev/null +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/robot/GetCallbackAction.java @@ -0,0 +1,42 @@ +/* + * 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.robot; + +import cn.wildfirechat.common.APIPath; +import cn.wildfirechat.common.ErrorCode; +import cn.wildfirechat.pojos.InputGetUserInfo; +import cn.wildfirechat.pojos.InputOutputUserInfo; +import cn.wildfirechat.pojos.RobotCallbackPojo; +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.Robot_Get_Callback) +@HttpMethod("POST") +public class GetCallbackAction extends RobotAction { + + @Override + public boolean action(Request request, Response response) { + if (request.getNettyRequest() instanceof FullHttpRequest) { + RobotCallbackPojo out = new RobotCallbackPojo(); + out.setUrl(robot.getCallback()); + response.setStatus(HttpResponseStatus.OK); + RestResult result = RestResult.ok(out); + response.setContent(new Gson().toJson(result)); + } + return true; + } +} diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/robot/SetCallbackAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/robot/SetCallbackAction.java new file mode 100755 index 00000000..74d5cb11 --- /dev/null +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/robot/SetCallbackAction.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.robot; + +import cn.wildfirechat.common.APIPath; +import cn.wildfirechat.common.ErrorCode; +import cn.wildfirechat.pojos.InputGetGroup; +import cn.wildfirechat.pojos.RobotCallbackPojo; +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.Robot_Set_Callback) +@HttpMethod("POST") +public class SetCallbackAction extends RobotAction { + + @Override + public boolean action(Request request, Response response) { + if (request.getNettyRequest() instanceof FullHttpRequest) { + RobotCallbackPojo input = getRequestBody(request.getNettyRequest(), RobotCallbackPojo.class); + if(!StringUtil.isNullOrEmpty(input.getUrl())) { + if(!input.getUrl().equals(robot.getCallback()) && robot.getState() != 1) { + robot = robot.toBuilder().setCallback(input.getUrl()).setState(1).build(); + messagesStore.addRobot(robot); + } + + response.setStatus(HttpResponseStatus.OK); + RestResult result = RestResult.ok(); + response.setContent(new Gson().toJson(result)); + } 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/com/xiaoleilu/loServer/action/robot/UpdateProfileAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/robot/UpdateProfileAction.java new file mode 100755 index 00000000..31064c5a --- /dev/null +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/robot/UpdateProfileAction.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.robot; + +import cn.wildfirechat.common.APIPath; +import cn.wildfirechat.common.ErrorCode; +import cn.wildfirechat.pojos.IntStringPairPojo; +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 static cn.wildfirechat.pojos.MyInfoType.*; + +@Route(APIPath.Robot_Update_Profile) +@HttpMethod("POST") +public class UpdateProfileAction extends RobotAction { + + @Override + public boolean action(Request request, Response response) { + if (request.getNettyRequest() instanceof FullHttpRequest) { + IntStringPairPojo input = getRequestBody(request.getNettyRequest(), IntStringPairPojo.class); + + WFCMessage.User.Builder builder = messagesStore.getUserInfo(robot.getUid()).toBuilder(); + boolean modified = false; + switch (input.getIntValue()) { + case Modify_DisplayName: + builder.setDisplayName(input.getStrValue()); + modified = true; + break; + case Modify_Gender: + builder.setGender(Integer.parseInt(input.getStrValue())); + modified = true; + break; + case Modify_Portrait: + builder.setPortrait(input.getStrValue()); + modified = true; + break; + //不允许修改电话号码,如果修改电话号码必须通过admin进行修改 +// case Modify_Mobile: +// builder.setMobile(input.getStrValue()); +// modified = true; +// break; + case Modify_Email: + builder.setEmail(input.getStrValue()); + modified = true; + break; + case Modify_Address: + builder.setAddress(input.getStrValue()); + modified = true; + break; + case Modify_Company: + builder.setCompany(input.getStrValue()); + modified = true; + break; + case Modify_Social: + builder.setSocial(input.getStrValue()); + modified = true; + break; + case Modify_Extra: + builder.setExtra(input.getStrValue()); + modified = true; + break; + default: + break; + } + + response.setStatus(HttpResponseStatus.OK); + RestResult result = RestResult.ok(); + if(modified) { + try { + messagesStore.updateUserInfo(builder.build()); + } catch (Exception e) { + e.printStackTrace(); + result.setErrorCode(ErrorCode.ERROR_CODE_SERVER_ERROR); + } + } + + response.setContent(new Gson().toJson(result)); + } + return true; + } +} diff --git a/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java b/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java index 3bb77eac..49d24f41 100755 --- a/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java +++ b/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java @@ -16,7 +16,6 @@ package io.moquette.persistence; -import cn.wildfirechat.common.IMExceptionEvent; import cn.wildfirechat.pojos.*; import cn.wildfirechat.proto.ProtoConstants; import cn.wildfirechat.proto.WFCMessage; @@ -63,7 +62,7 @@ import static cn.wildfirechat.proto.ProtoConstants.Platform.*; import static io.moquette.BrokerConstants.*; import static io.moquette.server.Constants.MAX_CHATROOM_MESSAGE_QUEUE; import static io.moquette.server.Constants.MAX_MESSAGE_QUEUE; -import static win.liyufan.im.MyInfoType.*; +import static cn.wildfirechat.pojos.MyInfoType.*; import static win.liyufan.im.UserSettingScope.kUserSettingPCOnline; public class MemoryMessagesStore implements IMessagesStore { @@ -2187,6 +2186,15 @@ public class MemoryMessagesStore implements IMessagesStore { } } + @Override + public void updateUserInfo(WFCMessage.User user) throws Exception { + HazelcastInstance hzInstance = m_Server.getHazelcastInstance(); + IMap mUserMap = hzInstance.getMap(USERS); + if(mUserMap.get(user.getUid()) != null) { + databaseStore.updateUser(user); + mUserMap.put(user.getUid(), user); + } + } @Override public WFCMessage.User getUserInfo(String userId) { diff --git a/broker/src/main/java/io/moquette/spi/IMessagesStore.java b/broker/src/main/java/io/moquette/spi/IMessagesStore.java index 5019b47f..16127c08 100755 --- a/broker/src/main/java/io/moquette/spi/IMessagesStore.java +++ b/broker/src/main/java/io/moquette/spi/IMessagesStore.java @@ -144,6 +144,7 @@ public interface IMessagesStore { void addUserInfo(WFCMessage.User user, String password) throws Exception; void destoryUser(String userId); + void updateUserInfo(WFCMessage.User user) throws Exception; WFCMessage.User getUserInfo(String userId); WFCMessage.User getUserInfoByName(String name); WFCMessage.User getUserInfoByMobile(String mobile); diff --git a/common/src/main/java/cn/wildfirechat/common/APIPath.java b/common/src/main/java/cn/wildfirechat/common/APIPath.java index b41d0a3b..1c973125 100644 --- a/common/src/main/java/cn/wildfirechat/common/APIPath.java +++ b/common/src/main/java/cn/wildfirechat/common/APIPath.java @@ -78,7 +78,9 @@ public interface APIPath { 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"; diff --git a/common/src/main/java/cn/wildfirechat/pojos/IntStringPairPojo.java b/common/src/main/java/cn/wildfirechat/pojos/IntStringPairPojo.java new file mode 100755 index 00000000..ae3864a9 --- /dev/null +++ b/common/src/main/java/cn/wildfirechat/pojos/IntStringPairPojo.java @@ -0,0 +1,38 @@ +/* + * 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 IntStringPairPojo { + private int intValue; + private String strValue; + + public IntStringPairPojo() { + } + + public IntStringPairPojo(int intValue, String strValue) { + this.intValue = intValue; + this.strValue = strValue; + } + + public int getIntValue() { + return intValue; + } + + public void setIntValue(int intValue) { + this.intValue = intValue; + } + + public String getStrValue() { + return strValue; + } + + public void setStrValue(String strValue) { + this.strValue = strValue; + } +} diff --git a/broker/src/main/java/win/liyufan/im/MyInfoType.java b/common/src/main/java/cn/wildfirechat/pojos/MyInfoType.java similarity index 94% rename from broker/src/main/java/win/liyufan/im/MyInfoType.java rename to common/src/main/java/cn/wildfirechat/pojos/MyInfoType.java index f9461532..5a2662e5 100755 --- a/broker/src/main/java/win/liyufan/im/MyInfoType.java +++ b/common/src/main/java/cn/wildfirechat/pojos/MyInfoType.java @@ -6,7 +6,7 @@ * file that was distributed with this source code. */ -package win.liyufan.im; +package cn.wildfirechat.pojos; public interface MyInfoType { int Modify_DisplayName = 0; diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/Main.java b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java index b28803ee..c5db2abe 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/Main.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java @@ -14,6 +14,7 @@ import java.util.Arrays; import java.util.Date; import java.util.List; +import static cn.wildfirechat.pojos.MyInfoType.Modify_DisplayName; import static cn.wildfirechat.proto.ProtoConstants.SystemSettingType.Group_Max_Member_Count; public class Main { @@ -809,11 +810,67 @@ public class Main { String robotId = "robot1"; RobotHttpUtils.init("http://localhost", robotId, "123456"); - String userId1 = "IrIhIhss"; - String userId2 = "IrIhIhss"; //*********************************************** //**** 机器人API //*********************************************** + + IMResult userInfoIMResult = RobotService.getProfile(robotId); + if(userInfoIMResult != null && userInfoIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { + System.out.println("get profile success"); + } else { + System.out.println("get profile failure"); + System.exit(-1); + } + + String displayName = "testrobot"+System.currentTimeMillis(); + IMResult voidIMResult1 = RobotService.updateProfile(Modify_DisplayName, displayName); + if(voidIMResult1 != null && voidIMResult1.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { + System.out.println("modify profile success"); + } else { + System.out.println("modify profile failure"); + System.exit(-1); + } + userInfoIMResult = RobotService.getProfile(robotId); + if(userInfoIMResult != null && userInfoIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS && displayName.equals(userInfoIMResult.getResult().getDisplayName())) { + System.out.println("get profile success"); + } else { + System.out.println("get profile failure"); + System.exit(-1); + } + + String robotCallback = "http://hellow123"; + voidIMResult1 = RobotService.setCallback(robotCallback); + if(voidIMResult1 != null && voidIMResult1.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { + System.out.println("set callback success"); + } else { + System.out.println("set callback failure"); + System.exit(-1); + } + + IMResult callbackPojoIMResult = RobotService.getCallback(); + if(callbackPojoIMResult != null && callbackPojoIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS && robotCallback.equals(callbackPojoIMResult.getResult().getUrl())) { + System.out.println("get callback success"); + } else { + System.out.println("get callback failure"); + System.exit(-1); + } + + voidIMResult1 = RobotService.deleteCallback(); + if(voidIMResult1 != null && voidIMResult1.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { + System.out.println("delete callback success"); + } else { + System.out.println("delete callback failure"); + System.exit(-1); + } + + callbackPojoIMResult = RobotService.getCallback(); + if(callbackPojoIMResult != null && callbackPojoIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS && StringUtil.isNullOrEmpty(callbackPojoIMResult.getResult().getUrl())) { + System.out.println("get callback success"); + } else { + System.out.println("get callback failure"); + System.exit(-1); + } + Conversation conversation = new Conversation(); conversation.setTarget("user2"); conversation.setType(ProtoConstants.ConversationType.ConversationType_Private); diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/RobotService.java b/sdk/src/main/java/cn/wildfirechat/sdk/RobotService.java index 8918b655..335cba0a 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/RobotService.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/RobotService.java @@ -44,10 +44,28 @@ public class RobotService { } public static IMResult getCallback() throws Exception { - String path = APIPath.Robot_Set_Callback; + String path = APIPath.Robot_Get_Callback; return RobotHttpUtils.httpJsonPost(path, null, RobotCallbackPojo.class); } + public static IMResult deleteCallback() throws Exception { + String path = APIPath.Robot_Delete_Callback; + return RobotHttpUtils.httpJsonPost(path, null, Void.class); + } + + public static IMResult getProfile(String robotId) throws Exception { + return getUserInfo(robotId); + } + + /* + type可选范围为MyInfoType,注意不能修改电话号码,如果要修改电话号码请使用adminapi进行修改 + */ + public static IMResult updateProfile(int/*MyInfoType*/ type, String value) throws Exception { + String path = APIPath.Robot_Update_Profile; + IntStringPairPojo pojo = new IntStringPairPojo(type, value); + return RobotHttpUtils.httpJsonPost(path, pojo, Void.class); + } + public static IMResult createGroup(PojoGroupInfo group_info, List members, List to_lines, MessagePayload notify_message) throws Exception { String path = APIPath.Robot_Create_Group; PojoGroup pojoGroup = new PojoGroup(); diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/utilities/RobotHttpUtils.java b/sdk/src/main/java/cn/wildfirechat/sdk/utilities/RobotHttpUtils.java index 6c7c22d5..f73271b9 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/utilities/RobotHttpUtils.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/utilities/RobotHttpUtils.java @@ -3,6 +3,7 @@ package cn.wildfirechat.sdk.utilities; import cn.wildfirechat.sdk.model.IMResult; import com.google.gson.Gson; import ikidou.reflect.TypeBuilder; +import io.netty.util.internal.StringUtil; import org.apache.commons.codec.digest.DigestUtils; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; @@ -63,10 +64,12 @@ public class RobotHttpUtils { } LOG.info("http request content: {}", jsonStr); - StringEntity entity = new StringEntity(jsonStr, Charset.forName("UTF-8")); - entity.setContentEncoding("UTF-8"); - entity.setContentType("application/json"); - post.setEntity(entity); + if(!StringUtil.isNullOrEmpty(jsonStr)) { + StringEntity entity = new StringEntity(jsonStr, Charset.forName("UTF-8")); + entity.setContentEncoding("UTF-8"); + entity.setContentType("application/json"); + post.setEntity(entity); + } HttpResponse response = httpClient.execute(post); int statusCode = response.getStatusLine().getStatusCode();