Class: Cosgrove::FindCommunitiesJob

Inherits:
Object
  • Object
show all
Defined in:
lib/cosgrove/find_communities_job.rb

Instance Method Summary collapse

Instance Method Details

#perform(event, args, limit = 1000, chain = :hive) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cosgrove/find_communities_job.rb', line 3

def perform(event, args, limit = 1000, chain = :hive)
  chain = chain.to_s.downcase.to_sym
  terms = args.map{|term| "%#{term.downcase.strip}%"}
  
  all_communities = case chain
  # when :steem then SteemApi::Tx::Custom::Community.where('id >= 217640816').op('updateProps').order(id: :desc).limit(limit)
  when :hive then HiveSQL::Tx::Custom::Community.where('id >= 217640816').op('updateProps').order(id: :desc).limit(limit)
  end
  
  all_communities = all_communities.select("*, (SELECT [Accounts].[recovery_account] FROM [Accounts] WHERE [Accounts].[name] = JSON_VALUE([TxCustoms].[json_metadata], '$[1].community')) AS community_owner")
  
  communities = all_communities.all
  
  terms.each do |term|
    communities = communities.where("LOWER([TxCustoms].[json_metadata]) LIKE ? OR required_posting_auth LIKE ? OR (SELECT [Accounts].[recovery_account] FROM [Accounts] WHERE [Accounts].[name] = JSON_VALUE([TxCustoms].[json_metadata], '$[1].community')) = ?", term, term, term)
  end
  
  event.channel.start_typing if !!event
  
  if communities.none?
    msg = "Unable to find communities with: `#{args.join(' ')}`"
    
    guess_communities = all_communities.all
    
    terms.each do |term|
      pattern = term.chars.each.map{ |c| c }.join('%')
      pattern = pattern.gsub('%%', '%')
      guess_communities = guess_communities.where("LOWER(JSON_VALUE([TxCustoms].[json_metadata], '$[1].props.title')) LIKE ?", pattern)
    end
    
    if guess_communities.any? && !!(guess = guess_communities.sample.payload['props']['title'] rescue nil)
      msg += "\nDid you mean: #{guess}"
    end
    
    if !!event
      event << msg
    end
    
    return []
  end
  
  communities
end