From 724be0b2738a80c3e6a003f5f558f4ef8c6e48a0 Mon Sep 17 00:00:00 2001 From: heavyrian2012 Date: Fri, 17 Nov 2023 09:06:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=8E=B7=E5=8F=96=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E8=81=8A=E5=A4=A9=E5=AE=A4=E7=8A=B6=E6=80=81=E7=9A=84?= =?UTF-8?q?API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../action/admin/GetUserChatroomAction.java | 61 +++++++++++++++++++ .../persistence/MemoryMessagesStore.java | 24 +++++++- .../java/io/moquette/spi/IMessagesStore.java | 2 + .../java/cn/wildfirechat/common/APIPath.java | 1 + .../pojos/OutputUserChatroom.java | 16 +++++ .../cn/wildfirechat/sdk/ChatroomAdmin.java | 6 ++ .../main/java/cn/wildfirechat/sdk/Main.java | 8 +++ 7 files changed, 117 insertions(+), 1 deletion(-) create mode 100755 broker/src/main/java/com/xiaoleilu/loServer/action/admin/GetUserChatroomAction.java create mode 100755 common/src/main/java/cn/wildfirechat/pojos/OutputUserChatroom.java diff --git a/broker/src/main/java/com/xiaoleilu/loServer/action/admin/GetUserChatroomAction.java b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/GetUserChatroomAction.java new file mode 100755 index 00000000..a963bdfc --- /dev/null +++ b/broker/src/main/java/com/xiaoleilu/loServer/action/admin/GetUserChatroomAction.java @@ -0,0 +1,61 @@ +/* + * 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.InputGetChatroomInfo; +import cn.wildfirechat.pojos.InputUserId; +import cn.wildfirechat.pojos.OutputStringList; +import cn.wildfirechat.pojos.OutputUserChatroom; +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.UserClientEntry; +import io.netty.handler.codec.http.FullHttpRequest; +import io.netty.util.internal.StringUtil; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +@Route(APIPath.Chatroom_GetUserChatroom) +@HttpMethod("POST") +public class GetUserChatroomAction extends AdminAction { + + @Override + public boolean isTransactionAction() { + return true; + } + + @Override + public boolean action(Request request, Response response) { + if (request.getNettyRequest() instanceof FullHttpRequest) { + InputUserId inputUserId = getRequestBody(request.getNettyRequest(), InputUserId.class); + String userId = inputUserId.getUserId(); + if (!StringUtil.isNullOrEmpty(userId)) { + OutputUserChatroom outputUserChatroom = messagesStore.getUserChatroom(userId); + + RestResult result; + if (outputUserChatroom != null) { + result = RestResult.ok(outputUserChatroom); + } else { + result = RestResult.resultOf(ErrorCode.ERROR_CODE_NOT_EXIST); + } + setResponseContent(result, response); + } else { + setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER), response); + } + + } + 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 d0fcb53f..c8bbaea8 100755 --- a/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java +++ b/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java @@ -933,7 +933,29 @@ public class MemoryMessagesStore implements IMessagesStore { Collection members = chatroomMembers.get(chatroomId); return members; } - //Todo chatroom + + @Override + public OutputUserChatroom getUserChatroom(String userId) { + String chatroomId = (String) m_Server.getHazelcastInstance().getMap(USER_CHATROOM).get(userId); + if(chatroomId != null) { + MultiMap chatroomMembers = m_Server.getHazelcastInstance().getMultiMap(CHATROOM_MEMBER_IDS); + for (UserClientEntry userClientEntry : chatroomMembers.get(chatroomId)) { + if(userClientEntry.userId.equals(userId)) { + OutputUserChatroom outputUserChatroom = new OutputUserChatroom(); + outputUserChatroom.userId = userId; + outputUserChatroom.chatroomId = chatroomId; + outputUserChatroom.clientId = userClientEntry.clientId; + MemorySessionStore.Session session = m_Server.getStore().sessionsStore().getSession(userClientEntry.clientId); + if(session != null) { + outputUserChatroom.platform = session.getPlatform(); + } + return outputUserChatroom; + } + } + } + return null; + } + @Override public WFCMessage.PullMessageResult fetchChatroomMessage(String fromUser, String chatroomId, String exceptClientId, long fromMessageId) { WFCMessage.PullMessageResult.Builder builder = WFCMessage.PullMessageResult.newBuilder(); diff --git a/broker/src/main/java/io/moquette/spi/IMessagesStore.java b/broker/src/main/java/io/moquette/spi/IMessagesStore.java index f6d999bf..f9aeee61 100755 --- a/broker/src/main/java/io/moquette/spi/IMessagesStore.java +++ b/broker/src/main/java/io/moquette/spi/IMessagesStore.java @@ -17,6 +17,7 @@ package io.moquette.spi; import cn.wildfirechat.pojos.InputOutputUserInfo; +import cn.wildfirechat.pojos.OutputUserChatroom; import cn.wildfirechat.pojos.SystemSettingPojo; import cn.wildfirechat.proto.ProtoConstants; import cn.wildfirechat.proto.WFCMessage; @@ -173,6 +174,7 @@ public interface IMessagesStore { long insertChatroomMessages(String target, int line, long messageId); Collection getChatroomMembers(String chatroomId); + OutputUserChatroom getUserChatroom(String userId); WFCMessage.PullMessageResult fetchChatroomMessage(String fromUser, String chatroomId, String exceptClientId, long fromMessageId); ErrorCode verifyToken(String userId, String token, List serverIPs, List ports); diff --git a/common/src/main/java/cn/wildfirechat/common/APIPath.java b/common/src/main/java/cn/wildfirechat/common/APIPath.java index a349b289..32f343f6 100644 --- a/common/src/main/java/cn/wildfirechat/common/APIPath.java +++ b/common/src/main/java/cn/wildfirechat/common/APIPath.java @@ -5,6 +5,7 @@ public interface APIPath { String Chatroom_Destroy = "/admin/chatroom/del"; String Chatroom_Info = "/admin/chatroom/info"; String Chatroom_GetMembers = "/admin/chatroom/members"; + String Chatroom_GetUserChatroom = "/admin/chatroom/user_chatroom"; String Chatroom_SetBlacklist = "/admin/chatroom/set_black_status"; String Chatroom_GetBlacklist = "/admin/chatroom/get_black_status"; String Chatroom_SetManager = "/admin/chatroom/set_manager"; diff --git a/common/src/main/java/cn/wildfirechat/pojos/OutputUserChatroom.java b/common/src/main/java/cn/wildfirechat/pojos/OutputUserChatroom.java new file mode 100755 index 00000000..8826271d --- /dev/null +++ b/common/src/main/java/cn/wildfirechat/pojos/OutputUserChatroom.java @@ -0,0 +1,16 @@ +/* + * 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 OutputUserChatroom { + public String userId; + public String chatroomId; + public String clientId; + public int/*ProtoConstants.Platform*/ platform; +} diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/ChatroomAdmin.java b/sdk/src/main/java/cn/wildfirechat/sdk/ChatroomAdmin.java index f2701204..f3cfb8f0 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/ChatroomAdmin.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/ChatroomAdmin.java @@ -37,6 +37,12 @@ public class ChatroomAdmin { return AdminHttpUtils.httpJsonPost(path, input, OutputStringList.class); } + public static IMResult getUserChatroom(String userId) throws Exception { + String path = APIPath.Chatroom_GetUserChatroom; + InputUserId input = new InputUserId(userId); + return AdminHttpUtils.httpJsonPost(path, input, OutputUserChatroom.class); + } + //仅专业版支持 //status:0,正常;1,禁言;2,禁止加入 public static IMResult setChatroomBlacklist(String chatroomId, String userId, int status) throws Exception { diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/Main.java b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java index 95cdf964..24d3918a 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/Main.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java @@ -974,6 +974,14 @@ public class Main { System.exit(-1); } + IMResult userChatroomIMResult = ChatroomAdmin.getUserChatroom("hygqmws2k"); + if(userChatroomIMResult != null && (userChatroomIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS || userChatroomIMResult.getErrorCode() == ErrorCode.ERROR_CODE_NOT_EXIST)) { + System.out.println("get user chatroom success"); + } else { + System.out.println("get user chatroom failure"); + System.exit(-1); + } + //下面仅专业版支持 if(commercialServer) { //设置用户聊天室黑名单。0正常;1禁言;2禁止加入。