Skip to content
Extraits de code Groupes Projets
user.rb 3,1 ko
Newer Older
  • Learn to ignore specific revisions
  • maxwell's avatar
    maxwell a validé
    class User < Person
    
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable
    
    maxwell's avatar
    maxwell a validé
             
    
    Raphael's avatar
    Raphael a validé
      before_validation_on_create :assign_key
    
      validates_presence_of :profile
      
      before_validation :do_bad_things
    
      ######## Posting ########
    
      def post(class_name, options = {})
        options[:person] = self
        model_class = class_name.to_s.camelize.constantize
        post = model_class.instantiate(options)
      end
    
      def comment(text, options = {})
    
        raise "must comment on something!" unless options[:on]
    
    maxwell's avatar
    maxwell a validé
        c = Comment.new(:person_id => self.id, :text => text, :post => options[:on])
        if c.save
          if mine?(c.post)
    
            c.push_to(c.post.people_with_permissions)  # should return plucky query
    
    maxwell's avatar
    maxwell a validé
          else
            c.push_to([c.post.person])
          end
          true
        end
        false
    
      
      ##profile
      def update_profile(params)
        if self.update_attributes(params)
          puts self.profile.class
          self.profile.notify_people!
          true
        else
          false
        end
      end
    
    
      ######### Friend Requesting
      def send_friend_request_to(friend_url)
    
        unless Person.where(:url => friend_url).first
    
          p = Request.instantiate(:to => friend_url, :from => self)
          if p.save
            p.push_to_url friend_url
    
    maxwell's avatar
    maxwell a validé
      end 
    
    
      def accept_friend_request(friend_request_id)
        request = Request.where(:id => friend_request_id).first
        request.activate_friend
        request.person = self
    
        request.exported_key = self.export_key
    
    maxwell's avatar
    maxwell a validé
        request.destination_url = request.callback_url
    
        request.push_to_url(request.callback_url)
    
      def ignore_friend_request(friend_request_id)
    
    maxwell's avatar
    maxwell a validé
        request = Request.first(:id => friend_request_id)
    
        person = request.person
        person.destroy unless person.active
        request.destroy
      end
    
    
    Raphael's avatar
    Raphael a validé
        Rails.logger.info("receiving friend request #{friend_request.to_json}")
    
        friend_request.person.serialized_key = friend_request.exported_key
    
    maxwell's avatar
    maxwell a validé
        if Request.where(:callback_url => friend_request.callback_url).first
          friend_request.activate_friend
    
    maxwell's avatar
    maxwell a validé
      def unfriend(friend_id)
        bad_friend  = Person.first(:id => friend_id, :active => true)
        if bad_friend 
           Retraction.for(self).push_to_url(bad_friend.url) 
           bad_friend.destroy
        end
      end
    
    
    maxwell's avatar
    maxwell a validé
      def send_request(rel_hash)
        if rel_hash[:friend]
          self.send_friend_request_to(rel_hash[:friend])
    
    maxwell's avatar
    maxwell a validé
      def mine?(post)
        self == post.person
      end
    
    
      def terse_url
        terse= self.url.gsub(/https?:\/\//, '')
        terse.gsub!(/www\./, '')
        terse = terse.chop! if terse[-1, 1] == '/'
        terse
      end
    
     
      def do_bad_things
        self.password_confirmation = self.password
      end
      
    
        self.serialized_key ||= generate_key.export
    
        OpenSSL::PKey::RSA::generate 1024 
    
    ilya's avatar
    ilya a validé
      end