PageRenderTime 71ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/linbox/im/server/router/handlers/dispatcher/DispatchToSingleHandler.java

https://gitlab.com/Mr.Tomato/linbox_server
Java | 161 lines | 83 code | 22 blank | 56 comment | 5 complexity | cc179b3df892bd7003e7befa8c714547 MD5 | raw file
  1. package com.linbox.im.server.router.handlers.dispatcher;
  2. import com.alibaba.fastjson.JSON;
  3. import com.linbox.im.exceptions.IMConsumerException;
  4. import com.linbox.im.exceptions.IMException;
  5. import com.linbox.im.message.MessageType;
  6. import com.linbox.im.message.NewMessage;
  7. import com.linbox.im.server.service.IInboxService;
  8. import com.linbox.im.server.service.IOutboxService;
  9. import com.linbox.im.message.Message;
  10. import com.linbox.im.server.router.handlers.Handler;
  11. import com.linbox.im.server.storage.dao.IUserDAO;
  12. import org.apache.kafka.clients.consumer.ConsumerRecord;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.beans.factory.annotation.Qualifier;
  17. import org.springframework.stereotype.Service;
  18. /**
  19. * Created by lrsec on 8/26/15.
  20. */
  21. @Service
  22. @Qualifier("dispatchToSingleHandler")
  23. public class DispatchToSingleHandler implements Handler<String, String> {
  24. private static final Logger logger = LoggerFactory.getLogger(DispatchToSingleHandler.class);
  25. @Autowired
  26. private IInboxService inboxService;
  27. @Autowired
  28. private IOutboxService outboxService;
  29. @Autowired
  30. private IUserDAO userDAO;
  31. @Override
  32. public void handle(ConsumerRecord<String, String> record) {
  33. String json = record.value();
  34. try {
  35. SendDispatchMessage dispatchMsg = JSON.parseObject(json, SendDispatchMessage.class);
  36. if (dispatchMsg == null) {
  37. throw new IMException("DispatchMessage could not be parsed correctly. Message: " + json);
  38. }
  39. MessageType type = dispatchMsg.getType();
  40. switch (type) {
  41. case Session:
  42. dealSingleMessage(dispatchMsg);
  43. break;
  44. case Group:
  45. dealGroupMessage(dispatchMsg);
  46. break;
  47. default:
  48. throw new IMException("Get an unknown message type " + type.name() + " for SingleSendDispatchCallback");
  49. }
  50. } catch (Exception e) {
  51. throw new IMConsumerException(e, json);
  52. }
  53. }
  54. private void dealSingleMessage(SendDispatchMessage dispatchMessage) {
  55. String userId = dispatchMessage.getUserId();
  56. String remoteId = dispatchMessage.getRemoteId();
  57. String sessionKey = dispatchMessage.getSessionKey();
  58. Message message = dispatchMessage.getMessage();
  59. inboxService.updateSessionMsg(userId, sessionKey, message);
  60. sendNewMsgToUser(userId, remoteId, MessageType.Session);
  61. sendPush(message);
  62. }
  63. private void dealGroupMessage(SendDispatchMessage dispatchMessage) {
  64. String userId = dispatchMessage.getUserId();
  65. String remoteId = dispatchMessage.getRemoteId();
  66. String sessionKey = dispatchMessage.getSessionKey();
  67. Message message = dispatchMessage.getMessage();
  68. inboxService.updateGroupMsg(userId, sessionKey, message);
  69. sendNewMsgToUser(userId, remoteId, MessageType.Group);
  70. sendConsultPush(userId, message);
  71. }
  72. private void sendNewMsgToUser(String userId, String remoteId, MessageType type) {
  73. NewMessage newMessage = new NewMessage();
  74. newMessage.userId = userId;
  75. newMessage.remoteId = remoteId;
  76. newMessage.type = type.getValue();
  77. if (type == MessageType.Group) {
  78. newMessage.groupId = remoteId;
  79. }
  80. outboxService.put(userId, newMessage.toWrapperJson());
  81. }
  82. private void sendPush(Message message) {
  83. // String fromUserId = message.fromUserId;
  84. // String fromUserName = userDAO.getUserName(fromUserId);
  85. // String toUserId = message.toUserId;
  86. // PushMessage pushMessage = new PushMessage();
  87. //
  88. // if (fromUserId.equalsIgnoreCase("10000")) {
  89. // pushMessage.Type = SystemMsgType.IM_SYSTEM_ASSISTANT;
  90. // } else {
  91. // pushMessage.Type = SystemMsgType.IM_SESSION;
  92. // }
  93. //
  94. // pushMessage.ActionType = MessageAction.Text;
  95. //
  96. // pushMessage.From = Long.parseLong(fromUserId);
  97. // pushMessage.To = Long.parseLong(toUserId);
  98. // pushMessage.Badge = ServiceFactory.getInboxService().getTotalUnreadCount(toUserId) + 1;
  99. //
  100. // if (message.mimeType.startsWith("text")) {
  101. // pushMessage.Description = fromUserName + ": " + message.content;
  102. // pushMessage.Message = fromUserName + ": " + message.content;
  103. // } else if (message.mimeType.startsWith("audio")) {
  104. // pushMessage.Description = fromUserName + ": " + "[发来一条语音消息]";
  105. // pushMessage.Message = fromUserName + ": " + "[发来一条语音消息]";
  106. // } else if (message.mimeType.startsWith("image")) {
  107. // pushMessage.Description = fromUserName + ": " + "[发来一张图片]";
  108. // pushMessage.Message = fromUserName + ": " + "[发来一张图片]";
  109. // }
  110. //
  111. // IPushService pushService = ServiceFactory.getPushService();
  112. // pushService.sendPush(pushMessage.From, pushMessage);
  113. }
  114. private void sendConsultPush(String userId, Message message) {
  115. // String fromUserId = message.fromUserId;
  116. // String fromUserName = userDAO.getUserName(fromUserId);
  117. // PushMessage pushMessage = new PushMessage();
  118. // pushMessage.ActionType = MessageAction.Text;
  119. // pushMessage.Type = SystemMsgType.IM_CONSULT;
  120. //
  121. // pushMessage.From = Long.parseLong(fromUserId);
  122. // pushMessage.To = Long.parseLong(userId);
  123. // pushMessage.Badge = ServiceFactory.getInboxService().getTotalUnreadCount(userId) + 1;
  124. //
  125. // if (message.mimeType.startsWith("text")) {
  126. // pushMessage.Description = fromUserName + ": " + message.content;
  127. // pushMessage.Message = fromUserName + ": " + message.content;
  128. // } else if (message.mimeType.startsWith("audio")) {
  129. // pushMessage.Description = fromUserName + ": " + "[发来一条语音消息]";
  130. // pushMessage.Message = fromUserName + ": " + "[发来一条语音消息]";
  131. // } else if (message.mimeType.startsWith("image")) {
  132. // pushMessage.Description = fromUserName + ": " + "[发来一张图片]";
  133. // pushMessage.Message = fromUserName + ": " + "[发来一张图片]";
  134. // }
  135. //
  136. // IPushService pushService = ServiceFactory.getPushService();
  137. // pushService.sendPush(pushMessage.From, pushMessage);
  138. }
  139. }