001/*
002 * (C) Copyright 2019-2020 Nuxeo (http://nuxeo.com/) and others.
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 * Contributors:
017 *     Salem Aouana
018 */
019
020package org.nuxeo.ecm.platform.comment.notification;
021
022import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_CREATED;
023import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_REMOVED;
024import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_UPDATED;
025import static org.nuxeo.ecm.platform.comment.api.AnnotationConstants.ANNOTATION_DOC_TYPE;
026import static org.nuxeo.ecm.platform.comment.api.CommentConstants.COMMENT_DOC_TYPE;
027
028import java.util.Set;
029
030import org.nuxeo.ecm.core.event.Event;
031import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
032import org.nuxeo.ecm.platform.ec.notification.NotificationListenerVeto;
033
034/**
035 * Veto that prevents sending notifications whose type is {@link #EXCLUDED_EVENT_TYPES} on the document comment model.
036 *
037 * @since 11.1
038 */
039public class CommentNotificationVeto implements NotificationListenerVeto {
040
041    protected static final Set<String> ACCEPTED_COMMENT_TYPE = Set.of(COMMENT_DOC_TYPE, ANNOTATION_DOC_TYPE, "Post");
042
043    protected static final Set<String> EXCLUDED_EVENT_TYPES = Set.of(DOCUMENT_CREATED, DOCUMENT_UPDATED,
044            DOCUMENT_REMOVED);
045
046    @Override
047    public boolean accept(Event event) {
048        if (!(event.getContext() instanceof DocumentEventContext)) {
049            return false;
050        }
051        DocumentEventContext docCtx = (DocumentEventContext) event.getContext();
052        String docType = docCtx.getSourceDocument().getType();
053
054        return !(EXCLUDED_EVENT_TYPES.contains(event.getName()) && ACCEPTED_COMMENT_TYPE.contains(docType));
055    }
056}