cosgrove 0.0.3.5 → 0.0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,9 +4,32 @@ module Cosgrove
4
4
  include ActionView::Helpers::TextHelper
5
5
  include ActionView::Helpers::DateHelper
6
6
 
7
- def suggest_account_name(account_name)
8
- regex = /.*#{account_name.chars.each.map { |c| c }.join('.*')}.*/
9
- guesses = SteemData::Account.where(name: regex).distinct(:name)
7
+ # Reduce RL bucket depletion
8
+ def start_typing(event)
9
+ return if event.nil?
10
+ return unless event.respond_to? :channel
11
+ return unless event.channel.respond_to? :start_typing
12
+
13
+ @channels_typing ||= {}
14
+
15
+ if !!@channels_typing[event.channel.id] && (Time.now - @channels_typing[event.channel.id]) < 15
16
+ return
17
+ end
18
+
19
+ @channels_typing[event.channel.id] = Time.now
20
+
21
+ event.channel.start_typing
22
+ end
23
+
24
+ def suggest_account_name(account_name, chain = :hive)
25
+ chain = chain.to_s.downcase.to_sym
26
+ pattern = account_name.chars.each.map{ |c| c }.join('%')
27
+ guesses = case chain
28
+ # when :steem then SteemApi::Account.where("name LIKE '%#{pattern}%'").pluck(:name)
29
+ when :hive then HiveSQL::Account.where("name LIKE '%#{pattern}%'").pluck(:name)
30
+ else
31
+ []
32
+ end
10
33
 
11
34
  if guesses.any?
12
35
  guesses.sample
@@ -15,7 +38,7 @@ module Cosgrove
15
38
 
16
39
  def unknown_account(account_name, event = nil)
17
40
  help = ["Unknown account: *#{account_name}*"]
18
- event.channel.start_typing if !!event
41
+ start_typing event
19
42
  guess = suggest_account_name(account_name)
20
43
 
21
44
  help << ", did you mean: #{guess}?" if !!guess
@@ -28,75 +51,77 @@ module Cosgrove
28
51
  help.join
29
52
  end
30
53
 
31
- def mongo_behind_warning(event)
32
- elapse = -1
33
-
34
- begin
35
- message = []
36
-
37
- if (blocks = head_block_number(:steem) - steem_data_head_block_number) > 1200
38
- elapse = blocks * 3
39
- message << "Mongo is behind by #{time_ago_in_words(elapse.seconds.ago)}."
40
- else
41
- 0
42
- end
43
-
44
- if message.size > 0
45
- event.respond message.join(' ')
46
- end
47
- rescue => e
48
- event.respond "Mongo might be behind, but the API is also acting up. Please try again later.\n\n```#{e.inspect}\n```"
49
- sleep 15
50
- event.respond Cosgrove::SnarkCommands::WITTY.sample
51
- end
52
-
53
- elapse
54
- end
55
-
56
54
  def cannot_find_input(event, message_prefix = "Unable to find that.")
57
55
  message = [message_prefix]
58
56
 
59
- message << if (blocks = head_block_number(:steem) - steem_data_head_block_number) > 86400
60
- elapse = blocks * 3
61
- " Mongo is behind by #{time_ago_in_words(elapse.seconds.ago)}. Try again later."
62
- else
63
- " Mongo might be behind or this is not a valid input."
64
- end
65
-
66
57
  event.respond message.join(' ')
67
58
  end
68
59
 
69
60
  def append_link_details(event, slug)
61
+ return if skipped_channel? event.channel.id
62
+
63
+ chain = :hive
70
64
  author_name, permlink = parse_slug slug
71
65
  created = nil
72
66
  cashout_time = nil
67
+ message = nil
73
68
 
74
- if slug =~ /steemit.com/
75
- chain = :steem
76
- elsif slug =~ /golos.io/
77
- chain = :golos
78
- elsif slug =~ /golos.blog/
79
- chain = :golos
80
- else
81
- return # silntlly ignore this slug
69
+ return unless !!author_name && !!permlink
70
+
71
+ if !!event
72
+ begin
73
+ message = event.respond "Looking up `#{author_name}/#{permlink}` ..."
74
+ rescue Discordrb::Errors::NoPermission => _
75
+ puts "Unable to append link details on #{event.channel.server.name} in #{event.channel.name}"
76
+ skip_channel event.channel.id
77
+ return nil
78
+ end
82
79
  end
