From 91fec868d8d971952532f504e051d2573bb39613 Mon Sep 17 00:00:00 2001 From: Akira Ouchi Date: Sun, 1 Dec 2024 12:15:01 +0900 Subject: [PATCH] add support for mastodon bookmark --- plugin/mastodon/mastodon.rb | 1 + plugin/mastodon/model/rest_authorized_type.rb | 8 +++++ plugin/mastodon/model/status.rb | 2 ++ plugin/mastodon/model/world.rb | 16 +++++++++ plugin/mastodon/spell.rb | 34 +++++++++++++++++++ plugin/mastodon_rest/mastodon_rest.rb | 1 + 6 files changed, 62 insertions(+) diff --git a/plugin/mastodon/mastodon.rb b/plugin/mastodon/mastodon.rb index fd05e553..bba8d0ca 100644 --- a/plugin/mastodon/mastodon.rb +++ b/plugin/mastodon/mastodon.rb @@ -110,6 +110,7 @@ Plugin.create(:mastodon) do world.sse.user, world.sse.mention, world.sse.direct, + world.rest.bookmarks, world.sse.public, world.sse.public(only_media: true), world.sse.public_local, diff --git a/plugin/mastodon/model/rest_authorized_type.rb b/plugin/mastodon/model/rest_authorized_type.rb index e011f71e..4f9d7bff 100644 --- a/plugin/mastodon/model/rest_authorized_type.rb +++ b/plugin/mastodon/model/rest_authorized_type.rb @@ -35,6 +35,14 @@ module Plugin::Mastodon set_endpoint_timelines('direct') end + def bookmarks + @datasource_slug = "mastodon-#{world.account.acct}-bookmarks".to_sym + @title = Plugin[:mastodon]._('Mastodon/%{domain}/%{acct}/ブックマーク') % {domain: world.server.domain, acct: world.account.acct} + @perma_link = Diva::URI.new('https://%{domain}/api/v1/bookmarks' % {domain: server.domain}) + @response_entities = :status + self + end + def list(list_id:, title:) # params[:list] = list_id @datasource_slug = "mastodon-#{world.account.acct}-list-#{list_id}".to_sym diff --git a/plugin/mastodon/model/status.rb b/plugin/mastodon/model/status.rb index 637c0f93..c93b1c1f 100644 --- a/plugin/mastodon/model/status.rb +++ b/plugin/mastodon/model/status.rb @@ -31,6 +31,7 @@ module Plugin::Mastodon field.has :application, Application field.string :language field.bool :pinned + field.bool :bookmarked field.string :domain, required: true # APIには無い追加フィールド @@ -51,6 +52,7 @@ module Plugin::Mastodon alias :perma_link :url alias :muted? :muted alias :pinned? :pinned + alias :bookmarked? :bookmarked alias :retweet_ancestor :reblog alias :sensitive? :sensitive # NSFW系プラグイン用 diff --git a/plugin/mastodon/model/world.rb b/plugin/mastodon/model/world.rb index 038b2827..79df8cd2 100644 --- a/plugin/mastodon/model/world.rb +++ b/plugin/mastodon/model/world.rb @@ -241,6 +241,22 @@ module Plugin::Mastodon } end + def bookmark(status) + Plugin::Mastodon::API.get_local_status_id(self, status).next{ |status_id| + Plugin::Mastodon::API.call(:post, domain, "/api/v1/statuses/#{status_id}/bookmark", access_token) + }.next{ + status.bookmarked = true + } + end + + def unbookmark(status) + Plugin::Mastodon::API.get_local_status_id(self, status).next{ |status_id| + Plugin::Mastodon::API.call(:post, domain, "/api/v1/statuses/#{status_id}/unbookmark", access_token) + }.next{ + status.bookmarked = false + } + end + def report_for_spam(statuses, comment) Deferred.when( Plugin::Mastodon::API.get_local_account_id(self, statuses.first.account), diff --git a/plugin/mastodon/spell.rb b/plugin/mastodon/spell.rb index e1847eef..786f8e84 100644 --- a/plugin/mastodon/spell.rb +++ b/plugin/mastodon/spell.rb @@ -61,6 +61,28 @@ Plugin.create(:mastodon) do } end + command(:mastodon_pin_message, name: _('ブックマークする'), visible: true, role: :timeline, + condition: lambda { |opt| + opt.messages.any? { |m| bookmark_message?(opt.world, m) } + }) do |opt| + opt.messages.select{ |m| + bookmark_message?(opt.world, m) + }.each { |status| + opt.world.bookmark(status) + } + end + + command(:mastodon_unpin_message, name: _('ブックマークを解除する'), visible: true, role: :timeline, + condition: lambda { |opt| + opt.messages.any? { |m| unbookmark_message?(opt.world, m) } + }) do |opt| + opt.messages.select{ |m| + unbookmark_message?(opt.world, m) + }.each { |status| + opt.world.unbookmark(status) + } + end + command(:mastodon_edit_list_membership, name: _('リストへの追加・削除'), visible: true, role: :timeline, condition: lambda { |opt| mastodon?(opt.world) @@ -470,6 +492,18 @@ Plugin.create(:mastodon) do world.unpin(status) end + defspell(:bookmark_message, :mastodon, :mastodon_status, + condition: -> (world, status) { !status.bookmarked? } + ) do |world, status| + world.bookmark(status) + end + + defspell(:unbookmark_message, :mastodon, :mastodon_status, + condition: -> (world, status) { status.bookmarked? } + ) do |world, status| + world.unbookmark(status) + end + defspell(:mastodon, :mastodon) do |mastodon| true end diff --git a/plugin/mastodon_rest/mastodon_rest.rb b/plugin/mastodon_rest/mastodon_rest.rb index bdf454cc..b1097da6 100644 --- a/plugin/mastodon_rest/mastodon_rest.rb +++ b/plugin/mastodon_rest/mastodon_rest.rb @@ -5,6 +5,7 @@ Plugin.create(:mastodon_rest) do [ new_world.rest.user, new_world.rest.mention, new_world.rest.direct, + new_world.rest.bookmarks, new_world.rest.public, new_world.rest.public_local, new_world.rest.public(only_media: true), -- 2.47.0