diff --git a/app/lib/potential_friendship_tracker.rb b/app/lib/potential_friendship_tracker.rb index f98acef303..b32deb1708 100644 --- a/app/lib/potential_friendship_tracker.rb +++ b/app/lib/potential_friendship_tracker.rb @@ -6,7 +6,7 @@ class PotentialFriendshipTracker WEIGHTS = { reply: 1, - emoji_reaction: 2, + emoji_reaction: 3, favourite: 10, reblog: 20, }.freeze diff --git a/app/models/emoji_reaction.rb b/app/models/emoji_reaction.rb index 85d2914d13..80d92c2424 100644 --- a/app/models/emoji_reaction.rb +++ b/app/models/emoji_reaction.rb @@ -28,6 +28,8 @@ class EmojiReaction < ApplicationRecord has_one :notification, as: :activity, dependent: :destroy + validate :status_emoji_reactions_count + after_create :refresh_cache after_destroy :refresh_cache after_destroy :invalidate_cleanup_info @@ -50,4 +52,10 @@ class EmojiReaction < ApplicationRecord query = query.where(arel_table[:id].gt(since_id)) if since_id.present? query end + + def status_emoji_reactions_count + if status && account && status.emoji_reactions.where(account: account).count >= EMOJI_REACTION_PER_ACCOUNT_LIMIT + raise Mastodon::ValidationError, I18n.t('reactions.errors.limit_reached') + end + end end diff --git a/app/models/status.rb b/app/models/status.rb index b67c0b529e..da4bbc1e9f 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -339,8 +339,8 @@ class Status < ApplicationRecord end def refresh_emoji_reactions_grouped_by_name! - generate_emoji_reactions_grouped_by_name.tap do |emoji_reactions| - update_status_stat!(emoji_reactions: emoji_reactions) + generate_emoji_reactions_grouped_by_name.tap do |emoji_reactions_json| + update_status_stat!(emoji_reactions: emoji_reactions_json, emoji_reactions_count: emoji_reactions.size) end end diff --git a/app/models/status_stat.rb b/app/models/status_stat.rb index 7e46f02769..80e7270318 100644 --- a/app/models/status_stat.rb +++ b/app/models/status_stat.rb @@ -4,14 +4,15 @@ # # Table name: status_stats # -# id :bigint(8) not null, primary key -# status_id :bigint(8) not null -# replies_count :bigint(8) default(0), not null -# reblogs_count :bigint(8) default(0), not null -# favourites_count :bigint(8) default(0), not null -# created_at :datetime not null -# updated_at :datetime not null -# emoji_reactions :string +# id :bigint(8) not null, primary key +# status_id :bigint(8) not null +# replies_count :bigint(8) default(0), not null +# reblogs_count :bigint(8) default(0), not null +# favourites_count :bigint(8) default(0), not null +# created_at :datetime not null +# updated_at :datetime not null +# emoji_reactions :string +# emoji_reactions_count :integer default(0), not null # class StatusStat < ApplicationRecord @@ -35,6 +36,10 @@ class StatusStat < ApplicationRecord attributes['emoji_reactions'] || '' end + def emoji_reactions_count + [attributes['emoji_reactions_count'], 0].max + end + private def reset_parent_cache diff --git a/app/models/trends/statuses.rb b/app/models/trends/statuses.rb index 84bff9c027..110c3da045 100644 --- a/app/models/trends/statuses.rb +++ b/app/models/trends/statuses.rb @@ -97,7 +97,7 @@ class Trends::Statuses < Trends::Base def calculate_scores(statuses, at_time) items = statuses.map do |status| expected = 1.0 - observed = (status.reblogs_count + status.favourites_count).to_f + observed = (status.reblogs_count + status.favourites_count + status.emoji_reactions_count * 0.3).to_f score = if expected > observed || observed < options[:threshold] 0 diff --git a/app/services/search_service.rb b/app/services/search_service.rb index 54b421a370..15a7b033fe 100644 --- a/app/services/search_service.rb +++ b/app/services/search_service.rb @@ -53,7 +53,7 @@ class SearchService < BaseService privacy_definition = privacy_definition.or(StatusesIndex.filter(term: { searchability: 'direct' }).filter(term: { account_id: @account.id }).min_score(MIN_SCORE)) end - definition = parsed_query.apply(StatusesIndex.min_score(MIN_SCORE)).order(id: :desc) + definition = parsed_query.apply(StatusesIndex.min_score(MIN_SCORE).track_scores(true)).order(id: :desc) definition = definition.filter(term: { account_id: @options[:account_id] }) if @options[:account_id].present? definition = definition.and(privacy_definition) diff --git a/db/migrate/20230410004651_add_emoji_reactions_count_to_status_stats.rb b/db/migrate/20230410004651_add_emoji_reactions_count_to_status_stats.rb new file mode 100644 index 0000000000..cd04d6c939 --- /dev/null +++ b/db/migrate/20230410004651_add_emoji_reactions_count_to_status_stats.rb @@ -0,0 +1,5 @@ +class AddEmojiReactionsCountToStatusStats < ActiveRecord::Migration[6.1] + def change + add_column :status_stats, :emoji_reactions_count, :integer, null: false, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index 57541246c3..645b0d0a90 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: 2023_04_06_041523) do +ActiveRecord::Schema.define(version: 2023_04_10_004651) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -933,6 +933,7 @@ ActiveRecord::Schema.define(version: 2023_04_06_041523) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "emoji_reactions" + t.integer "emoji_reactions_count", default: 0, null: false t.index ["status_id"], name: "index_status_stats_on_status_id", unique: true end