| 1 |
Plugin.create(:mastodon_rest) do |
| 2 |
@tags = {} |
| 3 |
|
| 4 |
subscribe(:mastodon_worlds__add).each do |new_world| |
| 5 |
[ new_world.rest.user, |
| 6 |
new_world.rest.mention, |
| 7 |
new_world.rest.direct, |
| 8 |
new_world.rest.bookmarks, |
| 9 |
new_world.rest.public, |
| 10 |
new_world.rest.public_local, |
| 11 |
new_world.rest.public(only_media: true), |
| 12 |
new_world.rest.public_local(only_media: true) |
| 13 |
].each { |stream| generate_stream(stream, tag: tag_of(new_world)) } |
| 14 |
new_world.get_lists.next { |lists| |
| 15 |
lists.each do |l| |
| 16 |
generate_stream(new_world.rest.list(list_id: l[:id].to_i, title: l[:title]), tag: tag_of(new_world)) |
| 17 |
end |
| 18 |
}.terminate(_('Mastodon: リスト取得時にエラーが発生しました')) |
| 19 |
end |
| 20 |
|
| 21 |
subscribe(:mastodon_worlds__delete).each do |lost_world| |
| 22 |
detach(tag_of(lost_world)) |
| 23 |
end |
| 24 |
|
| 25 |
subscribe(:mastodon_servers__add).each do |server| |
| 26 |
generate_stream(server.rest.public, tag: tag_of(server)) |
| 27 |
generate_stream(server.rest.public_local, tag: tag_of(server)) |
| 28 |
generate_stream(server.rest.public(only_media: true), tag: tag_of(server)) |
| 29 |
generate_stream(server.rest.public_local(only_media: true), tag: tag_of(server)) |
| 30 |
end |
| 31 |
|
| 32 |
subscribe(:mastodon_servers__delete).each do |lost_server| |
| 33 |
detach(tag_of(lost_server)) |
| 34 |
end |
| 35 |
|
| 36 |
def generate_stream(connection_type, tag:) |
| 37 |
generate(:extract_receive_message, connection_type.datasource_slug, tags: [tag]) do |stream_input| |
| 38 |
Delayer::Deferred.next do |
| 39 |
wait_count = Float::INFINITY |
| 40 |
loop do |
| 41 |
wait_count += 1 |
| 42 |
if wait_count >= UserConfig[:mastodon_rest_interval] |
| 43 |
wait_count = 0 |
| 44 |
(+query(connection_type))&.yield_self(&stream_input.method(:bulk_add)) |
| 45 |
end |
| 46 |
+Delayer::Deferred.sleep(60) |
| 47 |
end |
| 48 |
rescue Pluggaloid::NoReceiverError |
| 49 |
|
| 50 |
end.trap do |err| |
| 51 |
Delayer.new { raise err } |
| 52 |
end |
| 53 |
end |
| 54 |
end |
| 55 |
|
| 56 |
def query(connection_type) |
| 57 |
Plugin::Mastodon::API.call(:get, connection_type.uri.host, connection_type.uri.path, connection_type.token, limit: 200, **connection_type.params).next { |api_response| |
| 58 |
case connection_type.response_entities |
| 59 |
when :status |
| 60 |
Plugin::Mastodon::Status.bulk_build(connection_type.server, api_response.value) |
| 61 |
when :notification |
| 62 |
Plugin::Mastodon::Status.bulk_build(connection_type.server, api_response.value.map{ |h| h[:status] }) |
| 63 |
end |
| 64 |
}.terminate(_('Mastodon: %{title}取得時にエラーが発生しました') % {title: connection_type.title}) |
| 65 |
.trap{ nil } |
| 66 |
end |
| 67 |
|
| 68 |
def tag_of(world_or_server) |
| 69 |
@tags[world_or_server.hash] ||= handler_tag() |
| 70 |
end |
| 71 |
end |