minor cleanup in models

This commit is contained in:
danielvincent 2010-08-16 22:44:22 -07:00
parent 1d92d11841
commit 7e5ace7cbc
7 changed files with 38 additions and 61 deletions

View file

@ -30,10 +30,6 @@ class Post
end end
#Querying #Querying
def self.stream
Post.sort(:created_at.desc).all
end
def self.newest_for(person) def self.newest_for(person)
self.first(:person_id => person.id, :order => '_id desc') self.first(:person_id => person.id, :order => '_id desc')
end end

View file

@ -20,38 +20,27 @@ class Request
belongs_to :person belongs_to :person
validates_presence_of :destination_url, :callback_url 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 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) } scope :from_user, lambda{ |user| where(:destination_url.ne => user.receive_url) }
def self.instantiate(options = {}) def self.instantiate(options = {})
person = options[:from] person = options[:from]
self.new(:destination_url => options[:to], self.new(:destination_url => options[:to],
:callback_url => person.receive_url, :callback_url => person.receive_url,
:person => person, :person => person,
:exported_key => person.export_key, :exported_key => person.export_key,
:group_id => options[:into]) :group_id => options[:into])
end end
def reverse accepting_user def reverse_for accepting_user
self.person = accepting_user.person self.person = accepting_user.person
self.exported_key = accepting_user.export_key self.exported_key = accepting_user.export_key
self.destination_url = self.callback_url self.destination_url = self.callback_url
save self.save
end end
def set_pending_friend
p = Person.first(:id => self.person.id)
self.person.save #save pending friend
end
#ENCRYPTION #ENCRYPTION
#before_validation :sign_if_mine #before_validation :sign_if_mine
#validates_true_for :creator_signature, :logic => lambda {self.verify_creator_signature} #validates_true_for :creator_signature, :logic => lambda {self.verify_creator_signature}
@ -61,7 +50,8 @@ class Request
def signable_accessors def signable_accessors
accessors = self.class.roxml_attrs.collect{|definition| accessors = self.class.roxml_attrs.collect{|definition|
definition.accessor} definition.accessor}
accessors.delete 'person' accessors.delete 'person'
accessors.delete 'creator_signature' accessors.delete 'creator_signature'
accessors accessors
@ -69,7 +59,7 @@ class Request
def signable_string def signable_string
signable_accessors.collect{|accessor| signable_accessors.collect{|accessor|
(self.send accessor.to_sym).to_s}.join ';' (self.send accessor.to_sym).to_s}.join ';'
end end
protected protected

View file

@ -38,24 +38,21 @@ class Retraction
end end
def signature_valid? 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 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 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
end end
def self.person_id_from(object) def self.person_id_from(object)
if object.is_a? Person object.is_a?(Person) ? object.id : object.person.id
object.id
else
object.person.id
end
end end
def person def person
Person.first(:id => self.person_id) Person.find_by_id(self.person_id)
end end
#ENCRYPTION #ENCRYPTION

View file

