From 556c07df1f30482782cd94070d23b31ffdefca89 Mon Sep 17 00:00:00 2001 From: ysksn Date: Thu, 16 Nov 2017 15:46:29 +0900 Subject: [PATCH 1/7] Add tests for Status#verb (#5717) --- spec/models/status_spec.rb | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/spec/models/status_spec.rb b/spec/models/status_spec.rb index 0b8ed66f6..d58ff5019 100644 --- a/spec/models/status_spec.rb +++ b/spec/models/status_spec.rb @@ -47,8 +47,27 @@ RSpec.describe Status, type: :model do end describe '#verb' do - it 'is always post' do - expect(subject.verb).to be :post + context 'if destroyed?' do + it 'returns :delete' do + subject.destroy! + expect(subject.verb).to be :delete + end + end + + context 'unless destroyed?' do + context 'if reblog?' do + it 'returns :share' do + subject.reblog = other + expect(subject.verb).to be :share + end + end + + context 'unless reblog?' do + it 'returns :post' do + subject.reblog = nil + expect(subject.verb).to be :post + end + end end end From 9e3d24a150acf118789461797735fc0e4a8a30ea Mon Sep 17 00:00:00 2001 From: ysksn Date: Thu, 16 Nov 2017 20:13:03 +0900 Subject: [PATCH 2/7] Fix AccountFinderConcern::AccountFinder#with_usernames (#5722) --- app/models/concerns/account_finder_concern.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/concerns/account_finder_concern.rb b/app/models/concerns/account_finder_concern.rb index 561c7ab9f..2e8a7fb37 100644 --- a/app/models/concerns/account_finder_concern.rb +++ b/app/models/concerns/account_finder_concern.rb @@ -44,7 +44,7 @@ module AccountFinderConcern end def with_usernames - Account.where.not(username: [nil, '']) + Account.where.not(username: '') end def matching_username From ad207456d64c76d21c17b26a954b459fe2dc0f54 Mon Sep 17 00:00:00 2001 From: "Renato \"Lond\" Cerqueira" Date: Thu, 16 Nov 2017 10:51:38 -0200 Subject: [PATCH 3/7] Improve language filter (#5724) * Scrub text of html before detecting language. * Detect language on statuses coming from activitypub. * Fix rubocop comments. * Remove custom emoji from text before language detection --- app/lib/activitypub/activity/create.rb | 2 +- app/lib/language_detector.rb | 31 +++++++++++++++++++++----- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/app/lib/activitypub/activity/create.rb b/app/lib/activitypub/activity/create.rb index 376684c00..66e4f7c5e 100644 --- a/app/lib/activitypub/activity/create.rb +++ b/app/lib/activitypub/activity/create.rb @@ -173,7 +173,7 @@ class ActivityPub::Activity::Create < ActivityPub::Activity end def language_from_content - return nil unless language_map? + return LanguageDetector.instance.detect(text_from_content, @account) unless language_map? @object['contentMap'].keys.first end diff --git a/app/lib/language_detector.rb b/app/lib/language_detector.rb index a42460e10..c6f52f0c7 100644 --- a/app/lib/language_detector.rb +++ b/app/lib/language_detector.rb @@ -38,12 +38,31 @@ class LanguageDetector end def simplify_text(text) - text.dup.tap do |new_text| - new_text.gsub!(FetchLinkCardService::URL_PATTERN, '') - new_text.gsub!(Account::MENTION_RE, '') - new_text.gsub!(Tag::HASHTAG_RE, '') - new_text.gsub!(/\s+/, ' ') - end + new_text = remove_html(text) + new_text.gsub!(FetchLinkCardService::URL_PATTERN, '') + new_text.gsub!(Account::MENTION_RE, '') + new_text.gsub!(Tag::HASHTAG_RE, '') + new_text.gsub!(/:#{CustomEmoji::SHORTCODE_RE_FRAGMENT}:/, '') + new_text.gsub!(/\s+/, ' ') + new_text + end + + def new_scrubber + scrubber = Rails::Html::PermitScrubber.new + scrubber.tags = %w(br p) + scrubber + end + + def scrubber + @scrubber ||= new_scrubber + end + + def remove_html(text) + text = Loofah.fragment(text).scrub!(scrubber).to_s + text.gsub!('
', "\n") + text.gsub!('

', "\n\n") + text.gsub!(/(^

