-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_consent_requests.rb
130 lines (117 loc) · 5.66 KB
/
check_consent_requests.rb
1
2
3
4
5
6
7
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
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
require 'rubygems'
require 'json'
require 'yaml'
require 'sanitize'
require 'open-uri'
require 'rest-client'
# This URL gets (when the ID is inserted) details of the vote
status_url_tamplate = 'https://does.social/api/v1/statuses/%s'
# This URL gets (when the ID is inserted) details of any replies
status_context_url_tamplate = 'https://does.social/api/v1/statuses/%s/context'
# This URL lets us post toots
POST_STATUS_URL = "https://does.social/api/v1/statuses"
# Send a message to @amcewen to alert him to an error
def toot_error(message)
payload = { status: "@[email protected] #{message}", visibility: "direct" }
resp = RestClient.post(POST_STATUS_URL, payload, { "Authorization": "Bearer #{BEARER_TOKEN}"})
end
# Read in config
settings = nil
if ARGV.length == 1
settings = YAML.load_file(ARGV[0])
else
puts "No configuration provided."
exit
end
input_settings = settings["input"]
output_settings = settings["output"]
BEARER_TOKEN = input_settings['mastodon']['bearer_token']
consent = YAML.load_file(input_settings['mastodon']['consent_file'])
consent.each do |u|
puts u
if u[:consent].nil?
unless u[:consent_request].nil?
# Do we need to reply? Default to no
thanks_reply = nil
status_url = status_url_tamplate % u[:consent_request]
status_info = JSON.parse(URI.open(status_url, "Authorization" => "Bearer #{BEARER_TOKEN}").read)
#pp status_info
# Check for poll responses
if status_info["poll"]["votes_count"] > 1
toot_error("Check #{u[:consent_request]} - too many votes: #{status_info['poll']['votes_count']}")
elsif status_info["poll"]["votes_count"] > 0
puts "Voted upon!"
status_info["poll"]["options"].each do |v|
if v["votes_count"] > 0
# Tweak it to get rid of the "just" in "just weeknotes"
u[:consent] = v["title"].split(" ")[-1]
end
end
# Reply to the original poll post, if we don't find a reply
thanks_reply = { "in_reply_to_id": status_info["id"], "in_reply_to_account_id": status_info["account"]["id"] }
end
if u[:consent].nil?
# There wasn't a vote, look for a reply
status_context_url = status_context_url_tamplate % u[:consent_request]
status_context = JSON.parse(URI.open(status_context_url, "Authorization" => "Bearer #{BEARER_TOKEN}").read)
#pp status_context
# Search for any children with responses
status_context['descendants'].each do |d|
if d['account']['acct'] == status_info['mentions'][0]['acct']
# It's a reply from the account we asked
# Strip out any unwanted HTML to just get the text
reply = Sanitize.fragment(d['content'], Sanitize::Config::RESTRICTED)
#puts reply
if reply[/\bno\b/i]
u[:consent] = "no"
u[:consent_link] = d['url']
thanks_reply = { "in_reply_to_id": d["id"], "in_reply_to_account_id": d["account"]["id"] }
elsif reply[/\bweeknotes\b/i]
u[:consent] = "weeknotes"
u[:consent_link] = d['url']
thanks_reply = { "in_reply_to_id": d["id"], "in_reply_to_account_id": d["account"]["id"] }
elsif reply[/\ball\b/i]
u[:consent] = "all"
u[:consent_link] = d['url']
thanks_reply = { "in_reply_to_id": d["id"], "in_reply_to_account_id": d["account"]["id"] }
else
toot_error("unexpected reply with content >>#{reply}<<")
end
end
end
end
unless thanks_reply.nil?
# we've had a reply
message = "@#{u[:user]} Thanks for getting back to us!"
# Send the message to the user
puts "Spamming #{u} => #{message}"
thanks_reply["status"] = message
thanks_reply["visibility"] = "direct"
resp = RestClient.post(POST_STATUS_URL, thanks_reply, { "Authorization": "Bearer #{BEARER_TOKEN}"})
if resp.code == 200
else
toot_error("Error sending consent request to #{u}: #{message}")
puts "Error sending consent request to #{u}: #{message}"
puts resp.code
puts resp.body
end
# Now process any pending messages from that user!
user_consent_folder = File.join(input_settings['mastodon']['consent_folder'], u[:user])
if u[:consent] == "all" or u[:consent] == "weeknotes"
# Move the toots to be published
FileUtils.mv Dir.glob(File.join(user_consent_folder, "*")), File.join(input_settings['mastodon']['publication_folder'], u[:consent])
end
# Remove the user's pending consent folder (and any remaining
# toots if consent wasn't granted)
FileUtils.rm_rf user_consent_folder
end
else
puts "Strange consent entry: #{u}"
end
# else we've already had a reply to our request
end
end
# Save any updates to the database
File.open(input_settings['mastodon']['consent_file'], "w") do |f|
f.write(consent.to_yaml)
end