@ -20,7 +20,6 @@ class User
before_validation :do_bad_things before_validation :do_bad_things
######## Making things work ######## ######## Making things work ########
key :email, String key :email, String
def method_missing(method, *args) def method_missing(method, *args)
@ -33,20 +32,19 @@ class User
end end
######### Groups ###################### ######### Groups ######################
def group( opts = {} ) def group( opts = {} )
opts[:user] = self opts[:user] = self
Group.create(opts) Group.create(opts)
end end
######### Posts and Such ############### ######### Posts and Such ###############
def retract( post ) def retract( post )
retraction = Retraction.for(post) retraction = Retraction.for(post)
retraction.creator_signature = retraction.sign_with_key( encryption_key ) retraction.creator_signature = retraction.sign_with_key( encryption_key )
retraction.notify_people retraction.notify_people
retraction retraction
end end
######### Friend Requesting ########### ######### Friend Requesting ###########
def send_friend_request_to(friend_url, group_id) def send_friend_request_to(friend_url, group_id)
unless self.friends.detect{ |x| x.receive_url == friend_url} unless self.friends.detect{ |x| x.receive_url == friend_url}
@ -67,12 +65,12 @@ class User
end end
def accept_friend_request(friend_request_id, group_id) def accept_friend_request(friend_request_id, group_id)
request = Request.where(:id => friend_request_id).first request = Request.find_by_id(friend_request_id)
n = pending_requests.delete(request) pending_requests.delete(request)
activate_friend(request.person, group_by_id(group_id)) activate_friend(request.person, group_by_id(group_id))
request.reverse self request.reverse_for(self)
request request
end end
@ -86,28 +84,34 @@ class User
end end
def ignore_friend_request(friend_request_id) def ignore_friend_request(friend_request_id)
request = Request.first(:id => friend_request_id) request = Request.find_by_id(friend_request_id)
person = request.person person = request.person
person.user_refs -= 1 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 (person.user_refs > 0 || person.owner.nil? == false) ? person.save : person.destroy
request.destroy request.destroy
end end
def receive_friend_request(friend_request) def receive_friend_request(friend_request)
Rails.logger.info("receiving friend request #{friend_request.to_json}") Rails.logger.info("receiving friend request #{friend_request.to_json}")
if request_from_me?(friend_request) if request_from_me?(friend_request)
group = self.group_by_id(friend_request.group_id) group = self.group_by_id(friend_request.group_id)
activate_friend(friend_request.person, group) activate_friend(friend_request.person, group)
Rails.logger.info("#{self.real_name}'s friend request has been accepted") Rails.logger.info("#{self.real_name}'s friend request has been accepted")
friend_request.destroy friend_request.destroy
else else
friend_request.person.user_refs += 1 friend_request.person.user_refs += 1
friend_request.person.save friend_request.person.save
pending_requests << friend_request self.pending_requests << friend_request
save self.save
Rails.logger.info("#{self.real_name} has received a friend request") Rails.logger.info("#{self.real_name} has received a friend request")
friend_request.save friend_request.save
end end
@ -210,7 +214,7 @@ class User
elsif object.verify_creator_signature == true elsif object.verify_creator_signature == true
Rails.logger.debug("Saving object: #{object}") Rails.logger.debug("Saving object: #{object}")
object.save 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
end end

View file

@ -110,7 +110,7 @@ describe Diaspora::Parser do
it "should activate the Person if I initiated a request to that url" 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 = @user.send_friend_request_to( @user2.receive_url, @group.id)
request.reverse @user2 request.reverse_for @user2
xml = request.to_diaspora_xml xml = request.to_diaspora_xml
@ -131,7 +131,7 @@ describe Diaspora::Parser do
it 'should process retraction for a person' do it 'should process retraction for a person' do
person_count = Person.all.count person_count = Person.all.count
request = @user.send_friend_request_to( @user2.receive_url, @group.id) request = @user.send_friend_request_to( @user2.receive_url, @group.id)
request.reverse @user2 request.reverse_for @user2
xml = request.to_diaspora_xml xml = request.to_diaspora_xml
retraction = Retraction.for(@user2) retraction = Retraction.for(@user2)

View file

@ -41,16 +41,6 @@ describe Post do
Factory.create(:bookmark, :title => "Google", :link => "http://google.com", :created_at => Time.now+5, :person => @person_two) Factory.create(:bookmark, :title => "Google", :link => "http://google.com", :created_at => Time.now+5, :person => @person_two)
end 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 it "should get all posts for a specified user" do
person_posts = @person_one.posts person_posts = @person_one.posts
person_posts.count.should == 1 person_posts.count.should == 1

View file

@ -238,7 +238,7 @@ describe User do
@group2 = @user2.group(:name => "Gross people") @group2 = @user2.group(:name => "Gross people")
request = @user.send_friend_request_to( @user2.receive_url, @group.id) request = @user.send_friend_request_to( @user2.receive_url, @group.id)
request.reverse @user2 request.reverse_for @user2
@user2.activate_friend(@user.person, @group2) @user2.activate_friend(@user.person, @group2)
@user.receive request.to_diaspora_xml @user.receive request.to_diaspora_xml
end end