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.rb68
-rw-r--r--app/models/list.rb19
-rw-r--r--app/models/list_account.rb2
-rw-r--r--app/models/preview_card.rb1
-rw-r--r--app/models/remote_follow.rb4
-rw-r--r--app/models/session_activation.rb12
-rw-r--r--app/models/user.rb2
7 files changed, 79 insertions, 29 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index ffd19fa52..48b17bbb8 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -186,6 +186,21 @@ class Account < ApplicationRecord
     @keypair ||= OpenSSL::PKey::RSA.new(private_key || public_key)
   end
 
+  def magic_key
+    modulus, exponent = [keypair.public_key.n, keypair.public_key.e].map do |component|
+      result = []
+
+      until component.zero?
+        result << [component % 256].pack('C')
+        component >>= 8
+      end
+
+      result.reverse.join
+    end
+
+    (['RSA'] + [modulus, exponent].map { |n| Base64.urlsafe_encode64(n) }).join('.')
+  end
+
   def subscription(webhook_url)
     @subscription ||= OStatus2::Subscription.new(remote_url, secret: secret, webhook: webhook_url, hub: hub_url)
   end
@@ -279,23 +294,46 @@ class Account < ApplicationRecord
       find_by_sql([sql, limit])
     end
 
-    def advanced_search_for(terms, account, limit = 10)
+    def advanced_search_for(terms, account, limit = 10, following = false)
       textsearch, query = generate_query_for_search(terms)
 
