Class: Cosgrove::CommentJob

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

Instance Method Summary collapse

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

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

#merge(template, message = nil, username = 'someone', markup = :html) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/cosgrove/comment_job.rb', line 95

def merge(template, message = nil, username = 'someone', markup = :html)
  @comment_md ||= "support/#{template}.md"
  @comment_body ||= {}
  @comment_body[template] ||= if File.exist?(@comment_md)
    File.read(@comment_md)
  end
  
  raise "Cannot read #{template} template or template is empty." unless @comment_body[template].present?
  
  merged = @comment_body[template]
  merged = merged.gsub('${message}', message) unless message.nil?
  merged = merged.gsub('${username}', username) unless username.nil?
  
  case markup
  when :html then RDiscount.new(merged).to_html
  when :markdown then merged
  end
end

#perform(event, slug, template, message = nil) ⇒ Object



8
9
10
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
# File 'lib/cosgrove/comment_job.rb', line 8

def perform(event, slug, template, message = nil)
  chain = :hive
  author_name, permlink = parse_slug slug
  muted = muted by: , chain: chain
  
  author = find_author(chain: chain, author_name: author_name)
  
  return if author.nil?
  
  post = find_comment(chain: chain, author_name: author_name, permlink: permlink)
  
  if post.nil?
    cannot_find_input(event)
    return
  end
  
  created ||= post.created
  cashout_time ||= post.cashout_time
  
  if created.class == String
    created = Time.parse(created + 'Z')
    cashout_time = Time.parse(cashout_time + 'Z')
  end
  
  nope = if post.nil?
    "Sorry, couldn't find that."
  elsif cashout_time < Time.now.utc
    'Unable to comment on that.  Too old.'
  elsif post.parent_permlink == 'nsfw'
    puts "Won't comment because parent_permlink: nsfw"
    'Unable to comment on that.'
  elsif post..include?('nsfw')
    puts "Won't comment because json_metadata includes: nsfw"
    'Unable to comment on that.'
  elsif post.active_votes.map{ |v| v['voter'] }.include?('blacklist-a')
    puts "Won't comment blacklist-a voted."
    'Unable to comment on that.'
  elsif (rep = to_rep(post.author_reputation).to_f) < 25.0
    puts "Won't comment because rep too low: #{rep}"
    'Unable to comment on that.'
  elsif muted.include? author_name
    puts "Won't vote because author muted."
    'Unable to vote.'
  # elsif template == :welcome && author.post_count != 1
  #   'Sorry, this function is intended to welcome new authors.'
  elsif find_comment(chain: :hive, author_name: , parent_permlink: post.permlink).any?
    title = post.title
    title = post.permlink if title.empty?
    "I already commented on #{title} by #{post.author}."
  end

  if !!nope
    event.respond nope
    return
  end
  
  now = Time.now
  comment = {
    type: :comment,
    parent_permlink: post.permlink,
    author: ,
    permlink: "re-#{post.author.gsub(/[^a-z0-9\-]+/, '-')}-#{post.permlink}-#{now.utc.strftime('%Y%m%dt%H%M%S%Lz')}", # e.g.: 20170225t235138025z
    title: '',
    body: merge(template, message, event.author.username),
    json_metadata: "{\"tags\":[\"#{post.parent_permlink}\"],\"app\":\"#{Cosgrove::AGENT_ID}\"}",
    parent_author: post.author
  }
  
  ap comment
  
  tx = new_tx :steem
  op = Radiator::Operation.new(comment)
  tx.operations << op
  
  response = tx.process(true)
  
  ap response.inspect
  
  if !!response.error
    'Unable to comment right now.  Maybe I already commented on that.  Try again later.'
  elsif !!response.result.id
    "Wrote #{template} comment on: #{post.title} by #{author_name}"
  else
    ':question:'
  end
end

#preview(template, event, message) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cosgrove/comment_job.rb', line 114

def preview(template, event, message)
  m = "Your Message Here"
  discord_id = event.author.id
  
  @author = if (a = Cosgrove::Account.find_by_discord_id(discord_id)).nil?
    event.author.username
  else
    "@#{a.}"
  end
  
  if message.present?
    @message = message
  end
  
  preview = []
  preview << "Set by **#{@author}**" if @author.present?
  preview << "```md\n"
  preview << merge(template, @message || m, @author, :markdown)
  preview << "\n```"
  
  preview.join('')
end