Commit a39545d3 authored by Liang Ding's avatar Liang Ding

🎨 #12629

parent 1cb5e0ce
......@@ -42,7 +42,7 @@ import org.json.JSONObject;
/**
* This listener is responsible for sending article to B3log Rhythm. Sees <a href="https://hacpai.com/b3log">B3log 构思</a> for more details.
* <p>
* The B3log Rhythm article update interface: http://rhythm.b3log.org/article (POST).
* API spec: https://hacpai.com/article/1457158841475
* </p>
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
......@@ -58,21 +58,20 @@ public class B3ArticleSender extends AbstractEventListener<JSONObject> {
*/
private static final Logger LOGGER = Logger.getLogger(B3ArticleSender.class);
/**
* URL of adding article to Rhythm.
*/
private static final String ADD_ARTICLE_URL = Solos.B3LOG_RHYTHM_SERVE_PATH + "/article";
@Override
public void action(final Event<JSONObject> event) {
final JSONObject data = event.getData();
LOGGER.log(Level.DEBUG, "Processing an event [type={0}, data={1}] in listener [className={2}]",
event.getType(), data, B3ArticleSender.class.getName());
pushArticleToRhy(data);
}
static void pushArticleToRhy(JSONObject data) {
try {
final JSONObject originalArticle = data.getJSONObject(Article.ARTICLE);
if (!originalArticle.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
LOGGER.log(Level.DEBUG, "Ignores post article[title={0}] to Rhythm", originalArticle.getString(Article.ARTICLE_TITLE));
LOGGER.log(Level.DEBUG, "Ignored push an article [title={0}] to Rhy", originalArticle.getString(Article.ARTICLE_TITLE));
return;
}
......@@ -92,47 +91,40 @@ public class B3ArticleSender extends AbstractEventListener<JSONObject> {
return;
}
if (!originalArticle.optBoolean(Common.POST_TO_COMMUNITY)) {
return;
}
if (Latkes.getServePath().contains("localhost") || Strings.isIPv4(Latkes.getServePath())) {
LOGGER.log(Level.TRACE, "Solo runs on local server, so should not send this article[id={0}, title={1}] to Rhythm",
originalArticle.getString(Keys.OBJECT_ID), originalArticle.getString(Article.ARTICLE_TITLE));
return;
}
final JSONObject article = new JSONObject().
put(Keys.OBJECT_ID, originalArticle.getString(Keys.OBJECT_ID)).
put("title", originalArticle.getString(Article.ARTICLE_TITLE)).
put("permalink", originalArticle.getString(Article.ARTICLE_PERMALINK)).
put("tags", originalArticle.getString(Article.ARTICLE_TAGS_REF)).
put("content", originalArticle.getString(Article.ARTICLE_CONTENT));
final JSONObject author = articleQueryService.getAuthor(originalArticle);
final String authorEmail = author.optString(User.USER_EMAIL);
final JSONObject requestJSONObject = new JSONObject();
final JSONObject article = new JSONObject();
article.put(Keys.OBJECT_ID, originalArticle.getString(Keys.OBJECT_ID));
article.put(Article.ARTICLE_TITLE, originalArticle.getString(Article.ARTICLE_TITLE));
article.put(Article.ARTICLE_PERMALINK, originalArticle.getString(Article.ARTICLE_PERMALINK));
article.put(Article.ARTICLE_TAGS_REF, originalArticle.getString(Article.ARTICLE_TAGS_REF));
article.put(Article.ARTICLE_T_AUTHOR_EMAIL, authorEmail);
article.put(Article.ARTICLE_CONTENT, originalArticle.getString(Article.ARTICLE_CONTENT));
article.put(Article.ARTICLE_T_CREATE_DATE, originalArticle.getLong(Article.ARTICLE_CREATED));
article.put(Article.ARTICLE_T_CREATE_DATE, originalArticle.getLong(Article.ARTICLE_UPDATED));
article.put(Common.POST_TO_COMMUNITY, originalArticle.getBoolean(Common.POST_TO_COMMUNITY));
// Removes this property avoid to persist
originalArticle.remove(Common.POST_TO_COMMUNITY);
requestJSONObject.put(Article.ARTICLE, article);
requestJSONObject.put(Common.BLOG_VERSION, SoloServletListener.VERSION);
requestJSONObject.put(Common.BLOG, "Solo");
requestJSONObject.put(Option.ID_C_BLOG_TITLE, preference.getString(Option.ID_C_BLOG_TITLE));
requestJSONObject.put("blogHost", Latkes.getServePath());
requestJSONObject.put(UserExt.USER_B3_KEY, author.optString(UserExt.USER_B3_KEY));
requestJSONObject.put("clientAdminEmail", author.optString(User.USER_EMAIL));
requestJSONObject.put("clientRuntimeEnv", "LOCAL");
HttpRequest.post(ADD_ARTICLE_URL).bodyText(requestJSONObject.toString()).
final JSONObject client = new JSONObject().
put("title", preference.getString(Option.ID_C_BLOG_TITLE)).
put("host", Latkes.getServePath()).
put("name", "Solo").
put("ver", SoloServletListener.VERSION).
put("userName", author.optString(User.USER_NAME)).
put("userB3Key", author.optString(UserExt.USER_B3_KEY));
final JSONObject requestJSONObject = new JSONObject().
put("article", article).
put("client", client);
HttpRequest.post("https://rhythm.b3log.org/api/article").bodyText(requestJSONObject.toString()).
contentTypeJson().header("User-Agent", Solos.USER_AGENT).sendAsync();
LOGGER.log(Level.DEBUG, "Pushed an article to Rhy");
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Sends an article to Rhythm error: {0}", e.getMessage());
LOGGER.log(Level.ERROR, "Pushes an article to Rhy failed: " + e.getMessage());
}
LOGGER.log(Level.DEBUG, "Sent an article to Rhythm");
}
/**
......
......@@ -17,32 +17,17 @@
*/
package org.b3log.solo.event;
import jodd.http.HttpRequest;
import org.apache.commons.lang.StringUtils;
import org.b3log.latke.Keys;
import org.b3log.latke.Latkes;
import org.b3log.latke.event.AbstractEventListener;
import org.b3log.latke.event.Event;
import org.b3log.latke.ioc.BeanManager;
import org.b3log.latke.ioc.Singleton;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
import org.b3log.latke.model.User;
import org.b3log.latke.util.Strings;
import org.b3log.solo.SoloServletListener;
import org.b3log.solo.model.Article;
import org.b3log.solo.model.Common;
import org.b3log.solo.model.Option;
import org.b3log.solo.model.UserExt;
import org.b3log.solo.service.ArticleQueryService;
import org.b3log.solo.service.PreferenceQueryService;
import org.b3log.solo.util.Solos;
import org.json.JSONObject;
/**
* This listener is responsible for updating article to B3log Rhythm. Sees <a href="https://hacpai.com/b3log">B3log 构思</a> for more details.
* <p>
* The B3log Rhythm article update interface: http://rhythm.b3log.org/article (PUT).
* API spec: https://hacpai.com/article/1457158841475
* </p>
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
......@@ -57,80 +42,12 @@ public class B3ArticleUpdater extends AbstractEventListener<JSONObject> {
*/
private static final Logger LOGGER = Logger.getLogger(B3ArticleUpdater.class);
/**
* URL of updating article to Rhythm.
*/
private static String UPDATE_ARTICLE_URL = Solos.B3LOG_RHYTHM_SERVE_PATH + "/article";
public void action(final Event<JSONObject> event) {
final JSONObject data = event.getData();
LOGGER.log(Level.DEBUG, "Processing an event [type={0}, data={1}] in listener [className={2}]",
event.getType(), data, B3ArticleUpdater.class.getName());
try {
final JSONObject originalArticle = data.getJSONObject(Article.ARTICLE);
if (!originalArticle.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
LOGGER.log(Level.DEBUG, "Ignores post article[title={0}] to Rhythm", originalArticle.getString(Article.ARTICLE_TITLE));
return;
}
final BeanManager beanManager = BeanManager.getInstance();
final PreferenceQueryService preferenceQueryService = beanManager.getReference(PreferenceQueryService.class);
final ArticleQueryService articleQueryService = beanManager.getReference(ArticleQueryService.class);
final JSONObject preference = preferenceQueryService.getPreference();
if (null == preference) {
LOGGER.log(Level.ERROR, "Not found preference");
return;
}
if (StringUtils.isNotBlank(originalArticle.optString(Article.ARTICLE_VIEW_PWD))) {
return;
}
if (Latkes.getServePath().contains("localhost") || Strings.isIPv4(Latkes.getServePath())) {
LOGGER.log(Level.TRACE, "Solo runs on local server, so should not send this article[id={0}, title={1}] to Rhythm",
originalArticle.getString(Keys.OBJECT_ID), originalArticle.getString(Article.ARTICLE_TITLE));
return;
}
final JSONObject author = articleQueryService.getAuthor(originalArticle);
final String authorEmail = author.optString(User.USER_EMAIL);
final JSONObject requestJSONObject = new JSONObject();
final JSONObject article = new JSONObject();
article.put(Keys.OBJECT_ID, originalArticle.getString(Keys.OBJECT_ID));
article.put(Article.ARTICLE_TITLE, originalArticle.getString(Article.ARTICLE_TITLE));
article.put(Article.ARTICLE_PERMALINK, originalArticle.getString(Article.ARTICLE_PERMALINK));
article.put(Article.ARTICLE_TAGS_REF, originalArticle.getString(Article.ARTICLE_TAGS_REF));
article.put(Article.ARTICLE_T_AUTHOR_EMAIL, authorEmail);
article.put(Article.ARTICLE_CONTENT, originalArticle.getString(Article.ARTICLE_CONTENT));
article.put(Article.ARTICLE_T_CREATE_DATE, originalArticle.getLong(Article.ARTICLE_CREATED));
article.put(Article.ARTICLE_T_UPDATE_DATE, originalArticle.getLong(Article.ARTICLE_UPDATED));
article.put(Common.POST_TO_COMMUNITY, originalArticle.getBoolean(Common.POST_TO_COMMUNITY));
// Removes this property avoid to persist
originalArticle.remove(Common.POST_TO_COMMUNITY);
requestJSONObject.put(Article.ARTICLE, article);
requestJSONObject.put(Common.BLOG_VERSION, SoloServletListener.VERSION);
requestJSONObject.put(Common.BLOG, "Solo");
requestJSONObject.put(Option.ID_C_BLOG_TITLE, preference.getString(Option.ID_C_BLOG_TITLE));
requestJSONObject.put("blogHost", Latkes.getServePath());
requestJSONObject.put(UserExt.USER_B3_KEY, author.optString(UserExt.USER_B3_KEY));
requestJSONObject.put("clientAdminEmail", author.optString(User.USER_EMAIL));
requestJSONObject.put("clientRuntimeEnv", "LOCAL");
HttpRequest.put(UPDATE_ARTICLE_URL).bodyText(requestJSONObject.toString()).
contentTypeJson().header("User-Agent", Solos.USER_AGENT).sendAsync();
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Sends an article to Rhythm error: {0}", e.getMessage());
}
LOGGER.log(Level.DEBUG, "Sent an article to Rhythm");
B3ArticleSender.pushArticleToRhy(data);
}
/**
......
......@@ -23,6 +23,7 @@ import org.b3log.latke.Latkes;
import org.b3log.latke.event.AbstractEventListener;
import org.b3log.latke.event.Event;
import org.b3log.latke.ioc.BeanManager;
import org.b3log.latke.ioc.Inject;
import org.b3log.latke.ioc.Singleton;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
......@@ -30,6 +31,7 @@ import org.b3log.latke.model.User;
import org.b3log.latke.util.Strings;
import org.b3log.solo.SoloServletListener;
import org.b3log.solo.model.Comment;
import org.b3log.solo.model.Option;
import org.b3log.solo.model.UserExt;
import org.b3log.solo.repository.UserRepository;
import org.b3log.solo.service.PreferenceQueryService;
......@@ -52,9 +54,16 @@ public class B3CommentSender extends AbstractEventListener<JSONObject> {
private static final Logger LOGGER = Logger.getLogger(B3CommentSender.class);
/**
* URL of adding comment to Symphony.
* Preference query service.
*/
private static final String ADD_COMMENT_URL = Solos.B3LOG_SYMPHONY_SERVE_PATH + "/solo/comment";
@Inject
private PreferenceQueryService preferenceQueryService;
/**
* User repository.
*/
@Inject
private UserRepository userRepository;
@Override
public void action(final Event<JSONObject> event) {
......@@ -65,9 +74,6 @@ public class B3CommentSender extends AbstractEventListener<JSONObject> {
try {
final JSONObject originalComment = data.getJSONObject(Comment.COMMENT);
final BeanManager beanManager = BeanManager.getInstance();
final PreferenceQueryService preferenceQueryService = beanManager.getReference(PreferenceQueryService.class);
final JSONObject preference = preferenceQueryService.getPreference();
if (null == preference) {
LOGGER.log(Level.ERROR, "Not found preference");
......@@ -81,33 +87,32 @@ public class B3CommentSender extends AbstractEventListener<JSONObject> {
return;
}
final JSONObject requestJSONObject = new JSONObject();
final JSONObject comment = new JSONObject();
comment.put("commentId", originalComment.optString(Keys.OBJECT_ID));
comment.put("commentAuthorName", originalComment.getString(Comment.COMMENT_NAME));
comment.put("commentAuthorEmail", originalComment.getString(Comment.COMMENT_EMAIL));
comment.put(Comment.COMMENT_CONTENT, originalComment.getString(Comment.COMMENT_CONTENT));
comment.put("articleId", originalComment.getString(Comment.COMMENT_ON_ID));
final UserRepository userRepository = beanManager.getReference(UserRepository.class);
final JSONObject admin = userRepository.getAdmin();
requestJSONObject.put(Comment.COMMENT, comment);
requestJSONObject.put("clientVersion", SoloServletListener.VERSION);
requestJSONObject.put("clientRuntimeEnv", "LOCAL");
requestJSONObject.put("clientName", "Solo");
requestJSONObject.put("clientHost", Latkes.getServePath());
requestJSONObject.put("clientAdminEmail", admin.optString(User.USER_EMAIL));
requestJSONObject.put(UserExt.USER_B3_KEY, admin.optString(UserExt.USER_B3_KEY));
final JSONObject comment = new JSONObject().
put("id", originalComment.optString(Keys.OBJECT_ID)).
put("articleId", originalComment.getString(Comment.COMMENT_ON_ID)).
put("content", originalComment.getString(Comment.COMMENT_CONTENT));
final JSONObject author = userRepository.getByUserName(comment.optString(Comment.COMMENT_NAME));
if (null == author) {
return;
}
HttpRequest.post(ADD_COMMENT_URL).bodyText(requestJSONObject.toString()).
final JSONObject client = new JSONObject().
put("title", preference.getString(Option.ID_C_BLOG_TITLE)).
put("host", Latkes.getServePath()).
put("name", "Solo").
put("ver", SoloServletListener.VERSION).
put("userName", author.optString(User.USER_NAME)).
put("userB3Key", author.optString(UserExt.USER_B3_KEY));
final JSONObject requestJSONObject = new JSONObject().
put("comment", comment).
put("client", client);
HttpRequest.post("https://rhythm.b3log.org/api/comment").bodyText(requestJSONObject.toString()).
header("User-Agent", Solos.USER_AGENT).contentTypeJson().sendAsync();
LOGGER.log(Level.DEBUG, "Pushed a comment to Sym");
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Sends a comment to Symphony error: {0}", e.getMessage());
LOGGER.log(Level.ERROR, "Pushes a comment to Sym failed: " + e.getMessage());
}
LOGGER.log(Level.DEBUG, "Sent a comment to Symphony");
}
/**
......
......@@ -21,6 +21,7 @@ import org.b3log.latke.Keys;
import org.b3log.latke.ioc.Inject;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
import org.b3log.latke.model.User;
import org.b3log.latke.service.ServiceException;
import org.b3log.latke.servlet.HttpMethod;
import org.b3log.latke.servlet.RequestContext;
......@@ -188,22 +189,31 @@ public class B3ArticleReceiver {
try {
final JSONObject article = requestJSONObject.optJSONObject(Article.ARTICLE);
final String userB3Key = article.optString(UserExt.USER_B3_KEY);
final JSONObject admin = userQueryService.getAdmin();
if (!userB3Key.equals(admin.optString(UserExt.USER_B3_KEY))) {
LOGGER.log(Level.WARN, "B3 key not match, ignored update article");
return;
}
article.remove(UserExt.USER_B3_KEY);
final String articleId = article.getString(Keys.OBJECT_ID);
final JSONObject oldArticle = articleQueryService.getArticleById(articleId);
if (null == oldArticle) {
ret.put(Keys.MSG, "No found article [oId=" + articleId + "] to update");
ret.put(Keys.STATUS_CODE, false);
if (null == articleQueryService.getArticleById(articleId)) {
ret.put(Keys.MSG, "No found article[oId=" + articleId + "] to update");
return;
}
final String authorId = oldArticle.optString(Article.ARTICLE_AUTHOR_ID);
final JSONObject userResult = userQueryService.getUser(authorId);
if (null == userResult) {
ret.put(Keys.MSG, "No found article [oId=" + articleId + "]'s author");
ret.put(Keys.STATUS_CODE, false);
return;
}
final JSONObject author = userResult.optJSONObject(User.USER);
if (!userB3Key.equals(author.optString(UserExt.USER_B3_KEY))) {
LOGGER.log(Level.WARN, "B3 key not match, ignored update article");
return;
}
final String articleContent = article.optString(Article.ARTICLE_CONTENT);
article.put(Article.ARTICLE_ABSTRACT, Article.getAbstract(articleContent));
......
......@@ -191,10 +191,6 @@ public class B3CommentReceiver {
final String commentId = symphonyCmt.optString(Keys.OBJECT_ID);
String commentContent = symphonyCmt.getString(Comment.COMMENT_CONTENT);
// commentContent += "<p class='cmtFromSym'><i>该评论同步自 <a href='" + SoloServletListener.B3LOG_SYMPHONY_SERVE_PATH
// + "/article/" + symphonyCmt.optString("commentSymphonyArticleId") + "#" + commentId
// + "' target='_blank'>黑客派</a></i></p>";
final String originalCommentId = symphonyCmt.optString(Comment.COMMENT_ORIGINAL_COMMENT_ID);
// Step 1: Add comment
final JSONObject comment = new JSONObject();
......
......@@ -70,16 +70,6 @@ public final class Solos {
*/
private static final ResourceBundle mailConf = ResourceBundle.getBundle("mail");
/**
* B3log Rhythm address.
*/
public static final String B3LOG_RHYTHM_SERVE_PATH;
/**
* B3log Symphony address.
*/
public static final String B3LOG_SYMPHONY_SERVE_PATH;
/**
* Favicon API.
*/
......@@ -128,8 +118,6 @@ public final class Solos {
solo = ResourceBundle.getBundle("b3log"); // 2.8.0 向后兼容
}
B3LOG_RHYTHM_SERVE_PATH = solo.getString("rhythm.servePath");
B3LOG_SYMPHONY_SERVE_PATH = solo.getString("symphony.servePath");
FAVICON_API = solo.getString("faviconAPI");
GRAVATAR = solo.getString("gravatar");
String mobileSkin = Option.DefaultPreference.DEFAULT_SKIN_DIR_NAME;
......
......@@ -24,8 +24,5 @@
mobile.skin=Jane
rhythm.servePath=https://rhythm.b3log.org:443
symphony.servePath=https://hacpai.com:443
gravatar=https://secure.gravatar.com/avatar/
faviconAPI=https://api.byi.pw/favicon?url=
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment