Compare commits

...
10 Commits
22 changed files with 338 additions and 96 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
<relativePath>../</relativePath>
<artifactId>wildfirechat-parent</artifactId>
<groupId>cn.wildfirechat</groupId>
<version>0.76</version>
<version>0.77</version>
</parent>
<artifactId>moquette-broker</artifactId>
@@ -43,27 +43,17 @@ public class ServerSetting {
/** 根目录 */
private static File root;
/** Filter映射表 */
private static Map<String, Filter> filterMap;
private static Map<String, Filter> filterMap = new ConcurrentHashMap<>();
/** Action映射表 */
private static Map<String, Action> getActionMap;
private static Map<String, Action> postActionMap;
private static Map<String, Action> putActionMap;
private static Map<String, Action> deleteActionMap;
private static Map<String, Action> errorActionMap;
private static Map<String, Class<? extends Action>> getActionMap = new ConcurrentHashMap<>();
private static Map<String, Class<? extends Action>> postActionMap = new ConcurrentHashMap<>();
private static Map<String, Class<? extends Action>> putActionMap = new ConcurrentHashMap<>();
private static Map<String, Class<? extends Action>> deleteActionMap = new ConcurrentHashMap<>();
private static Map<String, Class<? extends Action>> errorActionMap = new ConcurrentHashMap<>();
static{
filterMap = new ConcurrentHashMap<String, Filter>();
getActionMap = new ConcurrentHashMap<String, Action>();
postActionMap = new ConcurrentHashMap<String, Action>();
putActionMap = new ConcurrentHashMap<String, Action>();
deleteActionMap = new ConcurrentHashMap<String, Action>();
errorActionMap = new ConcurrentHashMap<String, Action>();
errorActionMap.put(StrUtil.SLASH, new DefaultIndexAction());
errorActionMap.put(MAPPING_ERROR, new UnknownErrorAction());
errorActionMap.put(StrUtil.SLASH, DefaultIndexAction.class);
errorActionMap.put(MAPPING_ERROR, UnknownErrorAction.class);
}
/**
@@ -207,7 +197,7 @@ public class ServerSetting {
/**
* @return 获取ActionMap
*/
public static Map<String, Action> getActionMap(String method) {
public static Map<String, Class<? extends Action>> getActionMap(String method) {
if (method.equals("GET")) {
return getActionMap;
} else if(method.equals("POST")) {
@@ -221,7 +211,17 @@ public class ServerSetting {
}
public static Action getErrorAction(String path) {
return errorActionMap.get(path);
Class<? extends Action> cls = errorActionMap.get(path);
if(cls != null) {
try {
return cls.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return null;
}
/**
@@ -233,33 +233,17 @@ public class ServerSetting {
if(StrUtil.isBlank(path)){
path = StrUtil.SLASH;
}
return getActionMap(method).get(path.trim());
}
/**
* 设置Action类,已有的Action类将被覆盖
* @param path 拦截路径(必须以"/"开头)
* @param action Action类
*/
public static void setAction(String path, Action action) {
if(StrUtil.isBlank(path)){
path = StrUtil.SLASH;
}
if(null == action) {
Logger.warn("Added blank action, pass it.");
return;
}
//所有路径必须以 "/" 开头,如果没有则补全之
if(false == path.startsWith(StrUtil.SLASH)) {
path = StrUtil.SLASH + path;
}
String method = "GET";
HttpMethod methodAnnotation = action.getClass().getAnnotation(HttpMethod.class);
if (methodAnnotation != null) {
method = methodAnnotation.value();
Class<? extends Action> cls = getActionMap(method).get(path.trim());
if(cls != null) {
try {
return cls.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
ServerSetting.getActionMap(method).put(path, action);
return null;
}
/**
@@ -269,33 +253,41 @@ public class ServerSetting {
* @param actionClass Action类
*/
public static void setAction(String path, Class<? extends Action> actionClass) {
setAction(path, (Action)Singleton.get(actionClass));
if(StrUtil.isBlank(path)){
path = StrUtil.SLASH;
}
if(null == actionClass) {
Logger.warn("Added blank action, pass it.");
return;
}
//所有路径必须以 "/" 开头,如果没有则补全之
if(false == path.startsWith(StrUtil.SLASH)) {
path = StrUtil.SLASH + path;
}
String method = "GET";
HttpMethod methodAnnotation = actionClass.getAnnotation(HttpMethod.class);
if (methodAnnotation != null) {
method = methodAnnotation.value();
}
ServerSetting.getActionMap(method).put(path, actionClass);
}
/**
* 增加Action类,已有的Action类将被覆盖<br>
* 自动读取Route的注解来获得Path路径
* @param action 带注解的Action对象
*/
public static void setAction(Action action) {
final Route route = action.getClass().getAnnotation(Route.class);
if(route != null){
final String path = route.value();
if(StrUtil.isNotBlank(path)){
setAction(path, action);
return;
}
}
throw new ServerSettingException("Can not find Route annotation,please add annotation to Action class!");
}
/**
* 增加Action类,已有的Action类将被覆盖<br>
* 所有Action都是以单例模式存在的!
* @param actionClass 带注解的Action类
*/
public static void setAction(Class<? extends Action> actionClass) {
setAction((Action)Singleton.get(actionClass));
final Route route = actionClass.getAnnotation(Route.class);
if(route != null){
final String path = route.value();
if(StrUtil.isNotBlank(path)){
setAction(path, actionClass);
return;
}
}
throw new ServerSettingException("Can not find Route annotation,please add annotation to Action class!");
}
//----------------------------------------------------------------------------------------------- Action start
@@ -0,0 +1,62 @@
/*
* This file is part of the Wildfire Chat package.
* (c) Heavyrain2012 <[email protected]>
*
* 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.InputChannelId;
import cn.wildfirechat.pojos.OutputCreateChannel;
import cn.wildfirechat.proto.WFCMessage;
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.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.util.internal.StringUtil;
import win.liyufan.im.IMTopic;
@Route(APIPath.Destroy_Channel)
@HttpMethod("POST")
public class DestroyChannelAction extends AdminAction {
@Override
public boolean isTransactionAction() {
return true;
}
@Override
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {
InputChannelId inputCreateChannel = getRequestBody(request.getNettyRequest(), InputChannelId.class);
if (inputCreateChannel != null
&& !StringUtil.isNullOrEmpty(inputCreateChannel.channelId)) {
WFCMessage.ChannelInfo channelInfo = messagesStore.getChannelInfo(inputCreateChannel.channelId);
if(channelInfo == null) {
setResponseContent(RestResult.resultOf(ErrorCode.ERROR_CODE_NOT_EXIST), response);
return true;
}
WFCMessage.IDBuf.Builder builder = WFCMessage.IDBuf.newBuilder().setId(inputCreateChannel.channelId);
sendApiMessage(response, channelInfo.getOwner(), IMTopic.DestroyChannelInfoTopic, builder.build().toByteArray(), result -> {
ByteBuf byteBuf = Unpooled.buffer();
byteBuf.writeBytes(result);
ErrorCode errorCode = ErrorCode.fromCode(byteBuf.readByte());
return new Result(errorCode);
});
return false;
} else {
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER), response);
}
}
return true;
}
}
@@ -0,0 +1,51 @@
/*
* This file is part of the Wildfire Chat package.
* (c) Heavyrain2012 <[email protected]>
*
* 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.InputChannelId;
import cn.wildfirechat.pojos.OutputGetChannelInfo;
import cn.wildfirechat.proto.WFCMessage;
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.util.internal.StringUtil;
@Route(APIPath.Get_Channel_Info)
@HttpMethod("POST")
public class GetChannelAction extends AdminAction {
@Override
public boolean isTransactionAction() {
return true;
}
@Override
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {
InputChannelId inputCreateChannel = getRequestBody(request.getNettyRequest(), InputChannelId.class);
if (inputCreateChannel != null
&& !StringUtil.isNullOrEmpty(inputCreateChannel.channelId)) {
WFCMessage.ChannelInfo channelInfo = messagesStore.getChannelInfo(inputCreateChannel.channelId);
if (channelInfo == null) {
setResponseContent(RestResult.resultOf(ErrorCode.ERROR_CODE_NOT_EXIST), response);
} else {
setResponseContent(RestResult.ok(OutputGetChannelInfo.fromPbInfo(channelInfo)), response);
}
} else {
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER), response);
}
}
return true;
}
}
@@ -27,6 +27,7 @@ import org.slf4j.LoggerFactory;
import win.liyufan.im.RateLimiter;
import win.liyufan.im.Utility;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Executor;
abstract public class ChannelAction extends Action {
@@ -83,7 +84,7 @@ abstract public class ChannelAction extends Action {
}
if (StringUtil.isNullOrEmpty(channelInfo.getSecret())) {
return ErrorCode.ERROR_CODE_NOT_RIGHT;
return ErrorCode.ERROR_CODE_CHANNEL_NO_SECRET;
}
String str = nonce + "|" + channelInfo.getSecret() + "|" + timestamp;
@@ -106,7 +107,7 @@ abstract public class ChannelAction extends Action {
if (request instanceof FullHttpRequest) {
FullHttpRequest fullHttpRequest = (FullHttpRequest) request;
byte[] bytes = Utils.readBytesAndRewind(fullHttpRequest.content());
String content = new String(bytes);
String content = new String(bytes, StandardCharsets.UTF_8);
Gson gson = new Gson();
T t = gson.fromJson(content, cls);
return t;
@@ -29,6 +29,8 @@ public class DestroyUserHandler extends IMHandler<WFCMessage.IDBuf> {
m_messagesStore.clearUserGroups(fromUser);
m_messagesStore.clearUserChannels(fromUser);
m_messagesStore.destoryUser(fromUser);
m_messagesStore.destroyRobot(fromUser);
});
return ErrorCode.ERROR_CODE_SUCCESS;
} else {
@@ -17,8 +17,8 @@ import win.liyufan.im.IMTopic;
import static win.liyufan.im.IMTopic.PutUserSettingTopic;
import static win.liyufan.im.UserSettingScope.kUserSettingMyChannels;
@Handler(value = IMTopic.DestoryChannelInfoTopic)
public class DistoryChannelHandler extends GroupHandler<WFCMessage.IDBuf> {
@Handler(value = IMTopic.DestroyChannelInfoTopic)
public class DistroyChannelHandler extends GroupHandler<WFCMessage.IDBuf> {
@Override
public ErrorCode action(ByteBuf ackPayload, String clientID, String fromUser, boolean isAdmin, WFCMessage.IDBuf request, Qos1PublishHandler.IMCallback callback) {
ErrorCode errorCode = m_messagesStore.distoryChannel(fromUser, request.getId());
@@ -784,13 +784,13 @@ public class DatabaseStore {
}
}
List<WFCMessage.Message> loadRemoteMessages(String user, WFCMessage.Conversation conversation, long beforeUid, int count, Collection<Integer> contentTypes) {
List<WFCMessage.Message> loadRemoteMessages(String user, WFCMessage.Conversation conversation, long beforeUid, int count, Collection<Integer> contentTypes, String channelOwner) {
List<WFCMessage.Message> messages = new ArrayList<>();
long[] before = new long[1];
before[0] = beforeUid;
boolean hasMore = loadRemoteMessagesFromTable(user, conversation, before, count, MessageShardingUtil.getMessageTable(beforeUid), messages, contentTypes);
boolean hasMore = loadRemoteMessagesFromTable(user, conversation, before, count, MessageShardingUtil.getMessageTable(beforeUid), messages, contentTypes, channelOwner);
while (messages.size() < count && hasMore) {
hasMore = loadRemoteMessagesFromTable(user, conversation, before, count - messages.size(), MessageShardingUtil.getMessageTable(beforeUid), messages, contentTypes);
hasMore = loadRemoteMessagesFromTable(user, conversation, before, count - messages.size(), MessageShardingUtil.getMessageTable(beforeUid), messages, contentTypes, channelOwner);
}
int month = 0;
@@ -800,7 +800,7 @@ public class DatabaseStore {
int size = messages.size();
hasMore = true;
while (size == messages.size() && hasMore) {
hasMore = loadRemoteMessagesFromTable(user, conversation, before, count - messages.size(), nexTable, messages, contentTypes);
hasMore = loadRemoteMessagesFromTable(user, conversation, before, count - messages.size(), nexTable, messages, contentTypes, channelOwner);
}
if (size < messages.size()) {
@@ -811,11 +811,13 @@ public class DatabaseStore {
return messages;
}
boolean loadRemoteMessagesFromTable(String user, WFCMessage.Conversation conversation, long[] before, int count, String table, List<WFCMessage.Message> messages, Collection<Integer> contentTypes) {
boolean loadRemoteMessagesFromTable(String user, WFCMessage.Conversation conversation, long[] before, int count, String table, List<WFCMessage.Message> messages, Collection<Integer> contentTypes, String channelOwner) {
long beforeUid = before[0];
String sql = "select `_mid`, `_from`, `_type`, `_target`, `_line`, `_data`, `_dt`, `_to` from " + table +" where";
if (conversation.getType() == ProtoConstants.ConversationType.ConversationType_Private) {
sql += " _type = ? and _line = ? and _mid < ? and ((_target = ? and _from = ?) or (_target = ? and _from = ?))";
} else if (conversation.getType() == ProtoConstants.ConversationType.ConversationType_Channel && !user.equals(channelOwner)) {
sql += " _type = ? and _line = ? and _mid < ? and _target = ? and ((_from = ? and (_to = '' or _to = ?)) or (_from = ?))";
} else {
sql += " _type = ? and _line = ? and _mid < ? and _target = ?";
}
@@ -851,7 +853,12 @@ public class DatabaseStore {
statement.setString(index++, user);
statement.setString(index++, user);
statement.setString(index++, conversation.getTarget());
} else if (conversation.getType() == ProtoConstants.ConversationType.ConversationType_Channel && !user.equals(channelOwner)) {
statement.setString(index++, channelOwner);
statement.setString(index++, user);
statement.setString(index++, user);
}
statement.setInt(index++, count);
resultSet = statement.executeQuery();
while (resultSet.next()) {
@@ -740,8 +740,19 @@ public class MemoryMessagesStore implements IMessagesStore {
loadMessage = IS_CHATROOM_MESSAGE_REMOTE_HISTORY_MESSAGE;
}
String channelOwner = null;
if(conversation.getType() == ProtoConstants.ConversationType.ConversationType_Channel) {
IMap<String, WFCMessage.ChannelInfo> mIMap = m_Server.getHazelcastInstance().getMap(CHANNELS);
WFCMessage.ChannelInfo info = mIMap.get(conversation.getTarget());
if(info == null) {
loadMessage = false;
} else {
channelOwner = info.getOwner();
}
}
if (loadMessage) {
messages = databaseStore.loadRemoteMessages(user, conversation, beforeUid, count, contentTypes);
messages = databaseStore.loadRemoteMessages(user, conversation, beforeUid, count, contentTypes, channelOwner);
} else {
messages = new ArrayList<>();
}
@@ -2134,6 +2145,12 @@ public class MemoryMessagesStore implements IMessagesStore {
mUserMap.put(robot.getUid(), robot);
}
@Override
public void destroyRobot(String robotId) {
HazelcastInstance hzInstance = m_Server.getHazelcastInstance();
IMap<String, WFCMessage.Robot> mUserMap = hzInstance.getMap(ROBOTS);
mUserMap.remove(robotId);
}
@Override
public ErrorCode getUserInfo(String fromUser, List<WFCMessage.UserRequest> requestList, WFCMessage.PullUserResult.Builder builder) {
@@ -133,6 +133,7 @@ public interface IMessagesStore {
WFCMessage.Robot getRobot(String robotId);
void addRobot(WFCMessage.Robot robot);
void destroyRobot(String robotId);
ErrorCode getUserInfo(String fromUser, List<WFCMessage.UserRequest> requestList, WFCMessage.PullUserResult.Builder builder);
@@ -70,7 +70,7 @@ public interface IMTopic {
String CreateChannelTopic = "CHC";
String ModifyChannelInfoTopic = "CHMI";
String TransferChannelInfoTopic = "CHT";
String DestoryChannelInfoTopic = "CHD";
String DestroyChannelInfoTopic = "CHD";
String ChannelSearchTopic = "CHS";
String ChannelListenTopic = "CHL";
String ChannelPullTopic = "CHP";
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>wildfirechat-parent</artifactId>
<groupId>cn.wildfirechat</groupId>
<version>0.76</version>
<version>0.77</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -69,6 +69,8 @@ public interface APIPath {
String Group_Set_Member_Extra = "/admin/group/member/set_extra";
String Create_Channel = "/admin/channel/create";
String Destroy_Channel = "/admin/channel/destroy";
String Get_Channel_Info = "/admin/channel/get";
String Get_System_Setting = "/admin/system/get_setting";
String Put_System_Setting = "/admin/system/put_setting";
@@ -49,6 +49,7 @@ public enum ErrorCode {
ERROR_CODE_WS_NOT_CONFIGURED_CORRECTLY(28, "ws not configured correctly"),
ERROR_CODE_CONFERENCE_ROOM_NOT_EXIST(60, "conference room not exist"),
ERROR_CODE_CHANNEL_NO_SECRET(237, "channel no secret"),
ERROR_CODE_USER_NOT_PREPARED(238, "user not prepared"),
ERROR_CODE_API_NOT_SIGNED(239, "api not signed or sign parameter not completion"),
ERROR_CODE_GROUP_EXCEED_MAX_MEMBER_COUNT(240, "group exceed max member count"),
@@ -0,0 +1,12 @@
package cn.wildfirechat.pojos;
public class InputChannelId {
public String channelId;
public InputChannelId() {
}
public InputChannelId(String channelId) {
this.channelId = channelId;
}
}
@@ -18,6 +18,9 @@ public class OutputGetChannelInfo {
private String portrait;
private String extra;
private String owner;
private int state;
//此字段已经废弃,为了兼容旧的SDK版本,以后会移除
@Deprecated()
private int status;
private long updateDt;
private String callback;
@@ -72,12 +75,12 @@ public class OutputGetChannelInfo {
this.owner = owner;
}
public int getStatus() {
return status;
public int getState() {
return state;
}
public void setStatus(int status) {
this.status = status;
public void setState(int state) {
this.state = state;
}
public long getUpdateDt() {
@@ -104,6 +107,14 @@ public class OutputGetChannelInfo {
this.automatic = automatic;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public static OutputGetChannelInfo fromPbInfo(WFCMessage.ChannelInfo channelInfo) {
OutputGetChannelInfo out = new OutputGetChannelInfo();
out.automatic = channelInfo.getAutomatic();
@@ -114,7 +125,8 @@ public class OutputGetChannelInfo {
out.name = channelInfo.getName();
out.owner = channelInfo.getOwner();
out.portrait = channelInfo.getPortrait();
out.status = channelInfo.getStatus();
out.state = channelInfo.getStatus();
out.status = out.state;
out.updateDt = channelInfo.getUpdateDt();
return out;
}
+1 -1
View File
@@ -6,7 +6,7 @@
<!--<relativePath>../pom.xml</relativePath>-->
<artifactId>wildfirechat-parent</artifactId>
<groupId>cn.wildfirechat</groupId>
<version>0.76</version>
<version>0.77</version>
</parent>
<artifactId>distribution</artifactId>
+1 -1
View File
@@ -12,7 +12,7 @@
<artifactId>wildfirechat-parent</artifactId>
<packaging>pom</packaging>
<version>0.76</version>
<version>0.77</version>
<name>Wildfire Chat</name>
<description>Wildfire Chat Server</description>
<inceptionYear>2019</inceptionYear>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<artifactId>wildfirechat-parent</artifactId>
<groupId>cn.wildfirechat</groupId>
<version>0.76</version>
<version>0.77</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -27,6 +27,18 @@ public class GeneralAdmin {
return AdminHttpUtils.httpJsonPost(path, inputCreateChannel, OutputCreateChannel.class);
}
public static IMResult<Void> destroyChannel(String channelId) throws Exception {
String path = APIPath.Destroy_Channel;
InputChannelId inputChannelId = new InputChannelId(channelId);
return AdminHttpUtils.httpJsonPost(path, inputChannelId, Void.class);
}
public static IMResult<OutputGetChannelInfo> getChannelInfo(String channelId) throws Exception {
String path = APIPath.Get_Channel_Info;
InputChannelId inputChannelId = new InputChannelId(channelId);
return AdminHttpUtils.httpJsonPost(path, inputChannelId, OutputGetChannelInfo.class);
}
//以下仅专业版支持
public static IMResult<Void> setConversationTop(String userId, int conversationType, String target, int line, boolean isTop) throws Exception {
String key = conversationType + "-" + line + "-" + target;
@@ -15,10 +15,18 @@ import java.util.Date;
import java.util.List;
import static cn.wildfirechat.pojos.MyInfoType.Modify_DisplayName;
import static cn.wildfirechat.proto.ProtoConstants.ChannelState.*;
import static cn.wildfirechat.proto.ProtoConstants.SystemSettingType.Group_Max_Member_Count;
public class Main {
private static boolean commercialServer = false;
//管理端口是8080
private static String AdminUrl = "http://localhost:18080";
private static String AdminSecret = "123456";
//机器人和频道使用IM服务的公开端口80,注意不是18080
private static String IMUrl = "http://localhost";
public static void main(String[] args) throws Exception {
//admin使用的是18080端口,超级管理接口,理论上不能对外开放端口,也不能让非内部服务知悉密钥。
@@ -32,7 +40,7 @@ public class Main {
static void testAdmin() throws Exception {
//初始化服务API
AdminConfig.initAdmin("http://localhost:18080", "123456");
AdminConfig.initAdmin(AdminUrl, AdminSecret);
testUser();
testUserRelation();
@@ -84,7 +92,13 @@ public class Main {
System.exit(-1);
}
IMResult<Void> destroyResult = UserAdmin.destroyRobot("robot1");
if(destroyResult != null && destroyResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("success");
} else {
System.out.println("destroy user failure");
System.exit(-1);
}
IMResult<InputOutputUserInfo> resultGetUserInfo1 = UserAdmin.getUserByName(userInfo.getName());
if (resultGetUserInfo1 != null && resultGetUserInfo1.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
@@ -711,17 +725,46 @@ public class Main {
System.exit(-1);
}
String channelName = "MyChannel";
String channelOwner = "user1";
InputCreateChannel inputCreateChannel = new InputCreateChannel();
inputCreateChannel.setName("MyChannel");
inputCreateChannel.setOwner("user1");
inputCreateChannel.setName(channelName);
inputCreateChannel.setOwner(channelOwner);
IMResult<OutputCreateChannel> resultCreateChannel = GeneralAdmin.createChannel(inputCreateChannel);
if (resultCreateChannel != null && resultCreateChannel.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("success");
inputCreateChannel.setTargetId(resultCreateChannel.result.getTargetId());
} else {
System.out.println("create channel failure");
System.exit(-1);
}
IMResult<OutputGetChannelInfo> resultGetChannel = GeneralAdmin.getChannelInfo(inputCreateChannel.getTargetId());
if(resultGetChannel != null && resultGetChannel.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS
&& resultGetChannel.getResult().getName().equals(channelName)
&& resultGetChannel.getResult().getOwner().equals(channelOwner)) {
System.out.println("success");
} else {
System.out.println("get channel failure");
System.exit(-1);
}
IMResult<Void> voidIMResult = GeneralAdmin.destroyChannel(inputCreateChannel.getTargetId());
if(voidIMResult != null && voidIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("success");
} else {
System.out.println("destroy channel failure");
System.exit(-1);
}
resultGetChannel = GeneralAdmin.getChannelInfo(inputCreateChannel.getTargetId());
if(resultGetChannel != null && resultGetChannel.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS && (resultGetChannel.getResult().getState() & ProtoConstants.ChannelState.Channel_State_Mask_Deleted) > 0) {
System.out.println("success");
} else {
System.out.println("get channel failure");
System.exit(-1);
}
IMResult<HealthCheckResult> health = GeneralAdmin.healthCheck();
if(health != null && health.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println(health.result);
@@ -887,10 +930,27 @@ public class Main {
}
static void testRobot() throws Exception {
//初始化机器人API,注意端口是80,不是18080
String robotId = "robot1";
String robotSecret = "123456";
//初始化服务API
AdminConfig.initAdmin(AdminUrl, AdminSecret);
//创建机器人
InputCreateRobot createRobot = new InputCreateRobot();
createRobot.setUserId(robotId);
createRobot.setName(robotId);
createRobot.setDisplayName("机器人");
createRobot.setOwner("userId1");
createRobot.setSecret(robotSecret);
createRobot.setCallback("http://127.0.0.1:8883/robot/recvmsg");
IMResult<OutputCreateRobot> resultCreateRobot = UserAdmin.createRobot(createRobot);
if (resultCreateRobot != null && resultCreateRobot.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("Create robot " + resultCreateRobot.getResult().getUserId() + " success");
} else {
System.out.println("Create robot failure");
System.exit(-1);
}
RobotService robotService = new RobotService("http://localhost", robotId, "123456");
RobotService robotService = new RobotService(IMUrl, robotId, robotSecret);
//***********************************************
//**** 机器人API
@@ -1156,7 +1216,7 @@ public class Main {
//***测试频道API功能,仅专业版支持***
static void testChannel() throws Exception {
//初始化服务API
AdminConfig.initAdmin("http://localhost:18080", "123456");
AdminConfig.initAdmin(AdminUrl, AdminSecret);
//先创建3个用户
InputOutputUserInfo userInfo = new InputOutputUserInfo();
@@ -1207,7 +1267,7 @@ public class Main {
inputCreateChannel.setOwner("userId1");
String secret = "channelsecret";
inputCreateChannel.setSecret(secret);
inputCreateChannel.setState(0xff);
inputCreateChannel.setState(Channel_State_Mask_FullInfo | Channel_State_Mask_Unsubscribed_User_Access | Channel_State_Mask_Active_Subscribe | Channel_State_Mask_Message_Unsubscribed);
IMResult<OutputCreateChannel> resultCreateChannel = GeneralAdmin.createChannel(inputCreateChannel);
if (resultCreateChannel != null && resultCreateChannel.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("create channel success");
@@ -1218,7 +1278,7 @@ public class Main {
//2. 初始化api,注意端口是80,不是18080
ChannelServiceApi channelServiceApi = new ChannelServiceApi("http://localhost", resultCreateChannel.getResult().getTargetId(), secret);
ChannelServiceApi channelServiceApi = new ChannelServiceApi(IMUrl, resultCreateChannel.getResult().getTargetId(), secret);
@@ -42,6 +42,16 @@ public class UserAdmin {
return AdminHttpUtils.httpJsonPost(path, robot, OutputCreateRobot.class);
}
/*
销毁机器人和销毁用户是同一个接口
*/
public static IMResult<Void> destroyRobot(String userId) throws Exception {
String path = APIPath.Destroy_User;
InputDestroyUser inputDestroyUser = new InputDestroyUser();
inputDestroyUser.setUserId(userId);
return AdminHttpUtils.httpJsonPost(path, inputDestroyUser, Void.class);
}
public static IMResult<OutputGetIMTokenData> getUserToken(String userId, String clientId, int platform) throws Exception {
String path = APIPath.User_Get_Token;
InputGetToken getToken = new InputGetToken(userId, clientId, platform);