about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/account_domain_block.rb1
-rw-r--r--app/models/concerns/account_associations.rb1
-rw-r--r--app/models/concerns/account_avatar.rb2
-rw-r--r--app/models/concerns/account_header.rb2
-rw-r--r--app/models/concerns/domain_normalizable.rb2
-rw-r--r--app/models/concerns/redisable.rb11
-rw-r--r--app/models/export.rb1
-rw-r--r--app/models/featured_tag.rb46
-rw-r--r--app/models/feed.rb6
-rw-r--r--app/models/import.rb14
-rw-r--r--app/models/media_attachment.rb12
-rw-r--r--app/models/preview_card.rb2
-rw-r--r--app/models/tag.rb2
-rw-r--r--app/models/trending_tags.rb6
14 files changed, 90 insertions, 18 deletions
diff --git a/app/models/account_domain_block.rb b/app/models/account_domain_block.rb
index e352000c3..7c0d60379 100644
--- a/app/models/account_domain_block.rb
+++ b/app/models/account_domain_block.rb
@@ -12,6 +12,7 @@
 
 class AccountDomainBlock < ApplicationRecord
   include Paginable
+  include DomainNormalizable
 
   belongs_to :account
   validates :domain, presence: true, uniqueness: { scope: :account_id }
diff --git a/app/models/concerns/account_associations.rb b/app/models/concerns/account_associations.rb
index 4e730451a..3ab8a0daa 100644
--- a/app/models/concerns/account_associations.rb
+++ b/app/models/concerns/account_associations.rb
@@ -56,5 +56,6 @@ module AccountAssociations
 
     # Hashtags
     has_and_belongs_to_many :tags
+    has_many :featured_tags, -> { includes(:tag) }, dependent: :destroy, inverse_of: :account
   end
 end
diff --git a/app/models/concerns/account_avatar.rb b/app/models/concerns/account_avatar.rb
index 2d5ebfca3..5fff3ef5d 100644
--- a/app/models/concerns/account_avatar.rb
+++ b/app/models/concerns/account_avatar.rb
@@ -3,7 +3,7 @@
 module AccountAvatar
   extend ActiveSupport::Concern
 
-  IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
+  IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze
   LIMIT = 2.megabytes
 
   class_methods do
diff --git a/app/models/concerns/account_header.rb b/app/models/concerns/account_header.rb
index 067e166eb..a748fdff7 100644
--- a/app/models/concerns/account_header.rb
+++ b/app/models/concerns/account_header.rb
@@ -3,7 +3,7 @@
 module AccountHeader
   extend ActiveSupport::Concern
 
-  IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
+  IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze
   LIMIT = 2.megabytes
   MAX_PIXELS = 750_000 # 1500x500px
 
diff --git a/app/models/concerns/domain_normalizable.rb b/app/models/concerns/domain_normalizable.rb
index dff3e5414..fb84058fc 100644
--- a/app/models/concerns/domain_normalizable.rb
+++ b/app/models/concerns/domain_normalizable.rb
@@ -10,6 +10,6 @@ module DomainNormalizable
   private
 
   def normalize_domain
-    self.domain = TagManager.instance.normalize_domain(domain)
+    self.domain = TagManager.instance.normalize_domain(domain&.strip)
   end
 end
diff --git a/app/models/concerns/redisable.rb b/app/models/concerns/redisable.rb
new file mode 100644
index 000000000..c6cf97359
--- /dev/null
+++ b/app/models/concerns/redisable.rb
@@ -0,0 +1,11 @@
+# frozen_string_literal: true
+
+module Redisable
+  extend ActiveSupport::Concern
+
+  private
+
+  def redis
+    Redis.current
+  end
+end
diff --git a/app/models/export.rb b/app/models/export.rb
index a2520e9c2..fc4bb6964 100644
--- a/app/models/export.rb
+++ b/app/models/export.rb
@@ -1,4 +1,5 @@
 # frozen_string_literal: true
+
 require 'csv'
 
 class Export
