21 changed files with 234 additions and 162 deletions
@ -1,4 +1,4 @@ |
|||||||
package net.sopod.soim.logic.api.message.mode; |
package net.sopod.soim.logic.common.model.message; |
||||||
|
|
||||||
import lombok.Data; |
import lombok.Data; |
||||||
import lombok.experimental.Accessors; |
import lombok.experimental.Accessors; |
||||||
@ -0,0 +1,30 @@ |
|||||||
|
package net.sopod.soim.logic.common.model.message; |
||||||
|
|
||||||
|
import lombok.Data; |
||||||
|
import lombok.experimental.Accessors; |
||||||
|
|
||||||
|
import java.io.Serializable; |
||||||
|
|
||||||
|
/** |
||||||
|
* UserMessage |
||||||
|
* |
||||||
|
* @author tmy |
||||||
|
* @date 2022-06-08 16:53 |
||||||
|
*/ |
||||||
|
@Data |
||||||
|
@Accessors(chain = true) |
||||||
|
public class UserMessage implements Serializable { |
||||||
|
|
||||||
|
private static final long serialVersionUID = -572323953445204089L; |
||||||
|
|
||||||
|
private Long senderUid; |
||||||
|
|
||||||
|
private Long receiverUid; |
||||||
|
|
||||||
|
private String receiverName; |
||||||
|
|
||||||
|
private String message; |
||||||
|
|
||||||
|
private Long time; |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,17 @@ |
|||||||
|
package net.sopod.soim.logic.api.message.service; |
||||||
|
|
||||||
|
import net.sopod.soim.logic.common.model.message.UserMessage; |
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture; |
||||||
|
|
||||||
|
/** |
||||||
|
* ImUserChatService |
||||||
|
* |
||||||
|
* @author tmy |
||||||
|
* @date 2022-06-08 17:19 |
||||||
|
*/ |
||||||
|
public interface ImUserChatService { |
||||||
|
|
||||||
|
CompletableFuture<String> userMessage(UserMessage msg); |
||||||
|
|
||||||
|
} |
||||||
@ -1,15 +0,0 @@ |
|||||||
package net.sopod.soim.logic.api.user.service; |
|
||||||
|
|
||||||
import net.sopod.soim.logic.common.model.TextChat; |
|
||||||
|
|
||||||
/** |
|
||||||
* ChatService |
|
||||||
* |
|
||||||
* @author tmy |
|
||||||
* @date 2022-04-28 14:19 |
|
||||||
*/ |
|
||||||
public interface ChatService { |
|
||||||
|
|
||||||
Boolean textChat(TextChat textChat); |
|
||||||
|
|
||||||
} |
|
||||||
@ -0,0 +1,93 @@ |
|||||||
|
package net.sopod.soim.logic.message.service; |
||||||
|
|
||||||
|
import net.sopod.soim.common.dubbo.exception.LogicException; |
||||||
|
import net.sopod.soim.common.dubbo.exception.SoimException; |
||||||
|
import net.sopod.soim.common.util.ImClock; |
||||||
|
import net.sopod.soim.das.common.config.LogicTables; |
||||||
|
import net.sopod.soim.das.message.api.entity.ImMessage; |
||||||
|
import net.sopod.soim.das.message.api.service.DasMQMessagePersistentService; |
||||||
|
import net.sopod.soim.das.user.api.model.entity.ImUser; |
||||||
|
import net.sopod.soim.das.user.api.service.FriendDas; |
||||||
|
import net.sopod.soim.das.user.api.service.UserDas; |
||||||
|
import net.sopod.soim.logic.api.message.service.ImUserChatService; |
||||||
|
import net.sopod.soim.logic.api.segmentid.core.SegmentIdGenerator; |
||||||
|
import net.sopod.soim.logic.common.model.message.UserMessage; |
||||||
|
import net.sopod.soim.logic.common.util.RpcContextUtil; |
||||||
|
import net.sopod.soim.router.api.service.MessageRouteService; |
||||||
|
import net.sopod.soim.router.api.service.UserRouteService; |
||||||
|
import org.apache.dubbo.config.annotation.DubboReference; |
||||||
|
import org.apache.dubbo.config.annotation.DubboService; |
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
|
||||||
|
import javax.annotation.Resource; |
||||||
|
import java.util.Objects; |
||||||
|
import java.util.concurrent.CompletableFuture; |
||||||
|
|
||||||
|
/** |
||||||
|
* ImUserChatServiceImpl |
||||||
|
* 单聊消息 |
||||||
|
* |
||||||
|
* @author tmy |
||||||
|
* @date 2022-06-08 14:59 |
||||||
|
*/ |
||||||
|
@DubboService |
||||||
|
public class ImUserChatServiceImpl implements ImUserChatService { |
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(ImUserChatServiceImpl.class); |
||||||
|
|
||||||
|
@DubboReference |
||||||
|
private UserRouteService userRouteService; |
||||||
|
|
||||||
|
@DubboReference |
||||||
|
private MessageRouteService messageRouteService; |
||||||
|
|
||||||
|
@DubboReference |
||||||
|
private UserDas userDas; |
||||||
|
|
||||||
|
@DubboReference |
||||||
|
private FriendDas friendDas; |
||||||
|
|
||||||
|
@Resource |
||||||
|
private SegmentIdGenerator segmentIdGenerator; |
||||||
|
|
||||||
|
@Resource |
||||||
|
private DasMQMessagePersistentService dasMQMessagePersistentService; |
||||||
|
|
||||||
|
@Override |
||||||
|
public CompletableFuture<String> userMessage(UserMessage msg) { |
||||||
|
if (msg.getReceiverUid() == null |
||||||
|
|| Objects.equals(msg.getReceiverUid(), 0L)) { |
||||||
|
ImUser receiverUser = userDas.getNormalUserByAccount(msg.getReceiverName()); |
||||||
|
if (receiverUser == null) { |
||||||
|
// TODO receiverUid 由客户端传递
|
||||||
|
throw new LogicException("聊天对象不存在"); |
||||||
|
} |
||||||
|
msg.setReceiverUid(receiverUser.getId()); |
||||||
|
} |
||||||
|
// 查询好友关系
|
||||||
|
Long relationId = friendDas.getRelationId(msg.getSenderUid(), msg.getReceiverUid()); |
||||||
|
// 不是好友
|
||||||
|
if (relationId == null) { |
||||||
|
throw new LogicException("请添加对方好友后发送消息"); |
||||||
|
} |
||||||
|
// 通过消息队列持久化存储到db
|
||||||
|
ImMessage imMessage = new ImMessage() |
||||||
|
.setRelationId(relationId) |
||||||
|
.setId(segmentIdGenerator.nextId(LogicTables.IM_MESSAGE)) |
||||||
|
.setContent(msg.getMessage()) |
||||||
|
.setSender(msg.getSenderUid()) |
||||||
|
.setReceiver(msg.getReceiverUid()) |
||||||
|
.setCreateTime(ImClock.date()); |
||||||
|
dasMQMessagePersistentService.saveImMessage(imMessage); |
||||||
|
// 设置调用 router 为消息接受者地址
|
||||||
|
RpcContextUtil.setContextUid(msg.getReceiverUid()); |
||||||
|
try { |
||||||
|
Boolean success = messageRouteService.routeUserMessage(msg); |
||||||
|
return CompletableFuture.completedFuture(Boolean.TRUE.equals(success) ? null : "消息未接受"); |
||||||
|
} catch (SoimException e) { |
||||||
|
return CompletableFuture.failedFuture(e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -1,60 +0,0 @@ |
|||||||
package net.sopod.soim.logic.user.service; |
|
||||||
|
|
||||||
import net.sopod.soim.common.dubbo.exception.LogicException; |
|
||||||
import net.sopod.soim.das.user.api.model.entity.ImUser; |
|
||||||
import net.sopod.soim.das.user.api.service.FriendDas; |
|
||||||
import net.sopod.soim.das.user.api.service.UserDas; |
|
||||||
import net.sopod.soim.logic.api.user.service.ChatService; |
|
||||||
import net.sopod.soim.logic.common.model.TextChat; |
|
||||||
import net.sopod.soim.logic.common.util.RpcContextUtil; |
|
||||||
import net.sopod.soim.router.api.service.UserRouteService; |
|
||||||
import org.apache.dubbo.config.annotation.DubboReference; |
|
||||||
import org.apache.dubbo.config.annotation.DubboService; |
|
||||||
import org.slf4j.Logger; |
|
||||||
import org.slf4j.LoggerFactory; |
|
||||||
|
|
||||||
import java.util.Objects; |
|
||||||
|
|
||||||
/** |
|
||||||
* ChatServiceImpl |
|
||||||
* |
|
||||||
* @author tmy |
|
||||||
* @date 2022-04-28 14:22 |
|
||||||
*/ |
|
||||||
@DubboService |
|
||||||
public class ChatServiceImpl implements ChatService { |
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(ChatServiceImpl.class); |
|
||||||
|
|
||||||
@DubboReference |
|
||||||
private UserRouteService userRouteService; |
|
||||||
|
|
||||||
@DubboReference |
|
||||||
private UserDas userDas; |
|
||||||
|
|
||||||
@DubboReference |
|
||||||
private FriendDas friendDas; |
|
||||||
|
|
||||||
@Override |
|
||||||
public Boolean textChat(TextChat textChat) { |
|
||||||
if (textChat.getReceiverUid() == null |
|
||||||
|| Objects.equals(textChat.getReceiverUid(), 0L)) { |
|
||||||
ImUser receiverUser = userDas.getNormalUserByAccount(textChat.getReceiverName()); |
|
||||||
if (receiverUser == null) { |
|
||||||
// TODO receiverUid 由客户端传递
|
|
||||||
throw new LogicException("聊天对象不存在"); |
|
||||||
} |
|
||||||
textChat.setReceiverUid(receiverUser.getId()); |
|
||||||
} |
|
||||||
// 查询好友关系
|
|
||||||
Long relationId = friendDas.getRelationId(textChat.getUid(), textChat.getReceiverUid()); |
|
||||||
// 不是好友
|
|
||||||
if (relationId == null) { |
|
||||||
throw new LogicException("请添加好友后发送消息"); |
|
||||||
} |
|
||||||
// 设置调用 router 为消息接受者地址
|
|
||||||
RpcContextUtil.setContextUid(textChat.getReceiverUid()); |
|
||||||
return userRouteService.routeTextChat(relationId, textChat); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
Loading…
Reference in new issue