You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mastodon.rb 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. require 'mastodon'
  2. require 'sanitize'
  3. require 'awesome_print'
  4. class CrossPost
  5. class Mastodon
  6. def initialize(config)
  7. settings = config[:settings]
  8. @posts = config[:posts]
  9. url = settings['mastodon.url']
  10. token = settings['mastodon.token']
  11. user = settings['mastodon.user']
  12. LOGGER.debug "Mastodon base URL: #{url}"
  13. @client = ::Mastodon::REST::Client.new base_url: url, bearer_token: token
  14. stream_url = settings.fetch 'mastodon.stream_url', url
  15. LOGGER.debug "Mastodon stream URL: #{stream_url}"
  16. @stream = ::Mastodon::Streaming::Client.new base_url: stream_url, bearer_token: token
  17. @user_url = URI.join(ENV.fetch('BASE_USER_URL', url), "/@#{user}").to_s
  18. LOGGER.debug "Mastodon user URL: #{@user_url}"
  19. end
  20. def feed(twitter)
  21. @stream.user do |object|
  22. begin
  23. case object
  24. when ::Mastodon::Status
  25. LOGGER.info { 'Receiving status' }
  26. LOGGER.debug { object.ai }
  27. next if reject? object
  28. twitter.post_status object
  29. end
  30. rescue => e
  31. LOGGER.error e
  32. raise
  33. end
  34. end
  35. end
  36. private
  37. def reject?(status)
  38. return true if status.account.url != @user_url or
  39. status.visibility != 'public'
  40. reply = status.in_reply_to_id
  41. return true if reply and !@posts[reply]
  42. false
  43. end
  44. end
  45. end