From 7e5ace7cbcfd4da0211acfdb9047fded73879303 Mon Sep 17 00:00:00 2001 From: danielvincent Date: Mon, 16 Aug 2010 22:44:22 -0700 Subject: [PATCH] minor cleanup in models --- app/models/post.rb | 4 ---- app/models/request.rb | 34 +++++++++++--------------------- app/models/retraction.rb | 15 ++++++-------- app/models/user.rb | 30 ++++++++++++++++------------ spec/lib/diaspora_parser_spec.rb | 4 ++-- spec/models/post_spec.rb | 10 ---------- spec/models/user_spec.rb | 2 +- 7 files changed, 38 insertions(+), 61 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 2b2449e66..e1821d615 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -30,10 +30,6 @@ class Post end #Querying - def self.stream - Post.sort(:created_at.desc).all - end - def self.newest_for(person) self.first(:person_id => person.id, :order => '_id desc') end diff --git a/app/models/request.rb b/app/models/request.rb index 99625ad13..af57a9187 100644 --- a/app/models/request.rb +++ b/app/models/request.rb @@ -20,38 +20,27 @@ class Request belongs_to :person validates_presence_of :destination_url, :callback_url - - #validates_format_of :destination_url, :with => - #/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix - before_validation :clean_link - scope :for_user, lambda{ |user| where(:destination_url => user.receive_url) } + scope :for_user, lambda{ |user| where(:destination_url => user.receive_url) } scope :from_user, lambda{ |user| where(:destination_url.ne => user.receive_url) } def self.instantiate(options = {}) person = options[:from] self.new(:destination_url => options[:to], - :callback_url => person.receive_url, - :person => person, - :exported_key => person.export_key, - :group_id => options[:into]) + :callback_url => person.receive_url, + :person => person, + :exported_key => person.export_key, + :group_id => options[:into]) end - def reverse accepting_user - self.person = accepting_user.person - self.exported_key = accepting_user.export_key + def reverse_for accepting_user + self.person = accepting_user.person + self.exported_key = accepting_user.export_key self.destination_url = self.callback_url - save + self.save end - def set_pending_friend - p = Person.first(:id => self.person.id) - - self.person.save #save pending friend - - end - #ENCRYPTION #before_validation :sign_if_mine #validates_true_for :creator_signature, :logic => lambda {self.verify_creator_signature} @@ -61,7 +50,8 @@ class Request def signable_accessors accessors = self.class.roxml_attrs.collect{|definition| - definition.accessor} + definition.accessor} + accessors.delete 'person' accessors.delete 'creator_signature' accessors @@ -69,7 +59,7 @@ class Request def signable_string signable_accessors.collect{|accessor| - (self.send accessor.to_sym).to_s}.join ';' + (self.send accessor.to_sym).to_s}.join ';' end protected diff --git a/app/models/retraction.rb b/app/models/retraction.rb index 123a27a4e..e1f911d2d 100644 --- a/app/models/retraction.rb +++ b/app/models/retraction.rb @@ -38,24 +38,21 @@ class Retraction end def signature_valid? - target = self.type.constantize.first(:id => self.post_id) + target = self.type.constantize.find_by_id(self.post_id) + if target.is_a? Person - verify_signature(@creator_signature, self.type.constantize.first(:id => self.post_id)) + verify_signature(@creator_signature, self.type.constantize.find_by_id(self.post_id)) else - verify_signature(@creator_signature, self.type.constantize.first(:id => self.post_id).person) + verify_signature(@creator_signature, self.type.constantize.find_by_id(self.post_id).person) end end def self.person_id_from(object) - if object.is_a? Person - object.id - else - object.person.id - end + object.is_a?(Person) ? object.id : object.person.id end def person - Person.first(:id => self.person_id) + Person.find_by_id(self.person_id) end #ENCRYPTION diff --git a/app/models/user.rb b/app/models/user.rb index 4e07abb2a..a9b4a0b30 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -20,7 +20,6 @@ class User before_validation :do_bad_things ######## Making things work ######## - key :email, String def method_missing(method, *args) @@ -33,20 +32,19 @@ class User end ######### Groups ###################### - def group( opts = {} ) opts[:user] = self Group.create(opts) end ######### Posts and Such ############### - def retract( post ) retraction = Retraction.for(post) retraction.creator_signature = retraction.sign_with_key( encryption_key ) retraction.notify_people retraction end + ######### Friend Requesting ########### def send_friend_request_to(friend_url, group_id) unless self.friends.detect{ |x| x.receive_url == friend_url} @@ -67,12 +65,12 @@ class User end def accept_friend_request(friend_request_id, group_id) - request = Request.where(:id => friend_request_id).first - n = pending_requests.delete(request) + request = Request.find_by_id(friend_request_id) + pending_requests.delete(request) activate_friend(request.person, group_by_id(group_id)) - request.reverse self + request.reverse_for(self) request end @@ -86,28 +84,34 @@ class User end def ignore_friend_request(friend_request_id) - request = Request.first(:id => friend_request_id) - person = request.person + request = Request.find_by_id(friend_request_id) + person = request.person + person.user_refs -= 1 - pending_requests.delete(request) - save + + self.pending_requests.delete(request) + self.save + (person.user_refs > 0 || person.owner.nil? == false) ? person.save : person.destroy request.destroy end def receive_friend_request(friend_request) Rails.logger.info("receiving friend request #{friend_request.to_json}") + if request_from_me?(friend_request) group = self.group_by_id(friend_request.group_id) activate_friend(friend_request.person, group) Rails.logger.info("#{self.real_name}'s friend request has been accepted") + friend_request.destroy else + friend_request.person.user_refs += 1 friend_request.person.save - pending_requests << friend_request - save + self.pending_requests << friend_request + self.save Rails.logger.info("#{self.real_name} has received a friend request") friend_request.save end @@ -210,7 +214,7 @@ class User elsif object.verify_creator_signature == true Rails.logger.debug("Saving object: #{object}") object.save - object.socket_to_uid( id) if (object.respond_to?(:socket_to_uid) && !self.owns?(object)) + object.socket_to_uid(id) if (object.respond_to?(:socket_to_uid) && !self.owns?(object)) end end diff --git a/spec/lib/diaspora_parser_spec.rb b/spec/lib/diaspora_parser_spec.rb index d0065cfb8..38be7104d 100644 --- a/spec/lib/diaspora_parser_spec.rb +++ b/spec/lib/diaspora_parser_spec.rb @@ -110,7 +110,7 @@ describe Diaspora::Parser do it "should activate the Person if I initiated a request to that url" do request = @user.send_friend_request_to( @user2.receive_url, @group.id) - request.reverse @user2 + request.reverse_for @user2 xml = request.to_diaspora_xml @@ -131,7 +131,7 @@ describe Diaspora::Parser do it 'should process retraction for a person' do person_count = Person.all.count request = @user.send_friend_request_to( @user2.receive_url, @group.id) - request.reverse @user2 + request.reverse_for @user2 xml = request.to_diaspora_xml retraction = Retraction.for(@user2) diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index a20ffb504..12c84ccfe 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -41,16 +41,6 @@ describe Post do Factory.create(:bookmark, :title => "Google", :link => "http://google.com", :created_at => Time.now+5, :person => @person_two) end - it "should list child types in reverse chronological order" do - stream = Post.stream - stream.count.should == 5 - stream[0].class.should == Bookmark - stream[1].class.should == Blog - stream[2].class.should == StatusMessage - stream[3].class.should == Bookmark - stream[4].class.should == StatusMessage - end - it "should get all posts for a specified user" do person_posts = @person_one.posts person_posts.count.should == 1 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index fc1480ea7..e0d2415c7 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -238,7 +238,7 @@ describe User do @group2 = @user2.group(:name => "Gross people") request = @user.send_friend_request_to( @user2.receive_url, @group.id) - request.reverse @user2 + request.reverse_for @user2 @user2.activate_friend(@user.person, @group2) @user.receive request.to_diaspora_xml end