Welcome to mirror list, hosted at ThFree Co, Russian Federation.

connecting.rb « user « models « app - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 232dba405f975b2f5f353594897a3606214cf534 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true

#   Copyright (c) 2010-2011, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

class User
  module Connecting
    # This will create a contact on the side of the sharer and the sharee.
    # @param [Person] person The person to start sharing with.
    # @param [Aspect] aspect The aspect to add them to.
    # @return [Contact] The newly made contact for the passed in person.
    def share_with(person, aspect)
      return if blocks.where(person_id: person.id).exists?

      contact = contacts.find_or_initialize_by(person_id: person.id)
      return false unless contact.valid?

      needs_dispatch = !contact.receiving?
      contact.receiving = true
      contact.aspects << aspect
      contact.save

      if needs_dispatch
        Diaspora::Federation::Dispatcher.defer_dispatch(self, contact)
        deliver_profile_update(subscriber_ids: [person.id]) unless person.local?
      end

      Notifications::StartedSharing.where(recipient_id: id, target: person.id, unread: true)
                                   .update_all(unread: false)

      contact
    end

    def disconnect(contact)
      logger.info "event=disconnect user=#{diaspora_handle} target=#{contact.person.diaspora_handle}"

      if contact.person.local?
        raise "FATAL: user entry is missing from the DB. Aborting" if contact.person.owner.nil?
        contact.person.owner.disconnected_by(contact.user.person)
      else
        ContactRetraction.for(contact).defer_dispatch(self)
      end

      contact.aspect_memberships.delete_all

      disconnect_contact(contact, direction: :receiving, destroy: !contact.sharing)
    end

    def disconnected_by(person)
      logger.info "event=disconnected_by user=#{diaspora_handle} target=#{person.diaspora_handle}"
      contact_for(person).try {|contact| disconnect_contact(contact, direction: :sharing, destroy: !contact.receiving) }
    end

    private

    def disconnect_contact(contact, direction:, destroy:)
      if destroy
        contact.destroy
      else
        contact.update_attributes(direction => false)
      end
    end
  end
end