83
80
 
84
- post = case chain
85
- when :steem then SteemData::Post.where(author: author_name, permlink: permlink).last
86
- when :golos then GolosCloud::Comment.where(author: author_name, permlink: permlink).last
81
+ posts = case chain
82
+ # when :steem then SteemApi::Comment.where(author: author_name, permlink: permlink)
83
+ when :hive then HiveSQL::Comment.where(author: author_name, permlink: permlink)
87
84
  end
88
85
 
86
+ posts.select(:ID, :created, :cashout_time, :author, :permlink, :active_votes, :children, :category)
87
+
88
+ post = posts.last
89
+
89
90
  if post.nil?
90
91
  # Fall back to RPC
91
92
  api(chain).get_content(author_name, permlink) do |content, errors|
92
93
  unless content.author.empty?
94
+ post = content
93
95
  created = Time.parse(content.created + 'Z')
94
96
  cashout_time = Time.parse(content.cashout_time + 'Z')
95
97
  end
96
98
  end
97
99
  end
98
100
 
99
- return if post.nil?
101
+ if post.nil?
102
+ if !!message
103
+ message = message.edit 'Looking for content on Hive ...'
104
+ end
105
+
106
+ # Fall back to Hive RPC
107
+ api(:hive).get_content(author_name, permlink) do |content, errors|
108
+ unless content.author.empty?
109
+ post = content
110
+ created = Time.parse(content.created + 'Z')
111
+ cashout_time = Time.parse(content.cashout_time + 'Z')
112
+ end
113
+ end
114
+ end
115
+
116
+ if post.nil?
117
+ if !!message
118
+ message = message.edit 'Unable to locate.'
119
+ sleep 5
120
+ message.delete
121
+ end
122
+
123
+ return
124
+ end
100
125
 
101
126
  created ||= post.created
102
127
  cashout_time ||= post.cashout_time
@@ -105,55 +130,70 @@ module Cosgrove
105
130
  age = time_ago_in_words(created)
106
131
  age = age.slice(0, 1).capitalize + age.slice(1..-1)
107
132
 
108
- details << if created < 30.minutes.ago
133
+ details << if created < 15.minutes.ago
109
134
  "#{age} old"
110
135
  else
111
136
  "**#{age}** old"
112
137
  end
138
+ message = message.edit details.join('; ') if !!message
139
+
140
+ if !!post.category && !!post.category.starts_with?('hive-') && (communities = Cosgrove::FindCommunitiesJob.new.perform(nil, [post.category]), 1).any?
141
+ details << "in #{communities.first.payload['props']['title']}" rescue nil
142
+ message = message.edit details.join('; ') if !!message
143
+ end
144
+
145
+ active_votes = case post.active_votes
146
+ when String then JSON[post.active_votes] rescue []
147
+ else; active_votes
148
+ end
113
149
 
114
- if post.active_votes.any?
115
- upvotes = post.active_votes.map{ |v| v if v['percent'] > 0 }.compact.count
116
- downvotes = post.active_votes.map{ |v| v if v['percent'] < 0 }.compact.count
150
+ if active_votes.respond_to?(:any?) && active_votes.any?
151
+ upvotes = active_votes.map{ |v| v if v['weight'].to_i > 0 }.compact.count
152
+ downvotes = active_votes.map{ |v| v if v['weight'].to_i < 0 }.compact.count
117
153
  netvotes = upvotes - downvotes
118
154
  details << "Net votes: #{netvotes}"
155
+ message = message.edit details.join('; ') if !!message
119
156
 
120
157
  # Only append this detail of the post less than an hour old.
121
158
  if created > 1.hour.ago
122
159
  votes = case chain
123
- when :steem then SteemData::AccountOperation.type('vote').starting(post.created)
124
- when :golos then GolosCloud::Vote.where('timestamp > ?', post.created)
160
+ # when :steem then SteemApi::Tx::Vote.where('timestamp > ?', post.created)
161
+ when :hive then HiveSQL::Tx::Vote.where('timestamp > ?', post.created)
125
162
  end
