| 1 |
|
| 2 |
|
| 3 |
module Plugin::Mastodon |
| 4 |
CLIENT_NAME = Environment::NAME |
| 5 |
WEB_SITE = 'https://mikutter.hachune.net/' |
| 6 |
end |
| 7 |
|
| 8 |
require_relative 'util' |
| 9 |
require_relative 'api' |
| 10 |
require_relative 'parser' |
| 11 |
require_relative 'model/model' |
| 12 |
require_relative 'spell' |
| 13 |
require_relative 'extractcondition' |
| 14 |
require_relative 'score' |
| 15 |
|
| 16 |
Plugin.create(:mastodon) do |
| 17 |
defimageopener(_('Mastodon添付画像'), %r<\Ahttps?://.+/media_attachments/files/(?:[0-9]{3}/)+original/[0-9a-f]+\.>) do |url| |
| 18 |
URI.open(url) |
| 19 |
end |
| 20 |
|
| 21 |
defimageopener(_('Mastodon添付画像(短縮)'), %r<\Ahttps?://[^/]+/media/[0-9A-Za-z_-]+(?:\?\d+)?\Z>) do |url| |
| 22 |
URI.open(url) |
| 23 |
end |
| 24 |
|
| 25 |
defimageopener(_('Mastodon添付画像(proxy)'), %r<\Ahttps?://[^/]+/media_proxy/[0-9]+/(?:original|small)\z>) do |url| |
| 26 |
URI.open(url) |
| 27 |
end |
| 28 |
|
| 29 |
worldを返す |
| 30 |
defevent :mastodon_worlds, prototype: [Pluggaloid::COLLECT] |
| 31 |
|
| 32 |
バの集合。 |
| 33 |
defevent :mastodon_servers, prototype: [Pluggaloid::COLLECT] |
| 34 |
|
| 35 |
defevent :mastodon_appear_toots, prototype: [Pluggaloid::STREAM] |
| 36 |
|
| 37 |
てバックグラウンド通信を行った結果、成功した時の通知 |
| 38 |
を投稿した、ふぁぼった等) |
| 39 |
defactivity :mastodon_background_succeeded, _('Mastodonのバックグラウンド通信の成功') |
| 40 |
|
| 41 |
てバックグラウンド通信を行った結果、失敗した時の通知 |
| 42 |
ために通信したら、サーバエラーが返ってきた等) |
| 43 |
defactivity :mastodon_background_failed, _('Mastodonのバックグラウンド通信の失敗') |
| 44 |
|
| 45 |
defactivity :mastodon_followings_update, _('プロフィール・フォロー関係の取得通知(Mastodon)') |
| 46 |
|
| 47 |
filter_extract_datasources do |dss| |
| 48 |
datasources = { mastodon_appear_toots: _('受信したすべてのトゥート(Mastodon)') } |
| 49 |
[datasources.merge(dss)] |
| 50 |
end |
| 51 |
|
| 52 |
pear_toots」の定義 |
| 53 |
generate(:extract_receive_message, :mastodon_appear_toots) do |appear| |
| 54 |
subscribe(:mastodon_appear_toots, &appear.method(:bulk_add)) |
| 55 |
end |
| 56 |
|
| 57 |
に関してイベントを発生させる |
| 58 |
subscribe(:mastodon_appear_toots).each do |message| |
| 59 |
if message.reblog? |
| 60 |
Plugin.call(:share, message.user, message.reblog) |
| 61 |
message.to_me_world&.yield_self do |world| |
| 62 |
Plugin.call(:mention, world, [message]) |
| 63 |
end |
| 64 |
end |
| 65 |
end |
| 66 |
|
| 67 |
followings_updater = -> do |
| 68 |
activity(:mastodon_followings_update, _('自分のプロフィールやフォロー関係を取得しています...')) |
| 69 |
Plugin.collect(:mastodon_worlds).each do |world| |
| 70 |
Delayer::Deferred.when( |
| 71 |
world.update_account, |
| 72 |
world.blocks, |
| 73 |
world.followings(cache: false) |
| 74 |
).next{ |
| 75 |
activity(:mastodon_followings_update, _('自分のプロフィールやフォロー関係の取得が完了しました(%{acct})') % {acct: world.account.acct}) |
| 76 |
Plugin.call(:world_modify, world) |
| 77 |
}.terminate(_('自分のプロフィールやフォロー関係が取得できませんでした(%{acct})') % {acct: world.account.acct}) |
| 78 |
end |
| 79 |
|
| 80 |
Delayer.new(delay: 10 * HYDE, &followings_updater) ィールとフォロー一覧を更新する |
| 81 |
end |
| 82 |
|
| 83 |
ド完了時の処理 |
| 84 |
world_load_listener = on_mastodon_worlds__add do |
| 85 |
world_load_listener.detach |
| 86 |
Delayer.new { followings_updater.call } |
| 87 |
end |
| 88 |
|
| 89 |
動時 |
| 90 |
Delayer.new do |
| 91 |
followings_updater.call |
| 92 |
end |
| 93 |
|
| 94 |
されたら、サーバの集合に加える |
| 95 |
collection(:mastodon_servers) do |servers| |
| 96 |
on_mastodon_server_created do |server| |
| 97 |
servers.rewind do |stored| |
| 98 |
stored << server unless stored.include?(server) |
| 99 |
stored |
| 100 |
end |
| 101 |
end |
| 102 |
end |
| 103 |
|
| 104 |
、選択できる全ての抽出タブデータソースを提示する |
| 105 |
collection(:message_stream) do |message_stream| |
| 106 |
subscribe(:mastodon_worlds__add) do |worlds| |
| 107 |
message_stream.rewind do |a| |
| 108 |
a | worlds.flat_map do |world| |
| 109 |
[ |
| 110 |
world.sse.user, |
| 111 |
world.sse.mention, |
| 112 |
world.sse.direct, |
| 113 |
world.rest.bookmarks, |
| 114 |
world.sse.public, |
| 115 |
world.sse.public(only_media: true), |
| 116 |
world.sse.public_local, |
| 117 |
world.sse.public_local(only_media: true) |
| 118 |
] |
| 119 |
end |
| 120 |
end |
| 121 |
worlds.each do |world| |
| 122 |
world.get_lists.next do |lists| |
| 123 |
message_stream.rewind do |a| |
| 124 |
a | lists.map do |list| |
| 125 |
world.sse.list(list_id: list[:id], title: list[:title]) |
| 126 |
end |
| 127 |
end |
| 128 |
end |
| 129 |
end |
| 130 |
end |
| 131 |
|
| 132 |
subscribe(:mastodon_servers__add).each do |server| |
| 133 |
message_stream.rewind do |a| |
| 134 |
a | [ |
| 135 |
server.sse.public, |
| 136 |
server.sse.public(only_media: true), |
| 137 |
server.sse.public_local, |
| 138 |
server.sse.public_local(only_media: true) |
| 139 |
] |
| 140 |
end |
| 141 |
end |
| 142 |
end |
| 143 |
|
| 144 |
系 |
| 145 |
|
| 146 |
collection(:mastodon_worlds) do |mutation| |
| 147 |
subscribe(:worlds__add).select { |world| |
| 148 |
world.class.slug == :mastodon |
| 149 |
}.each(&mutation.method(:add)) |
| 150 |
|
| 151 |
subscribe(:worlds__delete).each(&mutation.method(:delete)) |
| 152 |
end |
| 153 |
|
| 154 |
defevent :mastodon_current, prototype: [NilClass] |
| 155 |
|
| 156 |
、そうでなければ適当に探す。 |
| 157 |
filter_mastodon_current do |
| 158 |
world, = Plugin.filtering(:world_current, nil) |
| 159 |
unless mastodon?(world) |
| 160 |
world = Plugin.collect(:mastodon_worlds).first |
| 161 |
end |
| 162 |
[world] |
| 163 |
end |
| 164 |
|
| 165 |
on_mastodon_server_created do |server| |
| 166 |
Plugin::Mastodon::API.call( |
| 167 |
:get, server.domain, '/api/v1/instance' |
| 168 |
).next do |response| |
| 169 |
server.configuration = response[:configuration] |
| 170 |
end |
| 171 |
end |
| 172 |
|
| 173 |
バー編集 |
| 174 |
on_mastodon_update_instance do |domain| |
| 175 |
Thread.new { |
| 176 |
instance = Plugin::Mastodon::Instance.load(domain) |
| 177 |
next unless instance |
| 178 |
} |
| 179 |
end |
| 180 |
|
| 181 |
バー削除 |
| 182 |
on_mastodon_delete_instance do |domain| |
| 183 |
if UserConfig[:mastodon_instances].has_key?(domain) |
| 184 |
config = UserConfig[:mastodon_instances].dup |
| 185 |
config.delete(domain) |
| 186 |
UserConfig[:mastodon_instances] = config |
| 187 |
end |
| 188 |
end |
| 189 |
|
| 190 |
加時用 |
| 191 |
on_mastodon_create_or_update_instance do |domain| |
| 192 |
Plugin::Mastodon::Instance.add_ifn(domain) |
| 193 |
end |
| 194 |
|
| 195 |
on_world_create do |world| |
| 196 |
next unless world.class.slug == :mastodon |
| 197 |
|
| 198 |
slug_param = {id: Digest::SHA1.hexdigest(world.uri.to_s), domain: world.domain} |
| 199 |
name_param = {name: world.user_obj.acct, domain: world.domain} |
| 200 |
htl_slug = ('mastodon_htl_%{id}' % slug_param).to_sym |
| 201 |
mention_slug = ('mastodon_mentions_%{id}' % slug_param).to_sym |
| 202 |
ltl_slug = ('mastodon_ltl_%{domain}' % slug_param).to_sym |
| 203 |
exists_slugs = Set.new(Plugin.filtering(:extract_tabs_get, []).first.map(&:slug)) |
| 204 |
unless exists_slugs.include?(htl_slug) |
| 205 |
Plugin.call(:extract_tab_create, { |
| 206 |
name: _('ホームタイムライン (%{name})') % name_param, |
| 207 |
slug: htl_slug, |
| 208 |
sources: Set[ ソースとする範囲を表現するため、明示する |
| 209 |
world.rest.user.datasource_slug, |
| 210 |
world.sse.user.datasource_slug, |
| 211 |
world.rest.mention.datasource_slug, |
| 212 |
world.sse.mention.datasource_slug, |
| 213 |
], |
| 214 |
icon: Skin[:timeline].uri, |
| 215 |
}) |
| 216 |
end |
| 217 |
unless exists_slugs.include?(mention_slug) |
| 218 |
Plugin.call(:extract_tab_create, { |
| 219 |
name: _('メンション (%{name})') % name_param, |
| 220 |
slug: mention_slug, |
| 221 |
sources: Set[ |
| 222 |
world.rest.mention.datasource_slug, |
| 223 |
world.sse.mention.datasource_slug, |
| 224 |
], |
| 225 |
icon: Skin[:reply].uri, |
| 226 |
}) |
| 227 |
end |
| 228 |
unless exists_slugs.include?(ltl_slug) |
| 229 |
Plugin.call(:extract_tab_create, { |
| 230 |
name: _('ローカルタイムライン (%{domain})') % name_param, |
| 231 |
slug: ltl_slug, |
| 232 |
sources: Set[ |
| 233 |
world.rest.public_local.datasource_slug, |
| 234 |
world.sse.public_local.datasource_slug, |
| 235 |
], |
| 236 |
icon: 'https://%{domain}/favicon.ico' % slug_param, |
| 237 |
}) |
| 238 |
end |
| 239 |
end |
| 240 |
|
| 241 |
追加 |
| 242 |
on_world_create do |world| |
| 243 |
if world.class.slug == :mastodon |
| 244 |
Plugin.call(:mastodon_create_or_update_instance, world.domain, true) |
| 245 |
end |
| 246 |
end |
| 247 |
|
| 248 |
作成 |
| 249 |
world_setting(:mastodon, _('Mastodon')) do |
| 250 |
set_value domain_selection: 'social.mikutter.hachune.net' |
| 251 |
error_msg = nil |
| 252 |
while true |
| 253 |
if error_msg.is_a? String |
| 254 |
label error_msg |
| 255 |
end |
| 256 |
select(_('サーバー'), :domain_selection, |
| 257 |
'social.mikutter.hachune.net' => _('mikutter'), |
| 258 |
'mstdn.maud.io' => _('末代'), |
| 259 |
'mstdn.nere9.help' => _('nere9'), |
| 260 |
'mstdn.y-zu.org' => _('Yづドン'), |
| 261 |
) do |
| 262 |
option :other do |
| 263 |
input _('その他'), :domain |
| 264 |
end |
| 265 |
end |
| 266 |
|
| 267 |
result = await_input |
| 268 |
domain = result[:domain_selection] == :other ? result[:domain] : result[:domain_selection] |
| 269 |
|
| 270 |
instance = await Plugin::Mastodon::Instance.add_ifn(domain).trap{ |err| |
| 271 |
error err |
| 272 |
nil |
| 273 |
} |
| 274 |
unless instance |
| 275 |
error_msg = _("%{domain} サーバーへの接続に失敗しました。やり直してください。") % {domain: domain} |
| 276 |
next |
| 277 |
end |
| 278 |
|
| 279 |
break |
| 280 |
end |
| 281 |
|
| 282 |
error_msg = nil |
| 283 |
while true |
| 284 |
if error_msg.is_a? String |
| 285 |
label error_msg |
| 286 |
end |
| 287 |
|
| 288 |
s = _('認証ページ') |
| 289 |
|
| 290 |
url = instance.authorize_url.gsub '&', '&' |
| 291 |
text = _('認証ページにアクセスして、表示されたコードを貼り付け、「進む」ボタンを押してください。') |
| 292 |
.sub s, "<a href=\"#{url}\">#{s}</a>" |
| 293 |
markup text |
| 294 |
|
| 295 |
input _('認証コード'), :authorization_code, :paste |
| 296 |
if error_msg.is_a? String |
| 297 |
input _('アクセストークンがあれば入力してください'), :access_token |
| 298 |
end |
| 299 |
result = await_input |
| 300 |
if result[:authorization_code] |
| 301 |
result[:authorization_code].strip! |
| 302 |
end |
| 303 |
if result[:access_token] |
| 304 |
result[:access_token].strip! |
| 305 |
end |
| 306 |
|
| 307 |
if ((!result[:authorization_code] || result[:authorization_code].empty?) && (!result[:access_token] || result[:access_token].empty?)) |
| 308 |
error_msg = _('認証コードを入力してください') |
| 309 |
next |
| 310 |
end |
| 311 |
|
| 312 |
break |
| 313 |
end |
| 314 |
|
| 315 |
if result[:authorization_code] |
| 316 |
resp = Plugin::Mastodon::API.call!(:post, domain, '/oauth/token', |
| 317 |
client_id: instance.client_key, |
| 318 |
client_secret: instance.client_secret, |
| 319 |
grant_type: 'authorization_code', |
| 320 |
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob', |
| 321 |
code: result[:authorization_code] |
| 322 |
) |
| 323 |
if !resp || resp.value.has_key?(:error) |
| 324 |
label _('認証に失敗しました。') + (resp && resp[:error] ? ":#{resp[:error]}" : '') |
| 325 |
await_input |
| 326 |
raise resp ? resp[:error] : 'error has occurred at /oauth/token' |
| 327 |
end |
| 328 |
token = resp[:access_token] |
| 329 |
else |
| 330 |
token = result[:access_token] |
| 331 |
end |
| 332 |
|
| 333 |
resp = Plugin::Mastodon::API.call!(:get, domain, '/api/v1/accounts/verify_credentials', token) |
| 334 |
if resp.nil? || resp.value.has_key?(:error) |
| 335 |
label _('アカウント情報の取得に失敗しました') + (resp && resp[:error] ? ":#{resp[:error]}" : '') |
| 336 |
raise resp ? resp[:error] : 'error has occurred at verify_credentials' |
| 337 |
end |
| 338 |
|
| 339 |
screen_name = resp[:acct] + '@' + domain |
| 340 |
resp[:acct] = screen_name |
| 341 |
account = Plugin::Mastodon::Account.new(resp.value) |
| 342 |
world = Plugin::Mastodon::World.new( |
| 343 |
id: screen_name, |
| 344 |
slug: :"mastodon:#{screen_name}", |
| 345 |
domain: domain, |
| 346 |
access_token: token, |
| 347 |
account: account |
| 348 |
) |
| 349 |
world.update_mutes! |
| 350 |
|
| 351 |
label _('認証に成功しました。このアカウントを追加しますか?') |
| 352 |
label(_('アカウント名:%{screen_name}') % {screen_name: screen_name}) |
| 353 |
label(_('ユーザー名:%{display_name}') % {display_name: resp[:display_name]}) |
| 354 |
world |
| 355 |
end |
| 356 |
end |