about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
authorReverite <github@reverite.sh>2018-12-06 15:56:22 -0800
committerReverite <github@reverite.sh>2018-12-06 15:56:22 -0800
commita78e3f2206ddaeeb3e6f1c23e4b524af29e92b04 (patch)
treea054dda635ffd019bf2fdc10ed4f3341264f1490 /app/models
parenta2cb6e3996f4e8f7775557e84c9e242d937f8f0c (diff)
parente3682c9c1750e5e7e5d2f817e29f6760a18400ca (diff)
Merge branch 'glitch'
Diffstat (limited to 'app/models')
-rw-r--r--app/models/account.rb60
-rw-r--r--app/models/concerns/account_associations.rb54
-rw-r--r--app/models/status.rb6
3 files changed, 74 insertions, 46 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index 645a303c3..e6b5bd69f 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -49,6 +49,7 @@ 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
 
+  include AccountAssociations
   include AccountAvatar
   include AccountFinderConcern
   include AccountHeader
@@ -63,9 +64,6 @@ class Account < ApplicationRecord
 
   enum protocol: [:ostatus, :activitypub]
 
-  # Local users
-  has_one :user, inverse_of: :account
-
   validates :username, presence: true
 
   # Remote user validations
@@ -80,46 +78,6 @@ class Account < ApplicationRecord
   validate :note_length_does_not_exceed_length_limit, if: -> { local? && will_save_change_to_note? }
   validates :fields, length: { maximum: MAX_FIELDS }, if: -> { local? && will_save_change_to_fields? }
 
-  # Timelines
-  has_many :stream_entries, inverse_of: :account, dependent: :destroy
-  has_many :statuses, inverse_of: :account, dependent: :destroy
-  has_many :favourites, inverse_of: :account, dependent: :destroy
-  has_many :bookmarks, inverse_of: :account, dependent: :destroy
-  has_many :mentions, inverse_of: :account, dependent: :destroy
-  has_many :notifications, inverse_of: :account, dependent: :destroy
-
-  # Pinned statuses
-  has_many :status_pins, inverse_of: :account, dependent: :destroy
-  has_many :pinned_statuses, -> { reorder('status_pins.created_at DESC') }, through: :status_pins, class_name: 'Status', source: :status
-
-  # Endorsements
-  has_many :account_pins, inverse_of: :account, dependent: :destroy
-  has_many :endorsed_accounts, through: :account_pins, class_name: 'Account', source: :target_account
-
-  # Media
-  has_many :media_attachments, dependent: :destroy
-
-  # PuSH subscriptions
-  has_many :subscriptions, dependent: :destroy
-
-  # Report relationships
-  has_many :reports
-  has_many :targeted_reports, class_name: 'Report', foreign_key: :target_account_id
-
-  has_many :report_notes, dependent: :destroy
-  has_many :custom_filters, inverse_of: :account, dependent: :destroy
-
-  # Moderation notes
-  has_many :account_moderation_notes, dependent: :destroy
-  has_many :targeted_moderation_notes, class_name: 'AccountModerationNote', foreign_key: :target_account_id, dependent: :destroy
-
-  # Lists
-  has_many :list_accounts, inverse_of: :account, dependent: :destroy
-  has_many :lists, through: :list_accounts
-
-  # Account migrations
-  belongs_to :moved_to_account, class_name: 'Account', optional: true
-
   scope :remote, -> { where.not(domain: nil) }
   scope :local, -> { where(domain: nil) }
   scope :expiring, ->(time) { remote.where.not(subscription_expires_at: nil).where('subscription_expires_at < ?', time) }
@@ -455,6 +413,7 @@ class Account < ApplicationRecord
   before_create :generate_keys
   before_validation :normalize_domain
   before_validation :prepare_contents, if: :local?
+  before_destroy :clean_feed_manager
 
   private
 
@@ -496,4 +455,19 @@ class Account < ApplicationRecord
   def emojifiable_text
     [note, display_name, fields.map(&:value)].join(' ')
   end
