From 97c05d3f862388eb146d7e2e2184f5915c3555c2 Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Tue, 5 Mar 2024 20:34:34 +1100 Subject: [PATCH] Syndicate code posts to Github Gists --- config/app.rb | 1 + config/providers/clients.rb | 1 + config/providers/syndication.rb | 1 + config/settings.rb | 2 ++ config/taglines.yaml | 4 +++ lib/adamantium/client/gist.rb | 31 +++++++++++++++++++ lib/adamantium/syndication/gist.rb | 23 ++++++++++++++ slices/main/templates/shared/_gist.html.slim | 3 ++ slices/micropub/actions/site/config.rb | 8 +++++ .../commands/posts/create_code_post.rb | 2 +- slices/micropub/commands/posts/syndicate.rb | 10 +++++- 11 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 config/taglines.yaml create mode 100644 lib/adamantium/client/gist.rb create mode 100644 lib/adamantium/syndication/gist.rb create mode 100644 slices/main/templates/shared/_gist.html.slim diff --git a/config/app.rb b/config/app.rb index 9238939..399b0f2 100644 --- a/config/app.rb +++ b/config/app.rb @@ -36,6 +36,7 @@ module Adamantium "syndication.dayone", "syndication.mastodon", "syndication.blue_sky", + "syndication.gist", "syndication.raindrop", "view_cache.cacher", "renderers.markdown" diff --git a/config/providers/clients.rb b/config/providers/clients.rb index b293054..4490a0b 100644 --- a/config/providers/clients.rb +++ b/config/providers/clients.rb @@ -3,5 +3,6 @@ Hanami.app.register_provider :clients, namespace: true do register "mastodon", Adamantium::Client::Mastodon.new register "blue_sky", Adamantium::Client::BlueSky.new register "omdb", Adamantium::Client::Omdb.new(api_key: target["settings"].omdb_api_key) + register "gist", Adamantium::Client::Gist.new(token: target["settings"].gist_client_token) end end diff --git a/config/providers/syndication.rb b/config/providers/syndication.rb index 2d71e48..a745ec5 100644 --- a/config/providers/syndication.rb +++ b/config/providers/syndication.rb @@ -1,6 +1,7 @@ Hanami.app.register_provider :syndication, namespace: true do start do register "mastodon", Adamantium::Syndication::Mastodon.new + register "gist", Adamantium::Syndication::Gist.new register "blue_sky", Adamantium::Syndication::BlueSky.new register "raindrop", Adamantium::Syndication::Raindrop.new(api_key: "Bearer #{target["settings"].raindrop_api_key}") register "dayone", Adamantium::Syndication::Dayone.new( diff --git a/config/settings.rb b/config/settings.rb index 251b106..d598b00 100644 --- a/config/settings.rb +++ b/config/settings.rb @@ -72,5 +72,7 @@ module Adamantium setting :sentry_dsn, default: nil setting :raise_exceptions, default: true + + setting :gist_client_token, default: nil end end diff --git a/config/taglines.yaml b/config/taglines.yaml new file mode 100644 index 0000000..c83458a --- /dev/null +++ b/config/taglines.yaml @@ -0,0 +1,4 @@ +taglines: + - lord of the dance + - would that it were so simple + - "*furious hand gestures*" \ No newline at end of file diff --git a/lib/adamantium/client/gist.rb b/lib/adamantium/client/gist.rb new file mode 100644 index 0000000..1dc8695 --- /dev/null +++ b/lib/adamantium/client/gist.rb @@ -0,0 +1,31 @@ +require "httparty" + +module Adamantium + module Client + class Gist + include Dry::Monads[:result] + + def initialize(token:) + @token = token + end + + def create_gist(description:, public:, files: {}) + request_body = { + description: description, + public: public, + files: files + } + + res = HTTParty.post("https://api.github.com/gists", + body: JSON.generate(request_body), + headers: {Authorization: "Bearer #{@token}"}) + + if res.code >= 200 && res.code < 400 + Success(res["html_url"]) + else + Failure(:could_not_save_gist) + end + end + end + end +end diff --git a/lib/adamantium/syndication/gist.rb b/lib/adamantium/syndication/gist.rb new file mode 100644 index 0000000..70d8cd6 --- /dev/null +++ b/lib/adamantium/syndication/gist.rb @@ -0,0 +1,23 @@ +require "dry/monads" +require "sanitize" + +module Adamantium + module Syndication + class Gist + include Dry::Monads[:result] + include Deps[gist_client: "clients.gist"] + + def call(post:) + description = "" + public = true + files = { + post[:name] => {content: Sanitize.fragment(post[:content])} + } + + gist_client.create_gist(description: description, + public: public, + files: files) + end + end + end +end diff --git a/slices/main/templates/shared/_gist.html.slim b/slices/main/templates/shared/_gist.html.slim new file mode 100644 index 0000000..b758089 --- /dev/null +++ b/slices/main/templates/shared/_gist.html.slim @@ -0,0 +1,3 @@ +- w_class = defined?(width) ? width : "w-6" + + diff --git a/slices/micropub/actions/site/config.rb b/slices/micropub/actions/site/config.rb index 946ece0..756409e 100644 --- a/slices/micropub/actions/site/config.rb +++ b/slices/micropub/actions/site/config.rb @@ -29,6 +29,10 @@ module Micropub { uid: "https://bsky.social", name: "Blue Sky" + }, + { + uid: "https://gist.github.com", + name: "Gist" } ] }.to_json @@ -44,6 +48,10 @@ module Micropub { uid: "https://bsky.social", name: "Blue Sky" + }, + { + uid: "https://gist.github.com", + name: "Gist" } ] }.to_json diff --git a/slices/micropub/commands/posts/create_code_post.rb b/slices/micropub/commands/posts/create_code_post.rb index c25b2ee..2eea09b 100644 --- a/slices/micropub/commands/posts/create_code_post.rb +++ b/slices/micropub/commands/posts/create_code_post.rb @@ -15,7 +15,7 @@ module Micropub post_params = prepare_params(params: post) created_post = post_repo.create(post_params) - # syndicate.call(created_post.id, post) + syndicate.call(created_post.id, post) # decorated_post = Decorators::Posts::Decorator.new(created_post) diff --git a/slices/micropub/commands/posts/syndicate.rb b/slices/micropub/commands/posts/syndicate.rb index 677d478..65aa08f 100644 --- a/slices/micropub/commands/posts/syndicate.rb +++ b/slices/micropub/commands/posts/syndicate.rb @@ -11,6 +11,7 @@ module Micropub include Deps["settings", "syndication.mastodon", "syndication.blue_sky", + "syndication.gist", add_post_syndication_source: "commands.posts.add_syndication_source", send_to_dayone: "syndication.dayone", ] @@ -30,7 +31,13 @@ module Micropub add_post_syndication_source.call(post_id, :blue_sky, res.value!) if res.success? end - if post[:category].include? "weekly" + if syndicate_to.include? :gist + res = gist.call(post: post) + + add_post_syndication_source.call(post_id, :gist, res.value!) if res.success? + end + + if post[:category]&.include? "weekly" send_to_dayone.call(name: post[:name], content: post[:content]) end @@ -43,6 +50,7 @@ module Micropub targets = [] targets << :mastodon if syndicate_to.any? { |url| settings.mastodon_server.match(/#{url}/) } targets << :blue_sky if syndicate_to.any? { |url| settings.blue_sky_url.match(/#{url}/) } + targets << :gist if syndicate_to.any? { |url| "https://gist.github.com".match(/#{url}/) } targets end end