| 1 |
|
| 2 |
|
| 3 |
require_relative 'connection' |
| 4 |
require_relative 'handler/favorite' |
| 5 |
require_relative 'handler/follow' |
| 6 |
require_relative 'handler/mention' |
| 7 |
require_relative 'handler/poll' |
| 8 |
require_relative 'handler/reblog' |
| 9 |
require_relative 'handler/update' |
| 10 |
|
| 11 |
Plugin.create(:mastodon_sse_streaming) do |
| 12 |
できるようにする |
| 13 |
|
| 14 |
subscribe(:mastodon_worlds__add).each do |new_world| |
| 15 |
[new_world.sse.user, |
| 16 |
new_world.sse.direct, |
| 17 |
new_world.sse.public, |
| 18 |
new_world.sse.public_local, |
| 19 |
new_world.sse.public(only_media: true), |
| 20 |
new_world.sse.public_local(only_media: true)].each { |stream| generate_stream(stream, tag: tag_of(new_world)) } |
| 21 |
new_world.get_lists.next { |lists| |
| 22 |
lists.each do |l| |
| 23 |
generate_stream(new_world.sse.list(list_id: l[:id].to_i, title: l[:title]), tag: tag_of(new_world)) |
| 24 |
end |
| 25 |
}.terminate(_('Mastodon: リスト取得時にエラーが発生しました')) |
| 26 |
generate_notification_stream(new_world.sse.user, tag: tag_of(new_world)) |
| 27 |
generate_mention_stream(new_world.sse.mention, tag: tag_of(new_world)) |
| 28 |
end |
| 29 |
|
| 30 |
subscribe(:mastodon_worlds__delete).each do |lost_world| |
| 31 |
detach(tag_of(lost_world)) |
| 32 |
end |
| 33 |
|
| 34 |
subscribe(:mastodon_servers__add).each do |server| |
| 35 |
generate_stream(server.sse.public, tag: tag_of(server)) |
| 36 |
generate_stream(server.sse.public_local, tag: tag_of(server)) |
| 37 |
generate_stream(server.sse.public(only_media: true), tag: tag_of(server)) |
| 38 |
generate_stream(server.sse.public_local(only_media: true), tag: tag_of(server)) |
| 39 |
end |
| 40 |
|
| 41 |
subscribe(:mastodon_servers__delete).each do |lost_server| |
| 42 |
detach(tag_of(lost_server)) |
| 43 |
end |
| 44 |
|
| 45 |
def generate_stream(connection_type, tag:) |
| 46 |
generate(:extract_receive_message, connection_type.datasource_slug, tags: [tag]) do |stream_input| |
| 47 |
connection_pool(connection_type).add_handler( |
| 48 |
Plugin::MastodonSseStreaming::Handler::Update.new( |
| 49 |
connection_type, |
| 50 |
&stream_input.method(:<<) |
| 51 |
) |
| 52 |
) |
| 53 |
end |
| 54 |
end |
| 55 |
|
| 56 |
def generate_mention_stream(connection_type, tag:) |
| 57 |
generate(:extract_receive_message, connection_type.datasource_slug, tags: [tag]) do |stream_input| |
| 58 |
connection_pool(connection_type).add_handler( |
| 59 |
Plugin::MastodonSseStreaming::Handler::Mention.new( |
| 60 |
connection_type, |
| 61 |
&stream_input.method(:<<) |
| 62 |
) |
| 63 |
) |
| 64 |
end |
| 65 |
end |
| 66 |
|
| 67 |
def generate_notification_stream(connection_type, tag:) |
| 68 |
connection_pool(connection_type).add_handler( |
| 69 |
Plugin::MastodonSseStreaming::Handler::Reblog.new( |
| 70 |
connection_type, |
| 71 |
&Plugin.method(:call).curry(3).(:share) |
| 72 |
) |
| 73 |
).add_handler( |
| 74 |
Plugin::MastodonSseStreaming::Handler::Favorite.new( |
| 75 |
connection_type, |
| 76 |
&Plugin.method(:call).curry(4).(:favorite, connection_type.world) |
| 77 |
) |
| 78 |
).add_handler( |
| 79 |
Plugin::MastodonSseStreaming::Handler::Follow.new( |
| 80 |
connection_type |
| 81 |
) do |user| |
| 82 |
Plugin.call(:followers_created, connection_type.world, [user]) |
| 83 |
end |
| 84 |
).add_handler( |
| 85 |
Plugin::MastodonSseStreaming::Handler::Poll.new( |
| 86 |
connection_type |
| 87 |
) do |message| |
| 88 |
activity(:poll, _('投票が終了しました'), description: message.uri.to_s) |
| 89 |
end |
| 90 |
) |
| 91 |
end |
| 92 |
|
| 93 |
def connection_pool(connection_type) |
| 94 |
@connection_pool ||= {} |
| 95 |
@connection_pool[connection_type.uri] ||= Plugin::MastodonSseStreaming::Connection.new( |
| 96 |
connection_type |
| 97 |
) |
| 98 |
end |
| 99 |
|
| 100 |
@tags = {} |
| 101 |
def tag_of(world_or_server) |
| 102 |
@tags[world_or_server.hash] ||= handler_tag |
| 103 |
end |
| 104 |
end |