001/* 002 * Copyright 2018 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.ametys.cms.repository.comment.actions; 018 019import java.util.HashMap; 020import java.util.Map; 021 022import org.apache.avalon.framework.parameters.Parameters; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.cocoon.environment.ObjectModelHelper; 026import org.apache.cocoon.environment.Redirector; 027import org.apache.cocoon.environment.Request; 028import org.apache.cocoon.environment.SourceResolver; 029import org.apache.commons.lang.StringUtils; 030 031import org.ametys.cms.ObservationConstants; 032import org.ametys.cms.repository.ReactionableObject.ReactionType; 033import org.ametys.cms.repository.comment.Comment; 034import org.ametys.cms.repository.comment.CommentManagerExtensionPoint; 035import org.ametys.cms.repository.comment.CommentableContent; 036import org.ametys.core.cocoon.ActionResultGenerator; 037import org.ametys.core.observation.Event; 038import org.ametys.core.user.UserIdentity; 039 040/** 041 * Like or unlike a comment 042 */ 043public class LikeCommentAction extends AbstractCommentAction 044{ 045 /** The listeners for this action */ 046 protected CommentManagerExtensionPoint _commentManager; 047 048 @Override 049 public void service(ServiceManager smanager) throws ServiceException 050 { 051 super.service(smanager); 052 _commentManager = (CommentManagerExtensionPoint) smanager.lookup(CommentManagerExtensionPoint.ROLE); 053 } 054 055 @Override 056 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 057 { 058 Request request = ObjectModelHelper.getRequest(objectModel); 059 060 Map<String, Object> results = new HashMap<>(); 061 062 String contentId = request.getParameter(PARAMETER_CONTENTID); 063 if (StringUtils.isBlank(contentId)) 064 { 065 throw new IllegalArgumentException("Missing or empty 'content-id' parameter to like a comment"); 066 } 067 068 String commentId = request.getParameter(PARAMETER_COMMENTID); 069 if (StringUtils.isBlank(commentId)) 070 { 071 throw new IllegalArgumentException("Missing or empty 'comment-id' parameter to like a comment"); 072 } 073 074 CommentableContent content = getContent(request, contentId); 075 Comment comment = content.getComment(commentId); 076 077 UserIdentity currentUser = getCurrentUser(); 078 if (comment.getReactionUsers(ReactionType.LIKE).contains(currentUser)) 079 { 080 comment.removeReaction(currentUser, ReactionType.LIKE); 081 } 082 else 083 { 084 comment.addReaction(currentUser, ReactionType.LIKE); 085 } 086 087 content.saveChanges(); 088 089 Map<String, Object> eventParams = new HashMap<>(); 090 eventParams.put(ObservationConstants.ARGS_CONTENT, content); 091 eventParams.put(ObservationConstants.ARGS_COMMENT, comment); 092 eventParams.put(ObservationConstants.ARGS_REACTION_TYPE, ReactionType.LIKE); 093 eventParams.put(ObservationConstants.ARGS_REACTION_ISSUER, currentUser); 094 _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_COMMENT_REACTION_CHANGED, getCurrentUser(), eventParams)); 095 096 results.put("redirect", request.getHeader("Referer") + "#" + comment.getId() + "-" + content.getId()); 097 results.put("liked", true); 098 results.put("contentId", content.getId()); 099 results.put("commentId", comment.getId()); 100 101 request.setAttribute(ActionResultGenerator.MAP_REQUEST_ATTR, results); 102 return EMPTY_MAP; 103 } 104}