Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed May 17, 2016
1 parent 42b7b00 commit 3b1e76b
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 25 deletions.
54 changes: 53 additions & 1 deletion src/main/java/org/b3log/symphony/cache/TagCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* Tag cache.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.1.0, May 15, 2016
* @version 1.1.1.0, May 17, 2016
* @since 1.4.0
*/
@Named
Expand Down Expand Up @@ -74,6 +74,11 @@ public class TagCache {
*/
private static final List<JSONObject> ICON_TAGS = new ArrayList<JSONObject>();

/**
* All tags.
*/
private static final List<JSONObject> TAGS = new ArrayList<JSONObject>();

/**
* Gets icon tags with the specified fetch size.
*
Expand All @@ -90,6 +95,19 @@ public List<JSONObject> getIconTags(final int fetchSize) {
return ICON_TAGS.subList(0, end);
}

/**
* Gets all tags.
*
* @return all tags
*/
public List<JSONObject> getTags() {
if (TAGS.isEmpty()) {
return Collections.emptyList();
}

return TAGS;
}

/**
* Loads icon tags.
*/
Expand Down Expand Up @@ -138,4 +156,38 @@ public void loadIconTags() {
LOGGER.log(Level.ERROR, "Load icon tags failed", e);
}
}

/**
* Loads all tags.
*/
public void loadAllTags() {
TAGS.clear();

final Query query = new Query().setFilter(
new PropertyFilter(Tag.TAG_STATUS, FilterOperator.EQUAL, Tag.TAG_STATUS_C_VALID))
.setCurrentPageNum(1).setPageSize(Integer.MAX_VALUE).setPageCount(1)
.addSort(Tag.TAG_RANDOM_DOUBLE, SortDirection.ASCENDING);
try {
final JSONObject result = tagRepository.get(query);
final List<JSONObject> tags = CollectionUtils.<JSONObject>jsonArrayToList(result.optJSONArray(Keys.RESULTS));

for (final JSONObject tag : tags) {
String description = tag.optString(Tag.TAG_DESCRIPTION);
String descriptionText = tag.optString(Tag.TAG_TITLE);
if (StringUtils.isNotBlank(description)) {
description = shortLinkQueryService.linkTag(description);
description = Markdowns.toHTML(description);

tag.put(Tag.TAG_DESCRIPTION, description);
descriptionText = Jsoup.parse(description).text();
}

tag.put(Tag.TAG_T_DESCRIPTION_TEXT, descriptionText);
}

TAGS.addAll(tags);
} catch (final RepositoryException e) {
LOGGER.log(Level.ERROR, "Load all tags failed", e);
}
}
}
51 changes: 34 additions & 17 deletions src/main/java/org/b3log/symphony/event/ArticleQQSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.scienjus.smartqq.model.Group;
import com.scienjus.smartqq.model.GroupMessage;
import com.scienjus.smartqq.model.Message;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
Expand All @@ -32,6 +33,7 @@
import org.b3log.latke.event.EventException;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
import org.b3log.latke.util.Strings;
import org.b3log.symphony.cache.TagCache;
import org.b3log.symphony.model.Article;
import org.b3log.symphony.model.Tag;
Expand All @@ -43,7 +45,7 @@
* Sends an article to QQ qun.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.0, May 16, 2016
* @version 1.1.0.0, May 17, 2016
* @since 1.4.0
*/
@Named
Expand All @@ -56,14 +58,24 @@ public class ArticleQQSender extends AbstractEventListener<JSONObject> {
private static final Logger LOGGER = Logger.getLogger(ArticleQQSender.class.getName());

/**
* QQ group name.
* QQ group names.
*/
private static final String QQ_GROUP_NAME = Symphonys.get("qq.groupName");
private static final List<String> QQ_GROUP_NAMES = new ArrayList<String>();

static {
final String namesStr = Symphonys.get("qq.groupNames");
if (StringUtils.isNotBlank(namesStr)) {
final String[] names = namesStr.split(",");
for (final String name : names) {
QQ_GROUP_NAMES.add(name);
}
}
}

/**
* QQ group id.
* QQ group ids.
*/
private static Long QQ_GROUP_ID;
private static List<Long> QQ_GROUP_IDS = new ArrayList<Long>();

/**
* QQ client.
Expand All @@ -86,7 +98,7 @@ public class ArticleQQSender extends AbstractEventListener<JSONObject> {
* Initializes QQ client.
*/
public void initQQClient() {
if (null == QQ_GROUP_NAME) {
if (QQ_GROUP_NAMES.isEmpty()) {
return;
}

Expand All @@ -101,21 +113,23 @@ public void onMessage(final Message message) {
@Override
public void onGroupMessage(final GroupMessage message) {
final long groupId = message.getGroupId();
if (QQ_GROUP_ID != groupId) {

if (QQ_GROUP_IDS.isEmpty() || !QQ_GROUP_IDS.contains(groupId)) {
return;
}

final String content = message.getContent();
if (StringUtils.length(content) < 12
|| (!StringUtils.contains(content, "?") && !StringUtils.contains(content, "?"))) {
if (StringUtils.length(content) < 7
|| (!StringUtils.contains(content, "?") && !StringUtils.contains(content, "?")
&& !StringUtils.contains(content, "问"))) {
return;
}

String keyword = "";
final List<JSONObject> tags = tagCache.getIconTags(Integer.MAX_VALUE);
final List<JSONObject> tags = tagCache.getTags();
for (final JSONObject tag : tags) {
final String tagTitle = tag.optString(Tag.TAG_TITLE);
if (content.contains(tagTitle)) {
if (StringUtils.containsIgnoreCase(content, tagTitle)) {
keyword = tagTitle;

break;
Expand All @@ -124,14 +138,14 @@ public void onGroupMessage(final GroupMessage message) {

String msg = "";
if (StringUtils.isNotBlank(keyword)) {
msg = "社区里可能有该问题的答案: "
msg = "这里可能有该问题的答案: "
+ Latkes.getServePath() + "/search?key=" + keyword;
} else if (StringUtils.contains(content, "@社区 Bot #1")) {
} else if (StringUtils.contains(content, "Bot #1")) {
msg = turingQueryService.chat("", content);
}

if (StringUtils.isNotBlank(msg)) {
qqClient.sendMessageToGroup(QQ_GROUP_ID, msg);
qqClient.sendMessageToGroup(groupId, msg);
}
}

Expand All @@ -140,14 +154,15 @@ public void onDiscussMessage(final DiscussMessage message) {
}
});

// Init group id
final List<Group> groups = qqClient.getGroupList();
for (final Group group : groups) {
final Long id = group.getId();
final String name = group.getName();
LOGGER.info(id + ": " + name);

if (StringUtils.equals(name, QQ_GROUP_NAME)) {
QQ_GROUP_ID = id;
if (Strings.containsIgnoreCase(name, QQ_GROUP_NAMES.toArray(new String[0]))) {
QQ_GROUP_IDS.add(id);
}
}
}
Expand Down Expand Up @@ -202,7 +217,9 @@ public void sendToQQGroup(final JSONObject article) {
final String title = article.optString(Article.ARTICLE_TITLE);
final String permalink = article.optString(Article.ARTICLE_PERMALINK);

qqClient.sendMessageToGroup(QQ_GROUP_ID, title + " " + Latkes.getServePath() + permalink);
for (final Long groupId : QQ_GROUP_IDS) {
qqClient.sendMessageToGroup(groupId, title + " " + Latkes.getServePath() + permalink);
}
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/b3log/symphony/processor/TagProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ public class TagProcessor {
private TagCache tagCache;

/**
* Caches icon tags.
* Caches tags.
*
* @param request the specified HTTP servlet request
* @param response the specified HTTP servlet response
* @param context the specified HTTP request context
* @throws Exception exception
*/
@RequestProcessing(value = "/cron/tag/cache-icon-tags", method = HTTPRequestMethod.GET)
@RequestProcessing(value = "/cron/tag/cache-tags", method = HTTPRequestMethod.GET)
@Before(adviceClass = StopwatchStartAdvice.class)
@After(adviceClass = StopwatchEndAdvice.class)
public void cacheIconTags(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context)
Expand All @@ -110,6 +110,7 @@ public void cacheIconTags(final HttpServletRequest request, final HttpServletRes
}

tagCache.loadIconTags();
tagCache.loadAllTags();

context.renderJSON().renderTrueResult();
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/symphony.properties
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ turing.name=V
turing.avatar=http://7xjz0r.com1.z0.glb.clouddn.com/robot_avatar.jpg

#### QQ Robot ####
# qq.groupName=B3log \u5f00\u6e90
qq.groupName=QQ Bot \u6d4b\u8bd5
qq.groupNames=QQ Bot \u6d4b\u8bd5,LiuYun Studio

#### Reserved ####
reservedTags=\u7cfb\u7edf\u516c\u544a,\u56de\u6536\u7ad9,B3log\u516c\u544a,B3log\u5e7f\u64ad,B3log Announcement,B3log Broadcast
Expand Down
6 changes: 3 additions & 3 deletions src/main/webapp/WEB-INF/cron.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
-->
<!--
Description: Cron job configurations.
Version: 1.8.0.4, Apr 23, 2016
Version: 1.8.0.5, May 17, 2016
Author: Liang Ding
-->
<cronentries>
Expand All @@ -36,8 +36,8 @@
</cron>

<cron>
<url>/cron/tag/cache-icon-tags?key=dev_key</url>
<description>Cache icon tags</description>
<url>/cron/tag/cache-tags?key=dev_key</url>
<description>Cache tags</description>
<schedule>every 30 minutes</schedule>
</cron>

Expand Down

0 comments on commit 3b1e76b

Please sign in to comment.