From 4147249d2d234e2a5ad4f4aaa11d31f5167c7273 Mon Sep 17 00:00:00 2001 From: Thorsten Claus Date: Sat, 10 Apr 2021 15:03:48 +0200 Subject: [PATCH] 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