添加频道sdk

This commit is contained in:
heavyrain.lee
2020-01-04 17:04:43 +08:00
parent 998f01a584
commit e4da3780f5
11 changed files with 327 additions and 15 deletions
@@ -59,7 +59,10 @@ public class CreateChannelAction extends AdminAction {
byteBuf.writeBytes(result);
ErrorCode errorCode = ErrorCode.fromCode(byteBuf.readByte());
if (errorCode == ErrorCode.ERROR_CODE_SUCCESS) {
sendResponse(response, null, new OutputCreateChannel(inputCreateChannel.getTargetId()));
byte[] data = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(data);
String secret = new String(data);
sendResponse(response, null, new OutputCreateChannel(inputCreateChannel.getTargetId(), secret));
} else {
sendResponse(response, errorCode, null);
}
@@ -41,7 +41,7 @@ public class CreateChannelHandler extends GroupHandler<WFCMessage.ChannelInfo> {
if (errorCode == ErrorCode.ERROR_CODE_SUCCESS) {
WFCMessage.ModifyUserSettingReq modifyUserSettingReq = WFCMessage.ModifyUserSettingReq.newBuilder().setScope(kUserSettingMyChannels).setKey(request.getTargetId()).setValue("1").build();
mServer.internalRpcMsg(fromUser, null, modifyUserSettingReq.toByteArray(), 0, fromUser, PutUserSettingTopic, false);
byte[] data = request.getTargetId().getBytes();
byte[] data = request.getSecret().getBytes();
ackPayload.ensureWritable(data.length).writeBytes(data);
return ErrorCode.ERROR_CODE_SUCCESS;
} else {
@@ -48,8 +48,12 @@ public interface APIPath {
String Get_System_Setting = "/admin/system/get_setting";
String Put_System_Setting = "/admin/system/put_setting";
String Channel_User_Info = "/channel/user_info";
String Channel_Update_Profile = "/channel/update_profile";
String Channel_Message_Send = "/channel/message/send";
String Channel_Subscribe = "/channel/subscribe";
String Channel_Unsubscribe = "/channel/unsubscribe";
String Channel_Subscriber_List = "/channel/subscriber_list";
String Robot_User_Info = "/robot/user_info";
String Robot_Message_Send = "/robot/message/send";
@@ -0,0 +1,36 @@
/*
* 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 cn.wildfirechat.pojos;
import cn.wildfirechat.proto.ProtoConstants;
import cn.wildfirechat.proto.WFCMessage;
import java.util.ArrayList;
import java.util.List;
public class InputChannelSubscribe {
private List<String> targets;
private int subscribe; //1,订阅;0,取消订阅
public List<String> getTargets() {
return targets;
}
public void setTargets(List<String> targets) {
this.targets = targets;
}
public int getSubscribe() {
return subscribe;
}
public void setSubscribe(int subscribe) {
this.subscribe = subscribe;
}
}
@@ -0,0 +1,36 @@
/*
* 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 cn.wildfirechat.pojos;
import cn.wildfirechat.proto.WFCMessage;
import io.netty.util.internal.StringUtil;
import java.util.Arrays;
public class InputModifyChannelInfo {
private int type;
private String value;
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
@@ -13,9 +13,11 @@ import cn.wildfirechat.proto.WFCMessage;
public class OutputCreateChannel {
private String targetId;
private String secret;
public OutputCreateChannel(String targetId) {
public OutputCreateChannel(String targetId, String secret) {
this.targetId = targetId;
this.secret = secret;
}
public String getTargetId() {
@@ -25,4 +27,12 @@ public class OutputCreateChannel {
public void setTargetId(String targetId) {
this.targetId = targetId;
}
public String getSecret() {
return secret;
}
public void setSecret(String secret) {
this.secret = secret;
}
}
@@ -0,0 +1,66 @@
/*
* 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 cn.wildfirechat.pojos;
import cn.wildfirechat.proto.ProtoConstants;
import cn.wildfirechat.proto.WFCMessage;
import io.netty.util.internal.StringUtil;
import java.util.ArrayList;
import java.util.List;
public class SendChannelMessageData {
private List<String> targets;
private int line;
private MessagePayload payload;
public List<String> getTargets() {
return targets;
}
public void setTargets(List<String> targets) {
this.targets = targets;
}
public int getLine() {
return line;
}
public void setLine(int line) {
this.line = line;
}
public MessagePayload getPayload() {
return payload;
}
public void setPayload(MessagePayload payload) {
this.payload = payload;
}
public static boolean isValide(SendChannelMessageData sendMessageData) {
if(sendMessageData == null ||
sendMessageData.getPayload() == null) {
return false;
}
return true;
}
public WFCMessage.Message toProtoMessage(String channelId, String channelOwner) {
if (targets == null) {
targets = new ArrayList<>();
}
return WFCMessage.Message.newBuilder().setFromUser(channelOwner)
.setConversation(WFCMessage.Conversation.newBuilder().setType(ProtoConstants.ConversationType.ConversationType_Channel).setTarget(channelId).setLine(line))
.addAllTo(targets)
.setContent(payload.toProtoMessageContent())
.build();
}
}
@@ -4,6 +4,7 @@ public class UserOnlineStatus {
public static final int ONLINE = 0;
public static final int OFFLINE = 1;
public static final int LOGOUT = -1;
public String userId;
public String clientId;
public int platform;
@@ -0,0 +1,65 @@
package cn.wildfirechat.sdk;
import cn.wildfirechat.common.APIPath;
import cn.wildfirechat.pojos.*;
import cn.wildfirechat.proto.ProtoConstants;
import cn.wildfirechat.sdk.model.IMResult;
import cn.wildfirechat.sdk.utilities.AdminHttpUtils;
import cn.wildfirechat.sdk.utilities.ChannelHttpUtils;
import cn.wildfirechat.sdk.utilities.RobotHttpUtils;
import java.util.List;
//仅专业版支持,社区版不支持
public class ChannelServiceApi {
private ChannelHttpUtils httpUtils;
public ChannelServiceApi(String imurl, String channelId, String secret) {
httpUtils = new ChannelHttpUtils(imurl, channelId, secret);
}
public IMResult<InputOutputUserInfo> 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);
}
public IMResult<Void> modifyChannelInfo(/*ProtoConstants.ModifyChannelInfoType*/int type, String value) throws Exception {
String path = APIPath.Channel_Update_Profile;
InputModifyChannelInfo modifyChannelInfo = new InputModifyChannelInfo();
modifyChannelInfo.setType(type);
modifyChannelInfo.setValue(value);
return httpUtils.httpJsonPost(path, modifyChannelInfo, Void.class);
}
public IMResult<SendMessageResult> sendMessage(int line, List<String> targets, MessagePayload payload) throws Exception {
String path = APIPath.Channel_Message_Send;
SendChannelMessageData messageData = new SendChannelMessageData();
messageData.setLine(0);
messageData.setTargets(targets);
messageData.setPayload(payload);
return httpUtils.httpJsonPost(path, messageData, SendMessageResult.class);
}
public IMResult<Void> subscribe(List<String> targets) throws Exception {
String path = APIPath.Channel_Subscribe;
InputChannelSubscribe input = new InputChannelSubscribe();
input.setTargets(targets);
input.setSubscribe(1);
return httpUtils.httpJsonPost(path, input, Void.class);
}
public IMResult<Void> unsubscribe(List<String> targets) throws Exception {
String path = APIPath.Channel_Unsubscribe;
InputChannelSubscribe input = new InputChannelSubscribe();
input.setTargets(targets);
input.setSubscribe(0);
return httpUtils.httpJsonPost(path, input, Void.class);
}
public IMResult<OutputStringList> getSubscriberList() throws Exception {
String path = APIPath.Channel_Subscriber_List;
return httpUtils.httpJsonPost(path, null, OutputStringList.class);
}
}
@@ -10,6 +10,7 @@ import cn.wildfirechat.sdk.utilities.RobotHttpUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import static cn.wildfirechat.proto.ProtoConstants.SystemSettingType.Group_Max_Member_Count;
@@ -455,6 +456,15 @@ public class Main {
}
voidIMResult = UserAdmin.destroyUser("user11");
if (voidIMResult != null && voidIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("destroy user success");
} else {
System.out.println("destroy user failure");
System.exit(-1);
}
//初始化机器人API
RobotHttpUtils.init("http://localhost", "robot1", "123456");
//***********************************************
@@ -483,8 +493,87 @@ public class Main {
System.exit(-1);
}
//测试频道API功能,仅专业版支持
//1. 先创建频道
inputCreateChannel = new InputCreateChannel();
inputCreateChannel.setName("testChannel");
inputCreateChannel.setOwner("user1");
resultCreateChannel = GeneralAdmin.createChannel(inputCreateChannel);
if (resultCreateChannel != null && resultCreateChannel.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("success");
} else {
System.out.println("create channel failure");
System.exit(-1);
}
voidIMResult = UserAdmin.destroyUser("user1");
//2. 初始化api
ChannelServiceApi channelServiceApi = new ChannelServiceApi("http://localhost", inputCreateChannel.getTargetId(), inputCreateChannel.getSecret());
//3. 测试channel api功能
resultVoid = channelServiceApi.subscribe(Arrays.asList("user2","user3"));
if (resultVoid != null && resultVoid.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("block user done");
} else {
System.out.println("block user failure");
System.exit(-1);
}
IMResult<OutputStringList> resultStringList = channelServiceApi.getSubscriberList();
if (resultStringList != null && resultStringList.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS && resultStringList.getResult().getList().contains("user2") && resultStringList.getResult().getList().contains("user3")) {
System.out.println("block user done");
} else {
System.out.println("block user failure");
System.exit(-1);
}
resultVoid = channelServiceApi.unsubscribe(Arrays.asList("user2"));
if (resultVoid != null && resultVoid.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("block user done");
} else {
System.out.println("block user failure");
System.exit(-1);
}
resultStringList = channelServiceApi.getSubscriberList();
if (resultStringList != null && resultStringList.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS && resultStringList.getResult().getList().contains("user2") && !resultStringList.getResult().getList().contains("user3")) {
System.out.println("block user done");
} else {
System.out.println("block user failure");
System.exit(-1);
}
resultGetUserInfo1 = channelServiceApi.getUserInfo("user2");
if (resultGetUserInfo1 != null && resultGetUserInfo1.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("get user info success");
} else {
System.out.println("get user info by name failure");
System.exit(-1);
}
payload = new MessagePayload();
payload.setType(1);
payload.setSearchableContent("hello world");
resultSendMessage = channelServiceApi.sendMessage(0, null,payload);
if (resultSendMessage != null && resultSendMessage.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("send message success");
} else {
System.out.println("send message failure");
System.exit(-1);
}
payload.setSearchableContent("hello to user2");
resultSendMessage = channelServiceApi.sendMessage(0, Arrays.asList("user2"),payload);
if (resultSendMessage != null && resultSendMessage.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("send message success");
} else {
System.out.println("send message failure");
System.exit(-1);
}
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("destroy user success");
} else {
@@ -22,23 +22,23 @@ import java.nio.charset.Charset;
public class ChannelHttpUtils {
private static final Logger LOG = LoggerFactory.getLogger(ChannelHttpUtils.class);
private static String url;
private static String channelId;
private static String channelSecret;
private String imurl;
private String channelId;
private String channelSecret;
public static void init(String url, String channelId, String secret) {
ChannelHttpUtils.url = url;
ChannelHttpUtils.channelId = channelId;
ChannelHttpUtils.channelSecret = secret;
public ChannelHttpUtils(String imurl, String channelId, String secret) {
this.imurl = imurl;
this.channelId = channelId;
this.channelSecret = secret;
}
public static <T> IMResult<T> httpJsonPost(String path, Object object, Class<T> clazz) throws Exception{
if (isNullOrEmpty(url) || isNullOrEmpty(path)) {
public <T> IMResult<T> httpJsonPost(String path, Object object, Class<T> clazz) throws Exception{
if (isNullOrEmpty(imurl) || isNullOrEmpty(path)) {
LOG.error("Not init IM SDK correctly. Do you forget init it?");
throw new Exception("SDK url or secret lack!");
}
String url = ChannelHttpUtils.url + path;
String url = imurl + path;
HttpPost post = null;
try {
HttpClient httpClient = HttpClientBuilder.create().build();
@@ -54,8 +54,10 @@ public class ChannelHttpUtils {
post.setHeader("Connection", "Keep-Alive");
post.setHeader("nonce", nonce + "");
post.setHeader("timestamp", "" + timestamp);
post.setHeader("cid", channelId);
post.setHeader("sign", sign);
String jsonStr = null;
if (object != null) {
jsonStr = new Gson().toJson(object);