Skip to content

Commit

Permalink
♻️ sc 调整为 code #30
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Jun 20, 2020
1 parent 3c6c8b7 commit 76cc731
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.b3log.symphony.service.*;
import org.b3log.symphony.util.Escapes;
import org.b3log.symphony.util.Sessions;
import org.b3log.symphony.util.StatusCodes;
import org.b3log.symphony.util.Symphonys;
import org.json.JSONArray;
import org.json.JSONObject;
Expand Down Expand Up @@ -631,8 +632,7 @@ public void removeBreezemoon(final RequestContext context) {
* @param context the specified context
*/
public void removeUnusedTags(final RequestContext context) {
context.renderJSON(true);

context.renderJSON(StatusCodes.SUCC);
tagMgmtService.removeUnusedTags();
operationMgmtService.addOperation(Operation.newOperation(context.getRequest(), Operation.OPERATION_CODE_C_REMOVE_UNUSED_TAGS, ""));
}
Expand Down Expand Up @@ -2410,7 +2410,7 @@ public void removeDomainTag(final RequestContext context) {
* @param context the specified context
*/
public void rebuildArticleSearchIndex(final RequestContext context) {
context.renderJSON(true);
context.renderJSON(StatusCodes.SUCC);

if (Symphonys.ES_ENABLED) {
searchMgmtService.rebuildESIndex();
Expand Down
54 changes: 23 additions & 31 deletions src/main/java/org/b3log/symphony/processor/ArticleProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,17 +250,15 @@ public void removeArticle(final RequestContext context) {
return;
}

context.renderJSON();
context.renderJSON(StatusCodes.ERR);
try {
articleMgmtService.removeArticle(id);

context.renderJSONValue(Keys.STATUS_CODE, StatusCodes.SUCC);
context.renderJSONValue(Keys.CODE, StatusCodes.SUCC);
context.renderJSONValue(Article.ARTICLE_T_ID, id);
} catch (final ServiceException e) {
final String msg = e.getMessage();

context.renderMsg(msg);
context.renderJSONValue(Keys.STATUS_CODE, StatusCodes.ERR);
}
}

Expand All @@ -279,7 +277,7 @@ public void checkArticleTitle(final RequestContext context) {

final JSONObject article = articleQueryService.getArticleByTitle(title);
if (null == article) {
context.renderJSON(true);
context.renderJSON(StatusCodes.SUCC);
return;
}

Expand All @@ -302,14 +300,14 @@ public void checkArticleTitle(final RequestContext context) {
}

final JSONObject ret = new JSONObject();
ret.put(Keys.STATUS_CODE, false);
ret.put(Keys.CODE, StatusCodes.ERR);
ret.put(Keys.MSG, msg);

context.renderJSON(ret);
} else { // Update
final JSONObject oldArticle = articleQueryService.getArticle(id);
if (oldArticle.optString(Article.ARTICLE_TITLE).equals(title)) {
context.renderJSON(true);
context.renderJSON(StatusCodes.SUCC);
return;
}

Expand All @@ -331,9 +329,8 @@ public void checkArticleTitle(final RequestContext context) {
}

final JSONObject ret = new JSONObject();
ret.put(Keys.STATUS_CODE, false);
ret.put(Keys.CODE, StatusCodes.ERR);
ret.put(Keys.MSG, msg);

context.renderJSON(ret);
}
}
Expand Down Expand Up @@ -400,11 +397,9 @@ public void getArticleImage(final RequestContext context) {
}

String url = "";

final JSONObject ret = new JSONObject();
ret.put(Keys.STATUS_CODE, true);
ret.put(Keys.CODE, StatusCodes.SUCC);
ret.put(Common.URL, url);

context.renderJSON(ret);
}

Expand All @@ -417,9 +412,8 @@ public void getArticleRevisions(final RequestContext context) {
final String id = context.pathVar("id");
final List<JSONObject> revisions = revisionQueryService.getArticleRevisions(id);
final JSONObject ret = new JSONObject();
ret.put(Keys.STATUS_CODE, true);
ret.put(Keys.CODE, StatusCodes.SUCC);
ret.put(Revision.REVISIONS, (Object) revisions);

context.renderJSON(ret);
}

Expand Down Expand Up @@ -875,7 +869,7 @@ public void showArticle(final RequestContext context) {
* @param context the specified context
*/
public void addArticle(final RequestContext context) {
context.renderJSON();
context.renderJSON(StatusCodes.ERR);

final Request request = context.getRequest();
final JSONObject requestJSONObject = context.requestJSON();
Expand Down Expand Up @@ -932,14 +926,13 @@ public void addArticle(final RequestContext context) {

final String articleId = articleMgmtService.addArticle(article);

context.renderJSONValue(Keys.STATUS_CODE, StatusCodes.SUCC);
context.renderJSONValue(Keys.CODE, StatusCodes.SUCC);
context.renderJSONValue(Article.ARTICLE_T_ID, articleId);
} catch (final ServiceException e) {
final String msg = e.getMessage();
LOGGER.log(Level.ERROR, "Adds article [title=" + articleTitle + "] failed: {}", e.getMessage());

context.renderMsg(msg);
context.renderJSONValue(Keys.STATUS_CODE, StatusCodes.ERR);
context.renderJSONValue(Keys.CODE, StatusCodes.ERR);
}
}

Expand Down Expand Up @@ -1025,11 +1018,11 @@ public void updateArticle(final RequestContext context) {
return;
}

context.renderJSON();
context.renderJSON(StatusCodes.ERR);

if (Article.ARTICLE_STATUS_C_VALID != oldArticle.optInt(Article.ARTICLE_STATUS)) {
context.renderMsg(langPropsService.get("articleLockedLabel"));
context.renderJSONValue(Keys.STATUS_CODE, StatusCodes.ERR);
context.renderJSONValue(Keys.CODE, StatusCodes.ERR);
return;
}

Expand Down Expand Up @@ -1089,14 +1082,13 @@ public void updateArticle(final RequestContext context) {
try {
articleMgmtService.updateArticle(article);

context.renderJSONValue(Keys.STATUS_CODE, StatusCodes.SUCC);
context.renderJSONValue(Keys.CODE, StatusCodes.SUCC);
context.renderJSONValue(Article.ARTICLE_T_ID, id);
} catch (final ServiceException e) {
final String msg = e.getMessage();
LOGGER.log(Level.ERROR, "Adds article [title=" + articleTitle + "] failed: {}", e.getMessage());

context.renderMsg(msg);
context.renderJSONValue(Keys.STATUS_CODE, StatusCodes.ERR);
context.renderJSONValue(Keys.CODE, StatusCodes.ERR);
}
}

Expand Down Expand Up @@ -1152,11 +1144,11 @@ public void getArticlePreviewContent(final RequestContext context) {
final String articleId = context.pathVar("articleId");
final String content = articleQueryService.getArticlePreviewContent(articleId, context);
if (StringUtils.isBlank(content)) {
context.renderJSON().renderFalseResult();
context.renderJSON(StatusCodes.ERR);
return;
}

context.renderJSON(true).renderJSONValue("html", content);
context.renderJSON(StatusCodes.SUCC).renderJSONValue("html", content);
}

/**
Expand All @@ -1177,7 +1169,7 @@ public void rewardArticle(final RequestContext context) {
return;
}

context.renderJSON();
context.renderJSON(StatusCodes.ERR);

try {
articleMgmtService.reward(articleId, currentUser.optString(Keys.OBJECT_ID));
Expand All @@ -1190,7 +1182,7 @@ public void rewardArticle(final RequestContext context) {
articleQueryService.processArticleContent(article);

final String rewardContent = article.optString(Article.ARTICLE_REWARD_CONTENT);
context.renderTrueResult().renderJSONValue(Article.ARTICLE_REWARD_CONTENT, rewardContent);
context.renderJSON(StatusCodes.SUCC).renderJSONValue(Article.ARTICLE_REWARD_CONTENT, rewardContent);
}

/**
Expand All @@ -1211,7 +1203,7 @@ public void thankArticle(final RequestContext context) {
return;
}

context.renderJSON();
context.renderJSON(StatusCodes.ERR);

try {
articleMgmtService.thank(articleId, currentUser.optString(Keys.OBJECT_ID));
Expand All @@ -1220,7 +1212,7 @@ public void thankArticle(final RequestContext context) {
return;
}

context.renderTrueResult();
context.renderJSON(StatusCodes.SUCC);
}

/**
Expand Down Expand Up @@ -1252,7 +1244,7 @@ public void stickArticle(final RequestContext context) {
return;
}

context.renderJSON();
context.renderJSON(StatusCodes.ERR);

try {
articleMgmtService.stick(articleId);
Expand All @@ -1261,6 +1253,6 @@ public void stickArticle(final RequestContext context) {
return;
}

context.renderTrueResult().renderMsg(langPropsService.get("stickSuccLabel"));
context.renderJSON(StatusCodes.SUCC).renderMsg(langPropsService.get("stickSuccLabel"));
}
}
25 changes: 11 additions & 14 deletions src/main/java/org/b3log/symphony/processor/BreezemoonProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public void showWatchBreezemoon(final RequestContext context) {
* @param context the specified context
*/
public void addBreezemoon(final RequestContext context) {
context.renderJSON();
context.renderJSON(StatusCodes.ERR);

final Request request = context.getRequest();
final JSONObject requestJSONObject = context.requestJSON();
Expand All @@ -190,10 +190,10 @@ public void addBreezemoon(final RequestContext context) {
try {
breezemoonMgmtService.addBreezemoon(breezemoon);

context.renderJSONValue(Keys.STATUS_CODE, StatusCodes.SUCC);
context.renderJSONValue(Keys.CODE, StatusCodes.SUCC);
} catch (final Exception e) {
context.renderMsg(e.getMessage());
context.renderJSONValue(Keys.STATUS_CODE, StatusCodes.ERR);
context.renderJSONValue(Keys.CODE, StatusCodes.ERR);
}
}

Expand All @@ -212,7 +212,7 @@ public void addBreezemoon(final RequestContext context) {
*/
public void updateBreezemoon(final RequestContext context) {
final String id = context.pathVar("id");
context.renderJSON();
context.renderJSON(StatusCodes.ERR);
final Request request = context.getRequest();
final JSONObject requestJSONObject = context.requestJSON();
if (isInvalid(context, requestJSONObject)) {
Expand Down Expand Up @@ -241,10 +241,10 @@ public void updateBreezemoon(final RequestContext context) {

breezemoonMgmtService.updateBreezemoon(breezemoon);

context.renderJSONValue(Keys.STATUS_CODE, StatusCodes.SUCC);
context.renderJSONValue(Keys.CODE, StatusCodes.SUCC);
} catch (final Exception e) {
context.renderMsg(e.getMessage());
context.renderJSONValue(Keys.STATUS_CODE, StatusCodes.ERR);
context.renderJSONValue(Keys.CODE, StatusCodes.ERR);
}
}

Expand All @@ -255,7 +255,7 @@ public void updateBreezemoon(final RequestContext context) {
*/
public void removeBreezemoon(final RequestContext context) {
final String id = context.pathVar("id");
context.renderJSON();
context.renderJSON(StatusCodes.ERR);

try {
final JSONObject breezemoon = breezemoonQueryService.getBreezemoon(id);
Expand All @@ -270,10 +270,10 @@ public void removeBreezemoon(final RequestContext context) {

breezemoonMgmtService.removeBreezemoon(id);

context.renderJSONValue(Keys.STATUS_CODE, StatusCodes.SUCC);
context.renderJSONValue(Keys.CODE, StatusCodes.SUCC);
} catch (final Exception e) {
context.renderMsg(e.getMessage());
context.renderJSONValue(Keys.STATUS_CODE, StatusCodes.ERR);
context.renderJSONValue(Keys.CODE, StatusCodes.ERR);
}
}

Expand All @@ -283,20 +283,17 @@ private boolean isInvalid(final RequestContext context, final JSONObject request
final long length = StringUtils.length(breezemoonContent);
if (1 > length || 512 < length) {
context.renderMsg(langPropsService.get("breezemoonLengthLabel"));
context.renderJSONValue(Keys.STATUS_CODE, StatusCodes.ERR);

context.renderJSONValue(Keys.CODE, StatusCodes.ERR);
return true;
}

if (optionQueryService.containReservedWord(breezemoonContent)) {
context.renderMsg(langPropsService.get("contentContainReservedWordLabel"));
context.renderJSONValue(Keys.STATUS_CODE, StatusCodes.ERR);

context.renderJSONValue(Keys.CODE, StatusCodes.ERR);
return true;
}

requestJSONObject.put(Breezemoon.BREEZEMOON_CONTENT, breezemoonContent);

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ public static void register() {
* @param context the specified context
*/
public synchronized void addChatRoomMsg(final RequestContext context) {
context.renderJSON();

final JSONObject requestJSONObject = (JSONObject) context.attr(Keys.REQUEST);
String content = requestJSONObject.optString(Common.CONTENT);

Expand Down Expand Up @@ -182,7 +180,7 @@ public synchronized void addChatRoomMsg(final RequestContext context) {
pushMsg.put(Common.TIME, Times.getTimeAgo(msg.optLong(Common.TIME), Locales.getLocale()));
ChatroomChannel.notifyChat(pushMsg);

context.renderTrueResult();
context.renderJSON(StatusCodes.SUCC);

try {
final String userId = currentUser.optString(Keys.OBJECT_ID);
Expand Down
Loading

0 comments on commit 76cc731

Please sign in to comment.