Skip to content
Extraits de code Groupes Projets
connecting.rb 2,53 ko
Newer Older
  • Learn to ignore specific revisions
  • #   Copyright (c) 2010, Diaspora Inc.  This file is
    #   licensed under the Affero General Public License version 3 or later.  See
    #   the COPYRIGHT file.
    
    module Diaspora
      module UserModules
        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)
            contact = self.contacts.find_or_initialize_by_person_id(person.id)
    
            unless contact.receiving?
    
              contact.dispatch_request
    
            contact.aspects << aspect
            contact.save
    
    danielgrippi's avatar
    danielgrippi a validé
    
            if notification = Notification.where(:target_id => person.id).first
              notification.update_attributes(:unread=>false)
            end
    
    Raphael Sofaer's avatar
    Raphael Sofaer a validé
    
    
    Raphael Sofaer's avatar
    Raphael Sofaer a validé
          # This puts the last 100 public posts by the passed in contact into the user's stream.
          # @param [Contact] contact
          # @return [void]
    
          def register_post_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|
    
              PostVisibility.new(:contact_id => contact.id, :post_id => post.id)
            end
    
            PostVisibility.import(p) unless posts.empty?
    
          def remove_contact(contact, opts={:force => false})
            posts = contact.posts.all
    
            if !contact.mutual? || opts[:force]
    
              contact.update_attributes(:receiving => false)
    
            posts.each do |p|
              if p.user_refs < 1
                p.destroy
    
          def disconnect(bad_contact)
    
            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.new(self, retraction).post
    
            AspectMembership.where(:contact_id => bad_contact.id).delete_all
    
          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)
            end