Fix #5366: Baraag broken.
* Use fixed access tokens instead of fetching an access token with the OAuth flow. This assumes access tokens won't expire, which seems to be true for the default app-level access token, unless you manually regenerate it. Fixes the OAuth flow not working on Baraag for some reason. * Eliminate the MastodonApiClient class. Just inline it in the extractor instead. Downstream users will need to update their configs to set the `pawoo_access_token` and `baraag_access_token` config options.
This commit is contained in:
@@ -1,89 +0,0 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
# An API client for Mastodon.
|
|
||||||
#
|
|
||||||
# @see https://docs.joinmastodon.org/api
|
|
||||||
class MastodonApiClient
|
|
||||||
extend Memoist
|
|
||||||
attr_reader :json, :id
|
|
||||||
|
|
||||||
def initialize(site_name, id)
|
|
||||||
@site_name = site_name
|
|
||||||
@id = id
|
|
||||||
end
|
|
||||||
|
|
||||||
def json
|
|
||||||
return {} if id.blank? || access_token.blank?
|
|
||||||
JSON.parse(access_token.get("/api/v1/statuses/#{id}").body)
|
|
||||||
rescue
|
|
||||||
{}
|
|
||||||
end
|
|
||||||
|
|
||||||
def profile_url
|
|
||||||
json&.dig("account", "url")
|
|
||||||
end
|
|
||||||
|
|
||||||
def account_name
|
|
||||||
json&.dig("account", "username")
|
|
||||||
end
|
|
||||||
|
|
||||||
def display_name
|
|
||||||
json&.dig("account", "display_name")
|
|
||||||
end
|
|
||||||
|
|
||||||
def account_id
|
|
||||||
json&.dig("account", "id")
|
|
||||||
end
|
|
||||||
|
|
||||||
def image_url
|
|
||||||
image_urls.first
|
|
||||||
end
|
|
||||||
|
|
||||||
def image_urls
|
|
||||||
json&.dig("media_attachments").to_a.map {|x| x["url"]}
|
|
||||||
end
|
|
||||||
|
|
||||||
def tags
|
|
||||||
json&.dig("tags").to_a.map { |tag| [tag["name"], tag["url"]] }
|
|
||||||
end
|
|
||||||
|
|
||||||
def commentary
|
|
||||||
commentary = "".dup
|
|
||||||
commentary << "<p>#{json["spoiler_text"]}</p>" if json["spoiler_text"].present?
|
|
||||||
commentary << json["content"] if json["content"].present?
|
|
||||||
commentary
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_h
|
|
||||||
json
|
|
||||||
end
|
|
||||||
|
|
||||||
def fetch_access_token
|
|
||||||
Cache.get("#{@site_name}-token") do
|
|
||||||
result = client.client_credentials.get_token
|
|
||||||
result.token
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def access_token
|
|
||||||
return if client.blank?
|
|
||||||
OAuth2::AccessToken.new(client, fetch_access_token)
|
|
||||||
end
|
|
||||||
|
|
||||||
def client
|
|
||||||
case @site_name
|
|
||||||
when "pawoo.net"
|
|
||||||
client_id = Danbooru.config.pawoo_client_id
|
|
||||||
client_secret = Danbooru.config.pawoo_client_secret
|
|
||||||
when "baraag.net"
|
|
||||||
client_id = Danbooru.config.baraag_client_id
|
|
||||||
client_secret = Danbooru.config.baraag_client_secret
|
|
||||||
end
|
|
||||||
|
|
||||||
return unless client_id && client_secret
|
|
||||||
|
|
||||||
OAuth2::Client.new(client_id, client_secret, :site => "https://#{@site_name}")
|
|
||||||
end
|
|
||||||
|
|
||||||
memoize :client, :json
|
|
||||||
end
|
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
# @see Source::URL::Mastodon
|
# @see Source::URL::Mastodon
|
||||||
|
# @see https://docs.joinmastodon.org/api
|
||||||
class Source::Extractor
|
class Source::Extractor
|
||||||
class Mastodon < Source::Extractor
|
class Mastodon < Source::Extractor
|
||||||
def match?
|
def match?
|
||||||
@@ -18,80 +19,99 @@ class Source::Extractor
|
|||||||
if parsed_url.image_url?
|
if parsed_url.image_url?
|
||||||
[parsed_url.full_image_url]
|
[parsed_url.full_image_url]
|
||||||
else
|
else
|
||||||
api_response.image_urls
|
api_response.dig("media_attachments").to_a.pluck("url")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def page_url
|
def page_url
|
||||||
artist_name = artist_name_from_url
|
if username.present? && status_id.present?
|
||||||
status_id = status_id_from_url
|
"https://#{domain}/@#{username}/#{status_id}"
|
||||||
return if status_id.blank?
|
elsif status_id.present?
|
||||||
|
|
||||||
if artist_name.present?
|
|
||||||
"https://#{domain}/@#{artist_name}/#{status_id}"
|
|
||||||
else
|
|
||||||
"https://#{domain}/web/statuses/#{status_id}"
|
"https://#{domain}/web/statuses/#{status_id}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def profile_url
|
def profile_url
|
||||||
if artist_name_from_url.present?
|
if username.present?
|
||||||
"https://#{domain}/@#{artist_name_from_url}"
|
"https://#{domain}/@#{username}"
|
||||||
elsif api_response.present? && api_response.profile_url.present?
|
else
|
||||||
api_response.profile_url
|
api_response.dig("account", "url")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def account_url
|
def account_url
|
||||||
return if account_id.blank?
|
"https://#{domain}/web/accounts/#{account_id}" if account_id.present?
|
||||||
"https://#{domain}/web/accounts/#{account_id}"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def profile_urls
|
def profile_urls
|
||||||
[profile_url, account_url].compact
|
[profile_url, account_url].compact
|
||||||
end
|
end
|
||||||
|
|
||||||
def artist_name
|
def username
|
||||||
api_response.account_name || artist_name_from_url
|
api_response.dig("account", "username") || artist_name_from_url
|
||||||
|
end
|
||||||
|
|
||||||
|
def tag_name
|
||||||
|
username
|
||||||
end
|
end
|
||||||
|
|
||||||
def artist_name_from_url
|
def artist_name_from_url
|
||||||
parsed_url.username || parsed_referer&.username
|
parsed_url.username || parsed_referer&.username
|
||||||
end
|
end
|
||||||
|
|
||||||
def other_names
|
def artist_name
|
||||||
[api_response.display_name].compact
|
api_response.dig("account", "display_name").presence
|
||||||
end
|
end
|
||||||
|
|
||||||
def account_id
|
def account_id
|
||||||
parsed_url.user_id || parsed_referer&.user_id || api_response.account_id
|
parsed_url.user_id || parsed_referer&.user_id || api_response.dig("account", "id")
|
||||||
end
|
end
|
||||||
|
|
||||||
def status_id_from_url
|
def status_id
|
||||||
parsed_url.work_id || parsed_referer&.work_id
|
parsed_url.work_id || parsed_referer&.work_id
|
||||||
end
|
end
|
||||||
|
|
||||||
def artist_commentary_desc
|
def artist_commentary_desc
|
||||||
api_response.commentary
|
commentary = "".dup
|
||||||
|
commentary << "<p>#{api_response["spoiler_text"]}</p>" if api_response["spoiler_text"].present?
|
||||||
|
commentary << api_response["content"] if api_response["content"].present?
|
||||||
|
commentary
|
||||||
end
|
end
|
||||||
|
|
||||||
def tags
|
def tags
|
||||||
api_response.tags
|
api_response.dig("tags").to_a.map do |tag|
|
||||||
|
[tag["name"], tag["url"]]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def dtext_artist_commentary_desc
|
def dtext_artist_commentary_desc
|
||||||
DText.from_html(artist_commentary_desc) do |element|
|
DText.from_html(artist_commentary_desc) do |element|
|
||||||
if element.name == "a"
|
if element.name == "a"
|
||||||
# don't include links to the toot itself.
|
# don't include links to the toot itself.
|
||||||
media_urls = api_response.json["media_attachments"].map { |attr| attr["text_url"] }
|
media_urls = api_response.dig("media_attachments").pluck("text_url")
|
||||||
element["href"] = nil if element["href"].in?(media_urls)
|
element["href"] = nil if element["href"].in?(media_urls)
|
||||||
end
|
end
|
||||||
end.strip
|
end.strip
|
||||||
end
|
end
|
||||||
|
|
||||||
def api_response
|
def api_response
|
||||||
MastodonApiClient.new(domain, status_id_from_url)
|
return {} if status_id.blank?
|
||||||
|
resp = http.get("https://#{domain}/api/v1/statuses/#{status_id}")
|
||||||
|
return {} if resp.status != 200
|
||||||
|
resp.parse
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def http
|
||||||
|
super.headers(Authorization: "Bearer #{access_token}")
|
||||||
|
end
|
||||||
|
|
||||||
|
def access_token
|
||||||
|
case site_name
|
||||||
|
when "Pawoo" then Danbooru.config.pawoo_access_token
|
||||||
|
when "Baraag" then Danbooru.config.baraag_access_token
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
memoize :api_response
|
memoize :api_response
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -381,19 +381,11 @@ module Danbooru
|
|||||||
end
|
end
|
||||||
|
|
||||||
# http://tinysubversions.com/notes/mastodon-bot/
|
# http://tinysubversions.com/notes/mastodon-bot/
|
||||||
def pawoo_client_id
|
def pawoo_access_token
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def pawoo_client_secret
|
def baraag_access_token
|
||||||
nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def baraag_client_id
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def baraag_client_secret
|
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,8 @@ module Sources
|
|||||||
"https://pawoo.net/web/statuses/1202176",
|
"https://pawoo.net/web/statuses/1202176",
|
||||||
image_urls: ["https://img.pawoo.net/media_attachments/files/000/128/953/original/4c0a06087b03343f.png"],
|
image_urls: ["https://img.pawoo.net/media_attachments/files/000/128/953/original/4c0a06087b03343f.png"],
|
||||||
profile_url: "https://pawoo.net/@9ed00e924818",
|
profile_url: "https://pawoo.net/@9ed00e924818",
|
||||||
artist_name: "9ed00e924818",
|
tag_name: "9ed00e924818",
|
||||||
|
artist_name: nil,
|
||||||
dtext_artist_commentary_desc: "a mind forever voyaging through strange seas of thought alone"
|
dtext_artist_commentary_desc: "a mind forever voyaging through strange seas of thought alone"
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
@@ -37,7 +38,8 @@ module Sources
|
|||||||
https://img.pawoo.net/media_attachments/files/001/298/084/original/media.mp4
|
https://img.pawoo.net/media_attachments/files/001/298/084/original/media.mp4
|
||||||
],
|
],
|
||||||
profile_urls: %w[https://pawoo.net/@evazion https://pawoo.net/web/accounts/47806],
|
profile_urls: %w[https://pawoo.net/@evazion https://pawoo.net/web/accounts/47806],
|
||||||
artist_name: "evazion",
|
tag_name: "evazion",
|
||||||
|
artist_name: nil,
|
||||||
tags: %w[foo bar baz],
|
tags: %w[foo bar baz],
|
||||||
dtext_artist_commentary_desc: desc
|
dtext_artist_commentary_desc: desc
|
||||||
)
|
)
|
||||||
@@ -57,7 +59,7 @@ module Sources
|
|||||||
strategy_should_work(
|
strategy_should_work(
|
||||||
"https://pawoo.net/@nonamethankswashere/12345678901234567890",
|
"https://pawoo.net/@nonamethankswashere/12345678901234567890",
|
||||||
profile_url: "https://pawoo.net/@nonamethankswashere",
|
profile_url: "https://pawoo.net/@nonamethankswashere",
|
||||||
artist_name: "nonamethankswashere",
|
tag_name: "nonamethankswashere",
|
||||||
deleted: true
|
deleted: true
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
@@ -74,7 +76,8 @@ module Sources
|
|||||||
image_urls: ["https://baraag.net/system/media_attachments/files/105/732/803/241/495/700/original/556e1eb7f5ca610f.png"],
|
image_urls: ["https://baraag.net/system/media_attachments/files/105/732/803/241/495/700/original/556e1eb7f5ca610f.png"],
|
||||||
download_size: 573_353,
|
download_size: 573_353,
|
||||||
profile_url: "https://baraag.net/@bardbot",
|
profile_url: "https://baraag.net/@bardbot",
|
||||||
artist_name: "bardbot",
|
tag_name: "bardbot",
|
||||||
|
artist_name: "SpicyBardo🔞",
|
||||||
dtext_artist_commentary_desc: "🍌"
|
dtext_artist_commentary_desc: "🍌"
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
@@ -91,7 +94,7 @@ module Sources
|
|||||||
strategy_should_work(
|
strategy_should_work(
|
||||||
"https://baraag.net/@nonamethankswashere/12345678901234567890",
|
"https://baraag.net/@nonamethankswashere/12345678901234567890",
|
||||||
profile_url: "https://baraag.net/@nonamethankswashere",
|
profile_url: "https://baraag.net/@nonamethankswashere",
|
||||||
artist_name: "nonamethankswashere",
|
tag_name: "nonamethankswashere",
|
||||||
deleted: true
|
deleted: true
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user