<commit_before>package org.lantern;

import org.lantern.event.Events;
import org.lantern.event.FriendStatusChangedEvent;
import org.lantern.state.Friend;
import org.lantern.state.Friends;
import org.lantern.state.InviteQueue;
import org.lantern.state.Model;
import org.lantern.state.ModelUtils;
import org.lantern.state.SyncPath;
import org.lantern.state.Friend.Status;
import org.lantern.state.Notification.MessageType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;
import com.google.inject.Singleton;

/**
 * Class for handling all friend management.
 */
@Singleton
public class DefaultFriender implements Friender {

    private final Logger log = LoggerFactory.getLogger(getClass());
    private final Model model;
    private final XmppHandler xmppHandler;
    private final ModelUtils modelUtils;
    private final InviteQueue inviteQueue;

    @Inject
    public DefaultFriender(final Model model, final XmppHandler xmppHandler,
        final ModelUtils modelUtils, final InviteQueue inviteQueue) {
        this.model = model;
        this.xmppHandler = xmppHandler;
        this.modelUtils = modelUtils;
        this.inviteQueue = inviteQueue;
    }
    
    @Override
    public void removeFriend(String json) {
        final String email = JsonUtils.getValueFromJson("email", json).toLowerCase();
        setFriendStatus(email, Status.rejected);
    }

    @Override
    public void addFriend(String json) {
        final String email = JsonUtils.getValueFromJson("email", json).toLowerCase();
        setFriendStatus(email, Status.friend);
        

        try {
            //if they have requested a subscription to us, we'll accept it.
            this.xmppHandler.subscribed(email);

            // We also automatically subscribe to them in turn so we know about
            // their presence.
            this.xmppHandler.subscribe(email);
        } catch (final IllegalStateException e) {
            log.error("IllegalStateException while friending (you are probably offline)", e);
            return;
        }
    }

    private void setFriendStatus(final String email, final Status status) {
        final Friends friends = model.getFriends();
        Friend friend = friends.get(email);<commit_msg>added unsubscribing at the xmpp level when friends are rejected closes #916<commit_after>package org.lantern;

import org.lantern.event.Events;
import org.lantern.event.FriendStatusChangedEvent;
import org.lantern.state.Friend;
import org.lantern.state.Friend.Status;
import org.lantern.state.Friends;
import org.lantern.state.InviteQueue;
import org.lantern.state.Model;
import org.lantern.state.ModelUtils;
import org.lantern.state.Notification.MessageType;
import org.lantern.state.SyncPath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;
import com.google.inject.Singleton;

/**
 * Class for handling all friend management.
 */
@Singleton
public class DefaultFriender implements Friender {

    private final Logger log = LoggerFactory.getLogger(getClass());
    private final Model model;
    private final XmppHandler xmppHandler;
    private final ModelUtils modelUtils;
    private final InviteQueue inviteQueue;

    @Inject
    public DefaultFriender(final Model model, final XmppHandler xmppHandler,
        final ModelUtils modelUtils, final InviteQueue inviteQueue) {
        this.model = model;
        this.xmppHandler = xmppHandler;
        this.modelUtils = modelUtils;
        this.inviteQueue = inviteQueue;
    }

    @Override
    public void removeFriend(final String json) {
        final String email = JsonUtils.getValueFromJson("email", json).toLowerCase();
        setFriendStatus(email, Status.rejected);
        
        this.xmppHandler.unsubscribe(email);
        this.xmppHandler.unsubscribed(email);
    }

    @Override
    public void addFriend(String json) {
        final String email = JsonUtils.getValueFromJson("email", json).toLowerCase();
        addFriendByEmail(email);
    }

    private void addFriendByEmail(final String email) {
        setFriendStatus(email, Status.friend);
        
        try {
            //if they have requested a subscription to us, we'll accept it.
            this.xmppHandler.subscribed(email);

            // We also automatically subscribe to them in turn so we know about
            // their presence.
            this.xmppHandler.subscribe(email);
        } catch (final IllegalStateException e) {
            log.error("IllegalStateException while friending (you are probably offline)", e);
            return;
        }
    }

    private void setFriendStatus(final String email, final Status status) {
        final Friends friends = model.getFriends();
        Friend friend = friends.get(email);
{'fraction_non_alphanumeric': 0.0842895889839349, 'fraction_numerical': 0.0014604631754642186, 'mean_word_length': 3.168695652173913, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1879629', 'n_tokens_mistral': 1304, 'n_tokens_neox': 1296, 'n_words': 349}