添加关注、取消关注、获取关注列表、粉丝列表相关接口

This commit is contained in:
2024-08-19 11:52:53 +08:00
parent ac28ba233c
commit fa86a2af45
10 changed files with 217 additions and 34 deletions

View File

@@ -1,12 +1,11 @@
package com.ai.da.controller; package com.ai.da.controller;
import com.ai.da.common.config.exception.BusinessException;
import com.ai.da.common.response.PageBaseResponse; import com.ai.da.common.response.PageBaseResponse;
import com.ai.da.common.response.Response; import com.ai.da.common.response.Response;
import com.ai.da.mapper.primary.entity.Account;
import com.ai.da.model.dto.*; import com.ai.da.model.dto.*;
import com.ai.da.model.vo.CommentVO; import com.ai.da.model.vo.*;
import com.ai.da.model.vo.PortfolioVO;
import com.ai.da.model.vo.UserLikeChooseVO;
import com.ai.da.model.vo.UserLikeGroupVO;
import com.ai.da.service.PortfolioService; import com.ai.da.service.PortfolioService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
@@ -16,6 +15,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.validation.Valid; import javax.validation.Valid;
import java.util.List;
@Api(tags = "Portfolio模块") @Api(tags = "Portfolio模块")
@Slf4j @Slf4j
@@ -117,4 +117,31 @@ public class PortfolioController {
public Response<Boolean> commentDelete(@Valid @RequestBody CommentDTO commentDTO) { public Response<Boolean> commentDelete(@Valid @RequestBody CommentDTO commentDTO) {
return Response.success(portfolioService.commentDelete(commentDTO)); return Response.success(portfolioService.commentDelete(commentDTO));
} }
@ApiOperation(value = "关注")
@GetMapping("/follow")
public Response<String> follow(@RequestParam("followeeId") Long followeeId) {
portfolioService.follow(followeeId);
return Response.success(BusinessException.getMessageFromResource("subscription.success"));
}
@ApiOperation(value = "取消关注")
@GetMapping("/cancelFollow")
public Response<String> cancelFollow(@RequestParam("followeeId") Long followeeId) {
portfolioService.cancelFollow(followeeId);
return Response.success(BusinessException.getMessageFromResource("unsubscribe.success"));
}
@ApiOperation(value = "获取关注列表")
@PostMapping("/getFolloweeList")
public Response<List<Account>> getFolloweeList(@Valid @RequestBody PageQueryBaseVo pageQueryBaseVo) {
return Response.success(portfolioService.getFolloweeList(pageQueryBaseVo));
}
@ApiOperation(value = "获取粉丝列表")
@PostMapping("/getFollowerList")
public Response<List<Account>> getFollowerList(@Valid @RequestBody PageQueryBaseVo pageQueryBaseVo) {
return Response.success(portfolioService.getFollowerList(pageQueryBaseVo));
}
} }

View File

@@ -0,0 +1,9 @@
package com.ai.da.mapper.primary;
import com.ai.da.common.config.mybatis.plus.CommonMapper;
import com.ai.da.mapper.primary.entity.UserFollow;
public interface UserFollowMapper extends CommonMapper<UserFollow> {
}

View File

