From e562fe90046209ae1abe50b00e205c67db8d1fcf Mon Sep 17 00:00:00 2001 From: forely <1605769034@qq.com> Date: Sun, 11 May 2025 19:03:27 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E9=83=A8=E5=88=86=E8=A1=A8?= =?UTF-8?q?=E4=B8=8E=E6=8E=A5=E5=8F=A3=EF=BC=8C=E6=96=B0=E5=A2=9E=E5=B8=96?= =?UTF-8?q?=E5=AD=90=EF=BC=8C=E8=AF=84=E8=AE=BA=E7=9A=84=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../interact/controller/ChatController.java | 26 +++++++ .../modules/interact/entity/Follow.java | 38 ++++++++++ .../service/impl/FollowServiceImpl.java | 73 +++++++++++++++++++ .../modules/post/dto/req/CommentSaveDTO.java | 20 +++++ .../modules/post/dto/resp/PostInfoDTO.java | 21 ++++++ 5 files changed, 178 insertions(+) create mode 100644 珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/interact/controller/ChatController.java create mode 100644 珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/interact/entity/Follow.java create mode 100644 珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/interact/service/impl/FollowServiceImpl.java create mode 100644 珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/post/dto/req/CommentSaveDTO.java create mode 100644 珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/post/dto/resp/PostInfoDTO.java diff --git a/珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/interact/controller/ChatController.java b/珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/interact/controller/ChatController.java new file mode 100644 index 0000000..16c4600 --- /dev/null +++ b/珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/interact/controller/ChatController.java @@ -0,0 +1,26 @@ +package com.luojia_channel.modules.interact.controller; + +import com.luojia_channel.modules.message.dto.MessageRequest; +import com.luojia_channel.modules.message.server.WebSocketServer; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +public class ChatController { + + private WebSocketServer webSocketServer; + + @PostMapping("/sendPrivateMessage") + public String sendPrivateMessage(@RequestParam Long senderId, @RequestBody MessageRequest request) { + try { + webSocketServer.sendPrivateMessage(senderId, request); + return "私信发送成功"; + } catch (Exception e) { + return "私信发送失败: " + e.getMessage(); + } + } +} \ No newline at end of file diff --git a/珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/interact/entity/Follow.java b/珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/interact/entity/Follow.java new file mode 100644 index 0000000..e7fd08d --- /dev/null +++ b/珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/interact/entity/Follow.java @@ -0,0 +1,38 @@ +package com.luojia_channel.modules.interact.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +import java.io.Serializable; +import java.time.LocalDateTime; + +@Data +@TableName("follow") +public class Follow implements Serializable { + + + /** + * 主键 + */ + @TableId(value = "id", type = IdType.AUTO) + private Long id; + + /** + * 用户id + */ + private Long userId; + + /** + * 关联的用户id + */ + private Long followUserId; + + /** + * 创建时间 + */ + private LocalDateTime createTime; + + +} diff --git a/珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/interact/service/impl/FollowServiceImpl.java b/珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/interact/service/impl/FollowServiceImpl.java new file mode 100644 index 0000000..5fee543 --- /dev/null +++ b/珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/interact/service/impl/FollowServiceImpl.java @@ -0,0 +1,73 @@ +package com.luojia_channel.modules.interact.service.impl; + +import cn.hutool.core.bean.BeanUtil; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.luojia_channel.common.domain.UserDTO; +import com.luojia_channel.common.utils.UserContext; +import com.luojia_channel.modules.interact.entity.Follow; +import com.luojia_channel.modules.interact.mapper.FollowMapper; +import com.luojia_channel.modules.interact.service.FollowService; +import com.luojia_channel.modules.user.mapper.UserMapper; +import lombok.RequiredArgsConstructor; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; + +import java.time.LocalDateTime; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + + +@Service +@RequiredArgsConstructor +public class FollowServiceImpl extends ServiceImpl implements FollowService { + private final FollowMapper followMapper; + private final UserMapper userMapper; + private final RedisTemplate redisTemplate; + @Override + public void follow(Long followUserId, Boolean isFollow) { + Long userId = UserContext.getUserId(); + String key = "follows:" + userId; + if(isFollow){ + Follow follow = new Follow(); + follow.setUserId(userId); + follow.setFollowUserId(followUserId); + follow.setCreateTime(LocalDateTime.now()); + boolean isSuccess = save(follow); + if(isSuccess){ + redisTemplate.opsForSet().add(key, followUserId); + } + }else{ + boolean isSuccess = followMapper.delete(userId, followUserId); + if(isSuccess){ + redisTemplate.opsForSet().remove(key, followUserId); + } + } + } + + @Override + public boolean isFollow(Long followUserId) { + Long userId = UserContext.getUserId(); + Integer count = followMapper.queryCount(userId, followUserId); + return count > 0; + } + + @Override + public List followCommons(Long id) { + Long userId = UserContext.getUserId(); + String userKey = "follows:" + userId; + String followKey = "follows:" + id; + Set intersect = redisTemplate.opsForSet().intersect(userKey, followKey); + if(intersect == null || intersect.isEmpty()){ + return Collections.emptyList(); + } + + List ids = intersect.stream().map(obj -> (Long)obj).collect(Collectors.toList()); + List userDTOS = userMapper.selectBatchIds(ids).stream() + .map(user -> BeanUtil.copyProperties(user, UserDTO.class)) + .toList(); + + return userDTOS; + } +} diff --git a/珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/post/dto/req/CommentSaveDTO.java b/珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/post/dto/req/CommentSaveDTO.java new file mode 100644 index 0000000..7affcbd --- /dev/null +++ b/珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/post/dto/req/CommentSaveDTO.java @@ -0,0 +1,20 @@ +package com.luojia_channel.modules.post.dto.req; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.time.LocalDateTime; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +public class CommentSaveDTO { + private Long id; + private String content; + private Long postId; + private Long parentCommentId; + private Long topId; +} diff --git a/珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/post/dto/resp/PostInfoDTO.java b/珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/post/dto/resp/PostInfoDTO.java new file mode 100644 index 0000000..7ed0806 --- /dev/null +++ b/珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/post/dto/resp/PostInfoDTO.java @@ -0,0 +1,21 @@ +package com.luojia_channel.modules.post.dto.resp; + +import lombok.Data; + +@Data +public class PostInfoDTO { + private Long id; + private String image; + private String title; + private String content; + private Integer likeCount; + private Integer commentCount; + private Integer favoriteCount; + + private Boolean isLike; + private Long userId; + + // 对于匿名的帖子,下述字段进行了处理,如匿名默认名称,头像 + private String userName; + private String userAvatar; +}