From ddee980426403841841053f4b97cf82139eba782 Mon Sep 17 00:00:00 2001 From: Thorsten Claus Date: Fri, 9 Apr 2021 12:38:47 +0200 Subject: [PATCH 1/7] Adds a local-public tag on the sidebar that shows all posts local to this pod --- app/assets/javascripts/app/router.js | 1 + app/controllers/streams_controller.rb | 4 ++++ app/helpers/stream_helper.rb | 5 ++++- app/models/post.rb | 6 +++++ app/views/streams/main_stream.html.haml | 2 ++ config/locales/diaspora/de.yml | 2 ++ config/locales/diaspora/en.yml | 3 +++ config/routes.rb | 1 + lib/stream.rb | 21 +++++++++--------- lib/stream/local_public.rb | 29 +++++++++++++++++++++++++ spec/helpers/stream_helper_spec.rb | 7 ++++++ spec/lib/stream/local_public_spec.rb | 20 +++++++++++++++++ spec/models/post_spec.rb | 21 ++++++++++++++++-- 13 files changed, 109 insertions(+), 13 deletions(-) create mode 100644 lib/stream/local_public.rb create mode 100644 spec/lib/stream/local_public_spec.rb diff --git a/app/assets/javascripts/app/router.js b/app/assets/javascripts/app/router.js index 265b3cd48..f7bd8a440 100644 --- a/app/assets/javascripts/app/router.js +++ b/app/assets/javascripts/app/router.js @@ -24,6 +24,7 @@ app.Router = Backbone.Router.extend({ "posts/:id(/)": "singlePost", "profile/edit(/)": "settings", "public(/)": "stream", + "local_public(/)": "stream", "stream(/)": "stream", "tags/:name(/)": "followed_tags", "u/:name(/)": "profile", diff --git a/app/controllers/streams_controller.rb b/app/controllers/streams_controller.rb index 0d0947616..57133e5c8 100644 --- a/app/controllers/streams_controller.rb +++ b/app/controllers/streams_controller.rb @@ -25,6 +25,10 @@ class StreamsController < ApplicationController stream_responder(Stream::Public) end + def local_public + stream_responder(Stream::LocalPublic) + end + def activity stream_responder(Stream::Activity) end diff --git a/app/helpers/stream_helper.rb b/app/helpers/stream_helper.rb index cada31f8b..503580dd6 100644 --- a/app/helpers/stream_helper.rb +++ b/app/helpers/stream_helper.rb @@ -22,7 +22,7 @@ module StreamHelper end private - + # rubocop:disable Rails/HelperInstanceVariable def next_stream_path if current_page?(:stream) stream_path(max_time: time_for_scroll(@stream)) @@ -30,6 +30,8 @@ module StreamHelper activity_stream_path(max_time: time_for_scroll(@stream)) elsif current_page?(:aspects_stream) aspects_stream_path(max_time: time_for_scroll(@stream), a_ids: session[:a_ids]) + elsif current_page?(:local_public_stream) + local_public_stream_path(max_time: time_for_scroll(@stream)) elsif current_page?(:public_stream) public_stream_path(max_time: time_for_scroll(@stream)) elsif current_page?(:commented_stream) @@ -44,6 +46,7 @@ module StreamHelper raise "in order to use pagination for this new stream, update next_stream_path in stream helper" end end + # rubocop:enable Rails/HelperInstanceVariable def time_for_scroll(stream) if stream.stream_posts.empty? diff --git a/app/models/post.rb b/app/models/post.rb index 76f829f23..1905854fa 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -51,6 +51,12 @@ class Post < ApplicationRecord scope :all_public, -> { where(public: true) } + scope :all_local_public, -> { + left_outer_joins(author: [:pod]) + .where("pods.host is null") # local posts have no host in pods + .where(public: true) + } + scope :commented_by, ->(person) { select('DISTINCT posts.*') .joins(:comments) diff --git a/app/views/streams/main_stream.html.haml b/app/views/streams/main_stream.html.haml index 56be82d92..11663651a 100644 --- a/app/views/streams/main_stream.html.haml +++ b/app/views/streams/main_stream.html.haml @@ -37,6 +37,8 @@ = render "aspects/aspect_listings", stream: @stream %li.nested-list = render "tags/followed_tags_listings" + %li{data: {stream: "local_public"}} + = link_to t("streams.local_public.title"), local_public_stream_path, rel: "backbone", class: "hoverable" %li{data: {stream: "public"}} = link_to t("streams.public.title"), public_stream_path, rel: "backbone", class: "hoverable" diff --git a/config/locales/diaspora/de.yml b/config/locales/diaspora/de.yml index 745af609a..38a92f6fa 100644 --- a/config/locales/diaspora/de.yml +++ b/config/locales/diaspora/de.yml @@ -1245,6 +1245,8 @@ de: title: "Stream" public: title: "Öffentliche Aktivität" + local_public: + title: "Lokale Posts" tags: title: "Getaggte Beiträge: %{tags}" tag_followings: diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index 12379c1b6..2e8a21915 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -1249,6 +1249,9 @@ en: public: title: "Public activity" + local_public: + title: "Local posts" + multi: title: "Stream" diff --git a/config/routes.rb b/config/routes.rb index 9a5782a41..e776cbb04 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -52,6 +52,7 @@ Rails.application.routes.draw do get "activity" => "streams#activity", :as => "activity_stream" get "stream" => "streams#multi", :as => "stream" get "public" => "streams#public", :as => "public_stream" + get "local_public" => "streams#local_public", :as => "local_public_stream" get "followed_tags" => "streams#followed_tags", :as => "followed_tags_stream" get "mentions" => "streams#mentioned", :as => "mentioned_stream" get "liked" => "streams#liked", :as => "liked_stream" diff --git a/lib/stream.rb b/lib/stream.rb index 27cb67e25..51c7512e6 100644 --- a/lib/stream.rb +++ b/lib/stream.rb @@ -1,14 +1,15 @@ # frozen_string_literal: true module Stream - require 'stream/activity' - require 'stream/aspect' - require 'stream/comments' - require 'stream/followed_tag' - require 'stream/likes' - require 'stream/mention' - require 'stream/multi' - require 'stream/person' - require 'stream/public' - require 'stream/tag' + require "stream/activity" + require "stream/aspect" + require "stream/comments" + require "stream/followed_tag" + require "stream/likes" + require "stream/mention" + require "stream/multi" + require "stream/person" + require "stream/public" + require "stream/local_public" + require "stream/tag" end diff --git a/lib/stream/local_public.rb b/lib/stream/local_public.rb new file mode 100644 index 000000000..be729c4ff --- /dev/null +++ b/lib/stream/local_public.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +# 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 Stream::LocalPublic < Stream::Base + def link(opts={}) + Rails.application.routes.url_helpers.local_public_stream_path(opts) + end + + def title + I18n.translate("streams.local_public.title") + end + + # @return [ActiveRecord::Association] AR association of posts + def posts + @posts ||= Post.all_local_public + end + + def can_comment?(post) + post.author.local? + end + + # Override base class method + def aspects + ["public"] + end +end diff --git a/spec/helpers/stream_helper_spec.rb b/spec/helpers/stream_helper_spec.rb index dac0196cb..979bc609d 100644 --- a/spec/helpers/stream_helper_spec.rb +++ b/spec/helpers/stream_helper_spec.rb @@ -20,6 +20,13 @@ describe StreamHelper, type: :helper do expect(helper.next_page_path).to include "/public" end + it "works for local-public page when current page is local-public stream" do + allow(helper).to receive(:current_page?).and_return(false) + expect(helper).to receive(:current_page?).with(:local_public_stream).and_return(true) + allow(helper).to receive(:controller).and_return(build_controller(StreamsController)) + expect(helper.next_page_path).to include "/local-public" + end + it "works for stream page when current page is stream" do allow(helper).to receive(:current_page?).and_return(false) expect(helper).to receive(:current_page?).with(:stream).and_return(true) diff --git a/spec/lib/stream/local_public_spec.rb b/spec/lib/stream/local_public_spec.rb new file mode 100644 index 000000000..431e0a134 --- /dev/null +++ b/spec/lib/stream/local_public_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require Rails.root.join('spec', 'shared_behaviors', 'stream') + +describe Stream::LocalPublic do + before do + @stream = Stream::LocalPublic.new(alice) + end + + describe 'shared behaviors' do + it_should_behave_like 'it is a stream' + end + + describe "#posts" do + it "calls Post#all_local_public" do + expect(Post).to receive(:all_local_public) + @stream.posts + end + end +end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 3e77e142f..36440e650 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -58,8 +58,25 @@ describe Post, type: :model do end end - describe ".for_a_stream" do - it "calls #for_visible_shareable_sql" do + describe ".all_local_public" do + it "includes all public posts from local" do + post1 = FactoryBot.create(:status_message, author: alice.person, public: true) + post2 = FactoryBot.create(:status_message, author: bob.person, public: true) + expect(Post.all_local_public.ids).to match_array([post1.id, post2.id]) + end + + it "doesn't include any posts from other pods" do + pod = FactoryBot.create(:pod) + external_person = FactoryBot.create(:person, pod: pod) + FactoryBot.create(:status_message, author: alice.person, public: true) + FactoryBot.create(:status_message, author: bob.person, public: true) + post_from_extern = FactoryBot.create(:status_message, author: external_person, public: true) + expect(Post.all_local_public.ids).not_to match_array([post_from_extern.id]) + end + end + + describe '.for_a_stream' do + it 'calls #for_visible_shareable_sql' do time = double order = double expect(Post).to receive(:for_visible_shareable_sql).with(time, order).and_return(Post) From 34d9d9c3eec31d2db326fd9e2adb54e27b9ab788 Mon Sep 17 00:00:00 2001 From: Thorsten Claus Date: Fri, 9 Apr 2021 12:52:55 +0200 Subject: [PATCH 2/7] Fixing test --- spec/helpers/stream_helper_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/helpers/stream_helper_spec.rb b/spec/helpers/stream_helper_spec.rb index 979bc609d..da403c42b 100644 --- a/spec/helpers/stream_helper_spec.rb +++ b/spec/helpers/stream_helper_spec.rb @@ -24,7 +24,7 @@ describe StreamHelper, type: :helper do allow(helper).to receive(:current_page?).and_return(false) expect(helper).to receive(:current_page?).with(:local_public_stream).and_return(true) allow(helper).to receive(:controller).and_return(build_controller(StreamsController)) - expect(helper.next_page_path).to include "/local-public" + expect(helper.next_page_path).to include local_public_stream_path end it "works for stream page when current page is stream" do From 4147249d2d234e2a5ad4f4aaa11d31f5167c7273 Mon Sep 17 00:00:00 2001 From: Thorsten Claus Date: Sat, 10 Apr 2021 15:03:48 +0200 Subject: [PATCH 3/7] Makes the local posts link be configured for special audiences --- app/controllers/streams_controller.rb | 13 ++++++++- app/helpers/application_helper.rb | 11 ++++++++ app/views/streams/main_stream.html.haml | 5 ++-- config/defaults.yml | 1 + config/diaspora.toml.example | 6 ++++ lib/stream/local_public.rb | 2 ++ spec/helpers/application_helper_spec.rb | 37 +++++++++++++++++++++++++ spec/lib/stream/local_public_spec.rb | 6 ++-- 8 files changed, 75 insertions(+), 6 deletions(-) diff --git a/app/controllers/streams_controller.rb b/app/controllers/streams_controller.rb index 57133e5c8..4df4a9c24 100644 --- a/app/controllers/streams_controller.rb +++ b/app/controllers/streams_controller.rb @@ -26,7 +26,18 @@ class StreamsController < ApplicationController end def local_public - stream_responder(Stream::LocalPublic) + return stream_responder(Stream::LocalPublic) if AppConfig.settings.enable_show_local_post_link == "always" + + if AppConfig.settings.enable_show_local_post_link == "admin" && Role.is_admin?(current_user) + return stream_responder(Stream::LocalPublic) + end + + if AppConfig.settings.enable_show_local_post_link == "moderator" && + (Role.moderator?(current_user) || Role.is_admin?(current_user)) + return stream_responder(Stream::LocalPublic) + end + + stream_responder(Stream::Public) end def activity diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 36cce9bce..2bd473b80 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -42,6 +42,17 @@ module ApplicationHelper "bookmarklet('#{bookmarklet_url}', #{width}, #{height});" end + def show_local_posts_link?(current_user) + return true if AppConfig.settings.enable_show_local_post_link == "always" + return true if AppConfig.settings.enable_show_local_post_link == "admin" && + Role.is_admin?(current_user) + + return true if AppConfig.settings.enable_show_local_post_link == "moderator" && + (Role.moderator?(current_user) || Role.is_admin?(current_user)) + + false + end + def all_services_connected? current_user.services.size == AppConfig.configured_services.size end diff --git a/app/views/streams/main_stream.html.haml b/app/views/streams/main_stream.html.haml index 11663651a..d1682d76e 100644 --- a/app/views/streams/main_stream.html.haml +++ b/app/views/streams/main_stream.html.haml @@ -37,8 +37,9 @@ = render "aspects/aspect_listings", stream: @stream %li.nested-list = render "tags/followed_tags_listings" - %li{data: {stream: "local_public"}} - = link_to t("streams.local_public.title"), local_public_stream_path, rel: "backbone", class: "hoverable" + - if show_local_posts_link?(current_user) + %li{data: {stream: "local_public"}} + = link_to t("streams.local_public.title"), local_public_stream_path, rel: "backbone", class: "hoverable" %li{data: {stream: "public"}} = link_to t("streams.public.title"), public_stream_path, rel: "backbone", class: "hoverable" diff --git a/config/defaults.yml b/config/defaults.yml index 619c43a0c..eb0980e0b 100644 --- a/config/defaults.yml +++ b/config/defaults.yml @@ -74,6 +74,7 @@ defaults: settings: pod_name: 'diaspora*' enable_registrations: true + enable_show_local_post_link: 'disabled' autofollow_on_join: true autofollow_on_join_user: 'hq@pod.diaspora.software' welcome_message: diff --git a/config/diaspora.toml.example b/config/diaspora.toml.example index bdd5ec391..e2ddb7861 100644 --- a/config/diaspora.toml.example +++ b/config/diaspora.toml.example @@ -287,6 +287,12 @@ ## (or commented out) to enable the first registration (you). #enable_registrations = true +## Show local posts (default="disabled") +## If any other setting than disabled local public posts +## created on this pod can be showed. +## Choose one setting +# enable_show_local_post_link= "disabled"|"admin"|"moderator"|"always" + ## Auto-follow on sign-up (default=true) ## Users will automatically follow a specified account on creation. ## Set this to false if you don't want your users to automatically diff --git a/lib/stream/local_public.rb b/lib/stream/local_public.rb index be729c4ff..4ef873d94 100644 --- a/lib/stream/local_public.rb +++ b/lib/stream/local_public.rb @@ -4,6 +4,7 @@ # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. +# rubocop:disable Style/ClassAndModuleChildren class Stream::LocalPublic < Stream::Base def link(opts={}) Rails.application.routes.url_helpers.local_public_stream_path(opts) @@ -27,3 +28,4 @@ class Stream::LocalPublic < Stream::Base ["public"] end end +# rubocop:enable Style/ClassAndModuleChildren diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index f0b0ab500..32a93d073 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -122,6 +122,43 @@ describe ApplicationHelper, :type => :helper do end end + describe "#enable_show_local_post_link" do + let!(:moderator) { create(:person) } + let!(:admin) { create(:person) } + before do + moderator.roles.create(name: "moderator") + admin.roles.create(name: "admin") + end + + it "return false if show_local_posts_link is 'disabled'" do + AppConfig.settings.enable_show_local_post_link = "disabled" + expect(helper.show_local_posts_link?(admin)).to be false + expect(helper.show_local_posts_link?(moderator)).to be false + expect(helper.show_local_posts_link?(alice)).to be false + end + + it "return true for admins if show_local_posts_link is 'admin'" do + AppConfig.settings.enable_show_local_post_link = "admin" + expect(helper.show_local_posts_link?(admin)).to be true + expect(helper.show_local_posts_link?(moderator)).to be false + expect(helper.show_local_posts_link?(alice)).to be false + end + + it "return true for admins and moderators if show_local_posts_link is 'moderator'" do + AppConfig.settings.enable_show_local_post_link = "moderator" + expect(helper.show_local_posts_link?(admin)).to be true + expect(helper.show_local_posts_link?(moderator)).to be true + expect(helper.show_local_posts_link?(alice)).to be false + end + + it "return true for everybody if show_local_posts_link is 'always'" do + AppConfig.settings.enable_show_local_post_link = "always" + expect(helper.show_local_posts_link?(admin)).to be true + expect(helper.show_local_posts_link?(moderator)).to be true + expect(helper.show_local_posts_link?(alice)).to be true + end + end + describe "#changelog_url" do let(:changelog_url_setting) { double.tap {|double| allow(AppConfig).to receive(:settings).and_return(double(changelog_url: double)) } diff --git a/spec/lib/stream/local_public_spec.rb b/spec/lib/stream/local_public_spec.rb index 431e0a134..69892a07b 100644 --- a/spec/lib/stream/local_public_spec.rb +++ b/spec/lib/stream/local_public_spec.rb @@ -1,14 +1,14 @@ # frozen_string_literal: true -require Rails.root.join('spec', 'shared_behaviors', 'stream') +require Rails.root.join("spec/shared_behaviors/stream") describe Stream::LocalPublic do before do @stream = Stream::LocalPublic.new(alice) end - describe 'shared behaviors' do - it_should_behave_like 'it is a stream' + describe "shared behaviors" do + it_should_behave_like "it is a stream" end describe "#posts" do From 2db1d5d641d3d1cb793d3153f54c8df1f769d07d Mon Sep 17 00:00:00 2001 From: Thorsten Claus Date: Sun, 11 Apr 2021 21:45:31 +0200 Subject: [PATCH 4/7] Fixing PR Rewview issues --- app/controllers/streams_controller.rb | 15 +++------- app/helpers/application_helper.rb | 11 -------- app/views/streams/main_stream.html.haml | 2 +- config/diaspora.toml.example | 10 ++++--- config/locales/diaspora/de.yml | 2 -- lib/configuration_methods.rb | 9 ++++++ lib/stream/local_public.rb | 4 --- spec/helpers/application_helper_spec.rb | 37 ------------------------- spec/lib/configuration_methods_spec.rb | 37 +++++++++++++++++++++++++ spec/models/post_spec.rb | 4 +-- 10 files changed, 59 insertions(+), 72 deletions(-) diff --git a/app/controllers/streams_controller.rb b/app/controllers/streams_controller.rb index 4df4a9c24..943295908 100644 --- a/app/controllers/streams_controller.rb +++ b/app/controllers/streams_controller.rb @@ -26,18 +26,11 @@ class StreamsController < ApplicationController end def local_public - return stream_responder(Stream::LocalPublic) if AppConfig.settings.enable_show_local_post_link == "always" - - if AppConfig.settings.enable_show_local_post_link == "admin" && Role.is_admin?(current_user) - return stream_responder(Stream::LocalPublic) + if AppConfig.local_posts_stream?(current_user) + stream_responder(Stream::LocalPublic) + else + head :not_found end - - if AppConfig.settings.enable_show_local_post_link == "moderator" && - (Role.moderator?(current_user) || Role.is_admin?(current_user)) - return stream_responder(Stream::LocalPublic) - end - - stream_responder(Stream::Public) end def activity diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 2bd473b80..36cce9bce 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -42,17 +42,6 @@ module ApplicationHelper "bookmarklet('#{bookmarklet_url}', #{width}, #{height});" end - def show_local_posts_link?(current_user) - return true if AppConfig.settings.enable_show_local_post_link == "always" - return true if AppConfig.settings.enable_show_local_post_link == "admin" && - Role.is_admin?(current_user) - - return true if AppConfig.settings.enable_show_local_post_link == "moderator" && - (Role.moderator?(current_user) || Role.is_admin?(current_user)) - - false - end - def all_services_connected? current_user.services.size == AppConfig.configured_services.size end diff --git a/app/views/streams/main_stream.html.haml b/app/views/streams/main_stream.html.haml index d1682d76e..cf4aaebae 100644 --- a/app/views/streams/main_stream.html.haml +++ b/app/views/streams/main_stream.html.haml @@ -37,7 +37,7 @@ = render "aspects/aspect_listings", stream: @stream %li.nested-list = render "tags/followed_tags_listings" - - if show_local_posts_link?(current_user) + - if AppConfig.local_posts_stream?(current_user) %li{data: {stream: "local_public"}} = link_to t("streams.local_public.title"), local_public_stream_path, rel: "backbone", class: "hoverable" %li{data: {stream: "public"}} diff --git a/config/diaspora.toml.example b/config/diaspora.toml.example index e2ddb7861..b41b4c1eb 100644 --- a/config/diaspora.toml.example +++ b/config/diaspora.toml.example @@ -287,11 +287,13 @@ ## (or commented out) to enable the first registration (you). #enable_registrations = true -## Show local posts (default="disabled") +## Show local posts stream (default="disabled") ## If any other setting than disabled local public posts -## created on this pod can be showed. -## Choose one setting -# enable_show_local_post_link= "disabled"|"admin"|"moderator"|"always" +## created on this pod can be shown. +## Setting this to admins shows the local posts stream only to users with the admin role. +## Setting this to moderators shows the local posts stream only to users with the moderator or admin role. +## Setting this to everyone shows the local posts stream to all users. +# enable_local_posts_stream= "disabled"|"admins"|"moderators"|"everyone" ## Auto-follow on sign-up (default=true) ## Users will automatically follow a specified account on creation. diff --git a/config/locales/diaspora/de.yml b/config/locales/diaspora/de.yml index 38a92f6fa..745af609a 100644 --- a/config/locales/diaspora/de.yml +++ b/config/locales/diaspora/de.yml @@ -1245,8 +1245,6 @@ de: title: "Stream" public: title: "Öffentliche Aktivität" - local_public: - title: "Lokale Posts" tags: title: "Getaggte Beiträge: %{tags}" tag_followings: diff --git a/lib/configuration_methods.rb b/lib/configuration_methods.rb index 0c7a358d6..22a5e1a16 100644 --- a/lib/configuration_methods.rb +++ b/lib/configuration_methods.rb @@ -50,6 +50,15 @@ module Configuration self["services.#{service}.authorized"] == true end + def local_posts_stream?(user) + return true if settings.enable_local_posts_stream == "admins" && + Role.is_admin?(user) + return true if settings.enable_local_posts_stream == "moderators" && + (Role.moderator?(user) || Role.is_admin?(user)) + + settings.enable_local_posts_stream == "everyone" + end + def secret_token if heroku? return ENV["SECRET_TOKEN"] if ENV["SECRET_TOKEN"] diff --git a/lib/stream/local_public.rb b/lib/stream/local_public.rb index 4ef873d94..9e0963cee 100644 --- a/lib/stream/local_public.rb +++ b/lib/stream/local_public.rb @@ -1,9 +1,5 @@ # frozen_string_literal: true -# Copyright (c) 2010-2011, Diaspora Inc. This file is -# licensed under the Affero General Public License version 3 or later. See -# the COPYRIGHT file. - # rubocop:disable Style/ClassAndModuleChildren class Stream::LocalPublic < Stream::Base def link(opts={}) diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 32a93d073..f0b0ab500 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -122,43 +122,6 @@ describe ApplicationHelper, :type => :helper do end end - describe "#enable_show_local_post_link" do - let!(:moderator) { create(:person) } - let!(:admin) { create(:person) } - before do - moderator.roles.create(name: "moderator") - admin.roles.create(name: "admin") - end - - it "return false if show_local_posts_link is 'disabled'" do - AppConfig.settings.enable_show_local_post_link = "disabled" - expect(helper.show_local_posts_link?(admin)).to be false - expect(helper.show_local_posts_link?(moderator)).to be false - expect(helper.show_local_posts_link?(alice)).to be false - end - - it "return true for admins if show_local_posts_link is 'admin'" do - AppConfig.settings.enable_show_local_post_link = "admin" - expect(helper.show_local_posts_link?(admin)).to be true - expect(helper.show_local_posts_link?(moderator)).to be false - expect(helper.show_local_posts_link?(alice)).to be false - end - - it "return true for admins and moderators if show_local_posts_link is 'moderator'" do - AppConfig.settings.enable_show_local_post_link = "moderator" - expect(helper.show_local_posts_link?(admin)).to be true - expect(helper.show_local_posts_link?(moderator)).to be true - expect(helper.show_local_posts_link?(alice)).to be false - end - - it "return true for everybody if show_local_posts_link is 'always'" do - AppConfig.settings.enable_show_local_post_link = "always" - expect(helper.show_local_posts_link?(admin)).to be true - expect(helper.show_local_posts_link?(moderator)).to be true - expect(helper.show_local_posts_link?(alice)).to be true - end - end - describe "#changelog_url" do let(:changelog_url_setting) { double.tap {|double| allow(AppConfig).to receive(:settings).and_return(double(changelog_url: double)) } diff --git a/spec/lib/configuration_methods_spec.rb b/spec/lib/configuration_methods_spec.rb index ae812578c..2453d406f 100644 --- a/spec/lib/configuration_methods_spec.rb +++ b/spec/lib/configuration_methods_spec.rb @@ -128,6 +128,43 @@ describe Configuration::Methods do end end + describe "#has_local_posts_stream" do + let!(:moderator) { create(:person) } + let!(:admin) { create(:person) } + before do + moderator.roles.create(name: "moderator") + admin.roles.create(name: "admin") + end + + it "return false if show_local_posts_link is 'disabled'" do + AppConfig.settings.enable_local_posts_stream = "disabled" + expect(AppConfig.local_posts_stream?(admin)).to be false + expect(AppConfig.local_posts_stream?(moderator)).to be false + expect(AppConfig.local_posts_stream?(alice)).to be false + end + + it "return true for admins if show_local_posts_link is 'admins'" do + AppConfig.settings.enable_local_posts_stream = "admins" + expect(AppConfig.local_posts_stream?(admin)).to be true + expect(AppConfig.local_posts_stream?(moderator)).to be false + expect(AppConfig.local_posts_stream?(alice)).to be false + end + + it "return true for admins and moderators if show_local_posts_link is 'moderators'" do + AppConfig.settings.enable_local_posts_stream = "moderators" + expect(AppConfig.local_posts_stream?(admin)).to be true + expect(AppConfig.local_posts_stream?(moderator)).to be true + expect(AppConfig.local_posts_stream?(alice)).to be false + end + + it "return true for everybody if show_local_posts_link is 'everyone'" do + AppConfig.settings.enable_local_posts_stream = "everyone" + expect(AppConfig.local_posts_stream?(admin)).to be true + expect(AppConfig.local_posts_stream?(moderator)).to be true + expect(AppConfig.local_posts_stream?(alice)).to be true + end + end + describe "#version_string" do before do @version = double diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 36440e650..11eaf55de 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -75,8 +75,8 @@ describe Post, type: :model do end end - describe '.for_a_stream' do - it 'calls #for_visible_shareable_sql' do + describe ".for_a_stream" do + it "calls #for_visible_shareable_sql" do time = double order = double expect(Post).to receive(:for_visible_shareable_sql).with(time, order).and_return(Post) From 7fae5ca3b81243941f54e1be585e05c283076da4 Mon Sep 17 00:00:00 2001 From: Thorsten Claus Date: Thu, 15 Apr 2021 08:14:54 +0200 Subject: [PATCH 5/7] More optimized and faster query for local public posts Even faster with new index --- app/models/post.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 1905854fa..211972931 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -52,9 +52,10 @@ class Post < ApplicationRecord scope :all_public, -> { where(public: true) } scope :all_local_public, -> { - left_outer_joins(author: [:pod]) - .where("pods.host is null") # local posts have no host in pods - .where(public: true) + where(" exists ( + select 1 from people where posts.author_id = people.id + and people.pod_id is null) + and posts.public = true") } scope :commented_by, ->(person) { From c937e173357092b8ec0cd74bb487732c0503bf94 Mon Sep 17 00:00:00 2001 From: Thorsten Claus Date: Sun, 18 Apr 2021 13:23:17 +0200 Subject: [PATCH 6/7] Set correct defaults for local mode stream --- config/defaults.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/defaults.yml b/config/defaults.yml index eb0980e0b..639ef6a52 100644 --- a/config/defaults.yml +++ b/config/defaults.yml @@ -74,7 +74,7 @@ defaults: settings: pod_name: 'diaspora*' enable_registrations: true - enable_show_local_post_link: 'disabled' + enable_local_posts_stream: 'disabled' autofollow_on_join: true autofollow_on_join_user: 'hq@pod.diaspora.software' welcome_message: From 64d65269d8ed043dd5b9e3148f1b237ae766b475 Mon Sep 17 00:00:00 2001 From: Thorsten Claus Date: Sun, 18 Apr 2021 13:25:50 +0200 Subject: [PATCH 7/7] Fixing robocop --- app/models/post.rb | 2 +- lib/stream/local_public.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 211972931..e8abc75ff 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -52,7 +52,7 @@ class Post < ApplicationRecord scope :all_public, -> { where(public: true) } scope :all_local_public, -> { - where(" exists ( + where(" exists ( select 1 from people where posts.author_id = people.id and people.pod_id is null) and posts.public = true") diff --git a/lib/stream/local_public.rb b/lib/stream/local_public.rb index 9e0963cee..bb25f359b 100644 --- a/lib/stream/local_public.rb +++ b/lib/stream/local_public.rb @@ -7,7 +7,7 @@ class Stream::LocalPublic < Stream::Base end def title - I18n.translate("streams.local_public.title") + I18n.t("streams.local_public.title") end # @return [ActiveRecord::Association] AR association of posts