-      sql = <<-SQL.squish
-        SELECT
-          accounts.*,
-          (count(f.id) + 1) * ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
-        FROM accounts
-        LEFT OUTER JOIN follows AS f ON (accounts.id = f.account_id AND f.target_account_id = ?) OR (accounts.id = f.target_account_id AND f.account_id = ?)
-        WHERE #{query} @@ #{textsearch}
-          AND accounts.suspended = false
-        GROUP BY accounts.id
-        ORDER BY rank DESC
-        LIMIT ?
-      SQL
-
-      find_by_sql([sql, account.id, account.id, limit])
+      if following
+        sql = <<-SQL.squish
+          WITH first_degree AS (
+            SELECT target_account_id
+            FROM follows
+            WHERE account_id = ?
+          )
+          SELECT
+            accounts.*,
+            (count(f.id) + 1) * ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
+          FROM accounts
+          LEFT OUTER JOIN follows AS f ON (accounts.id = f.account_id AND f.target_account_id = ?) OR (accounts.id = f.target_account_id AND f.account_id = ?)
+          WHERE accounts.id IN (SELECT * FROM first_degree)
+            AND #{query} @@ #{textsearch}
+            AND accounts.suspended = false
+          GROUP BY accounts.id
+          ORDER BY rank DESC
+          LIMIT ?
+        SQL
+
+        find_by_sql([sql, account.id, account.id, account.id, limit])
+      else
+        sql = <<-SQL.squish
+          SELECT
+            accounts.*,
+            (count(f.id) + 1) * ts_rank_cd(#{textsearch}, #{query}, 32) AS rank
+          FROM accounts
+          LEFT OUTER JOIN follows AS f ON (accounts.id = f.account_id AND f.target_account_id = ?) OR (accounts.id = f.target_account_id AND f.account_id = ?)
+          WHERE #{query} @@ #{textsearch}
+            AND accounts.suspended = false
+          GROUP BY accounts.id
+          ORDER BY rank DESC
+          LIMIT ?
+        SQL
+
+        find_by_sql([sql, account.id, account.id, limit])
+      end
     end
 
     private
diff --git a/app/models/list.rb b/app/models/list.rb
index 5d7ba0065..910864b26 100644
--- a/app/models/list.rb
+++ b/app/models/list.rb
@@ -19,4 +19,23 @@ class List < ApplicationRecord
   has_many :accounts, through: :list_accounts
 
   validates :title, presence: true
+
+  before_destroy :clean_feed_manager
+
+  private
+
+  def clean_feed_manager
+    reblog_key       = FeedManager.instance.key(:list, id, 'reblogs')
+    reblogged_id_set = Redis.current.zrange(reblog_key, 0, -1)
+
+    Redis.current.pipelined do
+      Redis.current.del(FeedManager.instance.key(:list, id))
+      Redis.current.del(reblog_key)
+
+      reblogged_id_set.each do |reblogged_id|
+        reblog_set_key = FeedManager.instance.key(:list, id, "reblogs:#{reblogged_id}")
+        Redis.current.del(reblog_set_key)
+      end
+    end
+  end
 end
diff --git a/app/models/list_account.rb b/app/models/list_account.rb
index c08239aa0..253932590 100644
--- a/app/models/list_account.rb
+++ b/app/models/list_account.rb
@@ -14,6 +14,8 @@ class ListAccount < ApplicationRecord
   belongs_to :account, required: true
   belongs_to :follow, required: true
 
+  validates :account_id, uniqueness: { scope: :list_id }
+
   before_validation :set_follow
 
   private
diff --git a/app/models/preview_card.rb b/app/models/preview_card.rb
index e2bf65d94..5baddba8a 100644
--- a/app/models/preview_card.rb
+++ b/app/models/preview_card.rb
@@ -21,6 +21,7 @@
 #  height             :integer          default(0), not null
 #  created_at         :datetime         not null
 #  updated_at         :datetime         not null
+#  embed_url          :string           default(""), not null
 #
 
 class PreviewCard < ApplicationRecord
diff --git a/app/models/remote_follow.rb b/app/models/remote_follow.rb
index c3f867743..070144e2d 100644
--- a/app/models/remote_follow.rb
+++ b/app/models/remote_follow.rb
@@ -7,8 +7,8 @@ class RemoteFollow
 
   validates :acct, presence: true
 
-  def initialize(attrs = {})
-    @acct = attrs[:acct].gsub(/\A@/, '').strip unless attrs[:acct].nil?
+  def initialize(attrs = nil)
+    @acct = attrs[:acct].gsub(/\A@/, '').strip if !attrs.nil? && !attrs[:acct].nil?
   end
 
   def valid?
diff --git a/app/models/session_activation.rb b/app/models/session_activation.rb
index d19489b36..1d4ebca02 100644
--- a/app/models/session_activation.rb
+++ b/app/models/session_activation.rb
@@ -14,16 +14,6 @@
 #  web_push_subscription_id :integer
 #
 
-#  id              :bigint           not null, primary key
-#  user_id         :bigint           not null
-#  session_id      :string           not null
-#  created_at      :datetime         not null
-#  updated_at      :datetime         not null
-#  user_agent      :string           default(""), not null
-#  ip              :inet
-#  access_token_id :bigint
-#
-
 class SessionActivation < ApplicationRecord
   belongs_to :user, inverse_of: :session_activations, required: true
   belongs_to :access_token, class_name: 'Doorkeeper::AccessToken', dependent: :destroy
@@ -53,7 +43,7 @@ class SessionActivation < ApplicationRecord
       id && where(session_id: id).exists?
     end
 
-    def activate(options = {})
+    def activate(**options)
       activation = create!(options)
       purge_old
       activation
diff --git a/app/models/user.rb b/app/models/user.rb
index 578622fdf..29bdcbd67 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -76,7 +76,7 @@ class User < ApplicationRecord
   has_many :session_activations, dependent: :destroy
 
   delegate :auto_play_gif, :default_sensitive, :unfollow_modal, :boost_modal, :delete_modal,
-           :reduce_motion, :system_font_ui, :noindex, :theme,
+           :reduce_motion, :system_font_ui, :noindex, :flavour, :skin,
            to: :settings, prefix: :setting, allow_nil: false
 
   attr_accessor :invite_code