/**
* Copyright 2018-2028 WindChat 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.
*/
package com.windchat.im.web.admin.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import com.windchat.im.web.admin.exception.UserPermissionException;
import com.windchat.im.web.admin.service.IConfigService;
import com.windchat.im.web.admin.service.IGroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.windchat.common.utils.GsonUtils;
import com.akaxin.proto.core.PluginProto;
import com.windchat.im.business.dao.UserProfileDao;
import com.windchat.im.storage.bean.GroupMemberBean;
import com.windchat.im.storage.bean.GroupProfileBean;
import com.windchat.im.storage.bean.SimpleGroupBean;
import com.windchat.im.storage.bean.UserProfileBean;
import com.google.protobuf.InvalidProtocolBufferException;
/**
* 群组管理控制器
*
* @author Sam{@link an.guoyue254@gmail.com}
* @since 2018-04-17 18:03:07
*/
@Controller
@RequestMapping("group")
public class GroupManageController extends AbstractController {
private static final Logger logger = LoggerFactory.getLogger(UserManageController.class);
@Resource(name = "groupManageService")
private IGroupService groupService;
@Autowired
private IConfigService configService;
// admin.html 为群列表页
@RequestMapping("/index")
public ModelAndView toGroupIndex(@RequestBody byte[] bodyParam) {
ModelAndView modelAndView = new ModelAndView("group/index");
PluginProto.ProxyPluginPackage pluginPackage = null;
try {
pluginPackage = PluginProto.ProxyPluginPackage.parseFrom(bodyParam);
String siteUserId = getRequestSiteUserId(pluginPackage);
if (!isManager(siteUserId)) {
throw new UserPermissionException("Current user is not a manager");
}
List defaultGroupList = configService.getUserDefaultGroupList();
ArrayList groupProfileBeans = new ArrayList<>();
modelAndView.addObject("groupDefaultSize", "0");
if (defaultGroupList != null && defaultGroupList.size() > 0) {
for (String s : defaultGroupList) {
GroupProfileBean groupProfile = groupService.getGroupProfile(s);
groupProfileBeans.add(groupProfile);
}
modelAndView.addObject("groupList", groupProfileBeans);
modelAndView.addObject("groupDefaultSize", String.valueOf(defaultGroupList.size()));
}
return modelAndView;
} catch (InvalidProtocolBufferException e) {
logger.error("to group index error", e);
} catch (UserPermissionException e) {
logger.error("to group index error : " + e.getMessage());
}
return new ModelAndView("error");
}
@SuppressWarnings("unchecked")
public Map getRequestDataMapObj(PluginProto.ProxyPluginPackage pluginPackage) {
return GsonUtils.fromJson(pluginPackage.getData(), Map.class);
}
// 跳转到manage界面
@RequestMapping("manage")
public ModelAndView toManage(@RequestBody byte[] bodyParams) {
ModelAndView modelAndView = new ModelAndView("group/manage");
Map model = modelAndView.getModel();
PluginProto.ProxyPluginPackage pluginPackage = null;
try {
pluginPackage = PluginProto.ProxyPluginPackage.parseFrom(bodyParams);
String siteUserId = getRequestSiteUserId(pluginPackage);
if (!isManager(siteUserId)) {
throw new UserPermissionException("Current user is not a manager");
}
Map reqMap = getRequestDataMap(pluginPackage);
String siteGroupId = reqMap.get("group_id");
GroupProfileBean groupProfile = groupService.getGroupProfile(siteGroupId);
model.put("group_id", siteGroupId);
model.put("defaultState", groupProfile.getDefaultState());
return modelAndView;
} catch (InvalidProtocolBufferException e) {
logger.error("to group manage error", e);
} catch (UserPermissionException e) {
logger.error("to group manage error : " + e.getMessage());
}
return new ModelAndView("error");
}
// 跳转到添加群成员界面
@RequestMapping("/siteUser")
public ModelAndView toAddMember(@RequestBody byte[] bodyParams) {
ModelAndView modelAndView = new ModelAndView("group/addMember");
Map model = modelAndView.getModel();
try {
PluginProto.ProxyPluginPackage pluginPackage = PluginProto.ProxyPluginPackage.parseFrom(bodyParams);
String siteUserId = getRequestSiteUserId(pluginPackage);
if (!isManager(siteUserId)) {
throw new UserPermissionException("Current user is not a manager");
}
Map reqMap = getRequestDataMap(pluginPackage);
String siteGroupId = reqMap.get("group_id");
model.put("siteGroupId", siteGroupId);
return modelAndView;
} catch (InvalidProtocolBufferException e) {
logger.error("to group add error", e);
} catch (UserPermissionException e) {
logger.error("to group add error : " + e.getMessage());
}
return new ModelAndView("error");
}
/**
* 设置用户的默认群组
*
* @param bodyParams
* @return
*/
@RequestMapping("/setUserDefaultGroup")
@ResponseBody
public String setUserDefaultGroup(@RequestBody byte[] bodyParams) {
try {
PluginProto.ProxyPluginPackage pluginPackage = PluginProto.ProxyPluginPackage.parseFrom(bodyParams);
String requestSiteUserId = getRequestSiteUserId(pluginPackage);
if (!isManager(requestSiteUserId)) {
throw new UserPermissionException("Current user is not a manager");
}
Map reqMap = getRequestDataMap(pluginPackage);
String siteGroupId = reqMap.get("group_id");
boolean flag = configService.setUserDefaultGroups(siteGroupId);
if (flag) {
return SUCCESS;
}
} catch (InvalidProtocolBufferException e) {
logger.error("to group add error", e);
} catch (UserPermissionException e) {
logger.error("to group add error : " + e.getMessage());
return NO_PERMISSION;
}
return ERROR;
}
@RequestMapping("/deleteUserDefaultGroup")
@ResponseBody
public String deleteUserDefaultGroup(@RequestBody byte[] bodyParams) {
try {
PluginProto.ProxyPluginPackage pluginPackage = PluginProto.ProxyPluginPackage.parseFrom(bodyParams);
String siteUserId = getRequestSiteUserId(pluginPackage);
if (!isManager(siteUserId)) {
throw new UserPermissionException("Current user is not a manager");
}
Map reqMap = getRequestDataMap(pluginPackage);
String siteGroupId = reqMap.get("group_id");
boolean flag = configService.deleteUserDefaultGroup(siteGroupId);
if (flag) {
return SUCCESS;
}
} catch (InvalidProtocolBufferException e) {
logger.error("del group default error", e);
} catch (UserPermissionException e) {
logger.error("del group default error : " + e.getMessage());
return NO_PERMISSION;
}
return ERROR;
}
@RequestMapping("toMemberList")
public ModelAndView toMemberList(@RequestBody byte[] bodyParams) {
ModelAndView modelAndView = new ModelAndView("group/memberList");
Map model = modelAndView.getModel();
try {
PluginProto.ProxyPluginPackage pluginPackage = PluginProto.ProxyPluginPackage.parseFrom(bodyParams);
String siteUserId = getRequestSiteUserId(pluginPackage);
// 增加权限校验
if (!isManager(siteUserId)) {
throw new UserPermissionException("Current user is not a manager");
}
Map reqMap = getRequestDataMap(pluginPackage);
String siteGroupId = reqMap.get("group_id");
model.put("siteGroupId", siteGroupId);
return modelAndView;
} catch (InvalidProtocolBufferException e) {
logger.error("to group add error", e);
} catch (UserPermissionException e) {
logger.error("to group add error : " + e.getMessage());
}
return new ModelAndView("error");
}
// 跳转群组资料(群信息页面,修改群信息页面)
@RequestMapping("/profile")
public ModelAndView toGroupProfile(HttpServletRequest request, @RequestBody byte[] bodyParams) {
ModelAndView modelAndView = new ModelAndView("group/profile");
try {
PluginProto.ProxyPluginPackage pluginPackage = PluginProto.ProxyPluginPackage.parseFrom(bodyParams);
String siteUserId = getRequestSiteUserId(pluginPackage);
if (!isManager(siteUserId)) {
throw new UserPermissionException("Current user is not a manager");
}
Map reqMap = getRequestDataMap(pluginPackage);
String siteGroupId = reqMap.get("group_id");
GroupProfileBean bean = groupService.getGroupProfile(siteGroupId);
modelAndView.addObject("siteGroupId", bean.getGroupId());
modelAndView.addObject("groupName", bean.getGroupName());
modelAndView.addObject("groupPhoto", bean.getGroupPhoto());
modelAndView.addObject("ownerUserId", bean.getCreateUserId());
modelAndView.addObject("groupNotice", bean.getGroupNotice());
modelAndView.addObject("groupStatus", bean.getGroupStatus());
modelAndView.addObject("createTime", bean.getCreateTime());
return modelAndView;
} catch (InvalidProtocolBufferException e) {
logger.error("to group profile error", e);
} catch (UserPermissionException e) {
logger.error("to group profile error : " + e.getMessage());
}
return new ModelAndView("error");
}
@RequestMapping("/refresh")
@ResponseBody
public Map refreshPage(@RequestBody byte[] bodyParams) {
HashMap dataMap = new HashMap<>();
PluginProto.ProxyPluginPackage pluginPackage = null;
try {
pluginPackage = PluginProto.ProxyPluginPackage.parseFrom(bodyParams);
String siteUserId = getRequestSiteUserId(pluginPackage);
if (!isManager(siteUserId)) {
throw new UserPermissionException("Current user is not a manager");
}
List defaultGroupList = configService.getUserDefaultGroupList();
if (defaultGroupList == null || defaultGroupList.size() <= 0) {
dataMap.put("size", 0);
return dataMap;
}
ArrayList