From 46419adfecfcff76b444ff992b8929f5a0e85bfd Mon Sep 17 00:00:00 2001 From: colorchestra Date: Thu, 23 Feb 2023 18:52:38 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=98=20hacky=20Mastodon=20support?= =?UTF-8?q?=20(toot=20purchased=20drinks;=20WIP)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 3 +++ app/models/user.rb | 20 ++++++++++++++++++++ config/environments/development.rb | 5 +++++ config/environments/production.rb | 5 +++++ config/environments/test.rb | 5 +++++ 5 files changed, 38 insertions(+) diff --git a/Gemfile b/Gemfile index e24055f4..0784a439 100644 --- a/Gemfile +++ b/Gemfile @@ -45,6 +45,9 @@ gem 'thor', '~> 1.0.1' gem 'sentry-ruby' gem 'sentry-rails' +# To toot purchases to Mastodon +gem 'httparty' + group :development do gem 'faker' gem 'ruby-prof' diff --git a/app/models/user.rb b/app/models/user.rb index ec34797b..58c6ed8f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,6 @@ # frozen_string_literal: true +require 'uri' +require 'httparty' class User < ApplicationRecord validates :name, presence: true @@ -23,6 +25,9 @@ def buy(drink) self.balance -= drink.price @purchased_drink = drink save! + if Rails.application.config.mastodon_token && Rails.application.config.mastodon_instance + toot(drink) + end end end @@ -38,4 +43,19 @@ def initial return first if first =~ /[a-z]/ '0' end + + def toot(drink) + response = HTTParty.post( + Rails.application.config.mastodon_instance + "/api/v1/statuses", + body: URI.encode_www_form({status: drink.name + '!'}), + :headers => {"Authorization" => 'Bearer ' + Rails.application.config.mastodon_token} + ) + case response.code + when 200...204 + puts "All good!" + when 300...600 + puts "Something went wrong! #{response.code}" + end + end + end end diff --git a/config/environments/development.rb b/config/environments/development.rb index 02cbd14a..f774d214 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -51,4 +51,9 @@ # Uncomment if you wish to allow Action Cable access from any origin. # config.action_cable.disable_request_forgery_protection = true + + # Get Mastodon data from environment variables + # Note: ENV["FOO"] returns nil if variable does not exist + config.mastodon_token = ENV["MASTODON_TOKEN"] + config.mastodon_instance = ENV["MASTODON_INSTANCE"] # expected format is https://example.social end diff --git a/config/environments/production.rb b/config/environments/production.rb index b1d1963a..7ffdfe3c 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -87,4 +87,9 @@ # Do not dump schema after migrations. config.active_record.dump_schema_after_migration = false + + # Get Mastodon data from environment variables + # Note: ENV["FOO"] returns nil if variable does not exist + config.mastodon_token = ENV["MASTODON_TOKEN"] + config.mastodon_instance = ENV["MASTODON_INSTANCE"] # expected format is https://example.social end diff --git a/config/environments/test.rb b/config/environments/test.rb index 6ea4d1e7..c4ec7284 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -57,4 +57,9 @@ # Annotate rendered view with file names. # config.action_view.annotate_rendered_view_with_filenames = true + + # Get Mastodon data from environment variables + # Note: ENV["FOO"] returns nil if variable does not exist + config.mastodon_token = ENV["MASTODON_TOKEN"] + config.mastodon_instance = ENV["MASTODON_INSTANCE"] # expected format is https://example.social end From c6c6294a45dfdb6ee86cb91d869e2899602a9fea Mon Sep 17 00:00:00 2001 From: colorchestra Date: Thu, 23 Feb 2023 21:14:56 +0100 Subject: [PATCH 2/2] make tooting threaded --- app/models/user.rb | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 58c6ed8f..45d63180 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -45,16 +45,17 @@ def initial end def toot(drink) - response = HTTParty.post( - Rails.application.config.mastodon_instance + "/api/v1/statuses", - body: URI.encode_www_form({status: drink.name + '!'}), - :headers => {"Authorization" => 'Bearer ' + Rails.application.config.mastodon_token} - ) - case response.code - when 200...204 - puts "All good!" - when 300...600 - puts "Something went wrong! #{response.code}" + t = Thread.new do + response = HTTParty.post( + Rails.application.config.mastodon_instance + "/api/v1/statuses", + body: URI.encode_www_form({status: drink.name + '!'}), + :headers => {"Authorization" => 'Bearer ' + Rails.application.config.mastodon_token} + ) + case response.code + when 200...204 + puts "I tooted! Status ID: " + response.parsed_response['id'] + when 300...600 + puts "Tooting failed! #{response.code}: #{response}" end end end