@Aop("redis") public CResult add(Topic topic, int userId) { if (userId < 1) { return _fail("请先登录"); } if (Strings.isBlank(topic.getTitle()) || topic.getTitle().length() > 1024 || topic.getTitle().length() < 5) { return _fail("标题长度不合法"); } if (Strings.isBlank(topic.getContent())) { return _fail("内容不合法"); } if (topic.getTags() != null && topic.getTags().size() > 10) { return _fail("最多只能有10个tag"); } if (0 != dao.count(Topic.class, Cnd.where("title", "=", topic.getTitle().trim()))) { return _fail("相同标题已经发过了"); } topic.setTitle(Strings.escapeHtml(topic.getTitle().trim())); topic.setUserId(userId); topic.setTop(false); topic.setTags(new HashSet<String>()); if (topic.getType() == null) topic.setType(TopicType.ask); topic.setContent(Toolkit.filteContent(topic.getContent())); String oldContent = topic.getContent(); topic.setContentId(bigContentService.put(topic.getContent())); topic.setContent(null); dao.insert(topic); try { topic.setContent(oldContent); topicSearchService.add(topic); } catch (Exception e) { } // 如果是ask类型,把帖子加入到 "未回复"列表 Pipeline pipe = jedis().pipelined(); if (TopicType.ask.equals(topic.getType())) { pipe.zadd(RKEY_TOPIC_NOREPLY, System.currentTimeMillis(), topic.getId()); } pipe.zadd(RKEY_TOPIC_UPDATE + topic.getType(), System.currentTimeMillis(), topic.getId()); if (topic.getType() != TopicType.shortit) pipe.zadd(RKEY_TOPIC_UPDATE_ALL, System.currentTimeMillis(), topic.getId()); pipe.zincrby(RKEY_USER_SCORE, 100, "" + userId); pipe.sync(); String replyAuthorName = dao.fetch(User.class, userId).getName(); for (Integer watcherId : globalWatcherIds) { if (watcherId != userId) pushUser( watcherId, "新帖:" + topic.getTitle(), topic.getId(), replyAuthorName, topic.getTitle(), PushService.PUSH_TYPE_REPLY); } updateTopicTypeCount(); return _ok(topic.getId()); }
@Aop("redis") public boolean updateTags(String topicId, @Param("tags") Set<String> tags) { if (Strings.isBlank(topicId) || tags == null) { return false; } Topic topic = dao.fetch(Topic.class, topicId); if (topic == null) return false; Set<String> oldTags = topic.getTags(); if (oldTags == null) oldTags = new HashSet<>(); log.debugf("update from '%s' to '%s'", oldTags, tags); topic.setTags(tags); dao.update(topic, "tags"); Set<String> newTags = new HashSet<>(tags); newTags.removeAll(oldTags); Set<String> removeTags = new HashSet<>(oldTags); ; removeTags.remove(tags); fillTopic(topic, null); Date lastReplyTime = topic.getCreateTime(); if (topic.getLastComment() != null) lastReplyTime = topic.getLastComment().getCreateTime(); Pipeline pipe = jedis().pipelined(); for (String tag : removeTags) { pipe.zrem(RKEY_TOPIC_TAG + tag.toLowerCase().trim(), topic.getId()); pipe.zincrby(RKEY_TOPIC_TAG_COUNT, -1, tag.toLowerCase().trim()); } for (String tag : newTags) { pipe.zadd(RKEY_TOPIC_TAG + tag.toLowerCase().trim(), lastReplyTime.getTime(), topic.getId()); pipe.zincrby(RKEY_TOPIC_TAG_COUNT, 1, tag.toLowerCase().trim()); } pipe.sync(); return true; }
@Aop("redis") public CResult addReply(final String topicId, final TopicReply reply, final int userId) { if (userId < 1) return _fail("请先登录"); if (reply == null || reply.getContent() == null || reply.getContent().trim().isEmpty()) { return _fail("内容不能为空"); } final String cnt = reply.getContent().trim(); final Topic topic = dao.fetch(Topic.class, topicId); // TODO 改成只fetch出type属性 if (topic == null) { return _fail("主题不存在"); } if (topic.isLock()) { return _fail("该帖子已经锁定,不能回复"); } reply.setTopicId(topicId); reply.setUserId(userId); reply.setContent(Toolkit.filteContent(reply.getContent())); reply.setContentId(bigContentService.put(reply.getContent())); reply.setContent(null); dao.insert(reply); // 更新索引 topicSearchService.add(topic); // 更新topic的时间戳 Pipeline pipe = jedis().pipelined(); if (topic.isTop()) { pipe.zadd(RKEY_TOPIC_TOP, reply.getCreateTime().getTime(), topicId); } else { pipe.zadd(RKEY_TOPIC_UPDATE + topic.getType(), reply.getCreateTime().getTime(), topicId); pipe.zadd(RKEY_TOPIC_UPDATE_ALL, reply.getCreateTime().getTime(), topicId); } pipe.zrem(RKEY_TOPIC_NOREPLY, topicId); if (topic.getTags() != null) { for (String tag : topic.getTags()) { pipe.zadd( RKEY_TOPIC_TAG + tag.toLowerCase().trim(), reply.getCreateTime().getTime(), topicId); } } pipe.hset(RKEY_REPLY_LAST, topicId, reply.getId()); pipe.zincrby(RKEY_REPLY_COUNT, 1, topicId); pipe.zincrby(RKEY_USER_SCORE, 10, "" + userId); pipe.sync(); notifyUsers(topic, reply, cnt, userId); return _ok(reply.getId()); }