001/* 002 * (C) Copyright 2016 Nuxeo SA (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 * Miguel Nixo 018 */ 019package org.nuxeo.ecm.platform.ec.notification.automation; 020 021import org.nuxeo.ecm.automation.core.Constants; 022import org.nuxeo.ecm.automation.core.annotations.Context; 023import org.nuxeo.ecm.automation.core.annotations.Operation; 024import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 025import org.nuxeo.ecm.automation.core.annotations.Param; 026import org.nuxeo.ecm.automation.core.util.StringList; 027import org.nuxeo.ecm.core.api.CoreSession; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.core.api.DocumentModelList; 030import org.nuxeo.ecm.core.api.NuxeoPrincipal; 031import org.nuxeo.ecm.platform.ec.notification.NotificationConstants; 032import org.nuxeo.ecm.platform.notification.api.NotificationManager; 033 034import java.util.List; 035 036/** 037 * Operation to unsubscribe a user to the notifications of one or more of documents. 038 * 039 * @since 8.10 040 */ 041@Operation(id = UnsubscribeOperation.ID, category = Constants.CAT_DOCUMENT, label = "Unsubscribe document", 042 description = "Unsubscribe one or more documents. No value is returned.") 043public class UnsubscribeOperation { 044 045 public static final String ID = "Document.Unsubscribe"; 046 047 @Context 048 protected CoreSession coreSession; 049 050 @Context 051 protected NotificationManager notificationManager; 052 053 @Param(name = "notifications", required = false) 054 protected StringList notifications; 055 056 @OperationMethod 057 public DocumentModelList run(DocumentModelList docs) { 058 docs.forEach(this::run); 059 return docs; 060 } 061 062 @OperationMethod 063 public DocumentModel run(DocumentModel doc) { 064 NuxeoPrincipal principal = coreSession.getPrincipal(); 065 String username = NotificationConstants.USER_PREFIX + principal.getName(); 066 if (notifications == null || notifications.isEmpty()) { 067 // unsubscribe all available notifications 068 List<String> userSubscriptions = notificationManager.getSubscriptionsForUserOnDocument(username, doc); 069 notificationManager.removeSubscriptions(username, userSubscriptions, doc); 070 } else { 071 // unsubscribe the specified notifications 072 for (String notification : notifications) { 073 notificationManager.removeSubscription(username, notification, doc); 074 } 075 } 076 return doc; 077 } 078 079}