diff --git a/app/models/comment.rb b/app/models/comment.rb index 7214f7b7b..41b8b8fd9 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -25,9 +25,9 @@ class Comment < ActiveRecord::Base belongs_to :post belongs_to :author, :class_name => 'Person' - + validates :text, :presence => true, :length => { :maximum => 2500 } - validates :post, :presence => true + validates :parent, :presence => true #should be in relayable (pending on fixing Message) serialize :youtube_titles, Hash @@ -56,23 +56,15 @@ class Comment < ActiveRecord::Base end def notification_type(user, person) - if user.owns?(self.post) + if self.post.author == user.person return Notifications::CommentOnPost - elsif user_has_commented_on_others_post?(person, self.post, user) + elsif self.post.comments.where(:author_id => user.person.id) != [] && self.author_id != user.person.id return Notifications::AlsoCommented else return false end end - def user_has_commented_on_others_post?(author, post, user) - Comment.comments_by_author_on_post_exist?(author, post.id) && self.author_id != user.person.id - end - - def self.comments_by_author_on_post_exist?(author, post_id) - Comment.exists?(:author_id => author.id, :post_id => post_id) - end - def parent_class Post end diff --git a/app/models/like.rb b/app/models/like.rb index 21f9f6194..029d96a08 100644 --- a/app/models/like.rb +++ b/app/models/like.rb @@ -20,8 +20,7 @@ class Like < ActiveRecord::Base belongs_to :author, :class_name => 'Person' validates_uniqueness_of :target_id, :scope => [:target_type, :author_id] - validates :author, :presence => true - validates :target, :presence => true + validates :parent, :presence => true #should be in relayable (pending on fixing Message) after_create do self.parent.update_likes_counter diff --git a/app/models/message.rb b/app/models/message.rb index 16cde59a2..c8741736a 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -13,23 +13,22 @@ class Message < ActiveRecord::Base belongs_to :author, :class_name => 'Person' belongs_to :conversation, :touch => true - + validates :text, :presence => true + validate :participant_of_parent_conversation after_create do #sign comment as commenter self.author_signature = self.sign_with_key(self.author.owner.encryption_key) if self.author.owner - if !self.parent.blank? && self.author.owns?(self.parent) + if self.author.owns?(self.parent) #sign comment as post owner - self.parent_author_signature = self.sign_with_key( self.parent.author.owner.encryption_key) if self.parent.author.owner + self.parent_author_signature = self.sign_with_key(self.parent.author.owner.encryption_key) if self.parent.author.owner end self.save! self end - validate :participant_of_parent_conversation - def diaspora_handle self.author.diaspora_handle end diff --git a/app/models/person.rb b/app/models/person.rb index 6abdd14fc..a36784532 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -158,7 +158,7 @@ class Person < ActiveRecord::Base end def owns?(obj) - self == obj.author + self.id == obj.author_id end def url diff --git a/lib/diaspora/relayable.rb b/lib/diaspora/relayable.rb index a1f51038e..08ad4a8bd 100644 --- a/lib/diaspora/relayable.rb +++ b/lib/diaspora/relayable.rb @@ -12,6 +12,9 @@ module Diaspora xml_attr :parent_guid xml_attr :parent_author_signature xml_attr :author_signature + + validates_associated :parent + validates :author, :presence => true end end diff --git a/lib/postzord/receiver/public.rb b/lib/postzord/receiver/public.rb index 55ef99092..abc7a37de 100644 --- a/lib/postzord/receiver/public.rb +++ b/lib/postzord/receiver/public.rb @@ -30,10 +30,6 @@ module Postzord end def receive_relayable - # unless @object.parent.present? - # raise RelayableObjectWithoutParent.new("Receiving a relayable object without parent object present locally!") - # end - if @object.parent.author.local? # receive relayable object only for the owner of the parent object @object.receive(@object.parent.author.user, @author) @@ -50,7 +46,6 @@ module Postzord @object.save! end - # @return [Array] User ids def recipient_user_ids User.all_sharing_with_person(@author).select('users.id').map!{ |u| u.id } diff --git a/spec/models/message_spec.rb b/spec/models/message_spec.rb index 00aacfb10..c5a5b653e 100644 --- a/spec/models/message_spec.rb +++ b/spec/models/message_spec.rb @@ -17,7 +17,7 @@ describe Message do :messages_attributes => [ {:author => @user1.person, :text => 'stuff'} ] } - @cnv = Conversation.create(@create_hash) + @cnv = Conversation.create!(@create_hash) @message = @cnv.messages.first @xml = @message.to_diaspora_xml end diff --git a/spec/shared_behaviors/relayable.rb b/spec/shared_behaviors/relayable.rb index 36b7873de..16e14aade 100644 --- a/spec/shared_behaviors/relayable.rb +++ b/spec/shared_behaviors/relayable.rb @@ -6,6 +6,12 @@ require 'spec_helper' describe Diaspora::Relayable do shared_examples_for "it is relayable" do + + context 'validation' do + it 'ensures an valid associated parent' + it 'ensures the presence of an author' + end + context 'encryption' do describe '#parent_author_signature' do it 'should sign the object if the user is the post author' do