parent
a0e9b5e4c0
commit
e562fe9004
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -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<FollowMapper, Follow> implements FollowService {
|
||||||
|
private final FollowMapper followMapper;
|
||||||
|
private final UserMapper userMapper;
|
||||||
|
private final RedisTemplate<String, Object> 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<UserDTO> followCommons(Long id) {
|
||||||
|
Long userId = UserContext.getUserId();
|
||||||
|
String userKey = "follows:" + userId;
|
||||||
|
String followKey = "follows:" + id;
|
||||||
|
Set<Object> intersect = redisTemplate.opsForSet().intersect(userKey, followKey);
|
||||||
|
if(intersect == null || intersect.isEmpty()){
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Long> ids = intersect.stream().map(obj -> (Long)obj).collect(Collectors.toList());
|
||||||
|
List<UserDTO> userDTOS = userMapper.selectBatchIds(ids).stream()
|
||||||
|
.map(user -> BeanUtil.copyProperties(user, UserDTO.class))
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
return userDTOS;
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
Loading…
Reference in new issue