|
30 | 30 | import org.b3log.latke.service.ServiceException; |
31 | 31 | import org.b3log.symphony.model.EmojiGroup; |
32 | 32 | import org.b3log.symphony.model.EmojiGroupItem; |
| 33 | +import org.b3log.latke.model.User; |
33 | 34 | import org.b3log.symphony.processor.middleware.LoginCheckMidware; |
34 | 35 | import org.b3log.symphony.service.CloudService; |
35 | 36 | import org.b3log.symphony.service.EmojiMgmtService; |
@@ -107,6 +108,8 @@ public static void register() { |
107 | 108 | Dispatcher.post("/api/emoji/group/add-emoji", emojiProcessor::addEmojiToGroup, loginCheck::handle); |
108 | 109 | Dispatcher.post("/api/emoji/group/add-url-emoji", emojiProcessor::addUrlEmojiToGroup, loginCheck::handle); |
109 | 110 | Dispatcher.post("/api/emoji/group/remove-emoji", emojiProcessor::removeEmojiFromGroup, loginCheck::handle); |
| 111 | + Dispatcher.post("/api/emoji/group/share/create", emojiProcessor::createGroupShare, loginCheck::handle); |
| 112 | + Dispatcher.post("/api/emoji/group/share/import", emojiProcessor::importGroupShare, loginCheck::handle); |
110 | 113 | Dispatcher.post("/api/emoji/emoji/update", emojiProcessor::updateEmojiItem, loginCheck::handle); |
111 | 114 | Dispatcher.post("/api/emoji/emoji/migrate", emojiProcessor::migrateOldEmoji, loginCheck::handle); |
112 | 115 | } |
@@ -567,6 +570,58 @@ public void removeEmojiFromGroup(final RequestContext context) { |
567 | 570 | } |
568 | 571 | } |
569 | 572 |
|
| 573 | + public void createGroupShare(final RequestContext context) { |
| 574 | + try { |
| 575 | + final JSONObject currentUser = getCurrentUser(context); |
| 576 | + final JSONObject requestJSONObject = context.requestJSON(); |
| 577 | + final String groupId = requestJSONObject.optString("groupId"); |
| 578 | + if (StringUtils.isBlank(groupId)) { |
| 579 | + context.renderJSON(new JSONObject()).renderCode(StatusCodes.ERR).renderMsg("缺少分组参数"); |
| 580 | + return; |
| 581 | + } |
| 582 | + |
| 583 | + final JSONObject resultData = emojiMgmtService.createShareSnapshot( |
| 584 | + currentUser.optString(Keys.OBJECT_ID), groupId |
| 585 | + ); |
| 586 | + final JSONObject result = new JSONObject(); |
| 587 | + result.put("code", StatusCodes.SUCC); |
| 588 | + result.put("data", resultData); |
| 589 | + context.renderJSON(result); |
| 590 | + } catch (final ServiceException e) { |
| 591 | + LOGGER.log(Level.WARN, "Create emoji group share failed", e); |
| 592 | + context.renderJSON(new JSONObject()).renderCode(StatusCodes.ERR).renderMsg(e.getMessage()); |
| 593 | + } catch (final Exception e) { |
| 594 | + LOGGER.log(Level.ERROR, "Create emoji group share failed", e); |
| 595 | + context.renderJSON(new JSONObject()).renderCode(StatusCodes.ERR).renderMsg("生成分享码失败"); |
| 596 | + } |
| 597 | + } |
| 598 | + |
| 599 | + public void importGroupShare(final RequestContext context) { |
| 600 | + try { |
| 601 | + final JSONObject currentUser = getCurrentUser(context); |
| 602 | + final JSONObject requestJSONObject = context.requestJSON(); |
| 603 | + final String shareCode = requestJSONObject.optString("shareCode"); |
| 604 | + if (StringUtils.isBlank(shareCode)) { |
| 605 | + context.renderJSON(new JSONObject()).renderCode(StatusCodes.ERR).renderMsg("请输入分享码"); |
| 606 | + return; |
| 607 | + } |
| 608 | + |
| 609 | + final JSONObject resultData = emojiMgmtService.importShareSnapshot( |
| 610 | + currentUser.optString(Keys.OBJECT_ID), shareCode |
| 611 | + ); |
| 612 | + final JSONObject result = new JSONObject(); |
| 613 | + result.put("code", StatusCodes.SUCC); |
| 614 | + result.put("data", resultData); |
| 615 | + context.renderJSON(result); |
| 616 | + } catch (final ServiceException e) { |
| 617 | + LOGGER.log(Level.WARN, "Import emoji group share failed", e); |
| 618 | + context.renderJSON(new JSONObject()).renderCode(StatusCodes.ERR).renderMsg(e.getMessage()); |
| 619 | + } catch (final Exception e) { |
| 620 | + LOGGER.log(Level.ERROR, "Import emoji group share failed", e); |
| 621 | + context.renderJSON(new JSONObject()).renderCode(StatusCodes.ERR).renderMsg("导入分享码失败"); |
| 622 | + } |
| 623 | + } |
| 624 | + |
570 | 625 | // 用户修改表情名字 |
571 | 626 | /** |
572 | 627 | * Updates an emoji item (name and sort). |
@@ -717,5 +772,31 @@ private boolean isSafeName(final String name) { |
717 | 772 | return SAFE_NAME_PATTERN.matcher(name).matches(); |
718 | 773 | } |
719 | 774 |
|
| 775 | + private JSONObject getCurrentUser(final RequestContext context) { |
| 776 | + Object userObj = context.attr(User.USER); |
| 777 | + if (userObj instanceof JSONObject) { |
| 778 | + return (JSONObject) userObj; |
| 779 | + } |
| 780 | + |
| 781 | + JSONObject currentUser = Sessions.getUser(); |
| 782 | + if (currentUser != null) { |
| 783 | + return currentUser; |
| 784 | + } |
| 785 | + |
| 786 | + try { |
| 787 | + final JSONObject requestJSONObject = context.requestJSON(); |
| 788 | + final String apiKey = requestJSONObject.optString("apiKey"); |
| 789 | + if (StringUtils.isNotBlank(apiKey)) { |
| 790 | + currentUser = ApiProcessor.getUserByKey(apiKey); |
| 791 | + if (currentUser != null) { |
| 792 | + return currentUser; |
| 793 | + } |
| 794 | + } |
| 795 | + } catch (final Exception ignored) { |
| 796 | + } |
| 797 | + |
| 798 | + return new JSONObject(); |
| 799 | + } |
| 800 | + |
720 | 801 |
|
721 | 802 | } |
0 commit comments