about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
authormultiple creatures <dev@multiple-creature.party>2019-12-11 22:00:22 -0600
committermultiple creatures <dev@multiple-creature.party>2019-12-11 22:00:22 -0600
commitdae7cda4abe135b3bb5fe9cfb3380721a2feb03e (patch)
tree4eda3ccfee17cb36a461d31cf74d266d80f6ebae /app/models
parent9a435494c2efdd2ca8fc7f5fa3dbb81bf88633a1 (diff)
move sharekeys & import metadata to own tables
Diffstat (limited to 'app/models')
-rw-r--r--app/models/imported_status.rb13
-rw-r--r--app/models/normalized_status.rb1
-rw-r--r--app/models/sharekey.rb13
-rw-r--r--app/models/status.rb49
4 files changed, 67 insertions, 9 deletions
diff --git a/app/models/imported_status.rb b/app/models/imported_status.rb
new file mode 100644
index 000000000..038301f3e
--- /dev/null
+++ b/app/models/imported_status.rb
@@ -0,0 +1,13 @@
+# == Schema Information
+#
+# Table name: imported_statuses
+#
+#  id        :bigint(8)        not null, primary key
+#  status_id :bigint(8)
+#  origin    :string
+#
+
+class ImportedStatus < ApplicationRecord
+  belongs_to :status, inverse_of: :imported_status
+  validates_uniqueness_of :status_id
+end
diff --git a/app/models/normalized_status.rb b/app/models/normalized_status.rb
index 10c2bf788..ad1ffaffc 100644
--- a/app/models/normalized_status.rb
+++ b/app/models/normalized_status.rb
@@ -9,4 +9,5 @@
 
 class NormalizedStatus < ApplicationRecord
   belongs_to :status, inverse_of: :normalized_status
+  validates_uniqueness_of :status_id
 end
diff --git a/app/models/sharekey.rb b/app/models/sharekey.rb
new file mode 100644
index 000000000..baa1c20a0
--- /dev/null
+++ b/app/models/sharekey.rb
@@ -0,0 +1,13 @@
+# == Schema Information
+#
+# Table name: sharekeys
+#
+#  id        :bigint(8)        not null, primary key
+#  status_id :bigint(8)
+#  key       :string
+#
+
+class Sharekey < ApplicationRecord
+  belongs_to :status, inverse_of: :sharekey
+  validates_uniqueness_of :status_id
+end
diff --git a/app/models/status.rb b/app/models/status.rb
index 27184591b..e46e36403 100644
--- a/app/models/status.rb
+++ b/app/models/status.rb
@@ -24,13 +24,10 @@
 #  local_only             :boolean
 #  poll_id                :bigint(8)
 #  curated                :boolean          default(FALSE), not null
-#  sharekey               :string
 #  network                :boolean          default(FALSE), not null
 #  content_type           :string
 #  footer                 :text
 #  edited                 :boolean
-#  imported               :boolean
-#  origin                 :string
 #  boostable              :boolean
 #  reject_replies         :boolean
 #
@@ -86,6 +83,8 @@ class Status < ApplicationRecord
   has_one :poll, inverse_of: :status, dependent: :destroy
   has_one :destructing_status, inverse_of: :status, dependent: :destroy
   has_one :normalized_status, inverse_of: :status, dependent: :destroy
+  has_one :imported_status, inverse_of: :status, dependent: :destroy
+  has_one :sharekey, inverse_of: :status, dependent: :destroy
 
   validates :uri, uniqueness: true, presence: true, unless: :local?
   validates :text, presence: true, unless: -> { with_media? || reblog? }
@@ -318,12 +317,22 @@ class Status < ApplicationRecord
     update_status_stat!(key => [public_send(key) - 1, 0].max)
   end
 
-  def session=(value)
-    @session = value
+  def sharekey=(value)
+    if value.nil? && !(new_record? || self.sharekey.nil?)
+      self.sharekey.destroy
+    else
+      @_sharekey = value
+      update_sharekey unless new_record? || changed?
+    end
   end
 
-  def session
-    @session || nil
+  def origin=(value)
+    if value.nil? && !(new_record? || self.imported_status.nil?)
+      self.imported_status.destroy
+    else
+      @_origin = value
+      update_origin unless new_record? || changed?
+    end
   end
 
   after_create_commit  :increment_counter_caches
@@ -344,8 +353,11 @@ class Status < ApplicationRecord
   before_validation :infer_reject_replies
 
   after_create :set_poll_id
-  after_create :update_normalized_text
-  after_create :process_bangtags, if: :local?
+
+  after_save :update_sharekey, if: :local?
+  after_save :update_origin, if: :local?
+  after_save :update_normalized_text
+  after_save :process_bangtags, if: :local?
 
   class << self
     include SearchHelper
@@ -629,9 +641,28 @@ class Status < ApplicationRecord
   end
 
   def process_bangtags
+    return unless text_changed? || saved_change_to_text?
     Bangtags.new(self).process
   end
 
+  def update_sharekey
+    return if @_sharekey.nil?
+    if self.sharekey.nil?
+      self.create_sharekey(key: @_sharekey)
+    else
+      self.sharekey.update_attributes(key: @_sharekey)
+    end
+  end
+
+  def update_origin
+    return if @_origin.nil?
+    if self.imported_status.nil?
+      self.create_imported_status(origin: @_origin)
+    else
+      self.imported_status.update_attributes(origin: @_origin)
+    end
+  end
+
   def update_normalized_text
     return if destroyed? || text.blank? || !(text_changed? || saved_change_to_text?)
     normalized_text = normalize_status(self)