From dfd128652146ac2a5fe764f854d2c7df693f107e Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg Date: Fri, 4 Nov 2011 18:10:20 -0700 Subject: [PATCH 1/4] dry up multi stream --- lib/stream/multi.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/stream/multi.rb b/lib/stream/multi.rb index e6e5403d7..934d6bbf5 100644 --- a/lib/stream/multi.rb +++ b/lib/stream/multi.rb @@ -117,5 +117,4 @@ class Stream::Multi < Stream::Base def ids(query) Post.connection.select_values(query.for_a_stream(max_time, order).select('posts.id').to_sql) end - end From 95c206df2442cb02cbd78e8e7bc3ecf29846c88c Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg Date: Fri, 4 Nov 2011 18:13:34 -0700 Subject: [PATCH 2/4] MS DG add participants --- app/models/jobs/http_multi.rb | 2 ++ app/models/jobs/notify_local_users.rb | 1 + app/models/jobs/receive_local_batch.rb | 4 ++++ app/models/notification.rb | 2 +- app/models/person.rb | 1 + lib/diaspora/relayable.rb | 4 ++++ lib/diaspora/shareable.rb | 15 +++++++++++++++ lib/postzord/receiver/local_batch.rb | 1 + spec/models/post_spec.rb | 10 ++++++++++ 9 files changed, 39 insertions(+), 1 deletion(-) diff --git a/app/models/jobs/http_multi.rb b/app/models/jobs/http_multi.rb index 044deb470..de333e74d 100644 --- a/app/models/jobs/http_multi.rb +++ b/app/models/jobs/http_multi.rb @@ -22,6 +22,8 @@ module Jobs def self.perform(user_id, encoded_object_xml, person_ids, dispatcher_class_as_string) user = User.find(user_id) + + #could be bad here people = Person.where(:id => person_ids) dispatcher = dispatcher_class_as_string.constantize diff --git a/app/models/jobs/notify_local_users.rb b/app/models/jobs/notify_local_users.rb index f75d5418e..791b3f628 100644 --- a/app/models/jobs/notify_local_users.rb +++ b/app/models/jobs/notify_local_users.rb @@ -16,6 +16,7 @@ module Jobs return if (object.author.diaspora_handle == 'diasporahq@joindiaspora.com' || (object.respond_to?(:relayable?) && object.parent.author.diaspora_handle == 'diasporahq@joindiaspora.com')) #end hax + #this is really terrible users = User.where(:id => user_ids) person = Person.find_by_id(person_id) diff --git a/app/models/jobs/receive_local_batch.rb b/app/models/jobs/receive_local_batch.rb index ec8b781a6..8d408c2ef 100644 --- a/app/models/jobs/receive_local_batch.rb +++ b/app/models/jobs/receive_local_batch.rb @@ -11,7 +11,11 @@ module Jobs @queue = :receive def self.perform(object_class_string, object_id, recipient_user_ids) + + object = object_class_string.constantize.find(object_id) + + #recipient user ids could be really bad receiver = Postzord::Receiver::LocalBatch.new(object, recipient_user_ids) receiver.perform! end diff --git a/app/models/notification.rb b/app/models/notification.rb index 40711786a..bd61c7684 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -1,7 +1,7 @@ # Copyright (c) 2010-2011, Diaspora Inc. This file is # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. -# + class Notification < ActiveRecord::Base require File.join(Rails.root, 'lib/diaspora/web_socket') include Diaspora::Socketable diff --git a/app/models/person.rb b/app/models/person.rb index 0d6d40829..4928fbb77 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -30,6 +30,7 @@ class Person < ActiveRecord::Base has_many :posts, :foreign_key => :author_id, :dependent => :destroy # This person's own posts has_many :photos, :foreign_key => :author_id, :dependent => :destroy # This person's own photos has_many :comments, :foreign_key => :author_id, :dependent => :destroy # This person's own comments + has_many :likes, :foreign_key => :author_id, :dependent => :destroy # This person's own likes belongs_to :owner, :class_name => 'User' diff --git a/lib/diaspora/relayable.rb b/lib/diaspora/relayable.rb index 18f62bff4..b7d9a7aa5 100644 --- a/lib/diaspora/relayable.rb +++ b/lib/diaspora/relayable.rb @@ -46,6 +46,10 @@ module Diaspora end end + def particpants + self.parent.participants + end + def receive(user, person=nil) comment_or_like = self.class.where(:guid => self.guid).first || self diff --git a/lib/diaspora/shareable.rb b/lib/diaspora/shareable.rb index 1ba5746ef..b7acf69e6 100644 --- a/lib/diaspora/shareable.rb +++ b/lib/diaspora/shareable.rb @@ -107,6 +107,21 @@ module Diaspora end end + # @return [Array] The list of participants to this shareable + def participants + @participants ||= lambda do + share_type = self.class.base_class.to_s + people = [] + if self.respond_to? :comments + people += Person.joins(:comments).where(:comments => {:commentable_id => self.id, :commentable_type => share_type}).all + end + + if self.respond_to? :likes + people += Person.joins(:likes).where(:likes => {:target_id => self.id, :target_type => share_type}).all + end + people + end.call + end protected diff --git a/lib/postzord/receiver/local_batch.rb b/lib/postzord/receiver/local_batch.rb index be91e2c93..24b607968 100644 --- a/lib/postzord/receiver/local_batch.rb +++ b/lib/postzord/receiver/local_batch.rb @@ -9,6 +9,7 @@ class Postzord::Receiver::LocalBatch < Postzord::Receiver def initialize(object, recipient_user_ids) @object = object @recipient_user_ids = recipient_user_ids + #this is a nightmare @users = User.where(:id => @recipient_user_ids) end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 55f9b3430..64c512953 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -195,6 +195,16 @@ describe Post do end end + describe '#participants' do + it 'only returns the people that commented and liked the post' do + status = Factory(:status_message, :author => bob.person, :public => true) + alice.comment('too', :post => status) + eve.like(true, :target => status) + + status.participants.map(&:id).should =~ [alice, eve].map{|x| x.person.id} + end + end + describe '#comments' do it 'returns the comments of a post in created_at order' do post = bob.post :status_message, :text => "hello", :to => 'all' From c3dc3d103bbca5a2693fb26b1fe59cb72d1ded55 Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg Date: Fri, 4 Nov 2011 18:19:37 -0700 Subject: [PATCH 3/4] include post author in participats --- lib/diaspora/shareable.rb | 2 +- spec/models/post_spec.rb | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/diaspora/shareable.rb b/lib/diaspora/shareable.rb index b7acf69e6..4ffd09b19 100644 --- a/lib/diaspora/shareable.rb +++ b/lib/diaspora/shareable.rb @@ -111,7 +111,7 @@ module Diaspora def participants @participants ||= lambda do share_type = self.class.base_class.to_s - people = [] + people = [self.author] if self.respond_to? :comments people += Person.joins(:comments).where(:comments => {:commentable_id => self.id, :commentable_type => share_type}).all end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 64c512953..ddf5dda54 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -196,13 +196,19 @@ describe Post do end describe '#participants' do + it 'includes the post author' do + status = Factory(:status_message, :author => bob.person, :public => true) + status.participants.map(&:id).should == [bob.person.id] + end + it 'only returns the people that commented and liked the post' do status = Factory(:status_message, :author => bob.person, :public => true) alice.comment('too', :post => status) eve.like(true, :target => status) - status.participants.map(&:id).should =~ [alice, eve].map{|x| x.person.id} + status.participants.map(&:id).should =~ [alice, eve, bob].map{|x| x.person.id} end + end describe '#comments' do From b7d0ec7564e9b62cdf14768197e6bd34ee0fc056 Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg Date: Fri, 4 Nov 2011 18:38:20 -0700 Subject: [PATCH 4/4] fix spelling and update job to use less obtrusive query --- app/models/jobs/notify_local_users.rb | 14 ++++---------- lib/diaspora/relayable.rb | 4 ++++ lib/diaspora/shareable.rb | 7 +++++++ 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/app/models/jobs/notify_local_users.rb b/app/models/jobs/notify_local_users.rb index 791b3f628..17f8c6409 100644 --- a/app/models/jobs/notify_local_users.rb +++ b/app/models/jobs/notify_local_users.rb @@ -9,18 +9,12 @@ module Jobs require File.join(Rails.root, 'app/models/notification') def self.perform(user_ids, object_klass, object_id, person_id) - object = object_klass.constantize.find_by_id(object_id) - - #hax - return if (object.author.diaspora_handle == 'diasporahq@joindiaspora.com' || (object.respond_to?(:relayable?) && object.parent.author.diaspora_handle == 'diasporahq@joindiaspora.com')) - #end hax - - #this is really terrible - users = User.where(:id => user_ids) + users = object.participant_users person = Person.find_by_id(person_id) - - users.each{|user| Notification.notify(user, object, person) } + users.each do |user| + Notification.notify(user, object, person) + end end end end diff --git a/lib/diaspora/relayable.rb b/lib/diaspora/relayable.rb index b7d9a7aa5..c58d0affa 100644 --- a/lib/diaspora/relayable.rb +++ b/lib/diaspora/relayable.rb @@ -50,6 +50,10 @@ module Diaspora self.parent.participants end + def participant_users + self.parent.participant_users + end + def receive(user, person=nil) comment_or_like = self.class.where(:guid => self.guid).first || self diff --git a/lib/diaspora/shareable.rb b/lib/diaspora/shareable.rb index 4ffd09b19..d6e13dee9 100644 --- a/lib/diaspora/shareable.rb +++ b/lib/diaspora/shareable.rb @@ -123,6 +123,13 @@ module Diaspora end.call end + def participant_users + @participant_users ||= lambda do + user_ids = participants.map{|x| x.owner_id}.compact + User.where(:id => user_ids) + end.call + end + protected # @return [Shareable,void]