Debian Codegraph @

plugin/mastodon/spell.rb

raw symbols SCIP
1 # coding: utf-8
2
3 Plugin.create(:mastodon) do
4 command(:mastodon_report_status, name: _('通報する'), visible: true, role: :timeline,
5 condition: lambda { |opt|
6 opt.messages.any? { |m| report_for_spam?(opt.world, m) }
7 }) do |opt|
8 dialog _('通報する') do
9 error_msg = nil
10 while true
11 label _('以下のトゥートを %{domain} サーバーの管理者に通報しますか?') % {domain: opt.world.domain}
12 opt.messages.each { |message|
13 link message
14 }
15 multitext _('コメント(1000文字以内) ※必須'), :comment
16 label error_msg if error_msg
17
18 result = await_input
19 error_msg = _('コメントを入力してください。') if (!result[:comment] || result[:comment].empty?)
20 error_msg = _('コメントが長すぎます(%{input_char_count}文字)') % {input_char_count: result[:comment].to_s.size} if result[:comment].to_s.size > 1000
21 break unless error_msg
22 end
23
24 label _('しばらくお待ち下さい...')
25
26 opt.messages.select { |message|
27 message.class.slug == :mastodon_status
28 }.map { |message|
29 message.reblog ? message.reblog : message
30 }.sort_by { |message|
31 message.account.acct
32 }.chunk { |message|
33 message.account.acct
34 }.each { |acct, messages|
35 await opt.world.report_for_spam(messages, result[:comment])
36 }
37
38 label _('完了しました。')
39 end.terminate(_('通報中にエラーが発生しました'))
40 end
41
42 command(:mastodon_pin_message, name: _('ピン留めする'), visible: true, role: :timeline,
43 condition: lambda { |opt|
44 opt.messages.any? { |m| pin_message?(opt.world, m) }
45 }) do |opt|
46 opt.messages.select{ |m|
47 pin_message?(opt.world, m)
48 }.each { |status|
49 opt.world.pin(status)
50 }
51 end
52
53 command(:mastodon_unpin_message, name: _('ピン留めを解除する'), visible: true, role: :timeline,
54 condition: lambda { |opt|
55 opt.messages.any? { |m| unpin_message?(opt.world, m) }
56 }) do |opt|
57 opt.messages.select{ |m|
58 unpin_message?(opt.world, m)
59 }.each { |status|
60 opt.world.unpin(status)
61 }
62 end
63
64 command(:mastodon_pin_message, name: _('ブックマークする'), visible: true, role: :timeline,
65 condition: lambda { |opt|
66 opt.messages.any? { |m| bookmark_message?(opt.world, m) }
67 }) do |opt|
68 opt.messages.select{ |m|
69 bookmark_message?(opt.world, m)
70 }.each { |status|
71 opt.world.bookmark(status)
72 }
73 end
74
75 command(:mastodon_unpin_message, name: _('ブックマークを解除する'), visible: true, role: :timeline,
76 condition: lambda { |opt|
77 opt.messages.any? { |m| unbookmark_message?(opt.world, m) }
78 }) do |opt|
79 opt.messages.select{ |m|
80 unbookmark_message?(opt.world, m)
81 }.each { |status|
82 opt.world.unbookmark(status)
83 }
84 end
85
86 command(:mastodon_edit_list_membership, name: _('リストへの追加・削除'), visible: true, role: :timeline,
87 condition: lambda { |opt|
88 mastodon?(opt.world)
89 }) do |opt|
90 user = opt.messages.first&.user
91 next unless user
92
93 Delayer::Deferred.when(
94 opt.world.get_lists,
95 Plugin::Mastodon::API.get_local_account_id(opt.world, user)
96 ).next{ |lists, user_id|
97 old_membership_ids = Set.new(
98 (+Plugin::Mastodon::API.call(:get, opt.world.domain, "/api/v1/accounts/#{user_id}/lists", opt.world.access_token))
99 .value.map { |member| member[:id].to_sym }
100 ).freeze
101
102 dialog _('リストへの追加・削除') do
103 self[:lists] = old_membership_ids
104 multiselect _('所属させるリストを選択してください'), :lists do
105 lists.each do |list|
106 option(list[:id].to_sym, list[:title])
107 end
108 end
109 end.next do |result|
110 new_membership_ids = Set.new(result[:lists])
111 Delayer::Deferred.when(
112 *(new_membership_ids - old_membership_ids).map do |list_id|
113 Plugin::Mastodon::API.call(:post, opt.world.domain, "/api/v1/lists/#{list_id}/accounts", opt.world.access_token, account_ids: [user_id])
114 end,
115 *(old_membership_ids - new_membership_ids).map do |list_id|
116 Plugin::Mastodon::API.call(:delete, opt.world.domain, "/api/v1/lists/#{list_id}/accounts", opt.world.access_token, account_ids: [user_id])
117 end
118 ).next { |*_succeed|
119 activity(:mastodon_background_succeeded, _('リストへの追加・削除が完了しました'))
120 }.trap do |failed|
121 activity(:mastodon_background_failed, _('リストへの追加・削除が失敗しました'), exception: failed)
122 end
123 end
124 }.terminate
125 end
126
127 command(:mastodon_vote, name: _('投票する'), visible: true, role: :timeline,
128 condition: ->(opt) {
129 m = opt.messages.first
130 (m.is_a?(Plugin::Mastodon::Status) && m.poll && mastodon?(opt.world))
131 }) do |opt|
132 m = opt.messages.first
133
134 # poll idはサーバーごとに異なる。worldが所属するサーバーでの値を取得する。
135 Plugin::Mastodon::API.status_by_url(opt.world.domain, opt.world.access_token, m.uri).next{ |statuses|
136 Plugin::Mastodon::Poll.new(statuses.dig(0, :poll))
137 }.next{ |poll|
138 Delayer::Deferred.fail("The poll already expired.") unless poll.expires_at >= Time.now
139
140 dialog _("投票") do
141 if poll.multiple
142 multiselect _("投票先を選択してください"), :vote do
143 poll.options.each_with_index do |vopt, i|
144 option(i.to_s.to_sym, vopt.title)
145 end
146 end
147 else
148 select _("投票先を選択してください"), :vote do
149 poll.options.each_with_index do |vopt, i|
150 option(i.to_s.to_sym, vopt.title) { } # ラジオボタン化のために空blockを渡す
151 end
152 end
153 end
154 end.next { |result|
155 Plugin::Mastodon::API.call(:post, opt.world.domain, "/api/v1/polls/#{poll.id}/votes", opt.world.access_token, choices: Array(result[:vote]))
156 }
157 }.terminate
158 end
159
160
161 # spell系
162
163 # 投稿
164 defspell(:compose, :mastodon) do |world, body:, **opts|
165 if opts[:visibility]
166 opts[:visibility] = opts[:visibility].to_s
167 else
168 opts.delete :visibility
169 end
170
171 unless opts[:sensitive] || opts[:media_ids] || opts[:spoiler_text]
172 opts[:sensitive] = false
173 end
174
175 world.post(message: body, **opts).next{ |result|
176 new_status = Plugin::Mastodon::Status.build(world.server, result.value)
177 Plugin.call(:posted, world, [new_status])
178 Plugin.call(:update, world, [new_status])
179 new_status
180 }
181 end
182
183 memoize def media_tmp_dir
184 path = Pathname(Environment::TMPDIR) / 'mastodon' / 'media'
185 FileUtils.mkdir_p(path.to_s)
186 path
187 end
188
189 defspell(:compose, :mastodon, :photo) do |world, photo, body:, **opts|
190 photo.download.next{|photo|
191 ext = photo.uri.path.split('.').last || 'png'
192 tmp_name = Digest::MD5.hexdigest(photo.uri.to_s) + ".#{ext}"
193 tmp_path = media_tmp_dir / tmp_name
194 file_put_contents(tmp_path, photo.blob)
195 Plugin::Mastodon::API.call(:post, world.domain, '/api/v1/media', world.access_token, file: tmp_path.to_s).next{ |hash|
196 media_id = hash[:id]
197 compose(world, body: body, media_ids: [media_id], **opts)
198 }
199 }
200 end
201
202 defspell(:compose, :mastodon, :mastodon_status) do |world, status, body:, **opts|
203 if opts[:visibility]
204 opts[:visibility] = opts[:visibility].to_s
205 else
206 opts.delete :visibility
207 if status.visibility == "direct"
208 # 返信先がDMの場合はデフォルトでDMにする。但し呼び出し元が明示的に指定してきた場合はそちらを尊重する。
209 opts[:visibility] = "direct"
210 end
211 end
212 unless opts[:sensitive] || opts[:media_ids] || opts[:spoiler_text]
213 opts[:sensitive] = false
214 end
215
216 world.post(to: status, message: body, **opts).next{ |result|
217 new_status = Plugin::Mastodon::Status.build(world.server, result.value)
218 Plugin.call(:posted, world, [new_status]) if new_status
219 Plugin.call(:update, world, [new_status]) if new_status
220 new_status
221 }
222 end
223
224 defspell(:destroy, :mastodon, :mastodon_status, condition: -> (world, status) {
225 world.account.acct == status.actual_status.account.acct
226 }) do |world, status|
227 Plugin::Mastodon::API.get_local_status_id(world, status.actual_status).next{ |status_id|
228 Plugin::Mastodon::API.call(:delete, world.domain, "/api/v1/statuses/#{status_id}", world.access_token)
229 }.next{
230 Plugin.call(:destroyed, [status.actual_status])
231 status.actual_status
232 }
233 end
234
235 # ふぁぼ
236 defspell(:favorite, :mastodon, :mastodon_status,
237 condition: -> (world, status) { !status.actual_status.favorite?(world) }
238 ) do |world, status|
239 Plugin::Mastodon::API.get_local_status_id(world, status.actual_status).next{ |status_id|
240 Plugin.call(:before_favorite, world, world.account, status)
241 Plugin::Mastodon::API.call(:post, world.domain, '/api/v1/statuses/' + status_id.to_s + '/favourite', world.access_token)
242 }.next{ |ret|
243 status.actual_status.favourited = true
244 status.actual_status.favorite_accts << world.account.acct
245 Plugin.call(:favorite, world, world.account, status)
246 }.trap {
247 Plugin.call(:fail_favorite, world, world.account, status)
248 }
249 end
250
251 defspell(:favorited, :mastodon, :mastodon_status,
252 condition: -> (world, status) { status.actual_status.favorite?(world) }
253 ) do |world, status|
254 Delayer::Deferred.new.next {
255 status.actual_status.favorite?(world)
256 }
257 end
258
259 defspell(:unfavorite, :mastodon, :mastodon_status, condition: -> (world, status) { status.favorite?(world) }) do |world, status|
260 Plugin::Mastodon::API.get_local_status_id(world, status.actual_status).next{ |status_id|
261 Plugin::Mastodon::API.call(:post, world.domain, '/api/v1/statuses/' + status_id.to_s + '/unfavourite', world.access_token)
262 }.next{ |ret|
263 status.actual_status.favourited = false
264 status.actual_status.favorite_accts.delete(world.account.acct)
265 Plugin.call(:favorite, world, world.account, status)
266 status.actual_status
267 }.terminate
268 end
269
270 # ブースト
271 defspell(:share, :mastodon, :mastodon_status,
272 condition: -> (world, status) { status.actual_status.rebloggable?(world) }
273 ) do |world, status|
274 world.reblog(status.actual_status).next{|shared|
275 Plugin.call(:posted, world, [shared])
276 Plugin.call(:update, world, [shared])
277 }
278 end
279
280 defspell(:shared, :mastodon, :mastodon_status,
281 condition: -> (world, status) { status.actual_status.shared?(world) }
282 ) do |world, status|
283 Delayer::Deferred.new.next {
284 status.actual_status.shared?(world)
285 }
286 end
287
288 defspell(:destroy_share, :mastodon, :mastodon_status, condition: -> (world, status) { status.actual_status.shared?(world) }) do |world, status|
289 Plugin::Mastodon::API.get_local_status_id(world, status.actual_status).next{ |status_id|
290 Plugin::Mastodon::API.call(:post, world.domain, '/api/v1/statuses/' + status_id.to_s + '/unreblog', world.access_token)
291 }.next{ |ret|
292 status.actual_status.reblogged = false
293 reblog = status.actual_status.retweeted_statuses.find{|s|
294 s.account.acct == world.user_obj.acct
295 }
296 status.actual_status.reblog_status_uris.delete_if {|pair| pair[:acct] == world.user_obj.acct }
297 Plugin.call(:destroyed, [reblog]) if reblog
298 reblog
299 }
300 end
301
302 # プロフィール更新系
303 update_profile_block = ->(world, **opts) do
304 world.update_profile(**opts)
305 end
306
307 defspell(:update_profile, :mastodon, &update_profile_block)
308 defspell(:update_profile_name, :mastodon, &update_profile_block)
309 defspell(:update_profile_biography, :mastodon, &update_profile_block)
310 defspell(:update_profile_icon, :mastodon, :photo) do |world, photo|
311 update_profile_block.call(world, icon: photo)
312 end
313 defspell(:update_profile_header, :mastodon, :photo) do |world, photo|
314 update_profile_block.call(world, header: photo)
315 end
316
317 command(
318 :mastodon_update_profile,
319 name: _('プロフィール変更'),
320 condition: -> (opt) {
321 mastodon?(opt.world)
322 },
323 visible: true,
324 role: :postbox
325 ) do |opt|
326 profiles = Hash.new
327 profiles[:name] = opt.world.account.display_name
328 profiles[:biography] = opt.world.account.source.note
329 profiles[:locked] = opt.world.account.locked
330 profiles[:bot] = opt.world.account.bot
331 profiles[:source] = {
332 privacy: opt.world.account.source.privacy,
333 sensitive: opt.world.account.source.sensitive,
334 language: opt.world.account.source.language,
335 fields: opt.world.account.source.fields.map{|f| { name: f.name, value: f.value } }
336 }
337
338 dialog _('プロフィール変更') do
339 self[:name] = profiles[:name]
340 self[:biography] = profiles[:biography]
341 self[:locked] = profiles[:locked]
342 self[:bot] = profiles[:bot]
343 self[:source_privacy] = Plugin::Mastodon::Util.visibility2select(profiles[:source][:privacy])
344 self[:source_sensitive] = profiles[:source][:sensitive]
345 (1..4).each do |i|
346 next unless profiles[:source][:fields][i - 1]
347 self[:"field_name#{i}"] = profiles[:source][:fields][i - 1][:name]
348 self[:"field_value#{i}"] = profiles[:source][:fields][i - 1][:value]
349 end
350
351 input _('表示名'), :name
352 multitext _('プロフィール'), :biography
353 photoselect _('アイコン'), :icon
354 photoselect _('ヘッダー'), :header
355 boolean _('承認制アカウントにする'), :locked
356 boolean _('これは BOT アカウントです'), :bot
357 select _('デフォルトの公開範囲'), :source_privacy do
358 option :"1public", _('公開')
359 option :"2unlisted", _('未収載')
360 option :"3private", _('非公開')
361 option :"4direct", _('ダイレクト')
362 end
363 boolean _('メディアを常に閲覧注意としてマークする'), :source_sensitive
364 settings _('プロフィール補足情報') do
365 input _('ラベル1'), :field_name1
366 input _('内容1'), :field_value1
367 input _('ラベル2'), :field_name2
368 input _('内容2'), :field_value2
369 input _('ラベル3'), :field_name3
370 input _('内容3'), :field_value3
371 input _('ラベル4'), :field_name4
372 input _('内容4'), :field_value4
373 end
374 end.next do |result|
375 diff = Hash.new
376 diff[:name] = result[:name] if (result[:name] && result[:name].size > 0 && profiles[:name] != result[:name])
377 diff[:biography] = result[:biography] if (result[:biography] && result[:biography].size > 0 && profiles[:biography] != result[:biography])
378 diff[:locked] = result[:locked] if profiles[:locked] != result[:locked]
379 diff[:bot] = result[:bot] if profiles[:bot] != result[:bot]
380 diff[:icon] = Pathname(result[:icon]) if result[:icon]
381 diff[:header] = Pathname(result[:header]) if result[:header]
382 diff[:source] = Hash.new
383 diff[:source][:privacy] = Plugin::Mastodon::Util.select2visibility(result[:source_privacy]) if profiles[:source][:privacy] != Plugin::Mastodon::Util.select2visibility(result[:source_privacy])
384 diff[:source][:sensitive] = result[:source_sensitive] if profiles[:source][:sensitive] != result[:source_sensitive]
385 diff.delete(:source) if diff[:source].empty?
386 if (1..4).any?{|i| result[:"field_name#{i}"] && result[:"field_value#{i}"] }
387 (1..4).each do |i|
388 name = result[:"field_name#{i}"]
389 next unless name
390 value = result[:"field_value#{i}"]
391 next unless value
392 diff[:"field_name#{i}"] = name
393 diff[:"field_value#{i}"] = value
394 end
395 end
396 next if diff.empty?
397
398 opt.world.update_profile(**diff)
399 end
400 end
401
402 # 検索
403 intent :mastodon_tag, label: _('Mastodonハッシュタグ(Mastodon)') do |token|
404 Plugin.call(:search_start, "##{token.model.name}")
405 end
406
407 defspell(:search, :mastodon) do |world, **opts|
408 count = [opts[:count], 40].min
409 q = opts[:q]
410 if q.start_with? '#'
411 q = URI.encode_www_form_component(q[1..-1])
412 Plugin::Mastodon::API.call(:get, world.domain, "/api/v1/timelines/tag/#{q}", world.access_token, limit: count).next(&:to_a)
413 else
414 Plugin::Mastodon::API.call(:get, world.domain, '/api/v2/search', world.access_token, q: q).next{ |resp|
415 resp[:statuses]
416 }
417 end.next{|resp|
418 Plugin::Mastodon::Status.bulk_build(world.server, resp)
419 }
420 end
421
422 defspell(:around_message, :mastodon_status) do |status|
423 Thread.new do
424 status.around(true)
425 end
426 end
427
428 defspell(:follow, :mastodon, :mastodon_account,
429 condition: -> (world, account) { !world.following?(account.acct) }
430 ) do |world, account|
431 world.follow(account)
432 end
433
434 defspell(:unfollow, :mastodon, :mastodon_account,
435 condition: -> (world, account) { world.following?(account.acct) }
436 ) do |world, account|
437 world.unfollow(account)
438 end
439
440 defspell(:following, :mastodon, :mastodon_account
441 ) do |world, account|
442 world.following?(account)
443 end
444
445 defspell(:mute_user, :mastodon, :mastodon_account,
446 condition: -> (world, account) { !Plugin::Mastodon::Status.muted?(account.acct) }
447 ) do |world, account|
448 world.mute(account)
449 end
450
451 defspell(:unmute_user, :mastodon, :mastodon_account,
452 condition: -> (world, account) { Plugin::Mastodon::Status.muted?(account.acct) }
453 ) do |world, account|
454 world.unmute(account)
455 end
456
457 defspell(:block_user, :mastodon, :mastodon_account,
458 condition: -> (world, account) { !world.block?(account.acct) }
459 ) do |world, account|
460 world.block(account)
461 end
462
463 defspell(:unblock_user, :mastodon, :mastodon_account,
464 condition: -> (world, account) { world.block?(account.acct) }
465 ) do |world, account|
466 world.unblock(account)
467 end
468
469 defspell(:report_for_spam, :mastodon, :mastodon_status) do |world, status, comment: raise|
470 world.report_for_spam([status], comment)
471 end
472
473 defspell(:report_for_spam, :mastodon) do |world, messages:, comment: raise|
474 world.report_for_spam(messages, comment)
475 end
476
477 defspell(:pin_message, :mastodon, :mastodon_status,
478 condition: -> (world, status) {
479 world.account.acct == status.account.acct && !status.pinned?
480 # 自分のStatusが(ピン留め状態が不正確になりうるタイミングで)他サーバーから取得されることはまずないと仮定している
481 }
482 ) do |world, status|
483 world.pin(status)
484 end
485
486 defspell(:unpin_message, :mastodon, :mastodon_status,
487 condition: -> (world, status) {
488 world.account.acct == status.account.acct && status.pinned?
489 # 自分のStatusが(ピン留め状態が不正確になりうるタイミングで)他サーバーから取得されることはまずないと仮定している
490 }
491 ) do |world, status|
492 world.unpin(status)
493 end
494
495 defspell(:bookmark_message, :mastodon, :mastodon_status,
496 condition: -> (world, status) { !status.bookmarked? }
497 ) do |world, status|
498 world.bookmark(status)
499 end
500
501 defspell(:unbookmark_message, :mastodon, :mastodon_status,
502 condition: -> (world, status) { status.bookmarked? }
503 ) do |world, status|
504 world.unbookmark(status)
505 end
506
507 defspell(:mastodon, :mastodon) do |mastodon|
508 true
509 end
510
511 defspell(:remain_charcount, :mastodon) do |mastodon, body:|
512 config = mastodon.server.configuration&.statuses
513 if config # Plugin::Mastodon::ServerConfigurationStatus
514 body.dup.then do |new_text|
515 new_text.gsub!(URI.regexp, "\1#{'x' * config.characters_reserved_per_url}")
516 new_text.gsub!(Plugin::Mastodon::Status::MENTION_RE, '@\2')
517 config.max_characters - new_text.size
518 end
519 else
520 nil
521 end
522 end
523 end