DG MS moved receive methods to their respective models

This commit is contained in:
maxwell 2011-01-10 16:03:14 -08:00
parent 220c6894fa
commit 23de681cb8
12 changed files with 100 additions and 126 deletions

View file

@ -61,6 +61,34 @@ class Comment
p
end
def receive(user, person)
commenter = self.person
unless self.post.person == user.person || self.verify_post_creator_signature
Rails.logger.info("event=receive status=abort reason='comment signature not valid' recipient=#{user.diaspora_handle} sender=#{self.post.person.diaspora_handle} payload_type=#{self.class} post_id=#{self.post_id}")
return
end
user.visible_people = user.visible_people | [commenter]
user.save
commenter.save
#sign comment as the post creator if you've been hit UPSTREAM
if user.owns? self.post
self.post_creator_signature = self.sign_with_key(user.encryption_key)
self.save
end
#dispatch comment DOWNSTREAM, received it via UPSTREAM
unless user.owns?(self)
self.save
user.dispatch_comment(self)
end
self.socket_to_uid(user, :aspect_ids => self.post.aspect_ids)
self
end
#ENCRYPTION
xml_reader :creator_signature

View file

@ -64,18 +64,36 @@ class Post
user.people_in_aspects(user.aspects_with_post(self.id))
end
def receive(postzord)
xml_author = object.diaspora_handle
if (postzord.salmon_author.diaspora_handle != xml_author)
Rails.logger.info("event=receive status=abort reason='author in xml does not match retrieved person' payload_type=#{object.class} recipient=#{self.diaspora_handle} sender=#{salmon_author.diaspora_handle}")
return nil
end
def receive(user, person)
#exists locally, but you dont know about it
#does not exsist locally, and you dont know about it
if postzord.user.contact_for(postzord.salmon_author)
self.person = postzord.salmon_author
#do post receive
end
#exists_locally?
#you know about it, and it is mutable
#you know about it, and it is not mutable
on_pod = Post.find_by_id(self.id)
if on_pod && on_pod.diaspora_handle == self.diaspora_handle
known_post = user.find_visible_post_by_id(self.id)
if known_post
if known_post.mutable?
known_post.update_attributes(self.to_mongo)
else
Rails.logger.info("event=receive payload_type=#{self.class} update=true status=abort sender=#{self.diaspora_handle} reason=immutable existing_post=#{known_post.id}")
end
elsif on_pod == self
user.update_user_refs_and_add_to_aspects(on_pod)
Rails.logger.info("event=receive payload_type=#{self.class} update=true status=complete sender=#{self.diaspora_handle} existing_post=#{on_pod.id}")
self
end
elsif !on_pod
user.update_user_refs_and_add_to_aspects(self)
Rails.logger.info("event=receive payload_type=#{self.class} update=false status=complete sender=#{self.diaspora_handle}")
self
else
Rails.logger.info("event=receive payload_type=#{self.class} update=true status=abort sender=#{self.diaspora_handle} reason='update not from post owner' existing_post=#{self.id}")
end
end
protected

View file

@ -48,6 +48,12 @@ class Profile
user.person_objects(user.contacts.where(:pending => false))
end
def receive(user, person)
person.profile = self
person.save
self
end
def diaspora_handle
#get the parent diaspora handle, unless we want to access a profile without a person
(self._parent_document) ? self.person.diaspora_handle : self[:diaspora_handle]

View file

@ -86,6 +86,11 @@ require File.join(Rails.root, 'lib/postzord/dispatch')
def subscribers(user)
[self.to]
end
def receive(user, person)
Rails.logger.info("event=receive payload_type=request sender=#{self.from} to=#{self.to}")
user.receive_contact_request(self)
end
private

View file

@ -9,7 +9,7 @@ class Retraction
xml_accessor :post_id
xml_accessor :diaspora_handle
xml_accessor :type
attr_accessor :person, :object, :subscribers
def subscribers(user)
@ -17,7 +17,7 @@ class Retraction
@subscribers ||= self.object.subscribers(user)
else
raise 'HAX: you must set the subscribers manaully before unfriending' if @subscribers.nil?
@subscribers
@subscribers
end
end
@ -53,4 +53,22 @@ class Retraction
end
end
end
def receive(user, person)
if self.type == 'Person'
unless self.person.id.to_s == self.post_id.to_s
Rails.logger.info("event=receive status=abort reason='sender is not the person he is trying to retract' recipient=#{self.diaspora_handle} sender=#{self.person.diaspora_handle} payload_type=#{self.class} retraction_type=person")
return
end
user.disconnected_by(user.visible_person_by_id(self.post_id))
else
self.perform(user)
aspects = user.aspects_with_person(self.person)
aspects.each do |aspect|
aspect.post_ids.delete(self.post_id.to_id)
aspect.save
end
end
self
end
end

View file

