minor cleanup in models
This commit is contained in:
parent
1d92d11841
commit
7e5ace7cbc
7 changed files with 38 additions and 61 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -20,10 +20,6 @@ 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) }
|
||||
|
|
@ -38,18 +34,11 @@ class Request
|
|||
:group_id => options[:into])
|
||||
end
|
||||
|
||||
def reverse accepting_user
|
||||
def reverse_for accepting_user
|
||||
self.person = accepting_user.person
|
||||
self.exported_key = accepting_user.export_key
|
||||
self.destination_url = self.callback_url
|
||||
save
|
||||
end
|
||||
|
||||
def set_pending_friend
|
||||
p = Person.first(:id => self.person.id)
|
||||
|
||||
self.person.save #save pending friend
|
||||
|
||||
self.save
|
||||
end
|
||||
|
||||
#ENCRYPTION
|
||||
|
|
@ -62,6 +51,7 @@ class Request
|
|||
def signable_accessors
|
||||
accessors = self.class.roxml_attrs.collect{|definition|
|
||||
definition.accessor}
|
||||
|
||||
accessors.delete 'person'
|
||||
accessors.delete 'creator_signature'
|
||||
accessors
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
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
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue