module Cosgrove::Support

Public Instance Methods

cannot_find_input(event, message_prefix = "Unable to find that.") click to toggle source
# File lib/cosgrove/support.rb, line 54
def cannot_find_input(event, message_prefix = "Unable to find that.")
  message = [message_prefix]
  
  event.respond message.join(' ')
end
find_account(key, event = nil, chain = :hive) click to toggle source
# File lib/cosgrove/support.rb, line 184
def find_account(key, event = nil, chain = :hive)
  key = key.to_s.downcase
  chain ||= :hive
  chain = chain.to_s.downcase.to_sym
  
  
  case chain
  # when :steem
  #   account = if (accounts = SteemApi::Account.where(name: key)).any?
  #     accounts.first
  #   end
  when :hive
    account = if (accounts = HiveSQL::Account.where(name: key)).any?
      accounts.first
    end
  end
  
  if account.nil?
    account = if !!(cb_account = Cosgrove::Account.find_by_discord_id(key, chain))
      cb_account.chain_account
    end
  end
  
  if account.nil?
    account = if !!key
      # if chain == :steem && (accounts = SteemApi::Account.where(name: key)).any?
      #   accounts.first
      if chain == :hive && (accounts = HiveSQL::Account.where(name: key)).any?
        accounts.first
      else
        # Fall back to RPC
        api(chain).get_accounts([key]) do |_accounts, errors|
          _accounts.first
        end
      end
    end
  end
    
  if account.nil?
    unknown_account(key, event)
  else
    account
  end
end
last_irreversible_block(chain = :hive) click to toggle source
# File lib/cosgrove/support.rb, line 266
def last_irreversible_block(chain = :hive)
  chain ||= :hive
  chain = chain.to_s.downcase.to_sym
  seconds_ago = (head_block_number(chain) - last_irreversible_block_num(chain)) * 3
  
  "Last Irreversible Block was #{time_ago_in_words(seconds_ago.seconds.ago)} ago."
end
muted(options = {}) click to toggle source
# File lib/cosgrove/support.rb, line 284
def muted(options = {})
  [] if options.empty?
  by = [options[:by]].flatten
  chain = options[:chain] || :hive
  chain = chain.to_s.downcase.to_sym
  muted = []
  
  by.each do |a|
    ignoring = []
    count = -1
    until count == ignoring.size
      count = ignoring.size
      follow_api(chain).get_following(a, ignoring.last, 'ignore', 100) do |ignores, errors|
        next unless defined? ignores.following
        
        ignoring += ignores.map(&:following)
        ignoring = ignoring.uniq
      end
    end
    muted += ignoring
  end
  
  muted.uniq
end
page_views(uri) click to toggle source
# File lib/cosgrove/support.rb, line 229
def page_views(uri)
  begin
    @agent ||= Cosgrove::Agent.new
    page = @agent.get("https://steemit.com#{uri}")
    
    _uri = URI.parse('https://steemit.com/api/v1/page_view')
    https = Net::HTTP.new(_uri.host,_uri.port)
    https.use_ssl = true
    request = Net::HTTP::Post.new(_uri.path)
    request.initialize_http_header({
      'Cookie' => @agent.cookies.join('; '),
      'accept' => 'application/json',
      'Accept-Encoding' => 'gzip, deflate, br',
      'Accept-Language' => 'en-US,en;q=0.8',
      'Connection' => 'keep-alive',
      'content-type' => 'text/plain;charset=UTF-8',
      'Host' => 'steemit.com',
      'Origin' => 'https://steemit.com'
    })
    
    csrf = page.parser.to_html.split(':{"csrf":"').last.split('","new_visit":').first
    # Uncomment in case views stop showing.
    # puts "DEBUG: #{csrf}"
    return unless csrf.size == 36
    
    post_data = {
      csrf: csrf,
      page: uri
    }
    request.set_form_data(post_data)
    response = https.request(request)
    JSON[response.body]['views']
  rescue => e
    puts "Attempting to get page_view failed: #{e}"
  end
end
send_url(event, url) click to toggle source
# File lib/cosgrove/support.rb, line 274
def send_url(event, url)
  open(url) do |f|
    tempfile = Tempfile.new(['send_url', ".#{url.split('.').last}"])
    tempfile.binmode
    tempfile.write(f.read)
    tempfile.close
    event.send_file File.open tempfile.path
  end
end
skip_channel(id) click to toggle source
# File lib/cosgrove/support.rb, line 317
def skip_channel(id)
  skipped_channels << id
end
skipped_channel?(id) click to toggle source
# File lib/cosgrove/support.rb, line 313
def skipped_channel?(id)
  skipped_channels.include? id
end
skipped_channels() click to toggle source
# File lib/cosgrove/support.rb, line 309
def skipped_channels
  @@skipped_channels ||= []
end
start_typing(event) click to toggle source

Reduce RL bucket depletion

# File lib/cosgrove/support.rb, line 8
def start_typing(event)
  return if event.nil?
  return unless event.respond_to? :channel
  return unless event.channel.respond_to? :start_typing
  
  @channels_typing ||= {}
  
  if !!@channels_typing[event.channel.id] && (Time.now - @channels_typing[event.channel.id]) < 15
    return
  end
  
  @channels_typing[event.channel.id] = Time.now
  
  event.channel.start_typing
end
suggest_account_name(account_name, chain = :hive) click to toggle source
# File lib/cosgrove/support.rb, line 24
def suggest_account_name(account_name, chain = :hive)
  chain = chain.to_s.downcase.to_sym
  pattern = account_name.chars.each.map{ |c| c }.join('%')
  guesses = case chain
  # when :steem then SteemApi::Account.where("name LIKE '%#{pattern}%'").pluck(:name)
  when :hive then HiveSQL::Account.where("name LIKE '%#{pattern}%'").pluck(:name)
  else
    []
  end
  
  if guesses.any?
    guesses.sample
  end
end
unknown_account(account_name, event = nil) click to toggle source
# File lib/cosgrove/support.rb, line 39
def unknown_account(account_name, event = nil)
  help = ["Unknown account: *#{account_name}*"]
  start_typing event
  guess = suggest_account_name(account_name)

  help << ", did you mean: #{guess}?" if !!guess
  
  if !!event
    event.respond help.join
    return
  end
  
  help.join
end