@ -26,7 +26,6 @@ module Diaspora
#special casey
if object.is_a?(Request)
salmon_author.save
object.sender_handle = salmon_author.diaspora_handle
end
if object.is_a?(Comment)
@ -57,115 +56,11 @@ module Diaspora
end
def receive_object(object,person)
if object.is_a?(Request)
obj = receive_request object, person
elsif object.is_a?(Profile)
obj = receive_profile object, person
elsif object.is_a?(Comment)
obj = receive_comment object
elsif object.is_a?(Retraction)
obj = receive_retraction object
else
obj = receive_post object
end
unless object.is_a? Retraction
Notification.notify(self, object, person)
end
return obj
obj = object.receive(self, person)
Notification.notify(self, object, person) unless object.is_a? Retraction
obj
end
def receive_retraction retraction
if retraction.type == 'Person'
unless retraction.person.id.to_s == retraction.post_id.to_s
Rails.logger.info("event=receive status=abort reason='sender is not the person he is trying to retract' recipient=#{self.diaspora_handle} sender=#{retraction.person.diaspora_handle} payload_type=#{retraction.class} retraction_type=person")
return
end
disconnected_by visible_person_by_id(retraction.post_id)
else
retraction.perform self
aspects = self.aspects_with_person(retraction.person)
aspects.each{ |aspect| aspect.post_ids.delete(retraction.post_id.to_id)
aspect.save
}
end
retraction
end
def receive_request request, person
Rails.logger.info("event=receive payload_type=request sender=#{request.from} to=#{request.to}")
receive_contact_request(request)
end
def receive_profile profile, person
person.profile = profile
person.save
profile
end
def receive_comment comment
commenter = comment.person
unless comment.post.person == self.person || comment.verify_post_creator_signature
Rails.logger.info("event=receive status=abort reason='comment signature not valid' recipient=#{self.diaspora_handle} sender=#{comment.post.person.diaspora_handle} payload_type=#{comment.class} post_id=#{comment.post_id}")
return
end
self.visible_people = self.visible_people | [commenter]
self.save
commenter.save
#sign comment as the post creator if you've been hit UPSTREAM
if owns? comment.post
comment.post_creator_signature = comment.sign_with_key(encryption_key)
comment.save
end
#dispatch comment DOWNSTREAM, received it via UPSTREAM
unless owns?(comment)
comment.save
dispatch_comment comment
end
comment.socket_to_uid(self, :aspect_ids => comment.post.aspect_ids)
comment
end
def exsists_on_pod?(post)
post.class.find_by_id(post.id)
end
def receive_post(post)
#exsists locally, but you dont know about it
#does not exsist locally, and you dont know about it
#exsists_locally?
#you know about it, and it is mutable
#you know about it, and it is not mutable
#
on_pod = exsists_on_pod?(post)
if on_pod && on_pod.diaspora_handle == post.diaspora_handle
known_post = find_visible_post_by_id(post.id)
if known_post
if known_post.mutable?
known_post.update_attributes(post.to_mongo)
else
Rails.logger.info("event=receive payload_type=#{post.class} update=true status=abort sender=#{post.diaspora_handle} reason=immutable existing_post=#{known_post.id}")
end
elsif on_pod == post
update_user_refs_and_add_to_aspects(on_pod)
Rails.logger.info("event=receive payload_type=#{post.class} update=true status=complete sender=#{post.diaspora_handle} existing_post=#{on_pod.id}")
post
end
elsif !on_pod
update_user_refs_and_add_to_aspects(post)
Rails.logger.info("event=receive payload_type=#{post.class} update=false status=complete sender=#{post.diaspora_handle}")
post
else
Rails.logger.info("event=receive payload_type=#{post.class} update=true status=abort sender=#{post.diaspora_handle} reason='update not from post owner' existing_post=#{post.id}")
end
end
def update_user_refs_and_add_to_aspects(post)
Rails.logger.debug("Saving post: #{post}")
post.user_refs += 1

View file

@ -20,7 +20,7 @@ module Diaspora
raise 'you must override subscribers in order to enable federation on this model'
end
def receive(user, salmon_author)
def receive(user, person)
raise 'you must override receive in order to enable federation on this model'
end
end

View file

@ -17,7 +17,7 @@ module Postzord
def perform
if @salmon_author && @salmon.verified_for_key?(@salmon_author.public_key)
@object = Diaspora::Parser.from_xml(@salmon.parsed_data)
@object.receive
else
Rails.logger.info("event=receive status=abort recipient=#{@user.diaspora_handle} sender=#{@salmon.author_email} reason='not_verified for key'")
nil

View file

@ -31,7 +31,6 @@ describe Postzord::Dispatch do
end
it 'sets the @sender_person object' do
zord = Postzord::Dispatch.new(@user, @sm)
zord.instance_variable_get(:@sender_person).should == @user.person
end

View file

@ -56,12 +56,14 @@ describe Postzord::Receiver do
context 'returns the sent object' do
it 'returns the received object on success' do
pending
object = @zord.perform
object.should respond_to(:to_diaspora_xml)
end
end
it 'parses the salmon object' do
pending
Diaspora::Parser.should_receive(:from_xml).with(@salmon.parsed_data)
@zord.perform
end

View file

@ -45,4 +45,7 @@ describe Post do
post.subscribers(@user).should =~ []
end
end
describe '#receive' do
end
end

View file

@ -68,16 +68,16 @@ describe Diaspora::UserModules::Connecting do
@acceptance = @original_request.reverse_for(user2)
end
it 'connects to the acceptor' do
user.receive_request(@acceptance, user2.person)
@acceptance.receive(user, user2.person)
user.contact_for(user2.person).should_not be_nil
end
it 'deletes the original request' do
user.receive_request(@acceptance, user2.person)
@acceptance.receive(user, user2.person)
Request.to(user).all.include?(@original_request).should be_false
Request.find(@original_request.id).should be_nil
end
it 'deletes the acceptance' do
user.receive_request(@acceptance, user2.person)
@acceptance.receive(user, user2.person)
Request.to(user).all.include?(@original_request).should be_false
Request.find(@acceptance.id).should be_nil
end