Skip to content

Commit

Permalink
Support disabling public listing of trends
Browse files Browse the repository at this point in the history
# Conflicts:
#	app/javascript/mastodon/components/navigation_portal.js
  • Loading branch information
dwrss committed Jul 7, 2023
1 parent 4aeffc9 commit a1fa7b0
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 10 deletions.
5 changes: 5 additions & 0 deletions app/controllers/api/v1/trends/links_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,6 +14,10 @@ def index

private

def require_auth?
!Setting.trends_preview
end

def enabled?
Setting.trends
end
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/api/v1/trends/statuses_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,6 +12,10 @@ def index

private

def require_auth?
!Setting.trends_preview
end

def enabled?
Setting.trends
end
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/api/v1/trends/tags_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,6 +14,10 @@ def index

private

def require_auth?
!Setting.trends_preview
end

def enabled?
Setting.trends
end
Expand Down
21 changes: 17 additions & 4 deletions app/javascript/mastodon/components/navigation_portal.js
Original file line number Diff line number Diff line change
@@ -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) && (
<>
<div className='flex-spacer' />
<Trends />
Expand All @@ -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 (
<Switch>
Expand All @@ -27,7 +38,9 @@ class NavigationPortal extends React.PureComponent {
<Route path='/@:acct/followers' exact component={AccountNavigation} />
<Route path='/@:acct/following' exact component={AccountNavigation} />
<Route path='/@:acct/media' exact component={AccountNavigation} />
<Route component={DefaultNavigation} />
<Route>
<DefaultNavigation signedIn={this.context.identity.signedIn} />
</Route>
</Switch>
);
}
Expand Down
5 changes: 3 additions & 2 deletions app/javascript/mastodon/features/explore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -59,7 +59,7 @@ class NavigationPanel extends React.Component {
</React.Fragment>
)}

{showTrends ? (
{showTrends && (signedIn || publicTrends) ? (
<ColumnLink transparent to='/explore' icon='hashtag' text={intl.formatMessage(messages.explore)} />
) : (
<ColumnLink transparent to='/search' icon='search' text={intl.formatMessage(messages.search)} />
Expand Down
4 changes: 2 additions & 2 deletions app/javascript/mastodon/features/ui/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -164,7 +164,7 @@ class SwitchingColumnsArea extends React.PureComponent {
}
} else if (singleUserMode && owner && initialState?.accounts[owner]) {
redirect = <Redirect from='/' to={`/@${initialState.accounts[owner].username}`} exact />;
} else if (showTrends && trendsAsLanding) {
} else if (showTrends && trendsAsLanding && (signedIn || publicTrends)) {
redirect = <Redirect from='/' to='/explore' exact />;
} else {
redirect = <Redirect from='/' to='/about' exact />;
Expand Down
1 change: 1 addition & 0 deletions app/javascript/mastodon/initial_state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 2 additions & 0 deletions app/models/form/admin_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,6 +54,7 @@ class Form::AdminSettings
trends
trends_as_landing_page
trendable_by_default
trends_preview
noindex
require_invite_text
).freeze
Expand Down
1 change: 1 addition & 0 deletions app/serializers/initial_state_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions app/views/admin/settings/discovery/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions config/locales/simple_form.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a1fa7b0

Please sign in to comment.