diff --git a/app/models/federated_relayable.rb b/app/models/federated_relayable.rb new file mode 100644 index 0000000000000000000000000000000000000000..540ee096726e2bbd389e25f603fcb2234c2297ff --- /dev/null +++ b/app/models/federated_relayable.rb @@ -0,0 +1,42 @@ +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 \ No newline at end of file diff --git a/app/models/like.rb b/app/models/like.rb index 5466bddd1adc1e9559afc5c5836f61e4507d117b..07dc4e2ea0ebd588535b98fbf86c61c3369dddcb 100644 --- a/app/models/like.rb +++ b/app/models/like.rb @@ -2,14 +2,16 @@ # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. -class Like < ActiveRecord::Base - include ROXML +class Like < FederatedRelayable + after_create do + self.parent.update_likes_counter + end - include Diaspora::Webhooks - include Diaspora::Guid + after_destroy do + self.parent.update_likes_counter + end - xml_attr :target_type - include Diaspora::Relayable + xml_attr :positive # NOTE API V1 to be extracted acts_as_api @@ -20,43 +22,6 @@ class Like < ActiveRecord::Base t.add :created_at end - xml_attr :positive - xml_attr :diaspora_handle - - belongs_to :target, :polymorphic => true - belongs_to :author, :class_name => 'Person' - - validates_uniqueness_of :target_id, :scope => [:target_type, :author_id] - validates :parent, :presence => true #should be in relayable (pending on fixing Message) - - after_create do - self.parent.update_likes_counter - end - - after_destroy do - self.parent.update_likes_counter - end - - 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 - def notification_type(user, person) #TODO(dan) need to have a notification for likes on comments, until then, return nil return nil if self.target_type == "Comment" diff --git a/app/models/participation.rb b/app/models/participation.rb new file mode 100644 index 0000000000000000000000000000000000000000..45ef129a6c720851e784b0a408067ac9179db8ce --- /dev/null +++ b/app/models/participation.rb @@ -0,0 +1,3 @@ +class Participation < FederatedRelayable + +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index 3fa47a37162ef0ebcace4d00795d691218581bd3..6bafb452c70efd719e7aa65fede4887f40daec1d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -294,6 +294,16 @@ class User < ActiveRecord::Base end end + def participate!(target, opts={}) + participation = build_participation(opts.merge!(:target => target)) + if participation.save + dispatch_post(participation) + participation + else + false + end + end + def like!(target, opts={}) like = build_like(opts.merge!(:target => target, :positive => true)) if like.save @@ -311,12 +321,14 @@ class User < ActiveRecord::Base r end - ######## Commenting ######## def build_comment(options = {}) build_relayable(Comment, options) end - ######## Liking ######## + def build_participation(options = {}) + build_relayable(Participation, options) + end + def build_like(options = {}) build_relayable(Like, options) end diff --git a/db/migrate/20120208231253_create_participations.rb b/db/migrate/20120208231253_create_participations.rb new file mode 100644 index 0000000000000000000000000000000000000000..62f840bc633832704ec4ede677b405a5e595f06a --- /dev/null +++ b/db/migrate/20120208231253_create_participations.rb @@ -0,0 +1,17 @@ +class CreateParticipations < ActiveRecord::Migration + def self.up + create_table "participations", :force => true do |t| + t.string "guid" + t.integer "target_id" + t.string "target_type", :limit => 60, :null => false + t.integer "author_id" + t.text "author_signature" + t.text "parent_author_signature" + t.timestamps + end + end + + def self.down + drop_table :participations + end +end diff --git a/db/schema.rb b/db/schema.rb index b863d086ef3623202d657c82df3cb3e48cb91173..c7d72dccbe89bf64691b2828a87b339b7117f5e2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120203220932) do +ActiveRecord::Schema.define(:version => 20120208231253) do create_table "account_deletions", :force => true do |t| t.string "diaspora_handle" @@ -244,6 +244,17 @@ ActiveRecord::Schema.define(:version => 20120203220932) do add_index "oauth_clients", ["name"], :name => "index_oauth_clients_on_name", :unique => true add_index "oauth_clients", ["nonce"], :name => "index_oauth_clients_on_nonce", :unique => true + create_table "participations", :force => true do |t| + t.string "guid" + t.integer "target_id" + t.string "target_type", :limit => 60, :null => false + t.integer "author_id" + t.text "author_signature" + t.text "parent_author_signature" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "people", :force => true do |t| t.string "guid", :null => false t.text "url", :null => false diff --git a/spec/models/like_spec.rb b/spec/models/like_spec.rb index 5687018744796b776c57bcf4f5876d9a0d10e10c..7e8bedfc9ea2d4e99b475b3a463f86881d6bf603 100644 --- a/spec/models/like_spec.rb +++ b/spec/models/like_spec.rb @@ -7,8 +7,7 @@ require File.join(Rails.root, "spec", "shared_behaviors", "relayable") describe Like do before do - bobs_aspect = bob.aspects.first - @status = bob.post(:status_message, :text => "hello", :to => bobs_aspect.id) + @status = bob.post(:status_message, :text => "hello", :to => bob.aspects.first.id) end it 'has a valid factory' do @@ -94,5 +93,4 @@ describe Like do let(:build_object) { alice.build_like(:target => @status, :positive => true) } it_should_behave_like 'it is relayable' end - end diff --git a/spec/models/participation_spec.rb b/spec/models/participation_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..7214c3e6962303d48bcc21e31edfc2979d254a9b --- /dev/null +++ b/spec/models/participation_spec.rb @@ -0,0 +1,22 @@ +require "spec_helper" + +describe Participation do + describe 'it is relayable' do + before do + @status = bob.post(:status_message, :text => "hello", :to => bob.aspects.first.id) + + @local_luke, @local_leia, @remote_raphael = set_up_friends + @remote_parent = Factory(:status_message, :author => @remote_raphael) + @local_parent = @local_luke.post :status_message, :text => "foobar", :to => @local_luke.aspects.first + + @object_by_parent_author = @local_luke.participate!(@local_parent) + @object_by_recipient = @local_leia.participate!(@local_parent) + @dup_object_by_parent_author = @object_by_parent_author.dup + + @object_on_remote_parent = @local_luke.participate!(@remote_parent) + end + + let(:build_object) { alice.build_participation(:target => @status) } + it_should_behave_like 'it is relayable' + end +end \ No newline at end of file