Module: User::Connecting

Included in:
User
Defined in:
app/models/user/connecting.rb

Overview

Copyright © 2010-2011, Diaspora Inc. This file is

licensed under the Affero General Public License version 3 or later.  See
the COPYRIGHT file.

Instance Method Summary collapse

Instance Method Details

#disconnect(bad_contact, opts = {}) ⇒ Object


56
57
58
59
60
61
62
63
64
65
# File 'app/models/user/connecting.rb', line 56

def disconnect(bad_contact, opts={})
  person = bad_contact.person
  Rails.logger.info("event=disconnect user=#{diaspora_handle} target=#{person.diaspora_handle}")
  retraction = Retraction.for(self)
  retraction.subscribers = [person]#HAX
  Postzord::Dispatcher.build(self, retraction).post

  AspectMembership.where(:contact_id => bad_contact.id).delete_all
  remove_contact(bad_contact, opts)
end

#disconnected_by(person) ⇒ Object


67
68
69
70
71
72
# File 'app/models/user/connecting.rb', line 67

def disconnected_by(person)
  Rails.logger.info("event=disconnected_by user=#{diaspora_handle} target=#{person.diaspora_handle}")
  if contact = self.contact_for(person)
    remove_contact(contact, :retracted => true)
  end
end

#register_share_visibilities(contact) ⇒ void

This method returns an undefined value.

This puts the last 100 public posts by the passed in contact into the user's stream.


34
35
36
37
38
39
40
41
42
# File 'app/models/user/connecting.rb', line 34

def register_share_visibilities(contact)
  #should have select here, but proven hard to test
  posts = Post.where(:author_id => contact.person_id, :public => true).limit(100)
  p = posts.map do |post|
    ShareVisibility.new(:contact_id => contact.id, :shareable_id => post.id, :shareable_type => 'Post')
  end
  ShareVisibility.import(p) unless posts.empty?
  nil
end

#remove_contact(contact, opts = {:force => false, :retracted => false}) ⇒ Object


44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/user/connecting.rb', line 44

def remove_contact(contact, opts={:force => false, :retracted => false})
  posts = contact.posts.all

  if !contact.mutual? || opts[:force]
    contact.destroy
  elsif opts[:retracted]
    contact.update_attributes(:sharing => false)
  else
    contact.update_attributes(:receiving => false)
  end
end

#share_with(person, aspect) ⇒ Contact

This will create a contact on the side of the sharer and the sharee.


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/user/connecting.rb', line 10

def share_with(person, aspect)
  contact = self.contacts.find_or_initialize_by_person_id(person.id)
  return false unless contact.valid?

  unless contact.receiving?
    contact.dispatch_request
    contact.receiving = true
  end

  contact.aspects << aspect
  contact.save

  if notification = Notification.where(:target_id => person.id).first
    notification.update_attributes(:unread=>false)
  end
  
  deliver_profile_update
  register_share_visibilities(contact)
  contact
end