From 9619227b0eb38bf276ab6a5b7cc144c92fb9f5e2 Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Sat, 4 Feb 2023 13:37:48 +1100 Subject: [PATCH] Upload media to mastodon --- lib/adamantium/syndication/mastodon.rb | 24 ++++++++++++++++++- .../unit/commands/media/upload_spec.rb | 8 +++---- spec/requests/create_post_spec.rb | 2 +- spec/requests/root_spec.rb | 2 +- spec/support/db/database_cleaner.rb | 4 ++-- spec/support/requests.rb | 5 ++-- spec/syndication/mastodon_spec.rb | 4 +++- 7 files changed, 37 insertions(+), 12 deletions(-) diff --git a/lib/adamantium/syndication/mastodon.rb b/lib/adamantium/syndication/mastodon.rb index 704c08a..0e6460d 100644 --- a/lib/adamantium/syndication/mastodon.rb +++ b/lib/adamantium/syndication/mastodon.rb @@ -1,6 +1,8 @@ require "digest" require "dry/monads" require "httparty" +require "tempfile" +require "open-uri" module Adamantium module Syndication @@ -20,19 +22,39 @@ module Adamantium post[:content] end + tags = post[:category].map { |tag| "##{tag}" }.join(" ") text_with_tags = "#{content} #{tags}" key = Digest::MD5.hexdigest text_with_tags mastodon_token = settings.mastodon_token mastodon_server = settings.mastodon_server.split("@").first + media_ids = post[:photos]&.map do |photo| + file = Tempfile.new(SecureRandom.uuid) + file.write(URL.open(file[:value]).read) + file.rewind + response = HTTParty.post("#{mastodon_server}api/v2/media", { + headers: { + Authorization: "Bearer #{mastodon_token}" + }, + body: { + file: file.read, + description: photo[:alt] + } + }) + file.close + file.unlink + JSON.parse(response.body).fetch(:id, nil) + end&.compact + response = HTTParty.post("#{mastodon_server}api/v1/statuses", { headers: { "Idempotency-Key": key, Authorization: "Bearer #{mastodon_token}" }, body: { - status: text_with_tags + status: text_with_tags, + media_ids: media_ids } }) diff --git a/spec/adamantium/unit/commands/media/upload_spec.rb b/spec/adamantium/unit/commands/media/upload_spec.rb index b9b9aa2..571092e 100644 --- a/spec/adamantium/unit/commands/media/upload_spec.rb +++ b/spec/adamantium/unit/commands/media/upload_spec.rb @@ -12,11 +12,11 @@ RSpec.describe Adamantium::Commands::Media::Upload do } result = subject.call(file: file) - expected_path = "media/#{Time.now.strftime("%m-%Y")}/foo.txt" + expected_path = /media\/#{Time.now.strftime("%m-%Y")}\/[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}.txt/ expect(result).to be_success - expect(result.value!).to eq "http://localhost/#{expected_path}" - File.read("public/#{expected_path}") - File.delete("public/#{expected_path}") + expect(result.value!).to match expected_path + File.read("public/#{result.value!.gsub("http://localhost/", "")}") + File.delete("public/#{result.value!.gsub("http://localhost/", "")}") end it "returns a Failure if the file couldn't be saved" do diff --git a/spec/requests/create_post_spec.rb b/spec/requests/create_post_spec.rb index 3f399b1..8093898 100644 --- a/spec/requests/create_post_spec.rb +++ b/spec/requests/create_post_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe "Post creation", type: [:request, :db] do +RSpec.describe "Post creation", :db, :requests do let(:post_repo) { Adamantium::Repos::PostRepo.new } context "posts" do diff --git a/spec/requests/root_spec.rb b/spec/requests/root_spec.rb index 27ecff2..9996de1 100644 --- a/spec/requests/root_spec.rb +++ b/spec/requests/root_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe "Root", type: :request do +RSpec.describe "Root", :requests do it "is successful" do get "/" diff --git a/spec/support/db/database_cleaner.rb b/spec/support/db/database_cleaner.rb index 0020528..3e3827f 100644 --- a/spec/support/db/database_cleaner.rb +++ b/spec/support/db/database_cleaner.rb @@ -9,14 +9,14 @@ RSpec.configure do |config| DatabaseCleaner[:sequel].clean_with :truncation end - config.prepend_before :each, type: :db do |example| + config.prepend_before :each, :db do |example| strategy = example.metadata[:js] ? :truncation : :transaction DatabaseCleaner[:sequel].strategy = strategy DatabaseCleaner[:sequel].start end - config.append_after :each, type: :db do + config.append_after :each, :db do DatabaseCleaner[:sequel].clean end end diff --git a/spec/support/requests.rb b/spec/support/requests.rb index f1c8f9a..6a4004a 100644 --- a/spec/support/requests.rb +++ b/spec/support/requests.rb @@ -1,3 +1,4 @@ +# require_with_metadata: true # frozen_string_literal: true require "rack/test" @@ -7,6 +8,6 @@ RSpec.shared_context "Hanami app" do end RSpec.configure do |config| - config.include Rack::Test::Methods, type: :request - config.include_context "Hanami app", type: :request + config.include Rack::Test::Methods, :requests + config.include_context "Hanami app", :requests end diff --git a/spec/syndication/mastodon_spec.rb b/spec/syndication/mastodon_spec.rb index 2fab3fe..8000587 100644 --- a/spec/syndication/mastodon_spec.rb +++ b/spec/syndication/mastodon_spec.rb @@ -13,7 +13,9 @@ RSpec.describe Adamantium::Syndication::Mastodon do slug: "my-post", category: ["ruby", "rspec"], published_at: Time.now, - post_type: "post" + post_type: "post", + syndicate_to: [], + photos: [] ) }