@@ -49,6 +49,12 @@ public class Notification extends BaseEntity{
public Notification() { public Notification() {
} }
public Notification(String type, Long senderId, Long receiverId) {
this.type = type;
this.senderId = senderId;
this.receiverId = receiverId;
}
public Notification(String type, Long senderId, Long receiverId, Long portfolioId) { public Notification(String type, Long senderId, Long receiverId, Long portfolioId) {
this.type = type; this.type = type;
this.senderId = senderId; this.senderId = senderId;

View File

@@ -0,0 +1,26 @@
package com.ai.da.mapper.primary.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
@EqualsAndHashCode(callSuper = true)
@TableName("t_user_follow")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserFollow extends BaseEntity{
/**
* 被关注者用户id
*/
private Long followeeId;
/**
* 关注者用户id
*/
private Long followerId;
}

View File

@@ -28,4 +28,6 @@ public class PortfolioVO extends Portfolio {
private Integer selected; private Integer selected;
private Integer jumpable; private Integer jumpable;
private Integer isFollow;
} }

View File

@@ -1,14 +1,18 @@
package com.ai.da.service; package com.ai.da.service;
import com.ai.da.common.response.PageBaseResponse; import com.ai.da.common.response.PageBaseResponse;
import com.ai.da.mapper.primary.entity.Account;
import com.ai.da.mapper.primary.entity.Portfolio; import com.ai.da.mapper.primary.entity.Portfolio;
import com.ai.da.model.dto.*; import com.ai.da.model.dto.*;
import com.ai.da.model.vo.CommentVO; import com.ai.da.model.vo.CommentVO;
import com.ai.da.model.vo.PageQueryBaseVo;
import com.ai.da.model.vo.PortfolioVO; import com.ai.da.model.vo.PortfolioVO;
import com.ai.da.model.vo.UserLikeChooseVO; import com.ai.da.model.vo.UserLikeChooseVO;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.util.List;
public interface PortfolioService extends IService<Portfolio> { public interface PortfolioService extends IService<Portfolio> {
Boolean publish(MultipartFile canvas, String data); Boolean publish(MultipartFile canvas, String data);
@@ -41,4 +45,12 @@ public interface PortfolioService extends IService<Portfolio> {
Boolean delete(Long id); Boolean delete(Long id);
Portfolio getByIdAll(Long originalPortfolioId); Portfolio getByIdAll(Long originalPortfolioId);
void follow(Long followeeId);
void cancelFollow(Long followeeId);
List<Account> getFolloweeList(PageQueryBaseVo pageQueryBaseVo);
List<Account> getFollowerList(PageQueryBaseVo pageQueryBaseVo);
} }

View File

@@ -67,21 +67,33 @@ public class MessageCenterServiceImpl extends ServiceImpl<NotificationMapper, No
// 获取历史消息 可指定消息类型 分页查询 // 获取历史消息 可指定消息类型 分页查询
@Override @Override
public PageBaseResponse<NotificationVO> getHistoryNotification(GetNotificationVO getNotificationVO) { public PageBaseResponse<NotificationVO> getHistoryNotification(GetNotificationVO getNotificationVO) {
Long accountId = UserContext.getUserHolder().getId();
QueryWrapper<Notification> queryWrapper = new QueryWrapper<>(); QueryWrapper<Notification> queryWrapper = new QueryWrapper<>();
if (!StringUtils.isNullOrEmpty(getNotificationVO.getType())) { if (!StringUtils.isNullOrEmpty(getNotificationVO.getType())) {
queryWrapper.eq("type", getNotificationVO.getType()); queryWrapper.eq("type", getNotificationVO.getType());
} }
queryWrapper.eq("receiver_id", UserContext.getUserHolder().getId()); if (!getNotificationVO.getType().equals("system")){
queryWrapper.eq("receiver_id", accountId);
}
Page<Notification> notificationPage = baseMapper.selectPage(new Page<>(getNotificationVO.getPage(), getNotificationVO.getSize()), queryWrapper); Page<Notification> notificationPage = baseMapper.selectPage(new Page<>(getNotificationVO.getPage(), getNotificationVO.getSize()), queryWrapper);
List<Long> unreadSysNotificationIds = baseMapper.getUnreadSysNotification(accountId);
IPage<NotificationVO> convert = notificationPage.convert(o -> { IPage<NotificationVO> convert = notificationPage.convert(o -> {
NotificationVO notificationVO = CopyUtil.copyObject(o, NotificationVO.class); NotificationVO notificationVO = CopyUtil.copyObject(o, NotificationVO.class);
Account account = accountService.getById(notificationVO.getSenderId()); Account account = accountService.getById(notificationVO.getSenderId());
notificationVO.setSenderUserName(account.getUserName()); notificationVO.setSenderUserName(account.getUserName());
notificationVO.setSenderUserAvatar(StringUtils.isNullOrEmpty(account.getAvatar()) ? null : minioUtil.getPreSignedUrl(account.getAvatar(), CommonConstant.MINIO_IMAGE_EXPIRE_TIME)); notificationVO.setSenderUserAvatar(StringUtils.isNullOrEmpty(account.getAvatar()) ? null : minioUtil.getPreSignedUrl(account.getAvatar(), CommonConstant.MINIO_IMAGE_EXPIRE_TIME));
notificationVO.setPortfolioName(portfolioService.getById(notificationVO.getPortfolioId()).getPortfolioName()); notificationVO.setPortfolioName(Objects.isNull(notificationVO.getPortfolioId()) ? null : portfolioService.getById(notificationVO.getPortfolioId()).getPortfolioName());
// 设置单个人 系统消息是否已读
if (notificationVO.getType().equals("system")){
if (unreadSysNotificationIds.contains(notificationVO.getId())){
notificationVO.setIsRead(0);
}else {
notificationVO.setIsRead(1);
}
}
return notificationVO; return notificationVO;
}); });
return PageBaseResponse.success(convert); return PageBaseResponse.success(convert);
} }
@@ -195,7 +207,7 @@ public class MessageCenterServiceImpl extends ServiceImpl<NotificationMapper, No
Long readCount = sysNotificationReadStatusMapper.selectCount(wrapper); Long readCount = sysNotificationReadStatusMapper.selectCount(wrapper);
// 计算差 // 计算差
return totalSysCount - readCount; return totalSysCount - readCount > 0 ? totalSysCount - readCount : 0;
} }
// 设置个人消息的已读状态 (允许一次已读多条个人消息) // 设置个人消息的已读状态 (允许一次已读多条个人消息)
@@ -212,8 +224,8 @@ public class MessageCenterServiceImpl extends ServiceImpl<NotificationMapper, No
baseMapper.update(null, updateWrapper); baseMapper.update(null, updateWrapper);
} }
pushMessage(type, UserContext.getUserHolder().getId());
return Boolean.TRUE; return Boolean.TRUE;
} }
// 设置系统消息的已读状态 (允许一次已读多条系统消息) // 设置系统消息的已读状态 (允许一次已读多条系统消息)

