put receiving in a transaction, to prevent the crazy duplicate index errors and hopefully fix federation visibility.

This commit is contained in:
Maxwell Salzberg 2011-09-03 16:04:53 -07:00
parent fd68915461
commit 26151263c5
2 changed files with 44 additions and 41 deletions

View file

@ -83,33 +83,34 @@ class Post < ActiveRecord::Base
#exists_locally? #exists_locally?
#you know about it, and it is mutable #you know about it, and it is mutable
#you know about it, and it is not mutable #you know about it, and it is not mutable
self.class.transaction do
local_post = Post.where(:guid => self.guid).first local_post = self.class.where(:guid => self.guid).first
if local_post && local_post.author_id == self.author_id if local_post && local_post.author_id == self.author_id
known_post = user.find_visible_post_by_id(self.guid, :key => :guid) known_post = user.find_visible_post_by_id(self.guid, :key => :guid)
if known_post if known_post
if known_post.mutable? if known_post.mutable?
known_post.update_attributes(self.attributes) known_post.update_attributes(self.attributes)
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
else 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}") user.contact_for(person).receive_post(local_post)
user.notify_if_mentioned(local_post)
Rails.logger.info("event=receive payload_type=#{self.class} update=true status=complete sender=#{self.diaspora_handle} existing_post=#{local_post.id}")
return local_post
end
elsif !local_post
if self.save
user.contact_for(person).receive_post(self)
user.notify_if_mentioned(self)
Rails.logger.info("event=receive payload_type=#{self.class} update=false status=complete sender=#{self.diaspora_handle}")
return self
else
Rails.logger.info("event=receive payload_type=#{self.class} update=false status=abort sender=#{self.diaspora_handle} reason=#{self.errors.full_messages}")
end end
else else
user.contact_for(person).receive_post(local_post) 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}")
user.notify_if_mentioned(local_post)
Rails.logger.info("event=receive payload_type=#{self.class} update=true status=complete sender=#{self.diaspora_handle} existing_post=#{local_post.id}")
return local_post
end end
elsif !local_post
if self.save
user.contact_for(person).receive_post(self)
user.notify_if_mentioned(self)
Rails.logger.info("event=receive payload_type=#{self.class} update=false status=complete sender=#{self.diaspora_handle}")
return self
else
Rails.logger.info("event=receive payload_type=#{self.class} update=false status=abort sender=#{self.diaspora_handle} reason=#{self.errors.full_messages}")
end
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
end end

View file

@ -36,28 +36,30 @@ module Diaspora
end end
def receive(user, person) def receive(user, person)
object = self.class.where(:guid => self.guid).first || self self.class.transaction do
object = self.class.where(:guid => self.guid).first || self
unless object.parent.author == user.person || object.verify_parent_author_signature unless object.parent.author == user.person || object.verify_parent_author_signature
Rails.logger.info("event=receive status=abort reason='object signature not valid' recipient=#{user.diaspora_handle} sender=#{self.parent.author.diaspora_handle} payload_type=#{self.class} parent_id=#{self.parent.id}") Rails.logger.info("event=receive status=abort reason='object signature not valid' recipient=#{user.diaspora_handle} sender=#{self.parent.author.diaspora_handle} payload_type=#{self.class} parent_id=#{self.parent.id}")
return return
end end
#sign object as the parent creator if you've been hit UPSTREAM #sign object as the parent creator if you've been hit UPSTREAM
if user.owns? object.parent if user.owns? object.parent
object.parent_author_signature = object.sign_with_key(user.encryption_key) object.parent_author_signature = object.sign_with_key(user.encryption_key)
object.save! object.save!
end end
#dispatch object DOWNSTREAM, received it via UPSTREAM #dispatch object DOWNSTREAM, received it via UPSTREAM
unless user.owns?(object) unless user.owns?(object)
object.save! object.save
Postzord::Dispatch.new(user, object).post Postzord::Dispatch.new(user, object).post
end end
object.socket_to_user(user) if object.respond_to? :socket_to_user object.socket_to_user(user) if object.respond_to? :socket_to_user
if object.after_receive(user, person) if object.after_receive(user, person)
object object
end
end end
end end