From 73be8f38c115c279e3d3961b98bd2b82b9706b05 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 6 Dec 2018 17:36:11 +0100 Subject: Add profile directory (#9427) Fix #5578 --- app/models/account.rb | 40 +++++++++++++++++++++++++++++ app/models/account_stat.rb | 12 +++++++-- app/models/account_tag_stat.rb | 24 +++++++++++++++++ app/models/concerns/account_associations.rb | 3 +++ app/models/concerns/account_counters.rb | 1 + app/models/tag.rb | 26 +++++++++++++++++++ 6 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 app/models/account_tag_stat.rb (limited to 'app/models') diff --git a/app/models/account.rb b/app/models/account.rb index fb089de90..20b0b7239 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -43,11 +43,13 @@ # featured_collection_url :string # fields :jsonb # actor_type :string +# discoverable :boolean # class Account < ApplicationRecord USERNAME_RE = /[a-z0-9_]+([a-z0-9_\.-]+[a-z0-9_]+)?/i MENTION_RE = /(?<=^|[^\/[:word:]])@((#{USERNAME_RE})(?:@[a-z0-9\.\-]+[a-z0-9]+)?)/i + MIN_FOLLOWERS_DISCOVERY = 10 include AccountAssociations include AccountAvatar @@ -89,6 +91,10 @@ class Account < ApplicationRecord scope :matches_display_name, ->(value) { where(arel_table[:display_name].matches("#{value}%")) } scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) } scope :searchable, -> { where(suspended: false).where(moved_to_account_id: nil) } + scope :discoverable, -> { where(silenced: false).where(discoverable: true).joins(:account_stat).where(AccountStat.arel_table[:followers_count].gteq(MIN_FOLLOWERS_DISCOVERY)) } + scope :tagged_with, ->(tag) { joins(:accounts_tags).where(accounts_tags: { tag_id: tag }) } + scope :popular, -> { order('account_stats.followers_count desc') } + scope :by_recent_status, -> { order('(case when account_stats.last_status_at is null then 1 else 0 end) asc, account_stats.last_status_at desc') } delegate :email, :unconfirmed_email, @@ -174,6 +180,40 @@ class Account < ApplicationRecord @keypair ||= OpenSSL::PKey::RSA.new(private_key || public_key) end + def tags_as_strings=(tag_names) + tag_names.map! { |name| name.mb_chars.downcase } + tag_names.uniq!(&:to_s) + + # Existing hashtags + hashtags_map = Tag.where(name: tag_names).each_with_object({}) { |tag, h| h[tag.name] = tag } + + # Initialize not yet existing hashtags + tag_names.each do |name| + next if hashtags_map.key?(name) + hashtags_map[name.downcase] = Tag.new(name: name) + end + + # Remove hashtags that are to be deleted + tags.each do |tag| + if hashtags_map.key?(tag.name) + hashtags_map.delete(tag.name) + else + transaction do + tags.delete(tag) + tag.decrement_count!(:accounts_count) + end + end + end + + # Add hashtags that were so far missing + hashtags_map.each_value do |tag| + transaction do + tags << tag + tag.increment_count!(:accounts_count) + end + end + end + def fields (self[:fields] || []).map { |f| Field.new(self, f) } end diff --git a/app/models/account_stat.rb b/app/models/account_stat.rb index d5715268e..9813aa84f 100644 --- a/app/models/account_stat.rb +++ b/app/models/account_stat.rb @@ -1,5 +1,4 @@ # frozen_string_literal: true - # == Schema Information # # Table name: account_stats @@ -11,16 +10,25 @@ # followers_count :bigint(8) default(0), not null # created_at :datetime not null # updated_at :datetime not null +# last_status_at :datetime # class AccountStat < ApplicationRecord belongs_to :account, inverse_of: :account_stat def increment_count!(key) - update(key => public_send(key) + 1) + update(attributes_for_increment(key)) end def decrement_count!(key) update(key => [public_send(key) - 1, 0].max) end + + private + + def attributes_for_increment(key) + attrs = { key => public_send(key) + 1 } + attrs[:last_status_at] = Time.now.utc if key == :statuses_count + attrs + end end diff --git a/app/models/account_tag_stat.rb b/app/models/account_tag_stat.rb new file mode 100644 index 000000000..3c36c155a --- /dev/null +++ b/app/models/account_tag_stat.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true +# == Schema Information +# +# Table name: account_tag_stats +# +# id :bigint(8) not null, primary key +# tag_id :bigint(8) not null +# accounts_count :bigint(8) default(0), not null +# hidden :boolean default(FALSE), not null +# created_at :datetime not null +# updated_at :datetime not null +# + +class AccountTagStat < ApplicationRecord + belongs_to :tag, inverse_of: :account_tag_stat + + def increment_count!(key) + update(key => public_send(key) + 1) + end + + def decrement_count!(key) + update(key => [public_send(key) - 1, 0].max) + end +end diff --git a/app/models/concerns/account_associations.rb b/app/models/concerns/account_associations.rb index 0f7482fa6..ae50860ed 100644 --- a/app/models/concerns/account_associations.rb +++ b/app/models/concerns/account_associations.rb @@ -49,5 +49,8 @@ module AccountAssociations # Account migrations belongs_to :moved_to_account, class_name: 'Account', optional: true + + # Hashtags + has_and_belongs_to_many :tags end end diff --git a/app/models/concerns/account_counters.rb b/app/models/concerns/account_counters.rb index fa3ec9a3d..3581df8dd 100644 --- a/app/models/concerns/account_counters.rb +++ b/app/models/concerns/account_counters.rb @@ -16,6 +16,7 @@ module AccountCounters :followers_count=, :increment_count!, :decrement_count!, + :last_status_at, to: :account_stat def account_stat diff --git a/app/models/tag.rb b/app/models/tag.rb index 4f31f796e..b28e2cc18 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -11,12 +11,31 @@ class Tag < ApplicationRecord has_and_belongs_to_many :statuses + has_and_belongs_to_many :accounts + + has_one :account_tag_stat, dependent: :destroy HASHTAG_NAME_RE = '[[:word:]_]*[[:alpha:]_ยท][[:word:]_]*' HASHTAG_RE = /(?:^|[^\/\)\w])#(#{HASHTAG_NAME_RE})/i validates :name, presence: true, uniqueness: true, format: { with: /\A#{HASHTAG_NAME_RE}\z/i } + scope :discoverable, -> { joins(:account_tag_stat).where(AccountTagStat.arel_table[:accounts_count].gt(0)).where(account_tag_stats: { hidden: false }).order(name: :asc) } + scope :hidden, -> { where(account_tag_stats: { hidden: true }) } + + delegate :accounts_count, + :accounts_count=, + :increment_count!, + :decrement_count!, + :hidden?, + to: :account_tag_stat + + after_save :save_account_tag_stat + + def account_tag_stat + super || build_account_tag_stat + end + def to_param name end @@ -43,4 +62,11 @@ class Tag < ApplicationRecord Tag.where('lower(name) like lower(?)', pattern).order(:name).limit(limit) end end + + private + + def save_account_tag_stat + return unless account_tag_stat&.changed? + account_tag_stat.save + end end -- cgit From c1c0f7c5161452ded4373e6aa1cc9f852f8836c1 Mon Sep 17 00:00:00 2001 From: Takeshi Umeda Date: Fri, 7 Dec 2018 12:18:37 +0900 Subject: Fix tag mb_chars comparison of profile directory (#9448) --- app/models/account.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'app/models') diff --git a/app/models/account.rb b/app/models/account.rb index 20b0b7239..c22836afe 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -181,8 +181,8 @@ class Account < ApplicationRecord end def tags_as_strings=(tag_names) - tag_names.map! { |name| name.mb_chars.downcase } - tag_names.uniq!(&:to_s) + tag_names.map! { |name| name.mb_chars.downcase.to_s } + tag_names.uniq! # Existing hashtags hashtags_map = Tag.where(name: tag_names).each_with_object({}) { |tag, h| h[tag.name] = tag } @@ -190,7 +190,7 @@ class Account < ApplicationRecord # Initialize not yet existing hashtags tag_names.each do |name| next if hashtags_map.key?(name) - hashtags_map[name.downcase] = Tag.new(name: name) + hashtags_map[name] = Tag.new(name: name) end # Remove hashtags that are to be deleted -- cgit From ecd303c097bf20aa971628ba8420a9f17f3dd1f7 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 7 Dec 2018 16:37:32 +0100 Subject: Fix various things in the directory (#9449) * Fix missing variable in directory page title * Order hashtags by number of people instead of alphabetically * Add icon to OpenGraph preview of directory page * Prevent line breaks in hashtags and ensure lowercase in the table --- app/javascript/styles/mastodon/widgets.scss | 3 +++ app/models/tag.rb | 2 +- app/views/directories/index.html.haml | 10 ++++++---- 3 files changed, 10 insertions(+), 5 deletions(-) (limited to 'app/models') diff --git a/app/javascript/styles/mastodon/widgets.scss b/app/javascript/styles/mastodon/widgets.scss index a838ca778..c863e3b4f 100644 --- a/app/javascript/styles/mastodon/widgets.scss +++ b/app/javascript/styles/mastodon/widgets.scss @@ -300,6 +300,9 @@ font-size: 18px; font-weight: 700; color: $primary-text-color; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; .fa { color: $darker-text-color; diff --git a/app/models/tag.rb b/app/models/tag.rb index b28e2cc18..41e58e3ca 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -20,7 +20,7 @@ class Tag < ApplicationRecord validates :name, presence: true, uniqueness: true, format: { with: /\A#{HASHTAG_NAME_RE}\z/i } - scope :discoverable, -> { joins(:account_tag_stat).where(AccountTagStat.arel_table[:accounts_count].gt(0)).where(account_tag_stats: { hidden: false }).order(name: :asc) } + scope :discoverable, -> { joins(:account_tag_stat).where(AccountTagStat.arel_table[:accounts_count].gt(0)).where(account_tag_stats: { hidden: false }).order('account_tag_stats.accounts_count desc') } scope :hidden, -> { where(account_tag_stats: { hidden: true }) } delegate :accounts_count, diff --git a/app/views/directories/index.html.haml b/app/views/directories/index.html.haml index 7cd6b50d4..219950a51 100644 --- a/app/views/directories/index.html.haml +++ b/app/views/directories/index.html.haml @@ -1,12 +1,14 @@ - content_for :page_title do - = t('directories.explore_mastodon') + = t('directories.explore_mastodon', title: site_title) - content_for :header_tags do %meta{ name: 'description', content: t('directories.explanation') } - = opengraph 'og:site_name', site_title + = opengraph 'og:site_name', t('about.hosted_on', domain: site_hostname) + = opengraph 'og:type', 'website' = opengraph 'og:title', t('directories.explore_mastodon', title: site_title) = opengraph 'og:description', t('directories.explanation') + = opengraph 'og:image', File.join(root_url, 'android-chrome-192x192.png') .page-header %h1= t('directories.explore_mastodon', title: site_title) @@ -29,10 +31,10 @@ %td= account_link_to account %td.accounts-table__count = number_to_human account.statuses_count, strip_insignificant_zeros: true - %small= t('accounts.posts', count: account.statuses_count) + %small= t('accounts.posts', count: account.statuses_count).downcase %td.accounts-table__count = number_to_human account.followers_count, strip_insignificant_zeros: true - %small= t('accounts.followers', count: account.followers_count) + %small= t('accounts.followers', count: account.followers_count).downcase %td.accounts-table__count - if account.last_status_at.present? %time.time-ago{ datetime: account.last_status_at.iso8601, title: l(account.last_status_at) }= l account.last_status_at -- cgit From 6b78e5b5ab813625152149b7e215d0568a595cdc Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sat, 8 Dec 2018 01:32:26 +0100 Subject: Cache hashtag sample accounts, and exclude ineligible ones (#9465) --- app/models/account.rb | 2 +- app/models/tag.rb | 7 ++++++- app/views/directories/index.html.haml | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) (limited to 'app/models') diff --git a/app/models/account.rb b/app/models/account.rb index c22836afe..9767e3767 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -94,7 +94,7 @@ class Account < ApplicationRecord scope :discoverable, -> { where(silenced: false).where(discoverable: true).joins(:account_stat).where(AccountStat.arel_table[:followers_count].gteq(MIN_FOLLOWERS_DISCOVERY)) } scope :tagged_with, ->(tag) { joins(:accounts_tags).where(accounts_tags: { tag_id: tag }) } scope :popular, -> { order('account_stats.followers_count desc') } - scope :by_recent_status, -> { order('(case when account_stats.last_status_at is null then 1 else 0 end) asc, account_stats.last_status_at desc') } + scope :by_recent_status, -> { order(Arel.sql('(case when account_stats.last_status_at is null then 1 else 0 end) asc, account_stats.last_status_at desc')) } delegate :email, :unconfirmed_email, diff --git a/app/models/tag.rb b/app/models/tag.rb index 41e58e3ca..99830ae92 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -12,6 +12,7 @@ class Tag < ApplicationRecord has_and_belongs_to_many :statuses has_and_belongs_to_many :accounts + has_and_belongs_to_many :sample_accounts, -> { searchable.discoverable.popular.limit(3) }, class_name: 'Account' has_one :account_tag_stat, dependent: :destroy @@ -20,7 +21,7 @@ class Tag < ApplicationRecord validates :name, presence: true, uniqueness: true, format: { with: /\A#{HASHTAG_NAME_RE}\z/i } - scope :discoverable, -> { joins(:account_tag_stat).where(AccountTagStat.arel_table[:accounts_count].gt(0)).where(account_tag_stats: { hidden: false }).order('account_tag_stats.accounts_count desc') } + scope :discoverable, -> { joins(:account_tag_stat).where(AccountTagStat.arel_table[:accounts_count].gt(0)).where(account_tag_stats: { hidden: false }).order(Arel.sql('account_tag_stats.accounts_count desc')) } scope :hidden, -> { where(account_tag_stats: { hidden: true }) } delegate :accounts_count, @@ -36,6 +37,10 @@ class Tag < ApplicationRecord super || build_account_tag_stat end + def cached_sample_accounts + Rails.cache.fetch("#{cache_key}/sample_accounts", expires_in: 12.hours) { sample_accounts } + end + def to_param name end diff --git a/app/views/directories/index.html.haml b/app/views/directories/index.html.haml index 219950a51..f70eb964a 100644 --- a/app/views/directories/index.html.haml +++ b/app/views/directories/index.html.haml @@ -57,5 +57,5 @@ %small= t('directories.people', count: tag.accounts_count) .avatar-stack - - tag.accounts.limit(3).each do |account| + - tag.cached_sample_accounts.each do |account| = image_tag current_account&.user&.setting_auto_play_gif ? account.avatar_original_url : account.avatar_static_url, width: 48, height: 48, alt: '', class: 'account__avatar' -- cgit From 81bda7d67c984c9bfcb5bca94e50cec6405b492e Mon Sep 17 00:00:00 2001 From: ThibG Date: Sun, 9 Dec 2018 13:03:01 +0100 Subject: Add setting to not aggregate reblogs (#9248) * Add setting to not aggregate reblogs Fixes #9222 * Handle cases where user is nil in add_to_home and add_to_list * Add hint for setting_aggregate_reblogs option * Reword setting_aggregate_reblogs label --- app/controllers/settings/preferences_controller.rb | 1 + app/lib/feed_manager.rb | 12 ++++++------ app/lib/user_settings_decorator.rb | 5 +++++ app/models/user.rb | 6 +++++- app/views/settings/preferences/show.html.haml | 3 +++ config/locales/simple_form.en.yml | 2 ++ config/settings.yml | 1 + 7 files changed, 23 insertions(+), 7 deletions(-) (limited to 'app/models') diff --git a/app/controllers/settings/preferences_controller.rb b/app/controllers/settings/preferences_controller.rb index 7bb5fb112..70e71b4a2 100644 --- a/app/controllers/settings/preferences_controller.rb +++ b/app/controllers/settings/preferences_controller.rb @@ -48,6 +48,7 @@ class Settings::PreferencesController < ApplicationController :setting_noindex, :setting_theme, :setting_hide_network, + :setting_aggregate_reblogs, notification_emails: %i(follow follow_request reblog favourite mention digest report), interactions: %i(must_be_follower must_be_following) ) diff --git a/app/lib/feed_manager.rb b/app/lib/feed_manager.rb index 31ff53860..f99df33e5 100644 --- a/app/lib/feed_manager.rb +++ b/app/lib/feed_manager.rb @@ -27,7 +27,7 @@ class FeedManager end def push_to_home(account, status) - return false unless add_to_feed(:home, account.id, status) + return false unless add_to_feed(:home, account.id, status, account.user&.aggregates_reblogs?) trim(:home, account.id) PushUpdateWorker.perform_async(account.id, status.id, "timeline:#{account.id}") if push_update_required?("timeline:#{account.id}") true @@ -45,7 +45,7 @@ class FeedManager should_filter &&= !ListAccount.where(list_id: list.id, account_id: status.in_reply_to_account_id).exists? return false if should_filter end - return false unless add_to_feed(:list, list.id, status) + return false unless add_to_feed(:list, list.id, status, list.account.user&.aggregates_reblogs?) trim(:list, list.id) PushUpdateWorker.perform_async(list.account_id, status.id, "timeline:list:#{list.id}") if push_update_required?("timeline:list:#{list.id}") true @@ -93,7 +93,7 @@ class FeedManager query.each do |status| next if status.direct_visibility? || status.limited_visibility? || filter?(:home, status, into_account) - add_to_feed(:home, into_account.id, status) + add_to_feed(:home, into_account.id, status, into_account.user&.aggregates_reblogs?) end trim(:home, into_account.id) @@ -131,7 +131,7 @@ class FeedManager statuses.each do |status| next if filter_from_home?(status, account) - added += 1 if add_to_feed(:home, account.id, status) + added += 1 if add_to_feed(:home, account.id, status, account.user&.aggregates_reblogs?) end break unless added.zero? @@ -230,11 +230,11 @@ class FeedManager # added, and false if it was not added to the feed. Note that this is # an internal helper: callers must call trim or push updates if # either action is appropriate. - def add_to_feed(timeline_type, account_id, status) + def add_to_feed(timeline_type, account_id, status, aggregate_reblogs = true) timeline_key = key(timeline_type, account_id) reblog_key = key(timeline_type, account_id, 'reblogs') - if status.reblog? + if status.reblog? && (aggregate_reblogs.nil? || aggregate_reblogs) # If the original status or a reblog of it is within # REBLOG_FALLOFF statuses from the top, do not re-insert it into # the feed diff --git a/app/lib/user_settings_decorator.rb b/app/lib/user_settings_decorator.rb index 40973c707..19b854410 100644 --- a/app/lib/user_settings_decorator.rb +++ b/app/lib/user_settings_decorator.rb @@ -31,6 +31,7 @@ class UserSettingsDecorator user.settings['noindex'] = noindex_preference if change?('setting_noindex') user.settings['theme'] = theme_preference if change?('setting_theme') user.settings['hide_network'] = hide_network_preference if change?('setting_hide_network') + user.settings['aggregate_reblogs'] = aggregate_reblogs_preference if change?('setting_aggregate_reblogs') end def merged_notification_emails @@ -97,6 +98,10 @@ class UserSettingsDecorator settings['setting_default_language'] end + def aggregate_reblogs_preference + boolean_cast_setting 'setting_aggregate_reblogs' + 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 453ffa8b0..f4130d7b1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -95,7 +95,7 @@ class User < ApplicationRecord delegate :auto_play_gif, :default_sensitive, :unfollow_modal, :boost_modal, :delete_modal, :reduce_motion, :system_font_ui, :noindex, :theme, :display_media, :hide_network, - :expand_spoilers, :default_language, to: :settings, prefix: :setting, allow_nil: false + :expand_spoilers, :default_language, :aggregate_reblogs, to: :settings, prefix: :setting, allow_nil: false attr_reader :invite_code @@ -231,6 +231,10 @@ class User < ApplicationRecord @hides_network ||= settings.hide_network end + def aggregates_reblogs? + @aggregates_reblogs ||= settings.aggregate_reblogs + end + def token_for_app(a) return nil if a.nil? || a.owner != self Doorkeeper::AccessToken diff --git a/app/views/settings/preferences/show.html.haml b/app/views/settings/preferences/show.html.haml index ecb789f93..a2c61c9a6 100644 --- a/app/views/settings/preferences/show.html.haml +++ b/app/views/settings/preferences/show.html.haml @@ -47,6 +47,9 @@ = f.input :setting_boost_modal, as: :boolean, wrapper: :with_label = f.input :setting_delete_modal, as: :boolean, wrapper: :with_label + .fields-group + = f.input :setting_aggregate_reblogs, as: :boolean, wrapper: :with_label + .fields-group = f.input :setting_auto_play_gif, as: :boolean, wrapper: :with_label = f.input :setting_expand_spoilers, as: :boolean, wrapper: :with_label diff --git a/config/locales/simple_form.en.yml b/config/locales/simple_form.en.yml index e24d8f4e6..ce6a62e87 100644 --- a/config/locales/simple_form.en.yml +++ b/config/locales/simple_form.en.yml @@ -19,6 +19,7 @@ en: password: Use at least 8 characters phrase: Will be matched regardless of casing in text or content warning of a toot 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 toots that have been recently boosted (only affects newly-received boosts) setting_default_language: The language of your toots can be detected automatically, but it's not always accurate setting_display_media_default: Hide media marked as sensitive setting_display_media_hide_all: Always hide all media @@ -65,6 +66,7 @@ en: otp_attempt: Two-factor code password: Password phrase: Keyword or phrase + setting_aggregate_reblogs: Group boosts in timelines setting_auto_play_gif: Auto-play animated GIFs setting_boost_modal: Show confirmation dialog before boosting setting_default_language: Posting language diff --git a/config/settings.yml b/config/settings.yml index 2bc9fe289..4036d419f 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -33,6 +33,7 @@ defaults: &defaults system_font_ui: false noindex: false theme: 'default' + aggregate_reblogs: true notification_emails: follow: false reblog: false -- cgit