From 2367bb31399eab8b77fa4844ac22e3a6e324f9fb Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Wed, 16 Nov 2022 22:31:49 +0100 Subject: [PATCH 1/6] Add noindex field to user --- .../20221116201723_add_privacy_noindex_to_user.rb | 11 +++++++++++ db/schema.rb | 6 +++--- 2 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20221116201723_add_privacy_noindex_to_user.rb diff --git a/db/migrate/20221116201723_add_privacy_noindex_to_user.rb b/db/migrate/20221116201723_add_privacy_noindex_to_user.rb new file mode 100644 index 00000000..b04ada0d --- /dev/null +++ b/db/migrate/20221116201723_add_privacy_noindex_to_user.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class AddPrivacyNoindexToUser < ActiveRecord::Migration[6.1] + def up + add_column :users, :privacy_noindex, :boolean, default: false + end + + def down + remove_column :users, :privacy_noindex + end +end diff --git a/db/schema.rb b/db/schema.rb index ebffd650..26f5d138 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_11_13_110942) do +ActiveRecord::Schema.define(version: 2022_11_16_201723) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -292,9 +292,9 @@ ActiveRecord::Schema.define(version: 2022_11_13_110942) do t.datetime "export_created_at" t.string "otp_secret_key" t.integer "otp_module", default: 0, null: false - t.datetime "deleted_at" - t.boolean "privacy_require_user", default: false t.boolean "privacy_lock_inbox", default: false + t.boolean "privacy_require_user", default: false + t.boolean "privacy_noindex", default: false t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true From f8e43a9bb05cda6122f7ee8f434d8459616be8d4 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Wed, 16 Nov 2022 22:32:40 +0100 Subject: [PATCH 2/6] Add noindex setting to privacy settings view --- app/controllers/settings/privacy_controller.rb | 3 ++- app/views/settings/privacy/edit.html.haml | 1 + config/locales/activerecord.en.yml | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/controllers/settings/privacy_controller.rb b/app/controllers/settings/privacy_controller.rb index 55d553e8..af5d5270 100644 --- a/app/controllers/settings/privacy_controller.rb +++ b/app/controllers/settings/privacy_controller.rb @@ -11,7 +11,8 @@ class Settings::PrivacyController < ApplicationController :privacy_allow_public_timeline, :privacy_allow_stranger_answers, :privacy_show_in_search, - :privacy_require_user) + :privacy_require_user, + :privacy_noindex) if current_user.update(user_attributes) flash[:success] = t(".success") else diff --git a/app/views/settings/privacy/edit.html.haml b/app/views/settings/privacy/edit.html.haml index a0f25626..27a33167 100644 --- a/app/views/settings/privacy/edit.html.haml +++ b/app/views/settings/privacy/edit.html.haml @@ -6,6 +6,7 @@ = f.check_box :privacy_require_user = f.check_box :privacy_allow_public_timeline = f.check_box :privacy_allow_stranger_answers + = f.check_box :privacy_noindex = f.primary diff --git a/config/locales/activerecord.en.yml b/config/locales/activerecord.en.yml index 956a3d5e..36e4336f 100644 --- a/config/locales/activerecord.en.yml +++ b/config/locales/activerecord.en.yml @@ -72,6 +72,7 @@ en: privacy_require_user: "Require users to be logged in to ask you questions" privacy_allow_public_timeline: "Show your answers in the public timeline" privacy_allow_stranger_answers: "Allow other people to answer your questions" + privacy_noindex: "Prevent search engines from indexing your profile" profile_picture: "Profile picture" profile_header: "Profile header" sign_in_count: "Sign in count" From 4c820b472e4983f67590c04f51e79280a04f5005 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Wed, 16 Nov 2022 22:33:04 +0100 Subject: [PATCH 3/6] Set noindex robots tag on content of users that enable it --- app/views/layouts/base.html.haml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/views/layouts/base.html.haml b/app/views/layouts/base.html.haml index a0e37c09..e5a7748d 100644 --- a/app/views/layouts/base.html.haml +++ b/app/views/layouts/base.html.haml @@ -9,6 +9,8 @@ %meta{ name: 'theme-color', content: mobile_theme_color, media: '(max-width: 992px)' } - else %meta{ name: 'theme-color', content: theme_color } + - if @user&.privacy_noindex? || @answer&.user&.privacy_noindex? || @question&.user&.privacy_noindex? + %meta{ name: 'robots', content: 'noindex' } %link{ rel: 'manifest', href: '/manifest.json', crossorigin: 'use-credentials' } %link{ rel: 'apple-touch-icon', href: '/icons/maskable_icon_x192.png' } %link{ rel: 'mask-icon', href: '/icons/icon.svg', color: theme_color } From 23fe0a2c6e9cbccfb064c6536c362d9ade5281eb Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Wed, 16 Nov 2022 22:33:30 +0100 Subject: [PATCH 4/6] Add tests for noindex setting --- spec/controllers/answer_controller_spec.rb | 16 ++++++++++++++++ spec/controllers/user_controller_spec.rb | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/spec/controllers/answer_controller_spec.rb b/spec/controllers/answer_controller_spec.rb index bca4b142..f58be191 100644 --- a/spec/controllers/answer_controller_spec.rb +++ b/spec/controllers/answer_controller_spec.rb @@ -18,5 +18,21 @@ describe AnswerController do expect(response).to render_template("answer/show") end end + + context "user opts out of search indexing" do + render_views + + before(:each) { + sign_in user + user.privacy_noindex = true + user.save + } + + it "renders the answer/show template" do + subject + expect(assigns(:answer)).to eq(answer) + expect(response.body).to include("") + end + end end end diff --git a/spec/controllers/user_controller_spec.rb b/spec/controllers/user_controller_spec.rb index 11599f0a..cccb727b 100644 --- a/spec/controllers/user_controller_spec.rb +++ b/spec/controllers/user_controller_spec.rb @@ -21,6 +21,22 @@ describe UserController, type: :controller do expect(response).to render_template("user/show") end end + + context "user opts out of search indexing" do + render_views + + before(:each) { + sign_in user + user.privacy_noindex = true + user.save + } + + it "renders the answer/show template" do + subject + expect(assigns(:user)).to eq(user) + expect(response.body).to include("") + end + end end describe "#followers" do From 3ddba9ae8afafbfa1061d4afc04e7766188ca0c3 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Wed, 16 Nov 2022 22:34:57 +0100 Subject: [PATCH 5/6] Fix lints --- spec/controllers/answer_controller_spec.rb | 18 +++++++++++------- spec/controllers/user_controller_spec.rb | 4 ++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/spec/controllers/answer_controller_spec.rb b/spec/controllers/answer_controller_spec.rb index f58be191..b2c39fd5 100644 --- a/spec/controllers/answer_controller_spec.rb +++ b/spec/controllers/answer_controller_spec.rb @@ -1,10 +1,14 @@ -require 'rails_helper' +# frozen_string_literal: true + +require "rails_helper" describe AnswerController do - let(:user) { FactoryBot.create :user, - otp_module: :disabled, - otp_secret_key: 'AAAAAAAA'} - let(:answer) { FactoryBot.create :answer, user: user } + let(:user) do + FactoryBot.create :user, + otp_module: :disabled, + otp_secret_key: "AAAAAAAA" + end + let(:answer) { FactoryBot.create :answer, user: } describe "#show" do subject { get :show, params: { username: user.screen_name, id: answer.id } } @@ -22,11 +26,11 @@ describe AnswerController do context "user opts out of search indexing" do render_views - before(:each) { + before(:each) do sign_in user user.privacy_noindex = true user.save - } + end it "renders the answer/show template" do subject diff --git a/spec/controllers/user_controller_spec.rb b/spec/controllers/user_controller_spec.rb index cccb727b..de804e32 100644 --- a/spec/controllers/user_controller_spec.rb +++ b/spec/controllers/user_controller_spec.rb @@ -25,11 +25,11 @@ describe UserController, type: :controller do context "user opts out of search indexing" do render_views - before(:each) { + before(:each) do sign_in user user.privacy_noindex = true user.save - } + end it "renders the answer/show template" do subject From 60819ff90e8127ee659f276a17d5a10845ecc4fe Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Fri, 18 Nov 2022 23:29:47 +0100 Subject: [PATCH 6/6] Fix schema --- db/schema.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/db/schema.rb b/db/schema.rb index c4f47f43..f871d86d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -296,6 +296,7 @@ ActiveRecord::Schema.define(version: 2022_11_16_201723) do t.boolean "privacy_lock_inbox", default: false t.boolean "privacy_require_user", default: false t.boolean "privacy_noindex", default: false + t.boolean "privacy_hide_social_graph", default: false t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true