diff --git a/app/models/featured_tag.rb b/app/models/featured_tag.rb
new file mode 100644
index 000000000..b5a10ad2d
--- /dev/null
+++ b/app/models/featured_tag.rb
@@ -0,0 +1,46 @@
+# frozen_string_literal: true
+# == Schema Information
+#
+# Table name: featured_tags
+#
+#  id             :bigint(8)        not null, primary key
+#  account_id     :bigint(8)
+#  tag_id         :bigint(8)
+#  statuses_count :bigint(8)        default(0), not null
+#  last_status_at :datetime
+#  created_at     :datetime         not null
+#  updated_at     :datetime         not null
+#
+
+class FeaturedTag < ApplicationRecord
+  belongs_to :account, inverse_of: :featured_tags, required: true
+  belongs_to :tag, inverse_of: :featured_tags, required: true
+
+  delegate :name, to: :tag, allow_nil: true
+
+  validates :name, presence: true
+  validate :validate_featured_tags_limit, on: :create
+
+  def name=(str)
+    self.tag = Tag.find_or_initialize_by(name: str.delete('#').mb_chars.downcase.to_s)
+  end
+
+  def increment(timestamp)
+    update(statuses_count: statuses_count + 1, last_status_at: timestamp)
+  end
+
+  def decrement(deleted_status_id)
+    update(statuses_count: [0, statuses_count - 1].max, last_status_at: account.statuses.where(visibility: %i(public unlisted)).tagged_with(tag).where.not(id: deleted_status_id).select(:created_at).first&.created_at)
+  end
+
+  def reset_data
+    self.statuses_count = account.statuses.where(visibility: %i(public unlisted)).tagged_with(tag).count
+    self.last_status_at = account.statuses.where(visibility: %i(public unlisted)).tagged_with(tag).select(:created_at).first&.created_at
+  end
+
+  private
+
+  def validate_featured_tags_limit
+    errors.add(:base, I18n.t('featured_tags.errors.limit')) if account.featured_tags.count >= 10
+  end
+end
diff --git a/app/models/feed.rb b/app/models/feed.rb
index 5bce88f25..0e8943ff8 100644
--- a/app/models/feed.rb
+++ b/app/models/feed.rb
@@ -1,6 +1,8 @@
 # frozen_string_literal: true
 
 class Feed
+  include Redisable
+
   def initialize(type, id)
     @type = type
     @id   = id
@@ -27,8 +29,4 @@ class Feed
   def key
     FeedManager.instance.key(@type, @id)
   end
-
-  def redis
-    Redis.current
-  end
 end
diff --git a/app/models/import.rb b/app/models/import.rb
index 55e970b0d..a7a0d8065 100644
--- a/app/models/import.rb
+++ b/app/models/import.rb
@@ -13,20 +13,30 @@
 #  data_file_size    :integer
 #  data_updated_at   :datetime
 #  account_id        :bigint(8)        not null
+#  overwrite         :boolean          default(FALSE), not null
 #
 
 class Import < ApplicationRecord
-  FILE_TYPES = ['text/plain', 'text/csv'].freeze
+  FILE_TYPES = %w(text/plain text/csv).freeze
+  MODES = %i(merge overwrite).freeze
 
   self.inheritance_column = false
 
   belongs_to :account
 
-  enum type: [:following, :blocking, :muting]
+  enum type: [:following, :blocking, :muting, :domain_blocking]
 
   validates :type, presence: true
 
   has_attached_file :data
   validates_attachment_content_type :data, content_type: FILE_TYPES
   validates_attachment_presence :data
+
+  def mode
+    overwrite? ? :overwrite : :merge
+  end
+
+  def mode=(str)
+    self.overwrite = str.to_sym == :overwrite
+  end
 end
diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb
index 601b14223..81397a18e 100644
--- a/app/models/media_attachment.rb
+++ b/app/models/media_attachment.rb
@@ -25,11 +25,11 @@ class MediaAttachment < ApplicationRecord
 
   enum type: [:image, :gifv, :video, :audio, :unknown]
 
