From 54ff7ee278d9cd5401daf1ce498c26065834e267 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Thu, 3 Mar 2016 02:04:35 +0100 Subject: [PATCH 1/3] migration to cleanup aspect_visibilities * remove all for public posts * remove all for deleted posts --- ...0160302025129_cleanup_aspect_visibility.rb | 27 +++++++++++++++++++ db/schema.rb | 10 +++---- 2 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20160302025129_cleanup_aspect_visibility.rb diff --git a/db/migrate/20160302025129_cleanup_aspect_visibility.rb b/db/migrate/20160302025129_cleanup_aspect_visibility.rb new file mode 100644 index 000000000..c937ac4df --- /dev/null +++ b/db/migrate/20160302025129_cleanup_aspect_visibility.rb @@ -0,0 +1,27 @@ +class CleanupAspectVisibility < ActiveRecord::Migration + class AspectVisibility < ActiveRecord::Base + end + + def up + AspectVisibility.joins("LEFT OUTER JOIN posts ON posts.id = aspect_visibilities.shareable_id") + .where(shareable_type: "Post").delete_all("posts.id is NULL") + AspectVisibility.joins("LEFT OUTER JOIN photos ON photos.id = aspect_visibilities.shareable_id") + .where(shareable_type: "Photo").delete_all("photos.id is NULL") + AspectVisibility.joins("INNER JOIN posts ON posts.id = aspect_visibilities.shareable_id") + .where(shareable_type: "Post").delete_all(posts: {public: true}) + AspectVisibility.joins("INNER JOIN photos ON photos.id = aspect_visibilities.shareable_id") + .where(shareable_type: "Photo").delete_all(photos: {public: true}) + + remove_columns :aspect_visibilities, :created_at, :updated_at + end + + def down + add_column :aspect_visibilities, :created_at, :datetime + add_column :aspect_visibilities, :updated_at, :datetime + + User.all.each do |user| + user.posts.where(public: true).each {|post| user.add_to_streams(post, user.aspects) } + user.photos.where(public: true).each {|photo| user.add_to_streams(photo, user.aspects) } + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 04230193d..ee9d27afa 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160225232049) do +ActiveRecord::Schema.define(version: 20160302025129) do create_table "account_deletions", force: :cascade do |t| t.string "diaspora_handle", limit: 255 @@ -31,11 +31,9 @@ ActiveRecord::Schema.define(version: 20160225232049) do add_index "aspect_memberships", ["contact_id"], name: "index_aspect_memberships_on_contact_id", using: :btree create_table "aspect_visibilities", force: :cascade do |t| - t.integer "shareable_id", limit: 4, null: false - t.integer "aspect_id", limit: 4, null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "shareable_type", limit: 255, default: "Post", null: false + t.integer "shareable_id", limit: 4, null: false + t.integer "aspect_id", limit: 4, null: false + t.string "shareable_type", limit: 255, default: "Post", null: false end add_index "aspect_visibilities", ["aspect_id"], name: "index_aspect_visibilities_on_aspect_id", using: :btree From f91312896789a45dfbaa0811ae57ee2d8a21d0b5 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Wed, 2 Mar 2016 02:58:27 +0100 Subject: [PATCH 2/3] don't add public shareables to aspect_visibilities --- app/controllers/photos_controller.rb | 6 ++++-- app/controllers/reshares_controller.rb | 1 - app/models/user/querying.rb | 5 ++++- app/services/status_message_creation_service.rb | 4 ++-- lib/diaspora/shareable.rb | 4 ++++ spec/controllers/reshares_controller_spec.rb | 5 ----- 6 files changed, 14 insertions(+), 11 deletions(-) diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index 52a9ef543..dafe53307 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -150,10 +150,12 @@ class PhotosController < ApplicationController @photo = current_user.build_post(:photo, params[:photo]) if @photo.save - aspects = current_user.aspects_from_ids(params[:photo][:aspect_ids]) unless @photo.pending - current_user.add_to_streams(@photo, aspects) + unless @photo.public? + aspects = current_user.aspects_from_ids(params[:photo][:aspect_ids]) + current_user.add_to_streams(@photo, aspects) + end current_user.dispatch_post(@photo, :to => params[:photo][:aspect_ids]) end diff --git a/app/controllers/reshares_controller.rb b/app/controllers/reshares_controller.rb index 1e49a5aa0..4be025598 100644 --- a/app/controllers/reshares_controller.rb +++ b/app/controllers/reshares_controller.rb @@ -11,7 +11,6 @@ class ResharesController < ApplicationController end if @reshare.save - current_user.add_to_streams(@reshare, current_user.aspects) current_user.dispatch_post(@reshare, :url => post_url(@reshare), :additional_subscribers => @reshare.root_author) render :json => ExtremePostPresenter.new(@reshare, current_user), :status => 201 else diff --git a/app/models/user/querying.rb b/app/models/user/querying.rb index 531c8f5fa..a3104930e 100644 --- a/app/models/user/querying.rb +++ b/app/models/user/querying.rb @@ -134,7 +134,10 @@ module User::Querying query = opts[:klass].where(conditions) unless opts[:all_aspects?] - query = query.joins(:aspect_visibilities).where(aspect_visibilities: {aspect_id: opts[:by_members_of]}) + query = query.with_aspects.where( + AspectVisibility.arel_table[:aspect_id].in(opts[:by_members_of]) + .or(opts[:klass].arel_table[:public].eq(true)) + ) end ugly_select_clause(query, opts) diff --git a/app/services/status_message_creation_service.rb b/app/services/status_message_creation_service.rb index 3f0093bca..d1de5c555 100644 --- a/app/services/status_message_creation_service.rb +++ b/app/services/status_message_creation_service.rb @@ -34,9 +34,9 @@ class StatusMessageCreationService end def destination_aspect_ids(params, user) - if params[:status_message][:public] || params[:status_message][:aspect_ids].first == "all_aspects" + if params[:status_message][:aspect_ids].first == "all_aspects" user.aspect_ids - else + elsif !params[:status_message][:public] params[:aspect_ids] end end diff --git a/lib/diaspora/shareable.rb b/lib/diaspora/shareable.rb index f0a5b3121..2c6b1b6ce 100644 --- a/lib/diaspora/shareable.rb +++ b/lib/diaspora/shareable.rb @@ -24,6 +24,10 @@ module Diaspora joins("LEFT OUTER JOIN share_visibilities ON share_visibilities.shareable_id = #{table_name}.id") } + scope :with_aspects, -> { + joins("LEFT OUTER JOIN aspect_visibilities ON aspect_visibilities.shareable_id = #{table_name}.id") + } + def self.owned_or_visible_by_user(user) with_visibility.where( visible_by_user(user).or(arel_table[:public].eq(true) diff --git a/spec/controllers/reshares_controller_spec.rb b/spec/controllers/reshares_controller_spec.rb index 8f9090784..90b2720d9 100644 --- a/spec/controllers/reshares_controller_spec.rb +++ b/spec/controllers/reshares_controller_spec.rb @@ -33,11 +33,6 @@ describe ResharesController, :type => :controller do }.to change(Reshare, :count).by(1) end - it 'after save, calls add to streams' do - expect(bob).to receive(:add_to_streams) - post_request! - end - it 'calls dispatch' do expect(bob).to receive(:dispatch_post).with(anything, hash_including(:additional_subscribers)) post_request! From 0c8d9e1907cd671650f0af9e2782c81473b8950b Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Thu, 3 Mar 2016 02:22:04 +0100 Subject: [PATCH 3/3] delete aspect_visibilities when deleting the shareable --- lib/diaspora/shareable.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/diaspora/shareable.rb b/lib/diaspora/shareable.rb index 2c6b1b6ce..a4c58ed86 100644 --- a/lib/diaspora/shareable.rb +++ b/lib/diaspora/shareable.rb @@ -8,7 +8,7 @@ module Diaspora module Shareable def self.included(model) model.instance_eval do - has_many :aspect_visibilities, as: :shareable, validate: false + has_many :aspect_visibilities, as: :shareable, validate: false, dependent: :delete_all has_many :aspects, through: :aspect_visibilities has_many :share_visibilities, as: :shareable, dependent: :delete_all