]> cat aescling's git repositories - mastodon.git/blob - app/models/concerns/account_finder_concern.rb
Create instance actor if it hasn't been properly seeded (#15693)
[mastodon.git] / app / models / concerns / account_finder_concern.rb
1 # frozen_string_literal: true
2
3 module AccountFinderConcern
4 extend ActiveSupport::Concern
5
6 class_methods do
7 def find_local!(username)
8 find_local(username) || raise(ActiveRecord::RecordNotFound)
9 end
10
11 def find_remote!(username, domain)
12 find_remote(username, domain) || raise(ActiveRecord::RecordNotFound)
13 end
14
15 def representative
16 Account.find(-99)
17 rescue ActiveRecord::RecordNotFound
18 Account.create!(id: -99, actor_type: 'Application', locked: true, username: Rails.configuration.x.local_domain)
19 end
20
21 def find_local(username)
22 find_remote(username, nil)
23 end
24
25 def find_remote(username, domain)
26 AccountFinder.new(username, domain).account
27 end
28 end
29
30 class AccountFinder
31 attr_reader :username, :domain
32
33 def initialize(username, domain)
34 @username = username
35 @domain = domain
36 end
37
38 def account
39 scoped_accounts.order(id: :asc).take
40 end
41
42 private
43
44 def scoped_accounts
45 Account.unscoped.tap do |scope|
46 scope.merge! with_usernames
47 scope.merge! matching_username
48 scope.merge! matching_domain
49 end
50 end
51
52 def with_usernames
53 Account.where.not(Account.arel_table[:username].lower.eq '')
54 end
55
56 def matching_username
57 Account.where(Account.arel_table[:username].lower.eq username.to_s.downcase)
58 end
59
60 def matching_domain
61 Account.where(Account.arel_table[:domain].lower.eq(domain.nil? ? nil : domain.to_s.downcase))
62 end
63 end
64 end
This page took 0.096808 seconds and 5 git commands to generate.