WindChat/windchat-message/src/main/java/com/windchat/im/message/group/handler/GroupDetectionHandler.java

182 lines
5.8 KiB
Java
Raw Normal View History

2019-07-29 23:42:16 +08:00
/**
* Copyright 2018-2028 Akaxin Group
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2019-11-27 23:33:33 +08:00
package com.windchat.im.message.group.handler;
2019-07-29 23:42:16 +08:00
2019-11-27 23:33:33 +08:00
import com.windchat.im.message.dao.ImUserGroupDao;
import com.windchat.im.message.dao.ImUserProfileDao;
2019-07-29 23:42:16 +08:00
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2019-11-27 23:36:54 +08:00
import com.windchat.common.command.Command;
import com.windchat.common.logs.LogUtils;
2019-07-29 23:42:16 +08:00
import com.akaxin.proto.core.CoreProto;
import com.akaxin.proto.core.UserProto;
import com.akaxin.proto.site.ImCtsMessageProto;
2019-11-27 23:36:54 +08:00
import com.windchat.im.message.dao.ImUserGroupDao;
import com.windchat.im.message.dao.ImUserProfileDao;
2019-07-31 23:12:30 +08:00
import com.windchat.im.storage.bean.GroupProfileBean;
import com.windchat.im.storage.bean.SimpleUserBean;
2019-07-29 23:42:16 +08:00
/**
* <pre>
* 检测群消息发送是否满足条件
* 1.群成员
* 2.用户是否被seal up
* 3.群是否存在或群状态
* 4.设置command中必要参数
* </pre>
*
* @author Sam{@link an.guoyue254@gmail.com}
* @since 2018-02-05 11:47:47
*/
public class GroupDetectionHandler extends AbstractGroupHandler<Command> {
private static final Logger logger = LoggerFactory.getLogger(GroupDetectionHandler.class);
public Boolean handle(Command command) {
try {
ImCtsMessageProto.ImCtsMessageRequest request = ImCtsMessageProto.ImCtsMessageRequest
.parseFrom(command.getParams());
int type = request.getType().getNumber();
command.setMsgType(type);
String siteUserId = command.getSiteUserId();
String siteGroupId = null;
String gmsgId = null;
switch (type) {
case CoreProto.MsgType.GROUP_TEXT_VALUE:
if (command.isProxy()) {
siteUserId = request.getGroupText().getSiteUserId();
}
gmsgId = request.getGroupText().getMsgId();
siteGroupId = request.getGroupText().getSiteGroupId();
case CoreProto.MsgType.GROUP_SECRET_TEXT_VALUE:
break;
case CoreProto.MsgType.GROUP_IMAGE_VALUE:
if (command.isProxy()) {
siteUserId = request.getGroupImage().getSiteUserId();
}
gmsgId = request.getGroupImage().getMsgId();
siteGroupId = request.getGroupImage().getSiteGroupId();
break;
case CoreProto.MsgType.GROUP_SECRET_IMAGE_VALUE:
break;
case CoreProto.MsgType.GROUP_VOICE_VALUE:
if (command.isProxy()) {
siteUserId = request.getGroupVoice().getSiteUserId();
}
gmsgId = request.getGroupVoice().getMsgId();
siteGroupId = request.getGroupVoice().getSiteGroupId();
break;
case CoreProto.MsgType.GROUP_SECRET_VOICE_VALUE:
break;
case CoreProto.MsgType.GROUP_NOTICE_VALUE:
if (command.isProxy()) {
siteUserId = request.getGroupMsgNotice().getSiteUserId();
}
siteGroupId = request.getGroupMsgNotice().getSiteGroupId();
command.setSiteGroupId(siteGroupId);
// 系统下发的消息直接return true
return true;
case CoreProto.MsgType.GROUP_WEB_VALUE:
if (command.isProxy()) {
siteUserId = request.getGroupWeb().getSiteUserId();
}
siteGroupId = request.getGroupWeb().getSiteGroupId();
command.setProxySiteUserId(siteUserId);
command.setSiteGroupId(siteGroupId);
// 系统下发的消息直接return true
return true;
case CoreProto.MsgType.GROUP_WEB_NOTICE_VALUE:
if (command.isProxy()) {
siteUserId = request.getGroupWebNotice().getSiteUserId();
}
siteGroupId = request.getGroupWebNotice().getSiteGroupId();
command.setProxySiteUserId(siteUserId);
command.setSiteGroupId(siteGroupId);
// 系统下发的消息直接return true
return true;
default:
break;
}
command.setProxySiteUserId(siteUserId);
// 群消息设置siteGroupId
command.setSiteGroupId(siteGroupId);
// check parameters
if (StringUtils.isAnyEmpty(siteUserId, siteGroupId)) {
return false;
}
// check is member of group
if (check(siteUserId, siteGroupId)) {
return true;
} else {
logger.warn("client={} siteUserId={} is not group={} member", command.getClientIp(), siteUserId,
siteGroupId);
int statusValue = -2;
msgStatusResponse(command, gmsgId, System.currentTimeMillis(), statusValue);
}
} catch (Exception e) {
LogUtils.requestErrorLog(logger, command, GroupDetectionHandler.class, e);
}
return false;
}
private boolean check(String siteUserId, String siteGroupId) {
return checkUser(siteUserId) && checkGroupStatus(siteGroupId) && isGroupMember(siteUserId, siteGroupId);
}
// 1.检测用户状态是否正常(被封禁用户)
private boolean checkUser(String siteUserId) {
// 检测发送者的状态
SimpleUserBean userBean = ImUserProfileDao.getInstance().getSimpleUserProfile(siteUserId);
if (userBean != null) {
if (userBean.getUserStatus() != UserProto.UserStatus.NORMAL_VALUE) {
return false;
}
} else {
return false;
}
return true;
}
// 2.检测群状态,是否为被删除群
private boolean checkGroupStatus(String groupId) {
try {
GroupProfileBean bean = ImUserGroupDao.getInstance().getGroupProfile(groupId);
if (bean != null && StringUtils.isNotBlank(bean.getGroupId())) {
if (bean.getGroupStatus() != 0) {
return true;
}
}
} catch (Exception e) {
logger.error("check group status error", e);
}
return false;
}
// 3.检测是否为群成员,可以发送群消息
private boolean isGroupMember(String siteUserId, String groupId) {
return ImUserGroupDao.getInstance().isGroupMember(siteUserId, groupId);
}
}