From a1fa7b0fa8c29935b1e988898277baf1814de439 Mon Sep 17 00:00:00 2001 From: David Stephens Date: Thu, 5 Jan 2023 03:17:23 +0000 Subject: [PATCH] Support disabling public listing of trends # Conflicts: # app/javascript/mastodon/components/navigation_portal.js --- .../api/v1/trends/links_controller.rb | 5 +++++ .../api/v1/trends/statuses_controller.rb | 5 +++++ .../api/v1/trends/tags_controller.rb | 5 +++++ .../mastodon/components/navigation_portal.js | 21 +++++++++++++++---- .../mastodon/features/explore/index.js | 5 +++-- .../ui/components/navigation_panel.js | 4 ++-- app/javascript/mastodon/features/ui/index.js | 4 ++-- app/javascript/mastodon/initial_state.js | 1 + app/models/form/admin_settings.rb | 2 ++ app/serializers/initial_state_serializer.rb | 1 + .../admin/settings/discovery/show.html.haml | 3 +++ config/locales/simple_form.en.yml | 2 ++ config/settings.yml | 1 + 13 files changed, 49 insertions(+), 10 deletions(-) diff --git a/app/controllers/api/v1/trends/links_controller.rb b/app/controllers/api/v1/trends/links_controller.rb index 8ff3b364e2c51..35fe41d685261 100644 --- a/app/controllers/api/v1/trends/links_controller.rb +++ b/app/controllers/api/v1/trends/links_controller.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true class Api::V1::Trends::LinksController < Api::BaseController + before_action :require_user!, only: [:index], if: :require_auth? before_action :set_links after_action :insert_pagination_headers @@ -13,6 +14,10 @@ def index private + def require_auth? + !Setting.trends_preview + end + def enabled? Setting.trends end diff --git a/app/controllers/api/v1/trends/statuses_controller.rb b/app/controllers/api/v1/trends/statuses_controller.rb index c275d5fc816f5..e7ee39cffd4b5 100644 --- a/app/controllers/api/v1/trends/statuses_controller.rb +++ b/app/controllers/api/v1/trends/statuses_controller.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true class Api::V1::Trends::StatusesController < Api::BaseController + before_action :require_user!, only: [:index], if: :require_auth? before_action :set_statuses after_action :insert_pagination_headers @@ -11,6 +12,10 @@ def index private + def require_auth? + !Setting.trends_preview + end + def enabled? Setting.trends end diff --git a/app/controllers/api/v1/trends/tags_controller.rb b/app/controllers/api/v1/trends/tags_controller.rb index 21adfa2a1fc6d..c375ab38639df 100644 --- a/app/controllers/api/v1/trends/tags_controller.rb +++ b/app/controllers/api/v1/trends/tags_controller.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true class Api::V1::Trends::TagsController < Api::BaseController + before_action :require_user!, only: [:index], if: :require_auth? before_action :set_tags after_action :insert_pagination_headers @@ -13,6 +14,10 @@ def index private + def require_auth? + !Setting.trends_preview + end + def enabled? Setting.trends end diff --git a/app/javascript/mastodon/components/navigation_portal.js b/app/javascript/mastodon/components/navigation_portal.js index 45407be43e5b2..3de8abf49407f 100644 --- a/app/javascript/mastodon/components/navigation_portal.js +++ b/app/javascript/mastodon/components/navigation_portal.js @@ -1,12 +1,13 @@ +import PropTypes from 'prop-types'; import React from 'react'; import { Switch, Route, withRouter } from 'react-router-dom'; -import { showTrends } from 'mastodon/initial_state'; +import { publicTrends, showTrends } from 'mastodon/initial_state'; import Trends from 'mastodon/features/getting_started/containers/trends_container'; import AccountNavigation from 'mastodon/features/account/navigation'; -const DefaultNavigation = () => ( +const DefaultNavigation = (signedIn) => ( <> - {showTrends && ( + {showTrends && (signedIn || publicTrends) && ( <>
@@ -15,9 +16,19 @@ const DefaultNavigation = () => ( ); +DefaultNavigation.propTypes = { + signedIn: PropTypes.bool, +}; + export default @withRouter class NavigationPortal extends React.PureComponent { + static contextTypes = { + identity: PropTypes.shape({ + signedIn: PropTypes.bool.isRequired, + }).isRequired, + }; + render () { return ( @@ -27,7 +38,9 @@ class NavigationPortal extends React.PureComponent { - + + + ); } diff --git a/app/javascript/mastodon/features/explore/index.js b/app/javascript/mastodon/features/explore/index.js index d91755ff64954..933f6672635a7 100644 --- a/app/javascript/mastodon/features/explore/index.js +++ b/app/javascript/mastodon/features/explore/index.js @@ -12,7 +12,7 @@ import Suggestions from './suggestions'; import Search from 'mastodon/features/compose/containers/search_container'; import SearchResults from './results'; import { Helmet } from 'react-helmet'; -import { showTrends } from 'mastodon/initial_state'; +import { publicTrends, showTrends } from 'mastodon/initial_state'; const messages = defineMessages({ title: { id: 'explore.title', defaultMessage: 'Explore' }, @@ -21,7 +21,8 @@ const messages = defineMessages({ const mapStateToProps = state => ({ layout: state.getIn(['meta', 'layout']), - isSearching: state.getIn(['search', 'submitted']) || !showTrends, + // !state.meta.me is the same as `this.context.identity.signedIn`, it's just hard to access context here + isSearching: state.getIn(['search', 'submitted']) || !showTrends || !state.getIn(['meta', 'me']) && !publicTrends, }); export default @connect(mapStateToProps) diff --git a/app/javascript/mastodon/features/ui/components/navigation_panel.js b/app/javascript/mastodon/features/ui/components/navigation_panel.js index 755b19349d79e..fae3277ebefd8 100644 --- a/app/javascript/mastodon/features/ui/components/navigation_panel.js +++ b/app/javascript/mastodon/features/ui/components/navigation_panel.js @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'; import { defineMessages, injectIntl } from 'react-intl'; import { Link } from 'react-router-dom'; import Logo from 'mastodon/components/logo'; -import { timelinePreview, showTrends } from 'mastodon/initial_state'; +import { timelinePreview, showTrends, publicTrends } from 'mastodon/initial_state'; import ColumnLink from './column_link'; import DisabledAccountBanner from './disabled_account_banner'; import FollowRequestsColumnLink from './follow_requests_column_link'; @@ -59,7 +59,7 @@ class NavigationPanel extends React.Component { )} - {showTrends ? ( + {showTrends && (signedIn || publicTrends) ? ( ) : ( diff --git a/app/javascript/mastodon/features/ui/index.js b/app/javascript/mastodon/features/ui/index.js index 2dd59f95d42b8..39fde8d941972 100644 --- a/app/javascript/mastodon/features/ui/index.js +++ b/app/javascript/mastodon/features/ui/index.js @@ -55,7 +55,7 @@ import { About, PrivacyPolicy, } from './util/async-components'; -import initialState, { me, owner, singleUserMode, showTrends, trendsAsLanding } from '../../initial_state'; +import initialState, { me, owner, singleUserMode, showTrends, trendsAsLanding, publicTrends } from '../../initial_state'; import { closeOnboarding, INTRODUCTION_VERSION } from 'mastodon/actions/onboarding'; import Header from './components/header'; @@ -164,7 +164,7 @@ class SwitchingColumnsArea extends React.PureComponent { } } else if (singleUserMode && owner && initialState?.accounts[owner]) { redirect = ; - } else if (showTrends && trendsAsLanding) { + } else if (showTrends && trendsAsLanding && (signedIn || publicTrends)) { redirect = ; } else { redirect = ; diff --git a/app/javascript/mastodon/initial_state.js b/app/javascript/mastodon/initial_state.js index d04c4a42d2147..7b7d04265655e 100644 --- a/app/javascript/mastodon/initial_state.js +++ b/app/javascript/mastodon/initial_state.js @@ -123,6 +123,7 @@ export const registrationsOpen = getMeta('registrations_open'); export const repository = getMeta('repository'); export const searchEnabled = getMeta('search_enabled'); export const showTrends = getMeta('trends'); +export const publicTrends = getMeta('trends_preview'); export const singleUserMode = getMeta('single_user_mode'); export const source_url = getMeta('source_url'); export const timelinePreview = getMeta('timeline_preview'); diff --git a/app/models/form/admin_settings.rb b/app/models/form/admin_settings.rb index c5cca8561615d..d903667e26f51 100644 --- a/app/models/form/admin_settings.rb +++ b/app/models/form/admin_settings.rb @@ -26,6 +26,7 @@ class Form::AdminSettings trends trends_as_landing_page trendable_by_default + trends_preview show_domain_blocks show_domain_blocks_rationale noindex @@ -53,6 +54,7 @@ class Form::AdminSettings trends trends_as_landing_page trendable_by_default + trends_preview noindex require_invite_text ).freeze diff --git a/app/serializers/initial_state_serializer.rb b/app/serializers/initial_state_serializer.rb index 4d76cc13ca17f..1f9d43f33617c 100644 --- a/app/serializers/initial_state_serializer.rb +++ b/app/serializers/initial_state_serializer.rb @@ -27,6 +27,7 @@ def meta mascot: instance_presenter.mascot&.file&.url, profile_directory: Setting.profile_directory, trends: Setting.trends, + trends_preview: Setting.trends_preview, registrations_open: Setting.registrations_mode != 'none' && !Rails.configuration.x.single_user_mode, timeline_preview: Setting.timeline_preview, activity_api_enabled: Setting.activity_api_enabled, diff --git a/app/views/admin/settings/discovery/show.html.haml b/app/views/admin/settings/discovery/show.html.haml index 759bbdcebe4b0..aa4c932772fad 100644 --- a/app/views/admin/settings/discovery/show.html.haml +++ b/app/views/admin/settings/discovery/show.html.haml @@ -24,6 +24,9 @@ .fields-group = f.input :trendable_by_default, as: :boolean, wrapper: :with_label, recommended: :not_recommended + .fields-group + = f.input :trends_preview, as: :boolean, wrapper: :with_label + %h4= t('admin.settings.discovery.public_timelines') .fields-group diff --git a/config/locales/simple_form.en.yml b/config/locales/simple_form.en.yml index a6c9fffad15ea..865c25d351214 100644 --- a/config/locales/simple_form.en.yml +++ b/config/locales/simple_form.en.yml @@ -99,6 +99,7 @@ en: trendable_by_default: Skip manual review of trending content. Individual items can still be removed from trends after the fact. trends: Trends show which posts, hashtags and news stories are gaining traction on your server. trends_as_landing_page: Show trending content to logged-out users and visitors instead of a description of this server. Requires trends to be enabled. + trends_preview: Logged out users will be able to browse trending posts, hashtags and news stories. form_challenge: current_password: You are entering a secure area imports: @@ -262,6 +263,7 @@ en: trendable_by_default: Allow trends without prior review trends: Enable trends trends_as_landing_page: Use trends as the landing page + trends_preview: Allow unauthenticated access to public trends interactions: must_be_follower: Block notifications from non-followers must_be_following: Block notifications from people you don't follow diff --git a/config/settings.yml b/config/settings.yml index 812b8e2a806da..e6376dfc013da 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -37,6 +37,7 @@ defaults: &defaults trends: true trends_as_landing_page: true trendable_by_default: false + trends_preview: true crop_images: true notification_emails: follow: true