Skip to content

Instantly share code, notes, and snippets.

@Akkiesoft
Created March 28, 2024 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Akkiesoft/a17ad7c0f1cf0d3f5c8f0abbd5f9e923 to your computer and use it in GitHub Desktop.
Save Akkiesoft/a17ad7c0f1cf0d3f5c8f0abbd5f9e923 to your computer and use it in GitHub Desktop.
mikutterのMastodonでブックマークをあれするやつ
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..acda13b0 100644
--- a/plugin/mastodon/model/rest_authorized_type.rb
+++ b/plugin/mastodon/model/rest_authorized_type.rb
@@ -35,6 +35,16 @@ module Plugin::Mastodon
set_endpoint_timelines('direct')
end
+ def bookmarks
+ @datasource_slug = "mastodon-#{world.account.acct}-bookmarks".to_sym
+ @title = "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..63715d07 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment