sferik / twitter-ruby

A Ruby interface to the Twitter API.
http://www.rubydoc.info/gems/twitter
MIT License
4.59k stars 1.31k forks source link

Getting users who liked a tweet #1026

Closed glaucocustodio closed 11 months ago

glaucocustodio commented 1 year ago

Hey, I am using the following hack to get user ids who liked a tweet:

gem 'twitter', '~> 7.0'
# config/initializers/twitter_ext.rb
module Twitter
  module REST
    module Tweets
      # return empty array if tweet has no likes
      # return array of user_ids otherwise
      def liking_user_ids(tweet_id)
        response = perform_get_with_objects("/2/tweets/#{tweet_id}/liking_users", {}, Twitter::Base)
        response.find { _1.attrs[0] == :data }&.attrs&.fetch(1, [])&.map{ _1[:id].to_i } || []
      end
    end
  end
end

It consumes the v2 api.

Would a PR adding such a method be welcome? Of course I would need to properly deal with the response instead of doing this mess.