From a9b0dfa876741ecf50fb378b10d2b3e95e2eb51e Mon Sep 17 00:00:00 2001 From: Kuba Suder Date: Sat, 3 May 2025 22:17:10 +0200 Subject: [PATCH] optional feature to extract links from quoted posts --- Gemfile | 2 +- Gemfile.lock | 2 +- app/tootify.rb | 33 +++++++++++++++++++++++++++++++-- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index 0a98453..325f71b 100644 --- a/Gemfile +++ b/Gemfile @@ -2,7 +2,7 @@ source 'https://rubygems.org' gem 'didkit', '~> 0.2' gem 'mastodon-api', git: 'https://github.com/mastodon/mastodon-api.git' -gem 'minisky', '~> 0.4' +gem 'minisky', '~> 0.5' gem 'io-console', '~> 0.5' gem 'json', '~> 2.5' diff --git a/Gemfile.lock b/Gemfile.lock index 4174964..71a7968 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -61,7 +61,7 @@ DEPENDENCIES io-console (~> 0.5) json (~> 2.5) mastodon-api! - minisky (~> 0.4) + minisky (~> 0.5) net-http (~> 0.2) uri (~> 0.13) yaml (~> 0.1) diff --git a/app/tootify.rb b/app/tootify.rb index ea8489e..5790693 100644 --- a/app/tootify.rb +++ b/app/tootify.rb @@ -1,19 +1,31 @@ require 'io/console' +require 'yaml' require_relative 'bluesky_account' require_relative 'mastodon_account' require_relative 'post_history' class Tootify + CONFIG_FILE = File.expand_path(File.join(__dir__, '..', 'config', 'tootify.yml')) + attr_accessor :check_interval def initialize @bluesky = BlueskyAccount.new @mastodon = MastodonAccount.new @history = PostHistory.new + @config = load_config @check_interval = 60 end + def load_config + if File.exist?(CONFIG_FILE) + YAML.load(File.read(CONFIG_FILE)) + else + {} + end + end + def login_bluesky(handle) handle = handle.gsub(/^@/, '') @@ -120,8 +132,17 @@ class Tootify repo, collection, rkey = quote_uri.split('/')[2..4] if collection == 'app.bsky.feed.post' - bsky_url = bsky_post_link(repo, rkey) - append_link(text, bsky_url) unless text.include?(bsky_url) + link_to_append = bsky_post_link(repo, rkey) + + if @config['extract_link_from_quotes'] + quoted_record = fetch_record_by_at_uri(quote_uri) + + if link_from_quote = link_embed(quoted_record) + link_to_append = link_from_quote + end + end + + append_link(text, link_to_append) unless text.include?(link_to_append) end end @@ -164,6 +185,14 @@ class Tootify @mastodon.post_status(text, media_ids, mastodon_parent_id) end + def fetch_record_by_at_uri(quote_uri) + repo, collection, rkey = quote_uri.split('/')[2..4] + pds = DID.new(repo).get_document.pds_endpoint + sky = Minisky.new(pds, nil) + resp = sky.get_request('com.atproto.repo.getRecord', { repo: repo, collection: collection, rkey: rkey }) + resp['value'] + end + def expand_facets(record) bytes = record['text'].bytes offset = 0 -- 2.51.2