<commit_before>require 'active_support/concern'

module Concerns
  module Person
    module Friend
      extend ActiveSupport::Concern

      included do
        has_many :friendships
        has_many :friends, -> { order('people.last_name', 'people.first_name') }, class_name: 'Person', through: :friendships
        has_many :friendship_requests
        has_many :pending_friendship_requests, -> { where(rejected: false) }, class_name: 'FriendshipRequest'
        after_destroy :destroy_friendships
      end

      def request_friendship_with(person)
        if person.friendship_waiting_on?(self)
          # already requested by other person
          self.friendships.create! friend: person
          self.friendship_requests.where(from_id: person.id).first.destroy
          I18n.t('friends.added_as_friend', name: person.name)
        elsif self.can_request_friendship_with?(person)
          # clean up past rejections
          FriendshipRequest.delete_all ['person_id = ? and from_id = ? and rejected = ?', self.id, person.id, true]
          person.friendship_requests.create!(from: self)
          I18n.t('friends.request_sent', name: person.name)
        elsif self.friendship_waiting_on?(person)
          I18n.t('friends.already_pending', name: person.name)
        elsif self.friendship_rejected_by?(person)
          I18n.t('friends.cannot_request', name: person.name)
        else
          raise I18n.t('friends.unknown_state')
        end
      end

      def can_request_friendship_with?(person)
        Setting.get(:features, :friends) and
        person != self and
        person.family_id != self.family_id and
        !friend?(person) and
        full_access? and
        person.full_access? and
        person.email.present? and
        person.friends_enabled and
        !friendship_rejected_by?(person) and
        !friendship_waiting_on?(person)
      end

      def friendship_rejected_by?(person)
        person.friendship_requests.where(from_id: id, rejected: true).count > 0
      end

      def friendship_waiting_on?(person)
        person.friendship_requests.where(from_id: id, rejected: false).count > 0
      end

      def friend?(person)
        friends.where('friendships.friend_id' => person.id).count > 0
      end

      def destroy_friendships
        friendships.destroy_all
        friendship_requests.destroy_all
      end
    end
  end<commit_msg>Simplify friendship code just a bit<commit_after>require 'active_support/concern'

module Concerns
  module Person
    module Friend
      extend ActiveSupport::Concern

      included do
        has_many :friendships
        has_many :friends, -> { order('people.last_name', 'people.first_name') }, class_name: 'Person', through: :friendships
        has_many :friendship_requests
        has_many :pending_friendship_requests, -> { where(rejected: false) }, class_name: 'FriendshipRequest'
        after_destroy :destroy_friendships
      end

      def request_friendship_with(person)
        if pending = self.pending_friendship_requests.where(from_id: person.id).first
          pending.accept
          I18n.t('friends.added_as_friend', name: person.name)
        elsif self.can_request_friendship_with?(person)
          friendship_requests.where(from_id: person.id, rejected: true).delete_all
          person.friendship_requests.create!(from: self)
          I18n.t('friends.request_sent', name: person.name)
        elsif self.friendship_waiting_on?(person)
          I18n.t('friends.already_pending', name: person.name)
        elsif self.friendship_rejected_by?(person)
          I18n.t('friends.cannot_request', name: person.name)
        else
          raise I18n.t('friends.unknown_state')
        end
      end

      def can_request_friendship_with?(person)
        Setting.get(:features, :friends) and
        person != self and
        person.family_id != self.family_id and
        !friend?(person) and
        full_access? and
        person.full_access? and
        person.email.present? and
        person.friends_enabled and
        !friendship_rejected_by?(person) and
        !friendship_waiting_on?(person)
      end

      def friendship_rejected_by?(person)
        person.friendship_requests.where(from_id: id, rejected: true).any?
      end

      def friendship_waiting_on?(person)
        person.friendship_requests.where(from_id: id, rejected: false).any?
      end

      def friend?(person)
        friends.where('friendships.friend_id' => person.id).count > 0
      end

      def destroy_friendships
        friendships.destroy_all
        friendship_requests.destroy_all
      end
    end
  end
{'fraction_non_alphanumeric': 0.08961347441157418, 'fraction_numerical': 0.005182465990066941, 'mean_word_length': 2.7781402936378465, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '912748', 'n_tokens_mistral': 1382, 'n_tokens_neox': 1351, 'n_words': 264}