Merge remote-tracking branch 'tootsuite/master' into glitchsoc/master

This commit is contained in:
Jenkins
2017-12-06 20:17:13 +00:00
78 changed files with 1382 additions and 199 deletions
+52 -14
View File
@@ -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
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, limit])
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
+19
View File
@@ -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
+2
View File
@@ -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
+2 -2
View File
@@ -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?
+1 -1
View File
@@ -53,7 +53,7 @@ class SessionActivation < ApplicationRecord
id && where(session_id: id).exists?
end
def activate(options = {})
def activate(**options)
activation = create!(options)
purge_old
activation