View File

@@ -33,6 +33,7 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.stream.Collectors;
@Service @Service
public class PortfolioServiceImpl extends ServiceImpl<PortfolioMapper, Portfolio> implements PortfolioService { public class PortfolioServiceImpl extends ServiceImpl<PortfolioMapper, Portfolio> implements PortfolioService {
@@ -75,7 +76,6 @@ public class PortfolioServiceImpl extends ServiceImpl<PortfolioMapper, Portfolio
@Resource @Resource
private DesignItemDetailPrintMapper designItemDetailPrintMapper; private DesignItemDetailPrintMapper designItemDetailPrintMapper;
@Resource @Resource
private MinioUtil minioUtil; private MinioUtil minioUtil;
@@ -109,6 +109,9 @@ public class PortfolioServiceImpl extends ServiceImpl<PortfolioMapper, Portfolio
@Resource @Resource
private WorkspaceRelStyleMapper workspaceRelStyleMapper; private WorkspaceRelStyleMapper workspaceRelStyleMapper;
@Resource
private UserFollowMapper userFollowMapper;
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public Boolean publish(MultipartFile file, String data) { public Boolean publish(MultipartFile file, String data) {
@@ -480,6 +483,7 @@ public class PortfolioServiceImpl extends ServiceImpl<PortfolioMapper, Portfolio
vo.setLikeNum(redisUtil.getLikeCount(vo.getId())); vo.setLikeNum(redisUtil.getLikeCount(vo.getId()));
if (userHolder == null) { if (userHolder == null) {
vo.setIsLike(0); vo.setIsLike(0);
vo.setIsFollow(0);
} else { } else {
boolean postLikedByUser = redisUtil.isPostLikedByUser(portfolioDTO.getId(), userHolder.getId()); boolean postLikedByUser = redisUtil.isPostLikedByUser(portfolioDTO.getId(), userHolder.getId());
if (postLikedByUser) { if (postLikedByUser) {
@@ -487,6 +491,15 @@ public class PortfolioServiceImpl extends ServiceImpl<PortfolioMapper, Portfolio
} else { } else {
vo.setIsLike(0); vo.setIsLike(0);
} }
// 设置当前用户是否关注了所查看作品的作者
QueryWrapper<UserFollow> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("followee_id", portfolio.getAccountId()).eq("follower_id", userHolder.getId());
UserFollow userFollow = userFollowMapper.selectOne(queryWrapper);
if (Objects.isNull(userFollow)){
vo.setIsFollow(0);
}else {
vo.setIsFollow(1);
}
} }
redisUtil.increaseViewCount(portfolioDTO.getId()); redisUtil.increaseViewCount(portfolioDTO.getId());
vo.setViewNums(redisUtil.getViewCount(portfolioDTO.getId())); vo.setViewNums(redisUtil.getViewCount(portfolioDTO.getId()));
@@ -809,6 +822,7 @@ public class PortfolioServiceImpl extends ServiceImpl<PortfolioMapper, Portfolio
@Resource @Resource
private CommentMapper commentMapper; private CommentMapper commentMapper;
@Override @Override
public Boolean comment(CommentDTO commentDTO) { public Boolean comment(CommentDTO commentDTO) {
AuthPrincipalVo userHolder = UserContext.getUserHolder(); AuthPrincipalVo userHolder = UserContext.getUserHolder();
@@ -944,4 +958,69 @@ public class PortfolioServiceImpl extends ServiceImpl<PortfolioMapper, Portfolio
// } // }
return resultList; return resultList;
} }
public void follow(Long followeeId) {
Long accountId = UserContext.getUserHolder().getId();
// 1、不能关注自己
if (Objects.equals(followeeId, accountId)) {
throw new BusinessException("you.cannot.follow.yourself", 1);
}
// 2、不能重复关注
QueryWrapper<UserFollow> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("followee_id", followeeId).eq("follower_id", accountId);
UserFollow userFollow = userFollowMapper.selectOne(queryWrapper);
if (!Objects.isNull(userFollow)) {
throw new BusinessException("you.have.already.followed.this.user", 1);
}
// 3、添加新follower到关注列表
UserFollow newFollower = new UserFollow(followeeId, accountId);
newFollower.setCreateTime(LocalDateTime.now());
userFollowMapper.insert(newFollower);
// 4、推送消息
messageCenterService.prePushMessage(new Notification("follow", accountId, followeeId));
}
// 取消关注
public void cancelFollow(Long followeeId){
Long accountId = UserContext.getUserHolder().getId();
// 1、确定是否关注了该用户
QueryWrapper<UserFollow> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("followee_id", followeeId).eq("follower_id", accountId);
UserFollow userFollow = userFollowMapper.selectOne(queryWrapper);
// 2、删除关注记录
if (!Objects.isNull(userFollow)) {
userFollowMapper.deleteById(userFollow.getId());
// 3、逻辑删除关注消息
messageCenterService.cancelPushMessage("follow", accountId, followeeId, null, null);
}else {
throw new BusinessException("you.have.not.followed.the.current.user", 1);
}
}
// 获取某个用户的关注列表
public List<Account> getFolloweeList(PageQueryBaseVo pageQueryBaseVo){
QueryWrapper<UserFollow> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("follower_id", UserContext.getUserHolder().getId()).select("followee_id");
Page<UserFollow> followPage = userFollowMapper.selectPage(new Page<>(pageQueryBaseVo.getPage(), pageQueryBaseVo.getSize()), queryWrapper);
if (!followPage.getRecords().isEmpty()){
List<Long> followeeIds = followPage.getRecords().stream().map(UserFollow::getFolloweeId).collect(Collectors.toList());
return accountMapper.selectBatchIds(followeeIds);
}
return new ArrayList<>();
}
// 获取某个用户的粉丝列表
public List<Account> getFollowerList(PageQueryBaseVo pageQueryBaseVo){
QueryWrapper<UserFollow> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("followee_id", UserContext.getUserHolder().getId()).select("follower_id");
Page<UserFollow> followPage = userFollowMapper.selectPage(new Page<>(pageQueryBaseVo.getPage(), pageQueryBaseVo.getSize()), queryWrapper);
if (!followPage.getRecords().isEmpty()){
List<Long> followerIds = followPage.getRecords().stream().map(UserFollow::getFollowerId).collect(Collectors.toList());
return accountMapper.selectBatchIds(followerIds);
}
return new ArrayList<>();
}
} }

View File

@@ -139,6 +139,11 @@ slogan.style.cannot.be.empty=Slogan style text cannot be empty.
slogan.image.cannot.be.empty=Slogan image cannot be empty. slogan.image.cannot.be.empty=Slogan image cannot be empty.
questionnaire.filled.out=You have filled out the current questionnaire. questionnaire.filled.out=You have filled out the current questionnaire.
user.has.no.account=The current user has no account user.has.no.account=The current user has no account
you.cannot.follow.yourself=You cannot follow yourself
you.have.already.followed.this.user=You have already followed this user
subscription.success=Subscription Success
unsubscribe.success=Unsubscribe Success
you.have.not.followed.the.current.user=You have not followed the current user
# 可能会报异常 # 可能会报异常
# Informative: # Informative:

View File

@@ -134,6 +134,11 @@ slogan.style.cannot.be.empty=标语风格文本不能为空。
slogan.image.cannot.be.empty=标语图片不能为空。 slogan.image.cannot.be.empty=标语图片不能为空。
questionnaire.filled.out=您已填写过当前问卷。 questionnaire.filled.out=您已填写过当前问卷。
user.has.no.account=当前用户没有账号。 user.has.no.account=当前用户没有账号。
you.cannot.follow.yourself=您不能关注您自己
you.have.already.followed.this.user=您已经关注当前用户
subscription.success=关注成功
unsubscribe.success=取消关注成功
you.have.not.followed.the.current.user=您还未关注当前用户
# 可能会报异常 # 可能会报异常
# Informative: # Informative: