Refactor Relayable Creation
This commit is contained in:
parent
8ae6303a91
commit
b0e81af1fa
9 changed files with 119 additions and 84 deletions
|
|
@ -84,4 +84,19 @@ class Comment < ActiveRecord::Base
|
|||
def parent= parent
|
||||
self.post = parent
|
||||
end
|
||||
|
||||
class Generator < Federated::Generator
|
||||
def self.federated_class
|
||||
Comment
|
||||
end
|
||||
|
||||
def initialize(person, target, text)
|
||||
@text = text
|
||||
super(person, target)
|
||||
end
|
||||
|
||||
def relayable_options
|
||||
{:post => @target, :text => @text}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,42 +0,0 @@
|
|||
class FederatedRelayable < ActiveRecord::Base
|
||||
self.abstract_class = true
|
||||
|
||||
#crazy ordering issues - DEATH TO ROXML
|
||||
include ROXML
|
||||
|
||||
include Diaspora::Webhooks
|
||||
include Diaspora::Guid
|
||||
|
||||
#seriously, don't try to move this shit around until you have killed ROXML
|
||||
xml_attr :target_type
|
||||
include Diaspora::Relayable
|
||||
|
||||
xml_attr :diaspora_handle
|
||||
|
||||
belongs_to :target, :polymorphic => true
|
||||
belongs_to :author, :class_name => 'Person'
|
||||
#end crazy ordering issues
|
||||
|
||||
validates_uniqueness_of :target_id, :scope => [:target_type, :author_id]
|
||||
validates :parent, :presence => true #should be in relayable (pending on fixing Message)
|
||||
|
||||
def diaspora_handle
|
||||
self.author.diaspora_handle
|
||||
end
|
||||
|
||||
def diaspora_handle=(nh)
|
||||
self.author = Webfinger.new(nh).fetch
|
||||
end
|
||||
|
||||
def parent_class
|
||||
self.target_type.constantize
|
||||
end
|
||||
|
||||
def parent
|
||||
self.target
|
||||
end
|
||||
|
||||
def parent= parent
|
||||
self.target = parent
|
||||
end
|
||||
end
|
||||
|
|
@ -2,7 +2,17 @@
|
|||
# licensed under the Affero General Public License version 3 or later. See
|
||||
# the COPYRIGHT file.
|
||||
|
||||
class Like < FederatedRelayable
|
||||
class Like < Federated::Relayable
|
||||
class Generator < Federated::Generator
|
||||
def self.federated_class
|
||||
Like
|
||||
end
|
||||
|
||||
def relayable_options
|
||||
{:target => @target, :positive => true}
|
||||
end
|
||||
end
|
||||
|
||||
after_create do
|
||||
self.parent.update_likes_counter
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,3 +1,11 @@
|
|||
class Participation < FederatedRelayable
|
||||
class Participation < Federated::Relayable
|
||||
class Generator < Federated::Generator
|
||||
def self.federated_class
|
||||
Participation
|
||||
end
|
||||
|
||||
def relayable_options
|
||||
{:target => @target}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -285,52 +285,19 @@ class User < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def comment!(post, text, opts={})
|
||||
comment = build_comment(opts.merge!(:post => post, :text => text))
|
||||
if comment.save
|
||||
dispatch_post(comment)
|
||||
comment
|
||||
else
|
||||
false
|
||||
end
|
||||
Comment::Generator.new(self.person, post, text).create!(opts)
|
||||
end
|
||||
|
||||
def participate!(target, opts={})
|
||||
participation = build_participation(opts.merge!(:target => target))
|
||||
if participation.save
|
||||
dispatch_post(participation)
|
||||
participation
|
||||
else
|
||||
false
|
||||
end
|
||||
Participation::Generator.new(self.person, target).create!(opts)
|
||||
end
|
||||
|
||||
def like!(target, opts={})
|
||||
like = build_like(opts.merge!(:target => target, :positive => true))
|
||||
if like.save
|
||||
dispatch_post(like)
|
||||
like
|
||||
else
|
||||
false
|
||||
end
|
||||
Like::Generator.new(self.person, target).create!(opts)
|
||||
end
|
||||
|
||||
def build_relayable(model, options = {})
|
||||
r = model.new(options.merge(:author_id => self.person.id))
|
||||
r.set_guid
|
||||
r.initialize_signatures
|
||||
r
|
||||
end
|
||||
|
||||
def build_comment(options = {})
|
||||
build_relayable(Comment, options)
|
||||
end
|
||||
|
||||
def build_participation(options = {})
|
||||
build_relayable(Participation, options)
|
||||
end
|
||||
|
||||
def build_like(options = {})
|
||||
build_relayable(Like, options)
|
||||
def build_comment(options={})
|
||||
Comment::Generator.new(self.person, options.delete(:post), options.delete(:text)).build(options)
|
||||
end
|
||||
|
||||
# Check whether the user has liked a post.
|
||||
|
|
|
|||
32
lib/federated/generator.rb
Normal file
32
lib/federated/generator.rb
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
module Federated
|
||||
class Generator
|
||||
def initialize(person, target)
|
||||
@person = person
|
||||
@target = target
|
||||
end
|
||||
|
||||
def build(options={})
|
||||
options.merge!(relayable_options)
|
||||
relayable = self.class.federated_class.new(options.merge(:author_id => @person.id))
|
||||
relayable.set_guid
|
||||
relayable.initialize_signatures
|
||||
relayable
|
||||
end
|
||||
|
||||
def create!(options={})
|
||||
relayable = build(options)
|
||||
if relayable.save
|
||||
Postzord::Dispatcher.defer_build_and_post(@person, relayable)
|
||||
relayable
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def relayable_options
|
||||
{}
|
||||
end
|
||||
end
|
||||
end
|
||||
44
lib/federated/relayable.rb
Normal file
44
lib/federated/relayable.rb
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
module Federated
|
||||
class Relayable < ActiveRecord::Base
|
||||
self.abstract_class = true
|
||||
|
||||
#crazy ordering issues - DEATH TO ROXML
|
||||
include ROXML
|
||||
|
||||
include Diaspora::Webhooks
|
||||
include Diaspora::Guid
|
||||
|
||||
#seriously, don't try to move this shit around until you have killed ROXML
|
||||
xml_attr :target_type
|
||||
include Diaspora::Relayable
|
||||
|
||||
xml_attr :diaspora_handle
|
||||
|
||||
belongs_to :target, :polymorphic => true
|
||||
belongs_to :author, :class_name => 'Person'
|
||||
#end crazy ordering issues
|
||||
|
||||
validates_uniqueness_of :target_id, :scope => [:target_type, :author_id]
|
||||
validates :parent, :presence => true #should be in relayable (pending on fixing Message)
|
||||
|
||||
def diaspora_handle
|
||||
self.author.diaspora_handle
|
||||
end
|
||||
|
||||
def diaspora_handle=(nh)
|
||||
self.author = Webfinger.new(nh).fetch
|
||||
end
|
||||
|
||||
def parent_class
|
||||
self.target_type.constantize
|
||||
end
|
||||
|
||||
def parent
|
||||
self.target
|
||||
end
|
||||
|
||||
def parent= parent
|
||||
self.target = parent
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -90,7 +90,7 @@ describe Like do
|
|||
@object_on_remote_parent = @local_luke.like!(@remote_parent)
|
||||
end
|
||||
|
||||
let(:build_object) { alice.build_like(:target => @status, :positive => true) }
|
||||
let(:build_object) { Like::Generator.new(alice.person, @status).build }
|
||||
it_should_behave_like 'it is relayable'
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -16,7 +16,8 @@ describe Participation do
|
|||
@object_on_remote_parent = @local_luke.participate!(@remote_parent)
|
||||
end
|
||||
|
||||
let(:build_object) { alice.build_participation(:target => @status) }
|
||||
let(:build_object) { Participation::Generator.new(alice.person, @status).build }
|
||||
|
||||
it_should_behave_like 'it is relayable'
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue