diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/channel/GetChannelAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/channel/GetChannelAction.java new file mode 100755 index 00000000..ed925478 --- /dev/null +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/channel/GetChannelAction.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.channel; + +import cn.wildfirechat.common.APIPath; +import cn.wildfirechat.common.ErrorCode; +import cn.wildfirechat.pojos.InputGetUserInfo; +import cn.wildfirechat.pojos.InputOutputUserInfo; +import cn.wildfirechat.pojos.OutputGetChannelInfo; +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_Get_Profile) +@HttpMethod("POST") +public class GetChannelAction extends ChannelAction { + + @Override + public boolean action(Request request, Response response) { + if (request.getNettyRequest() instanceof FullHttpRequest) { + response.setStatus(HttpResponseStatus.OK); + RestResult result = RestResult.ok(OutputGetChannelInfo.fromPbInfo(channelInfo)); + + response.setContent(new Gson().toJson(result)); + } + return true; + } +} diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/channel/GetChannelSubscribersAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/channel/GetChannelSubscribersAction.java new file mode 100755 index 00000000..ad56d208 --- /dev/null +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/channel/GetChannelSubscribersAction.java @@ -0,0 +1,46 @@ +/* + * 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.InputGetUserInfo; +import cn.wildfirechat.pojos.InputOutputUserInfo; +import cn.wildfirechat.pojos.OutputStringList; +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; + +import java.util.ArrayList; + +@Route(APIPath.Channel_Subscriber_List) +@HttpMethod("POST") +public class GetChannelSubscribersAction extends ChannelAction { + + @Override + public boolean action(Request request, Response response) { + if (request.getNettyRequest() instanceof FullHttpRequest) { + OutputStringList outputStringList = new OutputStringList(); + outputStringList.setList(new ArrayList<>()); + outputStringList.getList().addAll(messagesStore.getChannelSubscriber(channelInfo.getTargetId())); + + response.setStatus(HttpResponseStatus.OK); + RestResult result = RestResult.ok(outputStringList); + response.setContent(new Gson().toJson(result)); + } + return true; + } +} diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/channel/GetUserAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/channel/GetUserAction.java new file mode 100755 index 00000000..899f851b --- /dev/null +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/channel/GetUserAction.java @@ -0,0 +1,83 @@ +/* + * 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.InputGetUserInfo; +import cn.wildfirechat.pojos.InputOutputUserInfo; +import cn.wildfirechat.proto.ProtoConstants; +import cn.wildfirechat.proto.WFCMessage; +import com.google.gson.Gson; +import com.xiaoleilu.loServer.RestResult; +import com.xiaoleilu.loServer.action.admin.AdminAction; +import com.xiaoleilu.loServer.action.robot.RobotAction; +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_User_Info) +@HttpMethod("POST") +public class GetUserAction extends ChannelAction { + + @Override + public boolean action(Request request, Response response) { + if (request.getNettyRequest() instanceof FullHttpRequest) { + InputGetUserInfo inputUserId = getRequestBody(request.getNettyRequest(), InputGetUserInfo.class); + if (inputUserId != null + && (!StringUtil.isNullOrEmpty(inputUserId.getUserId()) || !StringUtil.isNullOrEmpty(inputUserId.getName()) || !StringUtil.isNullOrEmpty(inputUserId.getMobile()))) { + + WFCMessage.User user = null; + if(!StringUtil.isNullOrEmpty(inputUserId.getUserId())) { + user = messagesStore.getUserInfo(inputUserId.getUserId()); + } else if(!StringUtil.isNullOrEmpty(inputUserId.getName())) { + user = messagesStore.getUserInfoByName(inputUserId.getName()); + } else if(!StringUtil.isNullOrEmpty(inputUserId.getMobile())) { + user = messagesStore.getUserInfoByMobile(inputUserId.getMobile()); + } + + response.setStatus(HttpResponseStatus.OK); + RestResult result; + + if (user == null || StringUtil.isNullOrEmpty(user.getName())) { + result = RestResult.resultOf(ErrorCode.ERROR_CODE_NOT_EXIST); + } else { + if (channelInfo.getOwner().equals(inputUserId.getUserId()) || + messagesStore.checkUserInChannel(user.getUid(), channelInfo.getTargetId())) { + if((channelInfo.getStatus() & ProtoConstants.ChannelState.Channel_State_Mask_FullInfo) > 0) { + result = RestResult.ok(InputOutputUserInfo.fromPbUser(user)); + } else { + WFCMessage.User outUser = WFCMessage.User.newBuilder().setUid(user.getUid()).setName(user.getName()).setPortrait(user.getPortrait()).setDisplayName(user.getDisplayName()).build(); + result = RestResult.ok(InputOutputUserInfo.fromPbUser(outUser)); + } + } else { + if((channelInfo.getStatus() & ProtoConstants.ChannelState.Channel_State_Mask_Unsubscribed_User_Access) > 0) { + WFCMessage.User outUser = WFCMessage.User.newBuilder().setUid(user.getUid()).setName(user.getName()).setPortrait(user.getPortrait()).setDisplayName(user.getDisplayName()).build(); + result = RestResult.ok(InputOutputUserInfo.fromPbUser(outUser)); + } else { + result = RestResult.resultOf(ErrorCode.ERROR_CODE_NOT_RIGHT); + } + } + } + + 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/channel/ModifyChannelProfileAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/channel/ModifyChannelProfileAction.java new file mode 100755 index 00000000..6661b6f0 --- /dev/null +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/channel/ModifyChannelProfileAction.java @@ -0,0 +1,84 @@ +/* + * 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.InputGetUserInfo; +import cn.wildfirechat.pojos.InputModifyChannelInfo; +import cn.wildfirechat.pojos.InputOutputUserInfo; +import cn.wildfirechat.pojos.SendMessageResult; +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 java.util.concurrent.Executor; + +@Route(APIPath.Channel_Update_Profile) +@HttpMethod("POST") +public class ModifyChannelProfileAction extends ChannelAction { + @Override + public boolean action(Request request, Response response) { + if (request.getNettyRequest() instanceof FullHttpRequest) { + InputModifyChannelInfo input = getRequestBody(request.getNettyRequest(), InputModifyChannelInfo.class); + if (input != null) { + WFCMessage.ModifyChannelInfo modifyChannelInfo = WFCMessage.ModifyChannelInfo.newBuilder().setChannelId(channelInfo.getTargetId()).setType(input.getType()).setValue(input.getValue()).build(); + RPCCenter.getInstance().sendRequest(channelInfo.getOwner(), null, IMTopic.ModifyChannelInfoTopic, modifyChannelInfo.toByteArray(), channelInfo.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, null); + } 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); + }; + } + }, false); + 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/com/xiaoleilu/loServer/action/channel/SubscriberChannelAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/channel/SubscriberChannelAction.java new file mode 100755 index 00000000..83917351 --- /dev/null +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/channel/SubscriberChannelAction.java @@ -0,0 +1,90 @@ +/* + * 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.InputChannelSubscribe; +import cn.wildfirechat.pojos.InputModifyChannelInfo; +import cn.wildfirechat.proto.ProtoConstants; +import cn.wildfirechat.proto.WFCMessage; +import com.google.gson.Gson; +import com.hazelcast.util.StringUtil; +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 win.liyufan.im.IMTopic; + +import java.util.concurrent.Executor; + +@Route(APIPath.Channel_Subscribe) +@HttpMethod("POST") +public class SubscriberChannelAction extends ChannelAction { + + @Override + public boolean action(Request request, Response response) { + if (request.getNettyRequest() instanceof FullHttpRequest) { + InputChannelSubscribe input = getRequestBody(request.getNettyRequest(), InputChannelSubscribe.class); + if (input != null && !StringUtil.isNullOrEmpty(input.getTarget())) { + if(input.getSubscribe() > 0 && (channelInfo.getStatus() & ProtoConstants.ChannelState.Channel_State_Mask_Active_Subscribe) == 0) { + response.setStatus(HttpResponseStatus.OK); + RestResult result = RestResult.resultOf(ErrorCode.ERROR_CODE_NOT_RIGHT); + response.setContent(new Gson().toJson(result)); + return true; + } + WFCMessage.ListenChannel listenChannel = WFCMessage.ListenChannel.newBuilder().setChannelId(channelInfo.getTargetId()).setListen(input.getSubscribe()).build(); + RPCCenter.getInstance().sendRequest(input.getTarget(), null, IMTopic.ChannelListenTopic, listenChannel.toByteArray(), input.getTarget(), 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, null); + } 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/sdk/src/main/java/cn/wildfirechat/sdk/Main.java b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java index 73eb0b68..2bc1e1ff 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/Main.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java @@ -1184,8 +1184,6 @@ public class Main { ChannelServiceApi channelServiceApi = new ChannelServiceApi("http://localhost", resultCreateChannel.getResult().getTargetId(), secret); - - if (commercialServer) { //3. 测试channel api功能 IMResult resultVoid = channelServiceApi.subscribe("userId2"); if (resultVoid != null && resultVoid.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { @@ -1234,7 +1232,7 @@ public class Main { System.out.println("get user info failure"); System.exit(-1); } - } + MessagePayload payload = new MessagePayload();