+
+  def clean_feed_manager
+    reblog_key       = FeedManager.instance.key(:home, id, 'reblogs')
+    reblogged_id_set = Redis.current.zrange(reblog_key, 0, -1)
+
+    Redis.current.pipelined do
+      Redis.current.del(FeedManager.instance.key(:home, id))
+      Redis.current.del(reblog_key)
+
+      reblogged_id_set.each do |reblogged_id|
+        reblog_set_key = FeedManager.instance.key(:home, id, "reblogs:#{reblogged_id}")
+        Redis.current.del(reblog_set_key)
+      end
+    end
+  end
 end
diff --git a/app/models/concerns/account_associations.rb b/app/models/concerns/account_associations.rb
new file mode 100644
index 000000000..9dba8000d
--- /dev/null
+++ b/app/models/concerns/account_associations.rb
@@ -0,0 +1,54 @@
+# frozen_string_literal: true
+
+module AccountAssociations
+  extend ActiveSupport::Concern
+
+  included do
+    # Local users
+    has_one :user, inverse_of: :account, dependent: :destroy
+
+    # Timelines
+    has_many :stream_entries, inverse_of: :account, dependent: :destroy
+    has_many :statuses, inverse_of: :account, dependent: :destroy
+    has_many :favourites, inverse_of: :account, dependent: :destroy
+    has_many :bookmarks, inverse_of: :account, dependent: :destroy
+    has_many :mentions, inverse_of: :account, dependent: :destroy
+    has_many :notifications, inverse_of: :account, dependent: :destroy
+    has_many :conversations, class_name: 'AccountConversation', dependent: :destroy, inverse_of: :account
+
+    # Pinned statuses
+    has_many :status_pins, inverse_of: :account, dependent: :destroy
+    has_many :pinned_statuses, -> { reorder('status_pins.created_at DESC') }, through: :status_pins, class_name: 'Status', source: :status
+
+    # Endorsements
+    has_many :account_pins, inverse_of: :account, dependent: :destroy
+    has_many :endorsed_accounts, through: :account_pins, class_name: 'Account', source: :target_account
+
+    # Media
+    has_many :media_attachments, dependent: :destroy
+
+    # PuSH subscriptions
+    has_many :subscriptions, dependent: :destroy
+
+    # Report relationships
+    has_many :reports, dependent: :destroy, inverse_of: :account
+    has_many :targeted_reports, class_name: 'Report', foreign_key: :target_account_id, dependent: :destroy, inverse_of: :target_account
+
+    has_many :report_notes, dependent: :destroy
+    has_many :custom_filters, inverse_of: :account, dependent: :destroy
+
+    # Moderation notes
+    has_many :account_moderation_notes, dependent: :destroy, inverse_of: :account
+    has_many :targeted_moderation_notes, class_name: 'AccountModerationNote', foreign_key: :target_account_id, dependent: :destroy, inverse_of: :target_account
+
+    # Lists (that the account is on, not owned by the account)
+    has_many :list_accounts, inverse_of: :account, dependent: :destroy
+    has_many :lists, through: :list_accounts
+
+    # Lists (owned by the account)
+    has_many :owned_lists, class_name: 'List', dependent: :destroy, inverse_of: :account
+
+    # Account migrations
+    belongs_to :moved_to_account, class_name: 'Account', optional: true
+  end
+end
diff --git a/app/models/status.rb b/app/models/status.rb
index 65cd97c51..e709b16c8 100644
--- a/app/models/status.rb
+++ b/app/models/status.rb
@@ -241,8 +241,8 @@ class Status < ApplicationRecord
     update_status_stat!(key => [public_send(key) - 1, 0].max)
   end
 
-  after_create  :increment_counter_caches
-  after_destroy :decrement_counter_caches
+  after_create_commit  :increment_counter_caches
+  after_destroy_commit :decrement_counter_caches
 
   after_create_commit :store_uri, if: :local?
   after_create_commit :update_statistics, if: :local?
@@ -446,7 +446,7 @@ class Status < ApplicationRecord
   end
 
   def store_uri
-    update_attribute(:uri, ActivityPub::TagManager.instance.uri_for(self)) if uri.nil?
+    update_column(:uri, ActivityPub::TagManager.instance.uri_for(self)) if uri.nil?
   end
 
   def prepare_contents