-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmastodon.rb
306 lines (267 loc) · 7.78 KB
/
mastodon.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
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
183
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
228
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/ruby
# -*- coding: utf-8 -*-
require "fileutils"
require "json"
require "time"
require "thread"
require "uri"
require "faraday"
require "faraday/multipart"
require "websocket-client-simple"
require "tomlrb"
require "nokogiri"
require "sqlite3"
module IPCSolver
class MastodonClient
STATE_INITIAL = 0
STATE_COMPLETED = 1
POLL_PERIOD = 3 * 60
attr_reader :poll_queue
def start
@myself = get_myself
@poll_queue = Thread::Queue.new
periodic_poller = nil
watcher = nil
FileUtils.mkdir_p("workdir_mastodon")
SQLite3::Database.new("mastodon.sqlite3") do |db|
setup_database(db)
periodic_poller = Thread.start do
run_periodic_poller
end
watcher = create_watcher
$stderr.puts "Start polling..."
while event = @poll_queue.pop
$stderr.puts "Checking mentions (cause: #{event[:type]})"
poll_mentions(db)
poll_requests(db)
end
end
ensure
watcher&.close
periodic_poller&.raise Interrupt
periodic_poller&.join
end
def run_periodic_poller
loop do
@poll_queue.push({ type: :periodic })
sleep POLL_PERIOD
end
rescue Interrupt
# OK
end
def create_watcher
query = URI.encode_www_form({
access_token: access_token,
stream: "user:notification"
})
this = self
ws = WebSocket::Client::Simple.connect("wss://#{domain}/api/v1/streaming?#{query}") do |ws|
ws.on :message do |msg|
case msg.type
when :ping
ws.send("", type: :pong)
when :text
this.poll_queue&.push({ type: :streaming })
end
end
ws.on :error do |e|
$stderr.puts "WebSocket error: #{e.inspect} / #{e.backtrace}"
end
end
ws
end
def poll_mentions(db)
last_read = get_last_read(db)
new_last_read = last_read
mentions_after = Time.now - 24 * 60 * 60
mention_stream.each do |m|
time = Time.iso8601(m["created_at"])
new_last_read = [new_last_read, time].compact.max
break if last_read && time < last_read
process_mention(db, m)
end
set_last_read(db, new_last_read) if new_last_read
end
def process_mention(db, m)
html = m["status"]["content"]
status_id = m["status"]["id"]
requester = m["account"]["acct"]
return if m["account"]["bot"]
text = Nokogiri::HTML::DocumentFragment.parse(html).text
create_request(db, status_id: status_id, text: text, requester: requester)
end
def poll_requests(db)
pending = get_pending_requests(db)
pending.each do |req|
has_reply = get_status_context(req.status_id)["descendants"].any? do |child|
child["account"]["id"] == @myself["id"]
end
process_request(req) unless has_reply
req.state_cd = STATE_COMPLETED
update_request_state(db, req)
end
end
def process_request(req)
$stderr.puts "Responding to #{req.status_id}..."
tid = req.status_id
prop = req.text.dup
prop.sub!(/\A@\w+(@[\-\w.]+)?/, "")
prop.gsub!(/\A\s+/m, "")
File.open("workdir_mastodon/#{tid}-prop.txt", "w") do|t|
t.write prop
end
command = ["bash", "./mastodon-make-image.sh", "#{tid}"]
result = nil
media = nil
if system(command.join(" "))
result = File.read("workdir_mastodon/#{tid}.out").strip
if result == ""
result = "An error occured."
end
if File.exist?("workdir_mastodon/#{tid}1.png")
media = "workdir_mastodon/#{tid}1.png"
end
else
result = "An error occured"
end
result = "@#{req.requester} #{result}"
if media
media_obj = create_media(
Faraday::Multipart::FilePart.new(
media,
'image/png'
)
)
end
create_status(
result,
media: media_obj ? [media_obj] : nil,
idempotency_key: "result-#{tid}",
in_reply_to_id: req.status_id
)
end
def config
@config ||= Tomlrb.load_file("mastodon-config.toml")
end
def create_media(file)
client.post("/api/v2/media", { file: file }).body
end
def create_status(text, media:, idempotency_key:, in_reply_to_id:)
params = {
status: text,
media_ids: (media || []).map { |m| m["id"] },
in_reply_to_id: in_reply_to_id
}
client.post("/api/v1/statuses", **params) do |req|
req.headers["Idempotency-Key"] = idempotency_key
end.body
end
def mention_stream
enum_for(:each_mentions)
end
def each_mentions
opts = { types: ["mention"] }
loop do
notifications = client.get("/api/v1/notifications", opts).body
break if notifications.empty?
notifications.each do |notification|
yield notification
end
opts[:max_id] = notifications.last["id"]
end
end
def get_myself
client.get("/api/v1/accounts/verify_credentials").body
end
def get_status_context(id)
client.get("/api/v1/statuses/#{id}/context").body
end
def client
base_url = "https://#{domain}"
token = access_token
@client ||= Faraday.new(base_url) do |conn|
conn.request :authorization, 'Bearer', access_token
conn.request :multipart
conn.request :url_encoded
conn.response :json
conn.response :raise_error
end
end
def domain
config["app"]["domain"] || (raise "No domain configured")
end
def client_key
config["app"]["client_key"] || (raise "No client_key configured")
end
def client_secret
config["app"]["client_secret"] || (raise "No client_secret configured")
end
def access_token
config["user"]["access_token"] || (raise "No access_token configured")
end
def setup_database(db)
db.execute_batch <<~SQL
CREATE TABLE IF NOT EXISTS requests (
id INTEGER PRIMARY KEY AUTOINCREMENT,
status_id TEXT NOT NULL UNIQUE,
state_cd INTEGER NOT NULL DEFAULT 0,
text TEXT,
requester TEXT
);
CREATE INDEX IF NOT EXISTS index_pending_requests
ON requests (id)
WHERE state_cd = 0;
CREATE TABLE IF NOT EXISTS last_reads (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
last_read TEXT NOT NULL
);
SQL
end
def create_request(db, status_id:, text:, requester:)
db.execute(<<~SQL, [status_id, text, requester])
INSERT INTO requests (status_id, text, requester)
VALUES (?, ?, ?)
ON CONFLICT (status_id) DO NOTHING;
SQL
end
Request = Struct.new(:id, :status_id, :state_cd, :text, :requester)
def get_pending_requests(db)
requests = []
db.execute(<<~SQL) do |row|
SELECT id, status_id, state_cd, text, requester FROM requests
WHERE state_cd = 0;
SQL
requests << Request.new(*row)
end
requests
end
def update_request_state(db, req)
db.execute(<<~SQL, [req.state_cd, req.id])
UPDATE requests
SET state_cd = ?
WHERE id = ?;
SQL
end
def get_last_read(db)
db.execute <<~SQL do |row|
SELECT last_read FROM last_reads
WHERE name = 'mentions';
SQL
return Time.iso8601(row[0])
end
nil
end
def set_last_read(db, last_read)
db.execute(<<~SQL, [last_read.iso8601, last_read.iso8601])
INSERT INTO last_reads (name, last_read)
VALUES ('mentions', ?)
ON CONFLICT (name) DO
UPDATE
SET last_read = ?
WHERE name = 'mentions';
SQL
end
end
end
IPCSolver::MastodonClient.new.start