-  IMAGE_FILE_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.gif'].freeze
+  IMAGE_FILE_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.gif', '.webp'].freeze
   VIDEO_FILE_EXTENSIONS = ['.webm', '.mp4', '.m4v', '.mov'].freeze
   AUDIO_FILE_EXTENSIONS = ['.mp3', '.m4a', '.wav', '.ogg'].freeze
 
-  IMAGE_MIME_TYPES             = ['image/jpeg', 'image/png', 'image/gif'].freeze
+  IMAGE_MIME_TYPES             = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze
   VIDEO_MIME_TYPES             = ['video/webm', 'video/mp4', 'video/quicktime'].freeze
   VIDEO_CONVERTIBLE_MIME_TYPES = ['video/webm', 'video/quicktime'].freeze
   AUDIO_MIME_TYPES             = ['audio/mpeg', 'audio/mp4', 'audio/vnd.wav', 'audio/wav', 'audio/x-wav', 'audio/x-wave', 'audio/ogg',].freeze
@@ -105,8 +105,8 @@ class MediaAttachment < ApplicationRecord
                     convert_options: { all: '-quality 90 -strip' }
 
   validates_attachment_content_type :file, content_type: IMAGE_MIME_TYPES + VIDEO_MIME_TYPES + AUDIO_MIME_TYPES
-  validates_attachment_size :file, less_than: IMAGE_LIMIT, unless: :video?
-  validates_attachment_size :file, less_than: VIDEO_LIMIT, if: :video?
+  validates_attachment_size :file, less_than: IMAGE_LIMIT, unless: :video_or_gifv?
+  validates_attachment_size :file, less_than: VIDEO_LIMIT, if: :video_or_gifv?
   remotable_attachment :file, VIDEO_LIMIT
 
   include Attachmentable
@@ -129,6 +129,10 @@ class MediaAttachment < ApplicationRecord
     file.blank? && remote_url.present?
   end
 
+  def video_or_gifv?
+    video? || gifv?
+  end
+
   def to_param
     shortcode
   end
diff --git a/app/models/preview_card.rb b/app/models/preview_card.rb
index a792b352b..f26ea0c74 100644
--- a/app/models/preview_card.rb
+++ b/app/models/preview_card.rb
@@ -25,7 +25,7 @@
 #
 
 class PreviewCard < ApplicationRecord
-  IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
+  IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze
   LIMIT = 1.megabytes
 
   self.inheritance_column = false
diff --git a/app/models/tag.rb b/app/models/tag.rb
index 99830ae92..4373e967b 100644
--- a/app/models/tag.rb
+++ b/app/models/tag.rb
@@ -14,6 +14,7 @@ class Tag < ApplicationRecord
   has_and_belongs_to_many :accounts
   has_and_belongs_to_many :sample_accounts, -> { searchable.discoverable.popular.limit(3) }, class_name: 'Account'
 
+  has_many :featured_tags, dependent: :destroy, inverse_of: :tag
   has_one :account_tag_stat, dependent: :destroy
 
   HASHTAG_NAME_RE = '[[:word:]_]*[[:alpha:]_·][[:word:]_]*'
@@ -23,6 +24,7 @@ class Tag < ApplicationRecord
 
   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 }) }
+  scope :most_used, ->(account) { joins(:statuses).where(statuses: { account: account }).group(:id).order(Arel.sql('count(*) desc')) }
 
   delegate :accounts_count,
            :accounts_count=,
diff --git a/app/models/trending_tags.rb b/app/models/trending_tags.rb
index 3a8be2164..148535c21 100644
--- a/app/models/trending_tags.rb
+++ b/app/models/trending_tags.rb
@@ -7,6 +7,8 @@ class TrendingTags
   THRESHOLD            = 5
 
   class << self
+    include Redisable
+
     def record_use!(tag, account, at_time = Time.now.utc)
       return if disallowed_hashtags.include?(tag.name) || account.silenced? || account.bot?
 
@@ -59,9 +61,5 @@ class TrendingTags
       @disallowed_hashtags = @disallowed_hashtags.split(' ') if @disallowed_hashtags.is_a? String
       @disallowed_hashtags = @disallowed_hashtags.map(&:downcase)
     end
-
-    def redis
-      Redis.current
-    end
   end
 end