Search in sources :

Example 1 with ArticleRepository

use of org.b3log.symphony.repository.ArticleRepository in project symphony by b3log.

the class ArticleChannel method notifyComment.

/**
 * Notifies the specified comment message to browsers.
 *
 * @param message the specified message
 */
public static void notifyComment(final JSONObject message) {
    message.put(Common.TYPE, Comment.COMMENT);
    final LatkeBeanManager beanManager = LatkeBeanManagerImpl.getInstance();
    final UserQueryService userQueryService = beanManager.getReference(UserQueryService.class);
    final ArticleRepository articleRepository = beanManager.getReference(ArticleRepository.class);
    final RoleQueryService roleQueryService = beanManager.getReference(RoleQueryService.class);
    final LangPropsService langPropsService = beanManager.getReference(LangPropsServiceImpl.class);
    for (final Session session : SESSIONS) {
        final String viewingArticleId = (String) Channels.getHttpParameter(session, Article.ARTICLE_T_ID);
        if (Strings.isEmptyOrNull(viewingArticleId) || !viewingArticleId.equals(message.optString(Article.ARTICLE_T_ID))) {
            continue;
        }
        final int articleType = Integer.valueOf(Channels.getHttpParameter(session, Article.ARTICLE_TYPE));
        final JSONObject user = (JSONObject) Channels.getHttpSessionAttribute(session, User.USER);
        final boolean isLoggedIn = null != user;
        try {
            if (Article.ARTICLE_TYPE_C_DISCUSSION == articleType) {
                if (!isLoggedIn) {
                    continue;
                }
                final String userName = user.optString(User.USER_NAME);
                final String userRole = user.optString(User.USER_ROLE);
                final JSONObject article = articleRepository.get(viewingArticleId);
                if (null == article) {
                    continue;
                }
                final String articleAuthorId = article.optString(Article.ARTICLE_AUTHOR_ID);
                final String userId = user.optString(Keys.OBJECT_ID);
                if (!userId.equals(articleAuthorId)) {
                    final String articleContent = article.optString(Article.ARTICLE_CONTENT);
                    final Set<String> userNames = userQueryService.getUserNames(articleContent);
                    boolean invited = false;
                    for (final String inviteUserName : userNames) {
                        if (inviteUserName.equals(userName)) {
                            invited = true;
                            break;
                        }
                    }
                    if (Role.ROLE_ID_C_ADMIN.equals(userRole)) {
                        invited = true;
                    }
                    if (!invited) {
                        // next session
                        continue;
                    }
                }
            }
            message.put(Comment.COMMENT_T_NICE, false);
            message.put(Common.REWARED_COUNT, 0);
            message.put(Comment.COMMENT_T_VOTE, -1);
            message.put(Common.REWARDED, false);
            message.put(Comment.COMMENT_REVISION_COUNT, 1);
            final Map dataModel = new HashMap();
            dataModel.put(Common.IS_LOGGED_IN, isLoggedIn);
            dataModel.put(Common.CURRENT_USER, user);
            dataModel.put(Common.CSRF_TOKEN, Channels.getHttpSessionAttribute(session, Common.CSRF_TOKEN));
            Keys.fillServer(dataModel);
            dataModel.put(Comment.COMMENT, message);
            String templateDirName = Symphonys.get("skinDirName");
            if (isLoggedIn) {
                dataModel.putAll(langPropsService.getAll(Locales.getLocale(user.optString(UserExt.USER_LANGUAGE))));
                final String userId = user.optString(Keys.OBJECT_ID);
                final Map<String, JSONObject> permissions = roleQueryService.getUserPermissionsGrantMap(userId);
                dataModel.put(Permission.PERMISSIONS, permissions);
                templateDirName = user.optString(UserExt.USER_SKIN);
            } else {
                dataModel.putAll(langPropsService.getAll(Locales.getLocale()));
                final Map<String, JSONObject> permissions = roleQueryService.getPermissionsGrantMap(Role.ROLE_ID_C_VISITOR);
                dataModel.put(Permission.PERMISSIONS, permissions);
            }
            final Template template = SkinRenderer.getTemplate(templateDirName, "common/comment.ftl", false, user);
            final StringWriter stringWriter = new StringWriter();
            template.process(dataModel, stringWriter);
            stringWriter.close();
            message.put("cmtTpl", stringWriter.toString());
            final String msgStr = message.toString();
            if (session.isOpen()) {
                session.getAsyncRemote().sendText(msgStr);
            }
        } catch (final Exception e) {
            LOGGER.log(Level.ERROR, "Notify comment error", e);
        } finally {
            JdbcRepository.dispose();
        }
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UserQueryService(org.b3log.symphony.service.UserQueryService) ArticleRepository(org.b3log.symphony.repository.ArticleRepository) ServerEndpoint(javax.websocket.server.ServerEndpoint) LatkeBeanManager(org.b3log.latke.ioc.LatkeBeanManager) LangPropsService(org.b3log.latke.service.LangPropsService) Template(freemarker.template.Template) RoleQueryService(org.b3log.symphony.service.RoleQueryService) JSONObject(org.json.JSONObject) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

Template (freemarker.template.Template)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ServerEndpoint (javax.websocket.server.ServerEndpoint)1 LatkeBeanManager (org.b3log.latke.ioc.LatkeBeanManager)1 LangPropsService (org.b3log.latke.service.LangPropsService)1 ArticleRepository (org.b3log.symphony.repository.ArticleRepository)1 RoleQueryService (org.b3log.symphony.service.RoleQueryService)1 UserQueryService (org.b3log.symphony.service.UserQueryService)1 JSONObject (org.json.JSONObject)1