From 465ee7792ff48905088efdf3df6f718081b5e244 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 7 Apr 2022 18:06:15 +0200 Subject: Fix pagination header on empty trends responses in REST API (#17986) --- app/models/trends/query.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/models') diff --git a/app/models/trends/query.rb b/app/models/trends/query.rb index 231b65228..f19df162f 100644 --- a/app/models/trends/query.rb +++ b/app/models/trends/query.rb @@ -59,7 +59,7 @@ class Trends::Query @records end - delegate :each, :empty?, :first, :last, to: :records + delegate :each, :empty?, :first, :last, :size, to: :records def to_ary records.dup -- cgit From fd9a9b07c2cd19ef08d15e138fd3fc59fc5318b6 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 8 Apr 2022 17:10:53 +0200 Subject: Fix trends returning less results per page when filtered in REST API (#17996) - Change filtering and pagination to occur in SQL instead of Redis - Change rank/score displayed on trends in admin UI to be locale-specific --- app/models/trends/base.rb | 8 ++++---- app/models/trends/query.rb | 11 ++++++---- app/models/trends/statuses.rb | 24 +++++----------------- .../admin/trends/links/_preview_card.html.haml | 4 ++-- app/views/admin/trends/statuses/_status.html.haml | 4 ++-- 5 files changed, 20 insertions(+), 31 deletions(-) (limited to 'app/models') diff --git a/app/models/trends/base.rb b/app/models/trends/base.rb index 7ed13228d..38a49246b 100644 --- a/app/models/trends/base.rb +++ b/app/models/trends/base.rb @@ -37,12 +37,12 @@ class Trends::Base Trends::Query.new(key_prefix, klass) end - def score(id) - redis.zscore("#{key_prefix}:all", id) || 0 + def score(id, locale: nil) + redis.zscore([key_prefix, 'all', locale].compact.join(':'), id) || 0 end - def rank(id) - redis.zrevrank("#{key_prefix}:allowed", id) + def rank(id, locale: nil) + redis.zrevrank([key_prefix, 'allowed', locale].compact.join(':'), id) end def currently_trending_ids(allowed, limit) diff --git a/app/models/trends/query.rb b/app/models/trends/query.rb index f19df162f..cd5571bc6 100644 --- a/app/models/trends/query.rb +++ b/app/models/trends/query.rb @@ -14,8 +14,8 @@ class Trends::Query @records = [] @loaded = false @allowed = false - @limit = -1 - @offset = 0 + @limit = nil + @offset = nil end def allowed! @@ -73,7 +73,10 @@ class Trends::Query if tmp_ids.empty? klass.none else - klass.joins("join unnest(array[#{tmp_ids.join(',')}]) with ordinality as x (id, ordering) on #{klass.table_name}.id = x.id").reorder('x.ordering') + scope = klass.joins("join unnest(array[#{tmp_ids.join(',')}]) with ordinality as x (id, ordering) on #{klass.table_name}.id = x.id").reorder('x.ordering') + scope = scope.offset(@offset) if @offset.present? + scope = scope.limit(@limit) if @limit.present? + scope end end @@ -93,7 +96,7 @@ class Trends::Query end def ids - redis.zrevrange(key, @offset, @limit.positive? ? @limit - 1 : @limit).map(&:to_i) + redis.zrevrange(key, 0, -1).map(&:to_i) end def perform_queries diff --git a/app/models/trends/statuses.rb b/app/models/trends/statuses.rb index e785413ec..dc5309554 100644 --- a/app/models/trends/statuses.rb +++ b/app/models/trends/statuses.rb @@ -22,25 +22,11 @@ class Trends::Statuses < Trends::Base private def apply_scopes(scope) - scope.includes(:account) - end - - def perform_queries - return super if @account.nil? - - statuses = super - account_ids = statuses.map(&:account_id) - account_domains = statuses.map(&:account_domain) - - preloaded_relations = { - blocking: Account.blocking_map(account_ids, @account.id), - blocked_by: Account.blocked_by_map(account_ids, @account.id), - muting: Account.muting_map(account_ids, @account.id), - following: Account.following_map(account_ids, @account.id), - domain_blocking_by_domain: Account.domain_blocking_map_by_domain(account_domains, @account.id), - } - - statuses.reject { |status| StatusFilter.new(status, @account, preloaded_relations).filtered? } + if @account.nil? + scope + else + scope.not_excluded_by_account(@account).not_domain_blocked_by_account(@account) + end end end diff --git a/app/views/admin/trends/links/_preview_card.html.haml b/app/views/admin/trends/links/_preview_card.html.haml index 2e6a0c62f..7d4897c7e 100644 --- a/app/views/admin/trends/links/_preview_card.html.haml +++ b/app/views/admin/trends/links/_preview_card.html.haml @@ -18,9 +18,9 @@ = t('admin.trends.links.shared_by_over_week', count: preview_card.history.reduce(0) { |sum, day| sum + day.accounts }) - - if preview_card.trendable? && (rank = Trends.links.rank(preview_card.id)) + - if preview_card.trendable? && (rank = Trends.links.rank(preview_card.id, locale: params[:locale].presence)) • - %abbr{ title: t('admin.trends.tags.current_score', score: Trends.links.score(preview_card.id)) }= t('admin.trends.tags.trending_rank', rank: rank + 1) + %abbr{ title: t('admin.trends.tags.current_score', score: Trends.links.score(preview_card.id, locale: params[:locale].presence)) }= t('admin.trends.tags.trending_rank', rank: rank + 1) - if preview_card.decaying? • diff --git a/app/views/admin/trends/statuses/_status.html.haml b/app/views/admin/trends/statuses/_status.html.haml index 0c463c6b1..e4d75bbb9 100644 --- a/app/views/admin/trends/statuses/_status.html.haml +++ b/app/views/admin/trends/statuses/_status.html.haml @@ -25,9 +25,9 @@ - if status.trendable? && !status.account.discoverable? • = t('admin.trends.statuses.not_discoverable') - - if status.trendable? && (rank = Trends.statuses.rank(status.id)) + - if status.trendable? && (rank = Trends.statuses.rank(status.id, locale: params[:locale].presence)) • - %abbr{ title: t('admin.trends.tags.current_score', score: Trends.statuses.score(status.id)) }= t('admin.trends.tags.trending_rank', rank: rank + 1) + %abbr{ title: t('admin.trends.tags.current_score', score: Trends.statuses.score(status.id, locale: params[:locale].presence)) }= t('admin.trends.tags.trending_rank', rank: rank + 1) - elsif status.requires_review? • = t('admin.trends.pending_review') -- cgit From 8e20e16cf030fef48850df4764bbf13a317f6b0c Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 8 Apr 2022 18:03:31 +0200 Subject: Change e-mail notifications to only be sent when recipient is offline (#17984) * Change e-mail notifications to only be sent when recipient is offline Change the default for follow and mention notifications back on * Add preference to always send e-mail notifications * Change wording --- app/controllers/settings/preferences_controller.rb | 3 +- app/lib/user_settings_decorator.rb | 5 ++ app/models/user.rb | 2 +- app/services/notify_service.rb | 63 ++++++++++++++-------- .../preferences/notifications/show.html.haml | 3 ++ config/locales/simple_form.en.yml | 2 + config/settings.yml | 5 +- 7 files changed, 58 insertions(+), 25 deletions(-) (limited to 'app/models') diff --git a/app/controllers/settings/preferences_controller.rb b/app/controllers/settings/preferences_controller.rb index c7492700c..bfe651bc6 100644 --- a/app/controllers/settings/preferences_controller.rb +++ b/app/controllers/settings/preferences_controller.rb @@ -54,7 +54,8 @@ class Settings::PreferencesController < Settings::BaseController :setting_use_pending_items, :setting_trends, :setting_crop_images, - notification_emails: %i(follow follow_request reblog favourite mention digest report pending_account trending_tag), + :setting_always_send_emails, + notification_emails: %i(follow follow_request reblog favourite mention digest report pending_account trending_tag appeal), interactions: %i(must_be_follower must_be_following must_be_following_dm) ) end diff --git a/app/lib/user_settings_decorator.rb b/app/lib/user_settings_decorator.rb index de054e403..5fb7655a9 100644 --- a/app/lib/user_settings_decorator.rb +++ b/app/lib/user_settings_decorator.rb @@ -38,6 +38,7 @@ class UserSettingsDecorator user.settings['use_pending_items'] = use_pending_items_preference if change?('setting_use_pending_items') user.settings['trends'] = trends_preference if change?('setting_trends') user.settings['crop_images'] = crop_images_preference if change?('setting_crop_images') + user.settings['always_send_emails'] = always_send_emails_preference if change?('setting_always_send_emails') end def merged_notification_emails @@ -132,6 +133,10 @@ class UserSettingsDecorator boolean_cast_setting 'setting_crop_images' end + def always_send_emails_preference + boolean_cast_setting 'setting_always_send_emails' + end + def boolean_cast_setting(key) ActiveModel::Type::Boolean.new.cast(settings[key]) end diff --git a/app/models/user.rb b/app/models/user.rb index d19fe2c92..9da7b2639 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -130,7 +130,7 @@ class User < ApplicationRecord :reduce_motion, :system_font_ui, :noindex, :theme, :display_media, :expand_spoilers, :default_language, :aggregate_reblogs, :show_application, :advanced_layout, :use_blurhash, :use_pending_items, :trends, :crop_images, - :disable_swiping, + :disable_swiping, :always_send_emails, to: :settings, prefix: :setting, allow_nil: false attr_reader :invite_code diff --git a/app/services/notify_service.rb b/app/services/notify_service.rb index a90f17cfd..d30b33876 100644 --- a/app/services/notify_service.rb +++ b/app/services/notify_service.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class NotifyService < BaseService + include Redisable + def call(recipient, type, activity) @recipient = recipient @activity = activity @@ -8,10 +10,15 @@ class NotifyService < BaseService return if recipient.user.nil? || blocked? - create_notification! + @notification.save! + + # It's possible the underlying activity has been deleted + # between the save call and now + return if @notification.activity.nil? + push_notification! push_to_conversation! if direct_message? - send_email! if email_enabled? + send_email! if email_needed? rescue ActiveRecord::RecordInvalid nil end @@ -92,8 +99,8 @@ class NotifyService < BaseService end def blocked? - blocked = @recipient.suspended? # Skip if the recipient account is suspended anyway - blocked ||= from_self? && @notification.type != :poll # Skip for interactions with self + blocked = @recipient.suspended? + blocked ||= from_self? && @notification.type != :poll return blocked if message? && from_staff? @@ -117,38 +124,52 @@ class NotifyService < BaseService end end - def create_notification! - @notification.save! + def push_notification! + push_to_streaming_api! if subscribed_to_streaming_api? + push_to_web_push_subscriptions! end - def push_notification! - return if @notification.activity.nil? + def push_to_streaming_api! + redis.publish("timeline:#{@recipient.id}:notifications", Oj.dump(event: :notification, payload: InlineRenderer.render(@notification, @recipient, :notification))) + end - Redis.current.publish("timeline:#{@recipient.id}:notifications", Oj.dump(event: :notification, payload: InlineRenderer.render(@notification, @recipient, :notification))) - send_push_notifications! + def subscribed_to_streaming_api? + redis.exists?("subscribed:timeline:#{@recipient.id}") || redis.exists?("subscribed:timeline:#{@recipient.id}:notifications") end def push_to_conversation! - return if @notification.activity.nil? AccountConversation.add_status(@recipient, @notification.target_status) end - def send_push_notifications! - subscriptions_ids = ::Web::PushSubscription.where(user_id: @recipient.user.id) - .select { |subscription| subscription.pushable?(@notification) } - .map(&:id) + def push_to_web_push_subscriptions! + ::Web::PushNotificationWorker.push_bulk(web_push_subscriptions.select { |subscription| subscription.pushable?(@notification) }) { |subscription| [subscription.id, @notification.id] } + end - ::Web::PushNotificationWorker.push_bulk(subscriptions_ids) do |subscription_id| - [subscription_id, @notification.id] - end + def web_push_subscriptions + @web_push_subscriptions ||= ::Web::PushSubscription.where(user_id: @recipient.user.id).to_a + end + + def subscribed_to_web_push? + web_push_subscriptions.any? end def send_email! - return if @notification.activity.nil? - NotificationMailer.public_send(@notification.type, @recipient, @notification).deliver_later(wait: 2.minutes) + NotificationMailer.public_send(@notification.type, @recipient, @notification).deliver_later(wait: 2.minutes) if NotificationMailer.respond_to?(@notification.type) + end + + def email_needed? + (!recipient_online? || always_send_emails?) && send_email_for_notification_type? + end + + def recipient_online? + subscribed_to_streaming_api? || subscribed_to_web_push? + end + + def always_send_emails? + @recipient.user.settings.always_send_emails end - def email_enabled? + def send_email_for_notification_type? @recipient.user.settings.notification_emails[@notification.type.to_s] end end diff --git a/app/views/settings/preferences/notifications/show.html.haml b/app/views/settings/preferences/notifications/show.html.haml index 223e5d740..42754a852 100644 --- a/app/views/settings/preferences/notifications/show.html.haml +++ b/app/views/settings/preferences/notifications/show.html.haml @@ -25,6 +25,9 @@ = ff.input :pending_account, as: :boolean, wrapper: :with_label = ff.input :trending_tag, as: :boolean, wrapper: :with_label + .fields-group + = f.input :setting_always_send_emails, as: :boolean, wrapper: :with_label + .fields-group = f.simple_fields_for :notification_emails, hash_to_object(current_user.settings.notification_emails) do |ff| = ff.input :digest, as: :boolean, wrapper: :with_label diff --git a/config/locales/simple_form.en.yml b/config/locales/simple_form.en.yml index b19b7891f..b784b1da7 100644 --- a/config/locales/simple_form.en.yml +++ b/config/locales/simple_form.en.yml @@ -49,6 +49,7 @@ en: phrase: Will be matched regardless of casing in text or content warning of a post scopes: Which APIs the application will be allowed to access. If you select a top-level scope, you don't need to select individual ones. setting_aggregate_reblogs: Do not show new boosts for posts that have been recently boosted (only affects newly-received boosts) + setting_always_send_emails: Normally e-mail notifications won't be sent when you are actively using Mastodon setting_default_sensitive: Sensitive media is hidden by default and can be revealed with a click setting_display_media_default: Hide media marked as sensitive setting_display_media_hide_all: Always hide media @@ -151,6 +152,7 @@ en: phrase: Keyword or phrase setting_advanced_layout: Enable advanced web interface setting_aggregate_reblogs: Group boosts in timelines + setting_always_send_emails: Always send e-mail notifications setting_auto_play_gif: Auto-play animated GIFs setting_boost_modal: Show confirmation dialog before boosting setting_crop_images: Crop images in non-expanded posts to 16x9 diff --git a/config/settings.yml b/config/settings.yml index 06dd2b3f3..eaa05071e 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -38,16 +38,17 @@ defaults: &defaults trendable_by_default: false crop_images: true notification_emails: - follow: false + follow: true reblog: false favourite: false - mention: false + mention: true follow_request: true digest: true report: true pending_account: true trending_tag: true appeal: true + always_send_emails: false interactions: must_be_follower: false must_be_following: false -- cgit From 68273a7c6d6c630b6c88764579580682e12eebce Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 8 Apr 2022 19:35:31 +0200 Subject: Fix dangling language-specific trends (#17997) - Change score half-life for trending statuses from 2 to 6 hours - Change score threshold for trimming old items from 1 to 0.3 --- app/models/trends/base.rb | 4 ++-- app/models/trends/links.rb | 5 +++-- app/models/trends/statuses.rb | 7 ++++--- 3 files changed, 9 insertions(+), 7 deletions(-) (limited to 'app/models') diff --git a/app/models/trends/base.rb b/app/models/trends/base.rb index 38a49246b..200f8d635 100644 --- a/app/models/trends/base.rb +++ b/app/models/trends/base.rb @@ -65,8 +65,8 @@ class Trends::Base end def trim_older_items - redis.zremrangebyscore("#{key_prefix}:all", '-inf', '(1') - redis.zremrangebyscore("#{key_prefix}:allowed", '-inf', '(1') + redis.zremrangebyscore("#{key_prefix}:all", '-inf', '(0.3') + redis.zremrangebyscore("#{key_prefix}:allowed", '-inf', '(0.3') end def score_at_rank(rank) diff --git a/app/models/trends/links.rb b/app/models/trends/links.rb index 62308e706..5f046643a 100644 --- a/app/models/trends/links.rb +++ b/app/models/trends/links.rb @@ -30,7 +30,6 @@ class Trends::Links < Trends::Base def refresh(at_time = Time.now.utc) preview_cards = PreviewCard.where(id: (recently_used_ids(at_time) + currently_trending_ids(false, -1)).uniq) calculate_scores(preview_cards, at_time) - trim_older_items end def request_review @@ -101,6 +100,8 @@ class Trends::Links < Trends::Base }) end + trim_older_items + # Clean up localized sets by calculating the intersection with the main # set. We do this instead of just deleting the localized sets to avoid # having moments where the API returns empty results @@ -108,7 +109,7 @@ class Trends::Links < Trends::Base redis.pipelined do Trends.available_locales.each do |locale| redis.zinterstore("#{key_prefix}:all:#{locale}", ["#{key_prefix}:all:#{locale}", "#{key_prefix}:all"], aggregate: 'max') - redis.zinterstore("#{key_prefix}:allowed:#{locale}", ["#{key_prefix}:allowed:#{locale}", "#{key_prefix}:all"], aggregate: 'max') + redis.zinterstore("#{key_prefix}:allowed:#{locale}", ["#{key_prefix}:allowed:#{locale}", "#{key_prefix}:allowed"], aggregate: 'max') end end end diff --git a/app/models/trends/statuses.rb b/app/models/trends/statuses.rb index dc5309554..d9b07446e 100644 --- a/app/models/trends/statuses.rb +++ b/app/models/trends/statuses.rb @@ -6,7 +6,7 @@ class Trends::Statuses < Trends::Base self.default_options = { threshold: 5, review_threshold: 3, - score_halflife: 2.hours.freeze, + score_halflife: 6.hours.freeze, } class Query < Trends::Query @@ -48,7 +48,6 @@ class Trends::Statuses < Trends::Base def refresh(at_time = Time.now.utc) statuses = Status.where(id: (recently_used_ids(at_time) + currently_trending_ids(false, -1)).uniq).includes(:account, :media_attachments) calculate_scores(statuses, at_time) - trim_older_items end def request_review @@ -111,13 +110,15 @@ class Trends::Statuses < Trends::Base }) end + trim_older_items + # Clean up localized sets by calculating the intersection with the main # set. We do this instead of just deleting the localized sets to avoid # having moments where the API returns empty results Trends.available_locales.each do |locale| redis.zinterstore("#{key_prefix}:all:#{locale}", ["#{key_prefix}:all:#{locale}", "#{key_prefix}:all"], aggregate: 'max') - redis.zinterstore("#{key_prefix}:allowed:#{locale}", ["#{key_prefix}:allowed:#{locale}", "#{key_prefix}:all"], aggregate: 'max') + redis.zinterstore("#{key_prefix}:allowed:#{locale}", ["#{key_prefix}:allowed:#{locale}", "#{key_prefix}:allowed"], aggregate: 'max') end end end -- cgit