lib/cosgrove/support.rb in cosgrove-0.0.4.0pre2 vs lib/cosgrove/support.rb in cosgrove-0.0.4.0pre3

- old
+ new

@@ -36,86 +36,100 @@ def append_link_details(event, slug) author_name, permlink = parse_slug slug created = nil cashout_time = nil + message = nil + return unless !!author_name && !!permlink + if slug =~ /steemit.com/ chain = :steem - elsif slug =~ /golos.io/ - chain = :golos - elsif slug =~ /golos.blog/ - chain = :golos else return # silntlly ignore this slug end - post = case chain - when :steem then SteemApi::Comment.where(author: author_name, permlink: permlink).last - when :golos then GolosCloud::Comment.where(author: author_name, permlink: permlink).last + 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}" + return nil + end end + posts = case chain + when :steem then SteemApi::Comment.where(author: author_name, permlink: permlink) + end + + posts.select(:ID, :created, :cashout_time, :author, :permlink, :active_votes, :children) + + post = posts.last + if post.nil? # Fall back to RPC api(chain).get_content(author_name, permlink) do |content, errors| unless content.author.empty? created = Time.parse(content.created + 'Z') cashout_time = Time.parse(content.cashout_time + 'Z') end end end - return if post.nil? + if post.nil? + message = message.edit 'Unable to locate.' if !!message + + 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 < 30.minutes.ago + details << if created < 15.minutes.ago "#{age} old" else "**#{age}** old" end + message = message.edit details.join('; ') if !!message - active_votes = SteemApi::Tx::Vote.where(author: post.author, permlink: post.permlink) + active_votes = JSON[post.active_votes] rescue [] if active_votes.any? - upvotes = active_votes.map{ |v| v if v.weight > 0 }.compact.count - downvotes = active_votes.map{ |v| v if v.weight < 0 }.compact.count + 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 # Only append this detail of the post less than an hour old. if created > 1.hour.ago votes = case chain when :steem then SteemApi::Tx::Vote.where('timestamp > ?', post.created) - when :golos then GolosCloud::Vote.where('timestamp > ?', post.created) end - total_votes = votes.count - total_voters = votes.distinct(:voter).size + 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 + # Page View counter is no longer supported by steemit.com. # page_views = page_views("/#{post.parent_permlink}/@#{post.author}/#{post.permlink}") # details << "Views: #{page_views}" if !!page_views + # message = message.edit details.join('; ') if !!message - begin - event.respond details.join('; ') - rescue Discordrb::Errors::NoPermission => _ - puts "Unable to append link details on #{event.channel.server.name} in #{event.channel.name}" - end - - return nil + details.join('; ') if event.nil? end def find_account(key, event = nil, chain = :steem) key = key.to_s.downcase chain = chain.to_sym @@ -135,11 +149,9 @@ end if account.nil? account = if !!key if chain == :steem && (accounts = SteemApi::Account.where(name: key)).any? - accounts.first - elsif chain == :golos && (accounts = GolosCloud::Account.where(name: key)).any? accounts.first else # Fall back to RPC api(chain).get_accounts([key]) do |_accounts, errors| _accounts.first