create ShareVisibilities with batch import

and delete old batch worker and receiver
This commit is contained in:
Benjamin Neff 2016-06-11 15:38:49 +02:00
parent 4d8211b641
commit 0e6446d05f
12 changed files with 11 additions and 176 deletions

View file

@ -4,7 +4,7 @@
class ShareVisibility < ActiveRecord::Base class ShareVisibility < ActiveRecord::Base
belongs_to :user belongs_to :user
belongs_to :shareable, :polymorphic => :true belongs_to :shareable, polymorphic: :true
scope :for_a_user, ->(user) { scope :for_a_user, ->(user) {
where(user_id: user.id) where(user_id: user.id)
@ -18,7 +18,7 @@ class ShareVisibility < ActiveRecord::Base
# @param share [Shareable] # @param share [Shareable]
# @return [void] # @return [void]
def self.batch_import(user_ids, share) def self.batch_import(user_ids, share)
return false unless ShareVisibility.new(:shareable_id => share.id, :shareable_type => share.class.to_s).valid? return false unless ShareVisibility.new(shareable_id: share.id, shareable_type: share.class.to_s).valid?
if AppConfig.postgres? if AppConfig.postgres?
user_ids.each do |user_id| user_ids.each do |user_id|
@ -37,9 +37,8 @@ class ShareVisibility < ActiveRecord::Base
end end
private private
def not_public def not_public
if shareable.public? errors[:base] << "Cannot create visibility for a public object" if shareable.public?
errors[:base] << "Cannot create visibility for a public object"
end
end end
end end

View file

@ -115,10 +115,6 @@ class User < ActiveRecord::Base
InvitationCode.find_or_create_by(user_id: self.id) InvitationCode.find_or_create_by(user_id: self.id)
end end
def receive_shareable(shareable)
ShareVisibility.create!(shareable_id: shareable.id, shareable_type: shareable.class.base_class.to_s, user_id: id)
end
def hidden_shareables def hidden_shareables
self[:hidden_shareables] ||= {} self[:hidden_shareables] ||= {}
end end

View file

@ -1,16 +0,0 @@
# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
module Workers
class ReceiveLocalBatch < Base
sidekiq_options queue: :receive
def perform(object_class_string, object_id, recipient_user_ids)
object = object_class_string.constantize.find(object_id)
receiver = Postzord::Receiver::LocalBatch.new(object, recipient_user_ids)
receiver.perform!
rescue ActiveRecord::RecordNotFound # Already deleted before the job could run
end
end
end

View file

@ -39,7 +39,7 @@ module Diaspora
def receive(recipient_user_ids) def receive(recipient_user_ids)
return if recipient_user_ids.empty? || public? return if recipient_user_ids.empty? || public?
User.where(id: recipient_user_ids).find_each {|recipient| recipient.receive_shareable(self) } ShareVisibility.batch_import(recipient_user_ids, self)
end end
# @return [Integer] # @return [Integer]

View file

@ -3,6 +3,5 @@
# the COPYRIGHT file. # the COPYRIGHT file.
module Postzord module Postzord
require 'postzord/receiver'
require 'postzord/dispatcher' require 'postzord/dispatcher'
end end

View file

@ -113,7 +113,7 @@ class Postzord::Dispatcher
# @param people [Array<Person>] Recipients of the post # @param people [Array<Person>] Recipients of the post
def batch_deliver_to_local(people) def batch_deliver_to_local(people)
ids = people.map{ |p| p.owner_id } ids = people.map{ |p| p.owner_id }
Workers::ReceiveLocalBatch.perform_async(@object.class.to_s, @object.id, ids) #Workers::ReceiveLocalBatch.perform_async(@object.class.to_s, @object.id, ids)
logger.info "event=push route=local sender=#{@sender.diaspora_handle} recipients=#{ids.join(',')} " \ logger.info "event=push route=local sender=#{@sender.diaspora_handle} recipients=#{ids.join(',')} " \
"payload_type=#{@object.class}" "payload_type=#{@object.class}"
end end

View file

@ -1,14 +0,0 @@
# 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 Postzord::Receiver
include Diaspora::Logging
require 'postzord/receiver/local_batch'
def perform!
self.receive!
end
end

View file

@ -1,48 +0,0 @@
# 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 Postzord::Receiver::LocalBatch < Postzord::Receiver
attr_reader :object, :recipient_user_ids, :users
def initialize(object, recipient_user_ids)
@object = object
@recipient_user_ids = recipient_user_ids
@users = User.where(:id => @recipient_user_ids)
end
def receive!
logger.info "receiving local batch for #{@object.inspect}"
if @object.respond_to?(:relayable?)
receive_relayable
else
create_share_visibilities
end
logger.info "receiving local batch completed for #{@object.inspect}"
end
# NOTE(copied over from receiver public)
# @return [void]
def receive_relayable
if @object.parent_author.local?
# receive relayable object only for the owner of the parent object
# @object.receive(@object.parent_author.owner)
end
end
# Batch import post visibilities for the recipients of the given @object
# @note performs a bulk insert into mySQL
# @return [void]
def create_share_visibilities
ShareVisibility.batch_import(@recipient_user_ids, object)
end
private
def needs_notification?(person)
person && person != @object.author.owner && !@users.exists?(person.id)
end
end

View file

@ -1,44 +0,0 @@
require 'spec_helper'
describe Postzord::Receiver::LocalBatch do
before do
@object = FactoryGirl.create(:status_message, :author => alice.person)
@ids = [bob.id.to_s]
end
let(:receiver) { Postzord::Receiver::LocalBatch.new(@object, @ids) }
describe '.initialize' do
it 'sets @post, @recipient_user_ids, and @user' do
[:object, :recipient_user_ids, :users].each do |instance_var|
expect(receiver.send(instance_var)).not_to be_nil
end
end
end
describe '#receive!' do
it 'calls .create_share_visibilities' do
expect(receiver).to receive(:create_share_visibilities)
receiver.receive!
end
end
describe '#create_share_visibilities' do
it 'calls sharevisibility.batch_import with hashes' do
expect(ShareVisibility).to receive(:batch_import).with(@ids, @object)
receiver.create_share_visibilities
end
end
context 'integrates with a comment' do
before do
sm = FactoryGirl.create(:status_message, :author => alice.person)
@object = FactoryGirl.create(:comment, :author => bob.person, :post => sm)
end
it 'does not call create_visibilities and notify_mentioned_users' do
expect(receiver).not_to receive(:create_share_visibilities)
receiver.perform!
end
end
end

View file

@ -1,22 +0,0 @@
# Copyright (c) 2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require "spec_helper"
describe Postzord::Receiver do
before do
@receiver = Postzord::Receiver.new
end
describe "#perform!" do
before do
allow(@receiver).to receive(:receive!).and_return(true)
end
it "calls receive!" do
expect(@receiver).to receive(:receive!)
@receiver.perform!
end
end
end

View file

@ -257,22 +257,21 @@ describe Post, :type => :model do
describe "#receive" do describe "#receive" do
it "creates a share visibility for the user" do it "creates a share visibility for the user" do
user_ids = [alice.id, eve.id]
post = FactoryGirl.create(:status_message, author: bob.person) post = FactoryGirl.create(:status_message, author: bob.person)
expect_any_instance_of(User).to receive(:receive_shareable).with(post) do |user, _| expect(ShareVisibility).to receive(:batch_import).with(user_ids, post)
expect(user.id).to eq(alice.id) post.receive(user_ids)
end
post.receive([alice.id])
end end
it "does nothing for public post" do it "does nothing for public post" do
post = FactoryGirl.create(:status_message, author: bob.person, public: true) post = FactoryGirl.create(:status_message, author: bob.person, public: true)
expect_any_instance_of(User).not_to receive(:receive_shareable) expect(ShareVisibility).not_to receive(:batch_import)
post.receive([alice.id]) post.receive([alice.id])
end end
it "does nothing if no recipients provided" do it "does nothing if no recipients provided" do
post = FactoryGirl.create(:status_message, author: bob.person) post = FactoryGirl.create(:status_message, author: bob.person)
expect_any_instance_of(User).not_to receive(:receive_shareable) expect(ShareVisibility).not_to receive(:batch_import)
post.receive([]) post.receive([])
end end
end end

View file

@ -1,14 +0,0 @@
require "spec_helper"
describe Workers::ReceiveLocalBatch do
it "calls the postzord" do
post = double
allow(Post).to receive(:find).with(1).and_return(post)
zord = double
expect(Postzord::Receiver::LocalBatch).to receive(:new).with(post, [2]).and_return(zord)
expect(zord).to receive(:perform!)
Workers::ReceiveLocalBatch.new.perform("Post", 1, [2])
end
end