126
- total_votes = votes.count
127
- total_voters = votes.distinct(:voter).size
163
+ total_votes = votes.distinct("concat(author, permlink)").count
164
+ total_voters = votes.distinct(:voter).count
128
165
 
129
166
  if total_votes > 0 && total_voters > 0
130
167
  details << "Out of #{pluralize(total_votes - netvotes, 'vote')} cast by #{pluralize(total_voters, 'voter')}"
168
+ message = message.edit details.join('; ') if !!message
131
169
  end
132
170
  end
133
171
  end
134
172
 
135
173
  details << "Comments: #{post.children.to_i}"
174
+ message = message.edit details.join('; ') if !!message
136
175
 
137
- page_views = page_views("/#{post.parent_permlink}/@#{post.author}/#{post.permlink}")
138
- details << "Views: #{page_views}" if !!page_views
139
-
140
- begin
141
- event.respond details.join('; ')
142
- rescue Discordrb::Errors::NoPermission => _
143
- puts "Unable to append link details on #{event.channel.server.name} in #{event.channel.name}"
144
- end
176
+ # Page View counter is no longer supported by steemit.com.
177
+ # page_views = page_views("/#{post.parent_permlink}/@#{post.author}/#{post.permlink}")
178
+ # details << "Views: #{page_views}" if !!page_views
179
+ # message = message.edit details.join('; ') if !!message
145
180
 
146
- return nil
181
+ details.join('; ') if event.nil?
147
182
  end
148
183
 
149
- def find_account(key, event = nil, chain = :steem)
184
+ def find_account(key, event = nil, chain = :hive)
150
185
  key = key.to_s.downcase
151
- chain = chain.to_sym
186
+ chain ||= :hive
187
+ chain = chain.to_s.downcase.to_sym
152
188
 
153
- raise "Required argument: chain" if chain.nil?
154
189
 
155
- if chain == :steem
156
- account = if (accounts = SteemData::Account.where(name: key)).any?
190
+ case chain
191
+ # when :steem
192
+ # account = if (accounts = SteemApi::Account.where(name: key)).any?
193
+ # accounts.first
194
+ # end
195
+ when :hive
196
+ account = if (accounts = HiveSQL::Account.where(name: key)).any?
157
197
  accounts.first
158
198
  end
159
199
  end
@@ -166,9 +206,9 @@ module Cosgrove
166
206
 
167
207
  if account.nil?
168
208
  account = if !!key
169
- if chain == :steem && (accounts = SteemApi::Account.where(name: key)).any?
170
- accounts.first
171
- elsif chain == :golos && (accounts = GolosCloud::Account.where(name: key)).any?
209
+ # if chain == :steem && (accounts = SteemApi::Account.where(name: key)).any?
210
+ # accounts.first
211
+ if chain == :hive && (accounts = HiveSQL::Account.where(name: key)).any?
172
212
  accounts.first
173
213
  else
174
214
  # Fall back to RPC
@@ -206,7 +246,7 @@ module Cosgrove
206
246
  'Origin' => 'https://steemit.com'
207
247
  })
208
248
 
209
- csrf = page.parser.to_html.split(',"csrf":"').last.split('","new_visit":').first
249
+ csrf = page.parser.to_html.split(':{"csrf":"').last.split('","new_visit":').first
210
250
  # Uncomment in case views stop showing.
211
251
  # puts "DEBUG: #{csrf}"
212
252
  return unless csrf.size == 36
@@ -223,7 +263,9 @@ module Cosgrove
223
263
  end
224
264
  end
225
265
 
226
- def last_irreversible_block(chain = :steem)
266
+ def last_irreversible_block(chain = :hive)
267
+ chain ||= :hive
268
+ chain = chain.to_s.downcase.to_sym
227
269
  seconds_ago = (head_block_number(chain) - last_irreversible_block_num(chain)) * 3
228
270
 
229
271
  "Last Irreversible Block was #{time_ago_in_words(seconds_ago.seconds.ago)} ago."
