IZ, RS, Pulled receiving out of user file, refactored receive method to be clearer
This commit is contained in:
parent
08df7e1250
commit
11d31901e4
2 changed files with 95 additions and 73 deletions
|
|
@ -5,12 +5,14 @@
|
|||
|
||||
require 'lib/diaspora/user/friending.rb'
|
||||
require 'lib/diaspora/user/querying.rb'
|
||||
require 'lib/diaspora/user/receiving.rb'
|
||||
require 'lib/salmon/salmon'
|
||||
|
||||
class User
|
||||
include MongoMapper::Document
|
||||
include Diaspora::UserModules::Friending
|
||||
include Diaspora::UserModules::Querying
|
||||
include Diaspora::UserModules::Receiving
|
||||
include Encryptor::Private
|
||||
QUEUE = MessageHandler.new
|
||||
|
||||
|
|
@ -220,79 +222,6 @@ class User
|
|||
end
|
||||
end
|
||||
|
||||
###### Receiving #######
|
||||
def receive_salmon ciphertext
|
||||
cleartext = decrypt( ciphertext)
|
||||
salmon = Salmon::SalmonSlap.parse cleartext
|
||||
if salmon.verified_for_key?(salmon.author.public_key)
|
||||
Rails.logger.info("data in salmon: #{salmon.data}")
|
||||
self.receive(salmon.data)
|
||||
end
|
||||
end
|
||||
|
||||
def receive xml
|
||||
object = Diaspora::Parser.from_xml(xml)
|
||||
Rails.logger.debug("Receiving object for #{self.real_name}:\n#{object.inspect}")
|
||||
Rails.logger.debug("From: #{object.person.inspect}") if object.person
|
||||
|
||||
if object.is_a? Retraction
|
||||
if object.type == 'Person'
|
||||
|
||||
Rails.logger.info( "the person id is #{object.post_id} the friend found is #{visible_person_by_id(object.post_id).inspect}")
|
||||
unfriended_by visible_person_by_id(object.post_id)
|
||||
else
|
||||
object.perform self.id
|
||||
aspects = self.aspects_with_person(object.person)
|
||||
aspects.each{ |aspect| aspect.post_ids.delete(object.post_id.to_id)
|
||||
aspect.save
|
||||
}
|
||||
end
|
||||
elsif object.is_a? Request
|
||||
person = Diaspora::Parser.parse_or_find_person_from_xml( xml )
|
||||
person.serialized_key ||= object.exported_key
|
||||
object.person = person
|
||||
object.person.save
|
||||
old_request = Request.first(:id => object.id)
|
||||
object.aspect_id = old_request.aspect_id if old_request
|
||||
object.save
|
||||
receive_friend_request(object)
|
||||
elsif object.is_a? Profile
|
||||
person = Diaspora::Parser.owner_id_from_xml xml
|
||||
person.profile = object
|
||||
person.save
|
||||
|
||||
elsif object.is_a?(Comment)
|
||||
object.person = Diaspora::Parser.parse_or_find_person_from_xml( xml ).save if object.person.nil?
|
||||
self.visible_people = self.visible_people | [object.person]
|
||||
self.save
|
||||
Rails.logger.debug("The person parsed from comment xml is #{object.person.inspect}") unless object.person.nil?
|
||||
object.person.save
|
||||
Rails.logger.debug("From: #{object.person.inspect}") if object.person
|
||||
raise "In receive for #{self.real_name}, signature was not valid on: #{object.inspect}" unless object.post.person == self.person || object.verify_post_creator_signature
|
||||
object.save
|
||||
unless owns?(object)
|
||||
dispatch_comment object
|
||||
end
|
||||
object.socket_to_uid(id) if (object.respond_to?(:socket_to_uid) && !self.owns?(object))
|
||||
else
|
||||
Rails.logger.debug("Saving object: #{object}")
|
||||
object.user_refs += 1
|
||||
object.save
|
||||
|
||||
self.raw_visible_posts << object
|
||||
self.save
|
||||
|
||||
aspects = self.aspects_with_person(object.person)
|
||||
aspects.each{ |aspect|
|
||||
aspect.posts << object
|
||||
aspect.save
|
||||
object.socket_to_uid(id, :aspect_ids => [aspect.id]) if (object.respond_to?(:socket_to_uid) && !self.owns?(object))
|
||||
}
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
###Helpers############
|
||||
def self.instantiate!( opts = {} )
|
||||
hostname = opts[:url].gsub(/(https?:|www\.)\/\//, '')
|
||||
|
|
|
|||
93
lib/diaspora/user/receiving.rb
Normal file
93
lib/diaspora/user/receiving.rb
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
module Diaspora
|
||||
module UserModules
|
||||
module Receiving
|
||||
def receive_salmon ciphertext
|
||||
cleartext = decrypt( ciphertext)
|
||||
salmon = Salmon::SalmonSlap.parse cleartext
|
||||
if salmon.verified_for_key?(salmon.author.public_key)
|
||||
Rails.logger.info("data in salmon: #{salmon.data}")
|
||||
self.receive(salmon.data)
|
||||
end
|
||||
end
|
||||
|
||||
def receive xml
|
||||
object = Diaspora::Parser.from_xml(xml)
|
||||
Rails.logger.debug("Receiving object for #{self.real_name}:\n#{object.inspect}")
|
||||
Rails.logger.debug("From: #{object.person.inspect}") if object.person
|
||||
|
||||
if object.is_a? Retraction
|
||||
receive_retraction object, xml
|
||||
elsif object.is_a? Request
|
||||
receive_request object, xml
|
||||
elsif object.is_a? Profile
|
||||
receive_profile object, xml
|
||||
elsif object.is_a?(Comment)
|
||||
receive_comment object, xml
|
||||
else
|
||||
receive_post object, xml
|
||||
end
|
||||
end
|
||||
|
||||
def receive_retraction retraction, xml
|
||||
if retraction.type == 'Person'
|
||||
Rails.logger.info( "the person id is #{retraction.post_id} the friend found is #{visible_person_by_id(retraction.post_id).inspect}")
|
||||
unfriended_by visible_person_by_id(retraction.post_id)
|
||||
else
|
||||
retraction.perform self.id
|
||||
aspects = self.aspects_with_person(retraction.person)
|
||||
aspects.each{ |aspect| aspect.post_ids.delete(retraction.post_id.to_id)
|
||||
aspect.save
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def receive_request request, xml
|
||||
person = Diaspora::Parser.parse_or_find_person_from_xml( xml )
|
||||
person.serialized_key ||= request.exported_key
|
||||
request.person = person
|
||||
request.person.save
|
||||
old_request = Request.first(:id => request.id)
|
||||
request.aspect_id = old_request.aspect_id if old_request
|
||||
request.save
|
||||
receive_friend_request(request)
|
||||
end
|
||||
|
||||
def receive_profile profile, xml
|
||||
person = Diaspora::Parser.owner_id_from_xml xml
|
||||
person.profile = profile
|
||||
person.save
|
||||
end
|
||||
|
||||
def receive_comment comment, xml
|
||||
comment.person = Diaspora::Parser.parse_or_find_person_from_xml( xml ).save if comment.person.nil?
|
||||
self.visible_people = self.visible_people | [comment.person]
|
||||
self.save
|
||||
Rails.logger.debug("The person parsed from comment xml is #{comment.person.inspect}") unless comment.person.nil?
|
||||
comment.person.save
|
||||
Rails.logger.debug("From: #{comment.person.inspect}") if comment.person
|
||||
raise "In receive for #{self.real_name}, signature was not valid on: #{comment.inspect}" unless comment.post.person == self.person || comment.verify_post_creator_signature
|
||||
comment.save
|
||||
unless owns?(comment)
|
||||
dispatch_comment comment
|
||||
end
|
||||
comment.socket_to_uid(id) if (comment.respond_to?(:socket_to_uid) && !self.owns?(comment))
|
||||
end
|
||||
|
||||
def receive_post post, xml
|
||||
Rails.logger.debug("Saving post: #{post}")
|
||||
post.user_refs += 1
|
||||
post.save
|
||||
|
||||
self.raw_visible_posts << post
|
||||
self.save
|
||||
|
||||
aspects = self.aspects_with_person(post.person)
|
||||
aspects.each{ |aspect|
|
||||
aspect.posts << post
|
||||
aspect.save
|
||||
post.socket_to_uid(id, :aspect_ids => [aspect.id]) if (post.respond_to?(:socket_to_uid) && !self.owns?(post))
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue