PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/linbox/im/server/service/impl/PushService.java

https://gitlab.com/Mr.Tomato/linbox_server
Java | 252 lines | 193 code | 46 blank | 13 comment | 22 complexity | 847a9dca45fa29e0539e97deada8e49f MD5 | raw file
  1. package com.linbox.im.server.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.TypeReference;
  4. import com.alibaba.fastjson.annotation.JSONField;
  5. import com.alibaba.fastjson.parser.Feature;
  6. import com.linbox.im.utils.IdGenerator;
  7. import com.linbox.im.message.Message;
  8. import com.linbox.im.server.constant.RedisKey;
  9. import com.linbox.im.server.service.IInboxService;
  10. import com.linbox.im.server.service.IPushService;
  11. import com.linbox.im.server.storage.dao.IUserDAO;
  12. import org.apache.commons.lang.StringUtils;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Service;
  17. import redis.clients.jedis.Jedis;
  18. import redis.clients.jedis.JedisPool;
  19. import java.io.Serializable;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. /**
  24. * Created by lrsec on 7/16/15.
  25. */
  26. @Service
  27. public class PushService implements IPushService {
  28. private static Logger logger = LoggerFactory.getLogger(PushService.class);
  29. @Autowired
  30. private JedisPool jedisPool;
  31. @Autowired
  32. private IInboxService inboxService;
  33. @Autowired
  34. private IUserDAO userDAO;
  35. public void sendPush(Message message) {
  36. if (message == null) {
  37. logger.error("Get an empty message for sending push");
  38. return;
  39. }
  40. PushMessage pushMsg = new PushMessage();
  41. pushMsg.ID = IdGenerator.getUUID();
  42. pushMsg.Type = "InstanceMessage";
  43. pushMsg.Title = "医树";
  44. String realName = message.fromUserId.equals("10000") || message.fromUserId.equals("10001") ? "医树" : userDAO.getUserName(message.fromUserId);
  45. pushMsg.Title = realName;
  46. if (StringUtils.startsWith(message.mimeType, "text")) {
  47. pushMsg.Description = message.content;
  48. pushMsg.Message = message.content;
  49. } else if (StringUtils.startsWith(message.mimeType, "image")) {
  50. pushMsg.Description = "[图片]";
  51. pushMsg.Message = "[图片]";
  52. } else if (StringUtils.startsWith(message.mimeType, "audio")) {
  53. pushMsg.Description = "[音频]";
  54. pushMsg.Message = "[音频]";
  55. } else if (StringUtils.startsWith(message.mimeType, "video")){
  56. pushMsg.Description = "[视频]";
  57. pushMsg.Message = "[视频]";
  58. }
  59. pushMsg.ActionType = "Text";
  60. pushMsg.BatchId = 0;
  61. pushMsg.From = Long.parseLong(message.fromUserId);
  62. pushMsg.To = Long.parseLong( message.toUserId );
  63. pushMsg.Badge = getTotalUnread(message.toUserId);
  64. pushMsg.Channel = "unknown";
  65. pushMsg.Creator = Long.parseLong(message.fromUserId);
  66. pushMsg.Created = System.currentTimeMillis() / 1000l;
  67. pushMsg.Updated = pushMsg.Created;
  68. pushMsg.State = "Readed";
  69. UserMessage<PushMessage> msg = new UserMessage<PushMessage>();
  70. msg.data = pushMsg;
  71. msg.addMeta("isPush", true);
  72. //TODO push not work now
  73. // NSQHelper.produce(NSQTopic.TOPIC_PUSH, msg);
  74. logger.info("join push message nsq: {}", pushMsg.ID);
  75. }
  76. private int getTotalUnread(String userId) {
  77. int unread=0;
  78. if (StringUtils.isBlank(userId)) {
  79. logger.debug("Blank userId when getting total unread");
  80. return unread;
  81. }
  82. try (Jedis jedis = jedisPool.getResource()) {
  83. //获取缓存中新朋友的集合的长度 即新朋友的个数
  84. unread += Math.max(0, jedis.hlen(RedisKey.getFriendRequestsKey(Long.parseLong(userId))).intValue());
  85. //获取缓存中新评论的个数
  86. String newCount = jedis.hget(RedisKey.getUserUnReadCountsKey(Long.parseLong(userId)), NotifyMessageDataItems.NewCommentsCount);
  87. if (StringUtils.isNotBlank(newCount)) {
  88. unread += Math.max(0, Integer.parseInt(newCount));
  89. }
  90. //获取缓存中未读帮帮忙的个数
  91. newCount = jedis.hget(RedisKey.getUserUnReadCountsKey(Long.parseLong(userId)), NotifyMessageDataItems.NewForumHelpCommentCount);
  92. if (StringUtils.isNotBlank(newCount)) {
  93. unread += Math.max(0, Integer.parseInt(newCount));
  94. }
  95. newCount = jedis.hget(RedisKey.getUserUnReadCountsKey(Long.parseLong(userId)), NotifyMessageDataItems.NewForumHelpInviteCount);
  96. if (StringUtils.isNotBlank(newCount)) {
  97. unread += Math.max(0, Integer.parseInt(newCount));
  98. }
  99. newCount = jedis.hget(RedisKey.getUserUnReadCountsKey(Long.parseLong(userId)), NotifyMessageDataItems.NewForumHelpPointCount);
  100. if (StringUtils.isNotBlank(newCount)) {
  101. unread += Math.max(0, Integer.parseInt(newCount));
  102. }
  103. newCount = jedis.hget(RedisKey.getUserUnReadCountsKey(Long.parseLong(userId)), NotifyMessageDataItems.NewForumHelpCommentLikeCount);
  104. if (StringUtils.isNotBlank(newCount)) {
  105. unread += Math.max(0, Integer.parseInt(newCount));
  106. }
  107. //获取缓存中未读赞的个数
  108. newCount = jedis.hget(RedisKey.getUserUnReadCountsKey(Long.parseLong(userId)), NotifyMessageDataItems.NewLikesCount);
  109. if (StringUtils.isNotBlank(newCount)) {
  110. unread += Math.max(0, Integer.parseInt(newCount));
  111. }
  112. // 获取未读消息总数
  113. unread += inboxService.getTotalUnreadCount(userId);
  114. } catch (Exception e) {
  115. logger.error("Get exception for getting total unread for user " + userId, e);
  116. }
  117. unread = Math.max(1, unread);
  118. return unread;
  119. }
  120. private static class PushMessage implements Serializable {
  121. public long ID;
  122. public String Type = "Unknown";
  123. public String Title;
  124. public String Description;
  125. public String Message;
  126. public String ActionType = "Text";
  127. public long BatchId;
  128. public String URL;
  129. public long From;
  130. public Long To;
  131. public int Badge=1;
  132. public String Channel = "unknown";
  133. public long Creator;
  134. public long Created;
  135. public long Updated;
  136. public String State = "Readed";
  137. @Override
  138. public String toString() {
  139. return "ID:"+ID+",Type:"+Type+",Title:"+Title+",Description:"+ Description+
  140. ",Message:"+Message+",ActionType:"+ActionType+",BatchId:"+BatchId+
  141. ",URL:"+URL+",From:"+From+",To:"+To+",Badge:"+Badge+",Channel:"+Channel+",Creator:"+Creator;
  142. }
  143. }
  144. //TODO this should be deleted
  145. private static class UserMessage<T> extends NSQMessageBase {
  146. public UserMessage() {
  147. }
  148. @JSONField(
  149. serialize = false
  150. )
  151. public long getUserId() {
  152. Object uid = this.getMeta("user_id");
  153. return uid != null?Long.parseLong(uid.toString()):0L;
  154. }
  155. public void setUserId(long userId) {
  156. this.addMeta("user_id", String.valueOf(userId));
  157. }
  158. @JSONField(
  159. serialize = false
  160. )
  161. public UserMessage<T> getMessage(String json) {
  162. return (UserMessage) JSON.parseObject(json, new TypeReference() {
  163. }, new Feature[0]);
  164. }
  165. }
  166. //TODO This should be deleted
  167. private static class NSQMessageBase<T> implements Serializable {
  168. public HashMap<String, Object> meta;
  169. public List<T> list = new ArrayList();
  170. public T data;
  171. public NSQMessageBase() {
  172. }
  173. public Object getMeta(String key) {
  174. return this.meta != null && this.meta.containsKey(key)?this.meta.get(key):null;
  175. }
  176. public NSQMessageBase addMeta(String key, Object data) {
  177. if(this.meta == null) {
  178. this.meta = new HashMap();
  179. }
  180. this.meta.put(key, data);
  181. return this;
  182. }
  183. public NSQMessageBase<T> getMessage(String json) {
  184. return (NSQMessageBase)JSON.parseObject(json, new TypeReference() {
  185. }, new Feature[0]);
  186. }
  187. }
  188. private static class NotifyMessageDataItems{
  189. public static String NewLikesCount="new_likes_count";
  190. public static String NewCommentsCount="new_comments_count";
  191. public static String NewForumHelpCount="new_forum_help_count";
  192. public static String NewForumHelpInviteCount="new_forum_help_invite_count";
  193. public static String NewForumHelpCommentCount="new_forum_help_comment_count";
  194. public static String NewForumHelpPointCount="new_forum_help_point_count";
  195. public static String NewForumHelpCommentLikeCount="new_forum_help_comment_like_count";
  196. public static String ContactMobileMatchedUserCount="contact_mobile_match_count";
  197. public static String ContactNameMatchedUserCount="contact_name_match_count";
  198. public static String FriendRequestCount="friend_request_count";
  199. public static String IMNewMessageCount="im_new_message_count";
  200. //推荐好友和好友动态最后一条的用户头像
  201. @Deprecated
  202. public static String UserAvatar="user_avatar";
  203. public static String NewFriendFeedUserAvatar="new_feeds_user_avatar";
  204. public static String NewFriendRecommendUserAvatar="new_recommend_user_avatar";
  205. }
  206. }