@@ -242,7 +284,8 @@ module Cosgrove
242
284
  def muted(options = {})
243
285
  [] if options.empty?
244
286
  by = [options[:by]].flatten
245
- chain = options[:chain]
287
+ chain = options[:chain] || :hive
288
+ chain = chain.to_s.downcase.to_sym
246
289
  muted = []
247
290
 
248
291
  by.each do |a|
@@ -262,5 +305,17 @@ module Cosgrove
262
305
 
263
306
  muted.uniq
264
307
  end
308
+
309
+ def skipped_channels
310
+ @@skipped_channels ||= []
311
+ end
312
+
313
+ def skipped_channel?(id)
314
+ skipped_channels.include? id
315
+ end
316
+
317
+ def skip_channel(id)
318
+ skipped_channels << id
319
+ end
265
320
  end
266
321
  end
@@ -8,28 +8,32 @@ module Cosgrove
8
8
  @on_success = options[:on_success]
9
9
  end
10
10
 
11
- def perform(event, slug)
11
+ def perform(event, slug, chain = :hive)
12
12
  if slug.nil? || slug.empty?
13
13
  event.respond 'Sorry, I wasn\'t paying attention.'
14
14
  return
15
15
  end
16
16
 
17
+ chain = chain.to_s.downcase.to_sym
17
18
  author_name, permlink = parse_slug slug
18
19
  discord_id = event.author.id
19
20
  cb_account = Cosgrove::Account.find_by_discord_id(discord_id)
20
21
  registered = !!cb_account
21
22
  muters = cosgrove_operators
22
- muters << steem_account
23
+ muters << hive_account
23
24
  muted = muted by: muters, chain: :steem
24
25
 
25
- post = find_comment(chain: :steem, author_name: author_name, permlink: permlink)
26
+ post = find_comment(chain: chain, author_name: author_name, permlink: permlink)
26
27
 
27
28
  if post.nil?
28
29
  cannot_find_input(event)
29
30
  return
30
31
  end
31
32
 
32
- votes_today = SteemData::AccountOperation.type('vote').where(voter: steem_account).today
33
+ votes_today = case chain
34
+ # when :steem then SteemApi::Tx::Vote.where(voter: steem_account).today
35
+ when :hive then HiveSQL::Tx::Vote.where(voter: hive_account).today
36
+ end
33
37
  today_count = votes_today.count
34
38
  author_count = votes_today.where(author: author_name).count
35
39
  vote_ratio = if today_count == 0
@@ -83,7 +87,7 @@ module Cosgrove
83
87
  'Unable to vote. Your account has been resticted.'
84
88
  elsif today_count > 10 && vote_ratio > 0.1
85
89
  "Maybe later. It seems like I've been voting for #{author_name} quite a bit lately."
86
- elsif active_votes.map{ |v| v['voter'] }.include?(steem_account)
90
+ elsif active_votes.map{ |v| v['voter'] }.include?(hive_account)
87
91
  title = post.title
88
92
  title = post.permlink if title.empty?
89
93
  "I already voted on #{title} by #{post.author}."
@@ -96,7 +100,7 @@ module Cosgrove
96
100
 
97
101
  vote = {
98
102
  type: :vote,
99
- voter: steem_account,
103
+ voter: hive_account,
100
104
  author: post.author,
101
105
  permlink: post.permlink,
102
106
  weight: upvote_weight(event.channel.id)
@@ -180,13 +184,13 @@ module Cosgrove
180
184
 
181
185
  case upvote_weight
182
186
  when 'dynamic'
183
- bot_account = find_account(steem_account)
187
+ bot_account = find_account(hive_account)
184
188
  upvote_weight = bot_account.voting_power.to_i
185
189
  when 'upvote_rules'
186
190
  upvote_weight = channel_upvote_weight(channel_id)
187
191
 
188
192
  if upvote_weight == 'dynamic'
189
- bot_account = find_account(steem_account)
193
+ bot_account = find_account(hive_account)
190
194
  upvote_weight = bot_account.voting_power.to_i
191
195
  else
192
196
  upvote_weight = (((upvote_weight || '0.00 %').to_f) * 100).to_i
@@ -7,8 +7,8 @@ module Cosgrove
7
7
  end
8
8
 
9
9
  def reset_api
10
- @steem_api = @golos_api = @test_api = nil
11
- @steem_folow_api = @golos_follow_api = @test_folow_api = nil
10
+ @steem_api = @hive_api = @test_api = nil
11
+ @steem_follow_api = @hive_follow_api = @test_follow_api = nil
12
12
  @cycle_api_at = nil
13
13
  end
14
14
 
@@ -28,11 +28,11 @@ module Cosgrove
28
28
  url: steem_api_url,
29
29
  failover_urls: steem_api_failover_urls.any? ? steem_api_failover_urls : nil
30
30
  }
