<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.notifications.filters.internal.user;

import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Stream;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.slf4j.Logger;
import org.xwiki.component.annotation.Component;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.notifications.NotificationFormat;
import org.xwiki.notifications.filters.NotificationFilterManager;
import org.xwiki.notifications.filters.NotificationFilterPreference;
import org.xwiki.notifications.filters.NotificationFilterProperty;
import org.xwiki.notifications.filters.NotificationFilterType;

/**
 * Helper to get user preferences for the {@link EventUserFilter}.
 *
 * @version $Id$
 * @since 9.10RC1
 */
@Component(roles = EventUserFilterPreferencesGetter.class)
@Singleton
public class EventUserFilterPreferencesGetter
{
    @Inject
    private NotificationFilterManager notificationFilterManager;

    @Inject
    private Logger logger;

    /**
     * @param testUser user to test
     * @param user the user for who we compute the notifications
     * @param format the notification format (could be null)
     * @return either or not the user to test is part of the excluded users of the given user
     */
    public boolean isUserExcluded(String testUser, DocumentReference user, NotificationFormat format)
    {
        return getPreferences(user, format, NotificationFilterType.EXCLUSIVE).anyMatch(
            pref -> pref.getProperties(NotificationFilterProperty.USER).contains(testUser)
        );
    }

    /**
     * @param testUser user to test
     * @param user the user for who we compute the notifications
     * @param format the notification format (could be null)
     * @return either or not the user to test is part of the followed users of the given user
     * @since 10.3RC1
     * @since 9.11.5
     */
    public boolean isUsedFollowed(String testUser, DocumentReference user, NotificationFormat format)
    {
        return getPreferences(user, format, NotificationFilterType.INCLUSIVE).anyMatch(
            pref -> pref.getProperties(NotificationFilterProperty.USER).contains(testUser)
        );
    }

    /**
     * @param user the user for who we compute the notifications
     * @param format the notification format (could be null)
     * @return the collection of users followed by the given user
     * @since 10.3RC1
     * @since 9.11.5
     */
    public Collection<String> getFollowedUsers(DocumentReference user, NotificationFormat format)
    {
        return collect(getPreferences(user, format, NotificationFilterType.INCLUSIVE));
    }

    /**
     * @param user the user for who we compute the notifications
     * @param format the notification format (could be null)
     * @return the collection of excluded users by the given user
     */
    public Collection<String> getExcludedUsers(DocumentReference user, NotificationFormat format)
    {
        return collect(getPreferences(user, format, NotificationFilterType.EXCLUSIVE));
    }

    private Collection<String> collect(Stream<NotificationFilterPreference> stream)<commit_msg>XWIKI-15150: "Follow user" action does not really follow an user.<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.notifications.filters.internal.user;

import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Stream;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.slf4j.Logger;
import org.xwiki.component.annotation.Component;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.notifications.NotificationFormat;
import org.xwiki.notifications.filters.NotificationFilterManager;
import org.xwiki.notifications.filters.NotificationFilterPreference;
import org.xwiki.notifications.filters.NotificationFilterProperty;
import org.xwiki.notifications.filters.NotificationFilterType;

/**
 * Helper to get user preferences for the {@link EventUserFilter}.
 *
 * @version $Id$
 * @since 9.10RC1
 */
@Component(roles = EventUserFilterPreferencesGetter.class)
@Singleton
public class EventUserFilterPreferencesGetter
{
    @Inject
    private NotificationFilterManager notificationFilterManager;

    @Inject
    private Logger logger;

    /**
     * @param testUser user to test
     * @param user the user for who we compute the notifications
     * @param format the notification format (could be null)
     * @return either or not the user to test is part of the excluded users of the given user
     */
    public boolean isUserExcluded(String testUser, DocumentReference user, NotificationFormat format)
    {
        return getPreferences(user, format, NotificationFilterType.EXCLUSIVE).anyMatch(
            pref -> pref.getProperties(NotificationFilterProperty.USER).contains(testUser)
        );
    }

    /**
     * @param testUser user to test
     * @param user the user for who we compute the notifications
     * @param format the notification format (could be null)
     * @return either or not the user to test is part of the followed users of the given user
     * @since 10.3
     * @since 9.11.5
     */
    public boolean isUsedFollowed(String testUser, DocumentReference user, NotificationFormat format)
    {
        return getPreferences(user, format, NotificationFilterType.INCLUSIVE).anyMatch(
            pref -> pref.getProperties(NotificationFilterProperty.USER).contains(testUser)
        );
    }

    /**
     * @param user the user for who we compute the notifications
     * @param format the notification format (could be null)
     * @return the collection of users followed by the given user
     * @since 10.3
     * @since 9.11.5
     */
    public Collection<String> getFollowedUsers(DocumentReference user, NotificationFormat format)
    {
        return collect(getPreferences(user, format, NotificationFilterType.INCLUSIVE));
    }

    /**
     * @param user the user for who we compute the notifications
     * @param format the notification format (could be null)
     * @return the collection of excluded users by the given user
     */
    public Collection<String> getExcludedUsers(DocumentReference user, NotificationFormat format)
    {
        return collect(getPreferences(user, format, NotificationFilterType.EXCLUSIVE));
    }

    private Collection<String> collect(Stream<NotificationFilterPreference> stream)
{'fraction_non_alphanumeric': 0.07317970090708507, 'fraction_numerical': 0.008703113508212797, 'mean_word_length': 3.7994117647058823, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6967602', 'n_tokens_mistral': 2116, 'n_tokens_neox': 2039, 'n_words': 786}