Class: Cosgrove::UpvoteJob

Inherits:
Object
  • Object
show all
Includes:
Config, Support, Utils
Defined in:
lib/cosgrove/upvote_job.rb

Instance Method Summary collapse

Methods included from Config

#channel_disable_comment_voting, #channel_upvote_weight, #cosgrove_allow_pm_commands, #cosgrove_client_id, #cosgrove_disable_comment_voting, #cosgrove_operators, #cosgrove_secure, #cosgrove_token, #cosgrove_upvote_weight, #discord_channels, #discord_fancy_log, #discord_log_mode, #hive_account, #hive_api_failover_urls, #hive_api_url, #hive_engine_api_url, #hive_posting_wif, #meeseeker_url, #steem_account, #steem_api_failover_urls, #steem_api_url, #steem_engine_api_url, #steem_posting_wif, #test_api_failover_urls, #test_api_url, #test_posting_wif

Methods included from Support

#append_link_details, #cannot_find_input, #find_account, #last_irreversible_block, #muted, #page_views, #send_url, #skip_channel, #skipped_channel?, #skipped_channels, #start_typing, #suggest_account_name, #unknown_account

Methods included from Utils

#api, #chain_options, #core_asset, #cycle_api_at, #cycle_stream_at, #debt_asset, #find_author, #find_author_name_permlink, #find_comment, #find_comment_by_slug, #find_transfer, #follow_api, #head_block_number, #hive_engine, #hive_engine_blockchain, #hive_engine_contracts, #hive_engine_shutdown, #last_irreversible_block_num, #new_tx, #parse_slug, #ping_api, #properties, #reset_api, #reset_stream, #steem_engine, #steem_engine_blockchain, #steem_engine_contracts, #steem_engine_shutdown, #stream, #to_rep

Constructor Details

#initialize(options = {}) ⇒ UpvoteJob

Returns a new instance of UpvoteJob.



7
8
9
# File 'lib/cosgrove/upvote_job.rb', line 7

def initialize(options = {})
  @on_success = options[:on_success]
end

Instance Method Details

#perform(event, slug, chain = :hive) ⇒ Object



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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/cosgrove/upvote_job.rb', line 11

def perform(event, slug, chain = :hive)
  if slug.nil? || slug.empty?
    event.respond 'Sorry, I wasn\'t paying attention.'
    return
  end
  
  chain = chain.to_s.downcase.to_sym
  author_name, permlink = parse_slug slug
  discord_id = event.author.id
   = Cosgrove::Account.find_by_discord_id(discord_id)
  registered = !!
  muters = cosgrove_operators
  muters << 
  muted = muted by: muters, chain: :steem
  
  post = find_comment(chain: chain, author_name: author_name, permlink: permlink)
  
  if post.nil?
    cannot_find_input(event)
    return
  end
  
  votes_today = case chain
  # when :steem then SteemApi::Tx::Vote.where(voter: steem_account).today
  when :hive then HiveSQL::Tx::Vote.where(voter: ).today
  end
  today_count = votes_today.count
  author_count = votes_today.where(author: author_name).count
  vote_ratio = if today_count == 0
    0.0
  else
    author_count.to_f / today_count
  end
  
  created ||= post.created
  cashout_time ||= post.cashout_time
  root_post = post.parent_author == ''
  
  if created.class == String
    created = Time.parse(created + 'Z')
    cashout_time = Time.parse(cashout_time + 'Z')
  end
  
  active_votes = if post.active_votes.class == String
    active_votes = JSON[post.active_votes]
  else
    post.active_votes
  end
  
  nope = if created > 1.minute.ago
    "Give it a second!  It's going to SPACE!  Can you give it a second to come back from space?"
  elsif created > 20.minutes.ago
    "Unable to vote.  Please wait 20 minutes before voting."
  elsif cashout_time < Time.now.utc
    'Unable to vote on that.  Too old.'
  elsif post.parent_permlink == 'nsfw'
    puts "Won't vote because parent_permlink: nsfw"
    'Unable to vote on that.'
  elsif post..include?('nsfw')
    puts "Won't vote because json_metadata includes: nsfw"
    'Unable to vote on that.'
  elsif active_votes.map{ |v| v['voter'] }.include?('blacklist-a')
    puts "Won't vote blacklist-a voted."
    'Unable to vote on that.'
  elsif (rep = to_rep(post.author_reputation).to_f) < 25.0
    puts "Won't vote because rep too low: #{rep}"
    'Unable to vote on that.'
  elsif muted.include? author_name
    puts "Won't vote because author muted."
    'Unable to vote because the author has been muted by the operators.'
  elsif !root_post && channel_disable_comment_voting(event.channel.id)
    puts "Won't vote because comment voting is disabled."
    'Unable to vote.'
  elsif !registered
    'Unable to vote.  Feature resticted to registered users.'
  elsif .novote?
    'Unable to vote.  Your account has been resticted.'
  elsif today_count > 10 && vote_ratio > 0.1
    "Maybe later.  It seems like I've been voting for #{author_name} quite a bit lately."
  elsif active_votes.map{ |v| v['voter'] }.include?()
    title = post.title
    title = post.permlink if title.empty?
    "I already voted on #{title} by #{post.author}."
  end

  if !!nope
    event.respond nope
    return
  end
  
  vote = {
    type: :vote,
    voter: ,
    author: post.author,
    permlink: post.permlink,
    weight: upvote_weight(event.channel.id)
  }

  tx = new_tx :steem
  tx.operations << vote
  friendy_error = nil
  response = nil
  
  loop do
    begin
      response = tx.process(true)
    rescue => e
      puts "Unable to vote: #{e}"
      ap e
    end
    
    if !!response && !!response.error
      message = response.error.message
      if message.to_s =~ /missing required posting authority/
        friendy_error = "Failed: Check posting key."
        break
      elsif message.to_s =~ /You have already voted in a similar way./
        friendy_error = "Failed: duplicate vote."
        break
      elsif message.to_s =~ /Can only vote once every 3 seconds./
        puts "Retrying: voting too quickly."
        sleep 3
        redo
      elsif message.to_s =~ /Voting weight is too small, please accumulate more voting power or steem power./
        friendy_error = "Failed: voting weight too small"
        break
      elsif message.to_s =~ /unknown key/
        friendy_error = "Failed: unknown key (testing?)"
        break
      elsif message.to_s =~ /tapos_block_summary/
        puts "Retrying vote/comment: tapos_block_summary (?)"
        redo
      elsif message.to_s =~ /now < trx.expiration/
        puts "Retrying vote/comment: now < trx.expiration (?)"
        redo
      elsif message.to_s =~ /signature is not canonical/
        puts "Retrying vote/comment: signature was not canonical (bug in Radiator?)"
        redo
      elsif message.to_s =~ /STEEMIT_UPVOTE_LOCKOUT_HF17/
        friendy_error = "Failed: upvote lockout (last twelve hours before payout)"
        break
      else
        friendy_error = 'Unable to vote right now.  Maybe I already voted on that.  Try again later.'
        ap response.error
        break
      end
    end
    
    break
  end

  if !!friendy_error
    friendy_error
  elsif !!response.result.id
    ap response.to_json

    if !!@on_success
      begin
        @on_success.call(event, "@#{post.author}/#{post.permlink}")
      rescue => e
        ap e
        ap e.backtrace
      end
    end
    
    "Upvoted: #{post.title} by #{author_name}"
  else
    ':question:'
  end
end