31
- when :golos
31
+ when :hive
32
32
  {
33
- chain: :golos,
34
- url: golos_api_url,
35
- failover_urls: golos_api_failover_urls.any? ? golos_api_failover_urls : nil
33
+ chain: :steem, # TODO switch to :hive when supported by radiator
34
+ url: hive_api_url,
35
+ failover_urls: hive_api_failover_urls.any? ? hive_api_failover_urls : nil
36
36
  }
37
37
  when :test
38
38
  {
@@ -50,7 +50,7 @@ module Cosgrove
50
50
 
51
51
  case chain
52
52
  when :steem then @steem_api ||= Radiator::Api.new(chain_options(chain))
53
- when :golos then @golos_api ||= Radiator::Api.new(chain_options(chain))
53
+ when :hive then @hive_api ||= Radiator::Api.new(chain_options(chain))
54
54
  when :test then @test_api ||= Radiator::Api.new(chain_options(chain))
55
55
  end
56
56
  end
@@ -62,18 +62,118 @@ module Cosgrove
62
62
 
63
63
  case chain
64
64
  when :steem then @steem_follow_api ||= Radiator::FollowApi.new(chain_options(chain))
65
- when :golos then @golos_follow_api ||= Radiator::FollowApi.new(chain_options(chain))
65
+ when :hive then @hive_follow_api ||= Radiator::FollowApi.new(chain_options(chain))
66
66
  when :test then @test_follow_api ||= Radiator::FollowApi.new(chain_options(chain))
67
67
  end
68
68
  end
69
69
 
70
+ def steem_engine_shutdown
71
+ problem = false
72
+
73
+ begin
74
+ @steem_engine_blockchain.shutdown if !!@steem_engine_blockchain
75
+ rescue => e
76
+ puts "Unable to shut down steem engine blockchain rpc: #{e}"
77
+ problem = true
78
+ end
79
+
80
+ begin
81
+ @steem_engine_contracts.shutdown if !!@steem_engine_contracts
82
+ rescue => e
83
+ puts "Unable to shut down steem engine contracts rpc: #{e}"
84
+ problem = true
85
+ end
86
+
87
+ !problem
88
+ end
89
+
90
+ def steem_engine(method, params = {}, rpc)
91
+ begin
92
+ if params.respond_to?(:empty?) && params.empty?
93
+ rpc.send(method)
94
+ else
95
+ rpc.send(method, params)
96
+ end
97
+ rescue => e
98
+ steem_engine_shutdown
99
+
100
+ raise e
101
+ end
102
+ end
103
+
104
+ def steem_engine_blockchain(method, params = {}, &block)
105
+ @steem_engine_blockchain ||= Radiator::SSC::Blockchain.new(root_url: steem_engine_api_url)
106
+ result = steem_engine(method, params, @steem_engine_blockchain)
107
+
108
+ yield result if !!block
109
+ return result
110
+ end
111
+
112
+ def steem_engine_contracts(method, params = {}, &block)
113
+ @steem_engine_contracts ||= Radiator::SSC::Contracts.new(root_url: steem_engine_api_url)
114
+ result = steem_engine(method, params, @steem_engine_contracts)
115
+
116
+ yield result if !!block
117
+ return result
118
+ end
119
+
120
+ def hive_engine_shutdown
121
+ problem = false
122
+
123
+ begin
124
+ @hive_engine_blockchain.shutdown if !!@hive_engine_blockchain
125
+ rescue => e
126
+ puts "Unable to shut down hive engine blockchain rpc: #{e}"
127
+ problem = true
128
+ end
129
+
130
+ begin
131
+ @hive_engine_contracts.shutdown if !!@hive_engine_contracts
132
+ rescue => e
133
+ puts "Unable to shut down hive engine contracts rpc: #{e}"
134
+ problem = true
135
+ end
136
+
137
+ !problem
138
+ end
139
+
140
+ def hive_engine(method, params = {}, rpc)
141
+ begin
142
+ if params.respond_to?(:empty?) && params.empty?
143
+ rpc.send(method)
144
+ else
145
+ rpc.send(method, params)
146
+ end
147
+ rescue => e
148
+ hive_engine_shutdown
149
+
150
+ raise e
151
+ end
152
+ end
153
+
154
+ def hive_engine_blockchain(method, params = {}, &block)
155
+ @hive_engine_blockchain ||= Radiator::SSC::Blockchain.new(root_url: hive_engine_api_url)
156
+ result = hive_engine(method, params, @hive_engine_blockchain)
157
+
158
+ yield result if !!block
159
+ return result
160
+ end
161
+
162
+ def hive_engine_contracts(method, params = {}, &block)
163
+ @hive_engine_contracts ||= Radiator::SSC::Contracts.new(root_url: hive_engine_api_url)
164
+ result = hive_engine(method, params, @hive_engine_contracts)
165
+
166
+ yield result if !!block
167
+ return result
168
+ end
169
+
70
170
  def cycle_stream_at
71
171
  @cycle_stream_at if defined? @cycle_stream_at
72
172
  end
73
173
 
74
174
  def reset_stream
75
- @steem_stream = @golos_stream = @test_stream = nil
76
- @steem_folow_stream = @golos_follow_stream = @test_folow_stream = nil
175
+ @steem_stream = @hive_stream = @test_stream = nil
176
+ @steem_follow_stream = @hive_follow_stream = @test_follow_stream = nil
77
177
  @cycle_stream_at = nil
78
178
  end
79
179
 
@@ -84,19 +184,11 @@ module Cosgrove
84
184
 
85
185
  case chain
86
186
  when :steem then @steem_stream ||= Radiator::Stream.new(chain_options(chain))
87
- when :golos then @golos_stream ||= Radiator::Stream.new(chain_options(chain))
187
+ when :hive then @hive_stream ||= Radiator::Stream.new(chain_options(chain))
88
188
  when :test then @test_stream ||= Radiator::Stream.new(chain_options(chain))
89
189
  end
90
190
  end
91
191
 
92
- def steem_data_head_block_number
93
- begin
94
- SteemData::AccountOperation.order(timestamp: -1).limit(1).first.block
95
- rescue
96
- -1
97
- end
98
- end
99
-
100
192
  def properties(chain)
101
193
  api(chain).get_dynamic_global_properties.result
102
194
  end
@@ -105,14 +197,14 @@ module Cosgrove
105
197
  properties(chain)['head_block_number']
106
198
  end
107
199
 
108
- def last_irreversible_block_num(chain = :steem)
200
+ def last_irreversible_block_num(chain = :hive)
109
201
  properties(chain)['last_irreversible_block_num']
110
202
  end
111
203
 
112
204
  def new_tx(chain)
113
205
  case chain
114
206
  when :steem then Radiator::Transaction.new(chain_options(chain).merge(wif: steem_posting_wif))
115
- when :golos then Radiator::Transaction.new(chain_options(chain).merge(wif: golos_posting_wif))
207
+ when :hive then Radiator::Transaction.new(chain_options(chain).merge(wif: hive_posting_wif))
116
208
  when :test then Radiator::Transaction.new(chain_options(chain).merge(wif: test_posting_wif))
117
209
  end
118
210
  end
@@ -136,22 +228,30 @@ module Cosgrove
136
228
  slug = slug.split('@').last
137
229
  author_name = slug.split('/')[0]
138
230
  permlink = slug.split('/')[1..-1].join('/')
231
+ permlink = permlink.split('#')[0]
139
232
 
140
233
  [author_name, permlink]
141
234
  end
142
235
  end
143
236
 
144
- def find_author_name_permlink(slug)
237
+ def find_author_name_permlink(slug, chain = :hive)
238
+ chain = chain.to_s.downcase.to_sym
145
239
  op, author_name = slug.split(':')
146
240
  author_name, offset = author_name.split(/[\+-]/)
147
- author = find_account(author_name)
241
+ author = find_account(author_name, nil, chain)
148
242
 
149
243
  offset = offset.to_i
150
244
 
151
245
  posts = if op == 'latest'
152
- SteemData::Post.root_posts.where(author: author.name).order(created: :desc)
246
+ case chain
247
+ # when :steem then SteemApi::Comment.where(depth: 0, author: author.name).order(created: :desc)
248
+ when :hive then HiveSQL::Comment.where(depth: 0, author: author.name).order(created: :desc)
249
+ end
153
250
  elsif op == 'first'
154
- SteemData::Post.root_posts.where(author: author.name).order(created: :asc)
251
+ case chain
252
+ # when :steem then SteemApi::Comment.where(depth: 0, author: author.name).order(created: :asc)
253
+ when :hive then HiveSQL::Comment.where(depth: 0, author: author.name).order(created: :asc)
254
+ end
155
255
  else
156
256
  []
157
257
  end
@@ -163,19 +263,28 @@ module Cosgrove
163
263
  []
164
264
  end
165
265
 
166
- def find_comment_by_slug(slug)
266
+ def find_comment_by_slug(slug, chain = :hive)
267
+ chain ||= :hive
268
+ chain = chain.to_s.downcase.to_sym
167
269
  author_name, permlink = parse_slug slug
168
- find_comment(chain: :steem, author_name: author_name, permlink: permlink)
270
+ find_comment(chain: chain, author_name: author_name, permlink: permlink)
169
271
  end
170
272
 
171
273
  def find_comment(options)
172
274
  chain = options[:chain] || :steem
275
+ chain = chain.to_s.downcase.to_sym
173
276
  author_name = options[:author_name]
174
277
  permlink = options[:permlink]
175
278
  parent_permlink = options[:parent_permlink]
176
279
 
177
- post = if chain == :steem
178
- posts = SteemData::Post.root_posts.where(author: author_name)
280
+ post = if chain == :hive
281
+ # posts = SteemApi::Comment.where(depth: 0, author: author_name)
282
+ posts = posts.where(permlink: permlink) if !!permlink
283
+ posts = posts.where(parent_permlink: parent_permlink) if !!parent_permlink
284
+
285
+ posts.first
286
+ elsif chain == :hive
287
+ posts = HiveSQL::Comment.where(depth: 0, author: author_name)
179
288
  posts = posts.where(permlink: permlink) if !!permlink
180
289
  posts = posts.where(parent_permlink: parent_permlink) if !!parent_permlink
181
290
 
@@ -185,13 +294,13 @@ module Cosgrove
185
294
  if post.nil?
186
295
  post = case chain
187
296
  when :steem
188
- posts = SteemApi::Comment.where(author: author_name)
297
+ # posts = SteemApi::Comment.where(author: author_name)
189
298
  posts = posts.where(permlink: permlink) if !!permlink
190
299
  posts = posts.where(parent_permlink: parent_permlink) if !!parent_permlink
191
300
 
192
301
  posts.first
193
- when :golos
194
- posts = GolosCloud::Comment.where(author: author_name)
302
+ when :hive
303
+ posts = HiveSQL::Comment.where(author: author_name)
195
304
  posts = posts.where(permlink: permlink) if !!permlink
196
305
  posts = posts.where(parent_permlink: parent_permlink) if !!parent_permlink
197
306
 
@@ -212,22 +321,18 @@ module Cosgrove
212
321
  end
213
322
 
214
323
  def find_author(options)
215
- chain = options[:chain]
324
+ chain = options[:chain] || :steem
325
+ chain = chain.to_s.downcase.to_sym
216
326
  author_name = options[:author_name]
217
327
 
218
- author = SteemData::Account.where(name: author_name).last
219
-
220
- if author.nil?
221
- author = begin
222
- SteemApi::Account.where(name: author_name).last
223
- rescue => e
224
- puts e
225
- end
328
+ author = case chain
329
+ # when :steem then SteemApi::Account.where(name: author_name).first
330
+ when :hive then HiveSQL::Account.where(name: author_name).first
226
331
  end
227
332
 
228
333
  if author.nil?
229
334
  author = api(chain).get_accounts([author_name]) do |accounts, errors|
230
- accounts.last
335
+ accounts.first
231
336
  end
232
337
  end
233
338
 
@@ -241,30 +346,39 @@ module Cosgrove
241
346
  memo_key = options[:memo].to_s.strip
242
347
 
243
348
  op = case chain
244
- when :steem
245
- transfers = SteemData::AccountOperation.type('transfer').
246
- where(account: steem_account, from: from, to: steem_account, memo: {'$regex' => ".*#{memo_key}.*"})
349
+ # when :steem
350
+ # transfers = SteemApi::Tx::Transfer.
351
+ # where(from: from, to: steem_account).
352
+ # where("memo LIKE ?", "%#{memo_key}%")
353
+ #
354
+ # if transfers.any?
355
+ # transfers.last
356
+ # else
357
+ # SteemApi::Tx::Transfer.
358
+ # where(from: from).
359
+ # where(to: to).
360
+ # where("memo LIKE ?", "%#{memo_key}%").last
361
+ # end
362
+ when :hive
363
+ transfers = HiveSQL::Tx::Transfer.
364
+ where(from: from, to: hive_account).
365
+ where("memo LIKE ?", "%#{memo_key}%")
247
366
 
248
367
  if transfers.any?
249
368
  transfers.last
250
369
  else
251
- SteemApi::Tx::Transfer.
370
+ HiveSQL::Tx::Transfer.
252
371
  where(from: from).
253
372
  where(to: to).
254
373
  where("memo LIKE ?", "%#{memo_key}%").last
255
374
  end
256
- when :golos
257
- GolosCloud::Tx::Transfer.
258
- where(from: from).
259
- where(to: to).
260
- where("memo LIKE ?", "%#{memo_key}%").last
261
375
  end
262
376
 
263
377
  if op.nil?
264
- # Fall back to RPC. The transaction is so new, SteemData hasn't seen it
265
- # yet, SteemData is behind, or there is no such transfer.
378
+ # Fall back to RPC. The transaction is so new, SteemApi hasn't seen it
379
+ # yet, SteemApi is behind, or there is no such transfer.
266
380
 
267
- api(chain).get_account_history(steem_account, -1, 10000) do |history, error|
381
+ api(chain).get_account_history(hive_account, -1, 10000) do |history, error|
268
382
  if !!error
269
383
  ap error
270
384
  return "Try again later."
@@ -276,7 +390,7 @@ module Cosgrove
276
390
  next unless type == 'transfer'
277
391
  o = e.last
278
392
  next unless o.from == from
279
- next unless o.to == steem_account
393
+ next unless o.to == hive_account
280
394
  next unless o.memo =~ /.*#{memo_key}.*/
281
395
 
282
396
  o
@@ -287,20 +401,31 @@ module Cosgrove
287
401
  op
288
402
  end
289
403
 
290
- def core_asset(chain = :steem)
404
+ def core_asset(chain = :hive)
405
+ chain ||= :hive
406
+ chain = chain.to_s.downcase.to_sym
407
+
291
408
  case chain
292
409
  when :steem then 'STEEM'
293
- when :golos then 'GOLOS'
410
+ when :hive then 'HIVE'
294
411
  else; 'TESTS'
295
412
  end
296
413
  end
297
414
 
298
- def debt_asset(chain = :steem)
415
+ def debt_asset(chain = :hive)
416
+ chain ||= :hive
417
+ chain = chain.to_s.downcase.to_sym
418
+
299
419
  case chain
300
420
  when :steem then 'SBD'
301
- when :golos then 'GBG'
421
+ when :hive then 'HBD'
302
422
  else; 'TBD'
303
423
  end
304
424
  end
425
+ private
426
+ def rpc_id
427
+ @rpc_id ||= 0
428
+ @rpc_id += 1
429
+ end
305
430
  end
306
431
  end