Module: Cosgrove::Support
- Includes:
- ActionView::Helpers::DateHelper, ActionView::Helpers::TextHelper, Utils
- Included in:
- Bot, CommentJob, Market, UpvoteJob
- Defined in:
- lib/cosgrove/support.rb
Instance Method Summary
collapse
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
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
Instance Method Details
#append_link_details(event, slug) ⇒ Object
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
181
182
|
# File 'lib/cosgrove/support.rb', line 60
def append_link_details(event, slug)
return if skipped_channel? event.channel.id
chain = :hive
author_name, permlink = parse_slug slug
created = nil
cashout_time = nil
message = nil
return unless !!author_name && !!permlink
if !!event
begin
message = event.respond "Looking up `#{author_name}/#{permlink}` ..."
rescue Discordrb::Errors::NoPermission => _
puts "Unable to append link details on #{event.channel.server.name} in #{event.channel.name}"
skip_channel event.channel.id
return nil
end
end
posts = case chain
when :hive then HiveSQL::Comment.where(author: author_name, permlink: permlink)
end
posts.select(:ID, :created, :cashout_time, :author, :permlink, :active_votes, :children, :category)
post = posts.last
if post.nil?
api(chain).get_content(author_name, permlink) do |content, errors|
unless content.author.empty?
post = content
created = Time.parse(content.created + 'Z')
cashout_time = Time.parse(content.cashout_time + 'Z')
end
end
end
if post.nil?
if !!message
message = message.edit 'Looking for content on Hive ...'
end
api(:hive).get_content(author_name, permlink) do |content, errors|
unless content.author.empty?
post = content
created = Time.parse(content.created + 'Z')
cashout_time = Time.parse(content.cashout_time + 'Z')
end
end
end
if post.nil?
if !!message
message = message.edit 'Unable to locate.'
sleep 5
message.delete
end
return
end
created ||= post.created
cashout_time ||= post.cashout_time
details = []
age = time_ago_in_words(created)
age = age.slice(0, 1).capitalize + age.slice(1..-1)
details << if created < 15.minutes.ago
"#{age} old"
else
"**#{age}** old"
end
message = message.edit details.join('; ') if !!message
if !!post.category && !!post.category.starts_with?('hive-') && (communities = Cosgrove::FindCommunitiesJob.new.perform(nil, [post.category]), 1).any?
details << "in #{communities.first.payload['props']['title']}" rescue nil
message = message.edit details.join('; ') if !!message
end
active_votes = case post.active_votes
when String then JSON[post.active_votes] rescue []
else; active_votes
end
if active_votes.respond_to?(:any?) && active_votes.any?
upvotes = active_votes.map{ |v| v if v['weight'].to_i > 0 }.compact.count
downvotes = active_votes.map{ |v| v if v['weight'].to_i < 0 }.compact.count
netvotes = upvotes - downvotes
details << "Net votes: #{netvotes}"
message = message.edit details.join('; ') if !!message
if created > 1.hour.ago
votes = case chain
when :hive then HiveSQL::Tx::Vote.where('timestamp > ?', post.created)
end
total_votes = votes.distinct("concat(author, permlink)").count
total_voters = votes.distinct(:voter).count
if total_votes > 0 && total_voters > 0
details << "Out of #{pluralize(total_votes - netvotes, 'vote')} cast by #{pluralize(total_voters, 'voter')}"
message = message.edit details.join('; ') if !!message
end
end
end
details << "Comments: #{post.children.to_i}"
message = message.edit details.join('; ') if !!message
details.join('; ') if event.nil?
end
|
54
55
56
57
58
|
# 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) ⇒ Object
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
# 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 :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 == :hive && (accounts = HiveSQL::Account.where(name: key)).any?
accounts.first
else
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) ⇒ Object
266
267
268
269
270
271
272
|
# 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 = {}) ⇒ Object
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
# 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) ⇒ Object
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
# 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.({
'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
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) ⇒ Object
274
275
276
277
278
279
280
281
282
|
# 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) ⇒ Object
317
318
319
|
# File 'lib/cosgrove/support.rb', line 317
def skip_channel(id)
skipped_channels << id
end
|
#skipped_channel?(id) ⇒ Boolean
313
314
315
|
# File 'lib/cosgrove/support.rb', line 313
def skipped_channel?(id)
skipped_channels.include? id
end
|
#skipped_channels ⇒ Object
309
310
311
|
# File 'lib/cosgrove/support.rb', line 309
def skipped_channels
@@skipped_channels ||= []
end
|
#start_typing(event) ⇒ Object
Reduce RL bucket depletion
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# 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) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# 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 :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) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# 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
|