|<\/p>$)/, '') + text end def default_locale(account) From 2151fd315023a7f6849c6c9a519cd01deac09aa1 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 17 Nov 2017 01:22:38 +0100 Subject: [PATCH 4/7] Display moderator badge (green), admin badge changed to red (#5728) --- app/javascript/styles/mastodon/accounts.scss | 18 +++++++++++++++--- app/models/account.rb | 2 ++ app/views/accounts/_header.html.haml | 6 +++++- config/locales/en.yml | 1 + 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/app/javascript/styles/mastodon/accounts.scss b/app/javascript/styles/mastodon/accounts.scss index 30adf8cdc..ca55de28d 100644 --- a/app/javascript/styles/mastodon/accounts.scss +++ b/app/javascript/styles/mastodon/accounts.scss @@ -531,7 +531,19 @@ font-size: 12px; line-height: 12px; font-weight: 500; - color: $success-green; - background-color: rgba($success-green, 0.1); - border: 1px solid rgba($success-green, 0.5); + color: $ui-secondary-color; + background-color: rgba($ui-secondary-color, 0.1); + border: 1px solid rgba($ui-secondary-color, 0.5); + + &.moderator { + color: $success-green; + background-color: rgba($success-green, 0.1); + border-color: rgba($success-green, 0.5); + } + + &.admin { + color: $error-red; + background-color: rgba($error-red, 0.1); + border-color: rgba($error-red, 0.5); + } } diff --git a/app/models/account.rb b/app/models/account.rb index 6f6010f7a..bc01d2448 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -115,6 +115,8 @@ class Account < ApplicationRecord :current_sign_in_at, :confirmed?, :admin?, + :moderator?, + :staff?, :locale, to: :user, prefix: true, diff --git a/app/views/accounts/_header.html.haml b/app/views/accounts/_header.html.haml index 5530fcc20..e4c258acd 100644 --- a/app/views/accounts/_header.html.haml +++ b/app/views/accounts/_header.html.haml @@ -29,8 +29,12 @@ - if account.user_admin? .roles - .account-role + .account-role.admin = t 'accounts.roles.admin' + - elsif account.user_moderator? + .roles + .account-role.moderator + = t 'accounts.roles.moderator' .bio .account__header__content.p-note.emojify= Formatter.instance.simplified_format(account) diff --git a/config/locales/en.yml b/config/locales/en.yml index e94165317..11c34b912 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -48,6 +48,7 @@ en: reserved_username: The username is reserved roles: admin: Admin + moderator: Mod unfollow: Unfollow admin: account_moderation_notes: From 3e90987c8b0c620b6fc104350b8eb16d17d7e80a Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 17 Nov 2017 02:06:26 +0100 Subject: [PATCH 5/7] Fix some rubocop style issues (#5730) --- app/controllers/application_controller.rb | 2 +- app/controllers/auth/sessions_controller.rb | 2 +- app/helpers/admin/filter_helper.rb | 2 +- app/lib/extractor.rb | 3 ++- app/models/web/push_subscription.rb | 4 ++-- app/services/batched_remove_status_service.rb | 2 +- app/services/resolve_remote_account_service.rb | 4 ++-- 7 files changed, 10 insertions(+), 9 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f41a7f9be..a213302cb 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -104,7 +104,7 @@ class ApplicationController < ActionController::Base unless uncached_ids.empty? uncached = klass.where(id: uncached_ids).with_includes.map { |item| [item.id, item] }.to_h - uncached.values.each do |item| + uncached.each_value do |item| Rails.cache.write(item.cache_key, item) end end diff --git a/app/controllers/auth/sessions_controller.rb b/app/controllers/auth/sessions_controller.rb index 463a183e4..a5acb6c36 100644 --- a/app/controllers/auth/sessions_controller.rb +++ b/app/controllers/auth/sessions_controller.rb @@ -62,7 +62,7 @@ class Auth::SessionsController < Devise::SessionsController if user_params[:otp_attempt].present? && session[:otp_user_id] authenticate_with_two_factor_via_otp(user) - elsif user && user.valid_password?(user_params[:password]) + elsif user&.valid_password?(user_params[:password]) prompt_for_two_factor(user) end end diff --git a/app/helpers/admin/filter_helper.rb b/app/helpers/admin/filter_helper.rb index 6a57b3d63..e0fae9d9a 100644 --- a/app/helpers/admin/filter_helper.rb +++ b/app/helpers/admin/filter_helper.rb @@ -18,7 +18,7 @@ module Admin::FilterHelper def selected?(more_params) new_url = filtered_url_for(more_params) - filter_link_class(new_url) == 'selected' ? true : false + filter_link_class(new_url) == 'selected' end private diff --git a/app/lib/extractor.rb b/app/lib/extractor.rb index 957364293..738ec89a0 100644 --- a/app/lib/extractor.rb +++ b/app/lib/extractor.rb @@ -5,7 +5,8 @@ module Extractor module_function - def extract_mentions_or_lists_with_indices(text) # :yields: username, list_slug, start, end + # :yields: username, list_slug, start, end + def extract_mentions_or_lists_with_indices(text) return [] unless text =~ Twitter::Regex[:at_signs] possible_entries = [] diff --git a/app/models/web/push_subscription.rb b/app/models/web/push_subscription.rb index da67e7665..a41906227 100644 --- a/app/models/web/push_subscription.rb +++ b/app/models/web/push_subscription.rb @@ -24,12 +24,12 @@ class Web::PushSubscription < ApplicationRecord end def pushable?(notification) - data && data.key?('alerts') && data['alerts'][notification.type.to_s] + data&.key?('alerts') && data['alerts'][notification.type.to_s] end def as_payload payload = { id: id, endpoint: endpoint } - payload[:alerts] = data['alerts'] if data && data.key?('alerts') + payload[:alerts] = data['alerts'] if data&.key?('alerts') payload end diff --git a/app/services/batched_remove_status_service.rb b/app/services/batched_remove_status_service.rb index 5d83771c9..676a5d04d 100644 --- a/app/services/batched_remove_status_service.rb +++ b/app/services/batched_remove_status_service.rb @@ -26,7 +26,7 @@ class BatchedRemoveStatusService < BaseService statuses.each(&:destroy) # Batch by source account - statuses.group_by(&:account_id).each do |_, account_statuses| + statuses.group_by(&:account_id).each_value do |account_statuses| account = account_statuses.first.account unpush_from_home_timelines(account, account_statuses) diff --git a/app/services/resolve_remote_account_service.rb b/app/services/resolve_remote_account_service.rb index 3d0a36f6c..3293fe40f 100644 --- a/app/services/resolve_remote_account_service.rb +++ b/app/services/resolve_remote_account_service.rb @@ -124,11 +124,11 @@ class ResolveRemoteAccountService < BaseService end def auto_suspend? - domain_block && domain_block.suspend? + domain_block&.suspend? end def auto_silence? - domain_block && domain_block.silence? + domain_block&.silence? end def domain_block From 9dd5e329ab5edab705c5622bc97f93bd5c50976b Mon Sep 17 00:00:00 2001 From: ysksn Date: Fri, 17 Nov 2017 10:52:30 +0900 Subject: [PATCH 6/7] Remove empty strings (#5732) --- app/views/settings/applications/new.html.haml | 2 +- .../settings/applications/show.html.haml | 2 +- lib/mastodon/migration_helpers.rb | 56 +++++++++--------- .../settings/applications_controller_spec.rb | 10 ++-- spec/fixtures/requests/attachment1.txt | Bin 192053 -> 192052 bytes spec/fixtures/requests/attachment2.txt | Bin 109078 -> 109077 bytes spec/fixtures/requests/avatar.txt | Bin 109962 -> 109961 bytes spec/fixtures/requests/idn.txt | 14 ++--- spec/helpers/stream_entries_helper_spec.rb | 4 +- spec/lib/user_settings_decorator_spec.rb | 2 +- spec/models/remote_follow_spec.rb | 2 +- .../fetch_remote_status_service_spec.rb | 2 +- .../model/model_have_error_on_field.rb | 2 +- 13 files changed, 48 insertions(+), 48 deletions(-) diff --git a/app/views/settings/applications/new.html.haml b/app/views/settings/applications/new.html.haml index 5274a430c..aa2281fea 100644 --- a/app/views/settings/applications/new.html.haml +++ b/app/views/settings/applications/new.html.haml @@ -3,6 +3,6 @@ = simple_form_for @application, url: settings_applications_path do |f| = render 'fields', f: f - + .actions = f.button :button, t('doorkeeper.applications.buttons.submit'), type: :submit diff --git a/app/views/settings/applications/show.html.haml b/app/views/settings/applications/show.html.haml index 12baed088..390682d6f 100644 --- a/app/views/settings/applications/show.html.haml +++ b/app/views/settings/applications/show.html.haml @@ -25,7 +25,7 @@ = simple_form_for @application, url: settings_application_path(@application), method: :put do |f| = render 'fields', f: f - + .actions = f.button :button, t('generic.save_changes'), type: :submit diff --git a/lib/mastodon/migration_helpers.rb b/lib/mastodon/migration_helpers.rb index 80a8f440c..2b5a6cd42 100644 --- a/lib/mastodon/migration_helpers.rb +++ b/lib/mastodon/migration_helpers.rb @@ -84,7 +84,7 @@ module Mastodon BACKGROUND_MIGRATION_BATCH_SIZE = 1000 # Number of rows to process per job BACKGROUND_MIGRATION_JOB_BUFFER_SIZE = 1000 # Number of jobs to bulk queue at a time - + # Gets an estimated number of rows for a table def estimate_rows_in_table(table_name) exec_query('SELECT reltuples FROM pg_class WHERE relname = ' + @@ -313,14 +313,14 @@ module Mastodon end table = Arel::Table.new(table_name) - + total = estimate_rows_in_table(table_name).to_i if total == 0 count_arel = table.project(Arel.star.count.as('count')) count_arel = yield table, count_arel if block_given? - + total = exec_query(count_arel.to_sql).to_hash.first['count'].to_i - + return if total == 0 end @@ -339,7 +339,7 @@ module Mastodon # In case there are no rows but we didn't catch it in the estimated size: return unless first_row start_id = first_row['id'].to_i - + say "Migrating #{table_name}.#{column} (~#{total.to_i} rows)" started_time = Time.now @@ -347,7 +347,7 @@ module Mastodon migrated = 0 loop do stop_row = nil - + suppress_messages do stop_arel = table.project(table[:id]) .where(table[:id].gteq(start_id)) @@ -373,29 +373,29 @@ module Mastodon execute(update_arel.to_sql) end - + migrated += batch_size if Time.now - last_time > 1 status = "Migrated #{migrated} rows" - + percentage = 100.0 * migrated / total status += " (~#{sprintf('%.2f', percentage)}%, " - + remaining_time = (100.0 - percentage) * (Time.now - started_time) / percentage - + status += "#{(remaining_time / 60).to_i}:" status += sprintf('%02d', remaining_time.to_i % 60) status += ' remaining, ' - + # Tell users not to interrupt if we're almost done. if remaining_time > 10 status += 'safe to interrupt' else status += 'DO NOT interrupt' end - + status += ')' - + say status, true last_time = Time.now end @@ -483,7 +483,7 @@ module Mastodon check_trigger_permissions!(table) trigger_name = rename_trigger_name(table, old, new) - + # If we were in the middle of update_column_in_batches, we should remove # the old column and start over, as we have no idea where we were. if column_for(table, new) @@ -492,7 +492,7 @@ module Mastodon else remove_rename_triggers_for_mysql(trigger_name) end - + remove_column(table, new) end @@ -546,12 +546,12 @@ module Mastodon temp_column = rename_column_name(column) rename_column_concurrently(table, column, temp_column, type: new_type) - + # Primary keys don't necessarily have an associated index. if ActiveRecord::Base.get_primary_key(table) == column.to_s old_pk_index_name = "index_#{table}_on_#{column}" new_pk_index_name = "index_#{table}_on_#{column}_cm" - + unless indexes_for(table, column).find{|i| i.name == old_pk_index_name} add_concurrent_index(table, [temp_column], { unique: true, @@ -572,14 +572,14 @@ module Mastodon # Wait for the indices to be built indexes_for(table, column).each do |index| expected_name = index.name + '_cm' - + puts "Waiting for index #{expected_name}" sleep 1 until indexes_for(table, temp_column).find {|i| i.name == expected_name } end - + was_primary = (ActiveRecord::Base.get_primary_key(table) == column.to_s) old_default_fn = column_for(table, column).default_function - + old_fks = [] if was_primary # Get any foreign keys pointing at this column we need to recreate, and @@ -613,7 +613,7 @@ module Mastodon target_col: temp_column, on_delete: extract_foreign_key_action(old_fk['on_delete']) ) - + remove_foreign_key(old_fk['src_table'], name: old_fk['name']) end end @@ -629,15 +629,15 @@ module Mastodon transaction do # This has to be performed in a transaction as otherwise we might have # inconsistent data. - + cleanup_concurrent_column_rename(table, column, temp_column) rename_column(table, temp_column, column) - + # If there was an old default function, we didn't copy it. Do that now # in the transaction, so we don't miss anything. change_column_default(table, column, -> { old_default_fn }) if old_default_fn end - + # Rename any indices back to what they should be. indexes_for(table, column).each do |index| next unless index.name.end_with?('_cm') @@ -645,7 +645,7 @@ module Mastodon real_index_name = index.name.sub(/_cm$/, '') rename_index(table, index.name, real_index_name) end - + # Rename any foreign keys back to names based on the real column. foreign_keys_for(table, column).each do |fk| old_fk_name = concurrent_foreign_key_name(fk.from_table, temp_column, 'id') @@ -653,7 +653,7 @@ module Mastodon execute("ALTER TABLE #{fk.from_table} RENAME CONSTRAINT " + "#{old_fk_name} TO #{new_fk_name}") end - + # Rename any foreign keys from other tables to names based on the real # column. old_fks.each do |old_fk| @@ -664,7 +664,7 @@ module Mastodon execute("ALTER TABLE #{old_fk['src_table']} RENAME CONSTRAINT " + "#{old_fk_name} TO #{new_fk_name}") end - + # If the old column was a primary key, mark the new one as a primary key. if was_primary execute("ALTER TABLE #{table} ADD PRIMARY KEY USING INDEX " + @@ -791,7 +791,7 @@ module Mastodon # This is necessary as we can't properly rename indexes such as # "ci_taggings_idx". name = index.name + '_cm' - + # If the order contained the old column, map it to the new one. order = index.orders if order.key?(old) diff --git a/spec/controllers/settings/applications_controller_spec.rb b/spec/controllers/settings/applications_controller_spec.rb index ca66f8d23..90e6a63d5 100644 --- a/spec/controllers/settings/applications_controller_spec.rb +++ b/spec/controllers/settings/applications_controller_spec.rb @@ -2,10 +2,10 @@ require 'rails_helper' describe Settings::ApplicationsController do render_views - + let!(:user) { Fabricate(:user) } let!(:app) { Fabricate(:application, owner: user) } - + before do sign_in user, scope: :user end @@ -21,7 +21,7 @@ describe Settings::ApplicationsController do end end - + describe 'GET #show' do it 'returns http success' do get :show, params: { id: app.id } @@ -110,7 +110,7 @@ describe Settings::ApplicationsController do end end end - + describe 'PATCH #update' do context 'success' do let(:opts) { @@ -131,7 +131,7 @@ describe Settings::ApplicationsController do call_update expect(app.reload.website).to eql(opts[:website]) end - + it 'redirects back to applications page' do expect(call_update).to redirect_to(settings_applications_path) end diff --git a/spec/fixtures/requests/attachment1.txt b/spec/fixtures/requests/attachment1.txt index 77fd9c83612cdc950bec101058febd578203edeb..30bd456befaa03f30e96da613b7cfa0f6eaf62ad 100644 GIT binary patch delta 18 acmdmbhkMH%?g_6O-?YAA-1>&e^DO{m$_i@$ delta 20 ccmdmThkNTC?g_6Mc^hB1zGmF|n#uDm0B*Djb^rhX diff --git a/spec/fixtures/requests/attachment2.txt b/spec/fixtures/requests/attachment2.txt index 917a1d3987305f43ae88e57f45f0dbfca1ed7cc9..2a252d2def99241a209e0670916531d5c8370666 100644 GIT binary patch delta 15 XcmbPsg>C8;wh6Br-)w!usI?LRLc<6> delta 17 ZcmbPwg>Bjuwh6Bpc^hAEea)z~5&%ox2uT0{ diff --git a/spec/fixtures/requests/avatar.txt b/spec/fixtures/requests/avatar.txt index d57b0984fb2069a45592084c47e3c463fea878d9..d771f5dda20a43340234726b13eeffa717e3cc2f 100644 GIT binary patch delta 18 ZcmeCW&DMFFZ9^qva~0$EDn>?uH2_L(2JHX< delta 20 acmeCY&DM3BZ9^p^BX4sh<8~k|um%89>jwD% diff --git a/spec/fixtures/requests/idn.txt b/spec/fixtures/requests/idn.txt index 3c76c59c0..5d07f2b79 100644 --- a/spec/fixtures/requests/idn.txt +++ b/spec/fixtures/requests/idn.txt @@ -6,7 +6,7 @@ Content-Length: 38111 Last-Modified: Wed, 20 Jul 2016 02:50:52 GMT Connection: keep-alive Accept-Ranges: bytes - + @@ -21,16 +21,16 @@ Accept-Ranges: bytes var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); - + - - + + 中国域名网站 - +

@@ -453,7 +453,7 @@ Accept-Ranges: bytes
  • 新疆农业大学.中国
  • 浙江万里学院.中国
  • 重庆大学.中国
  • - +
    @@ -472,7 +472,7 @@ Accept-Ranges: bytes