twitter 6.2.0 → 7.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +19 -7
  3. data/LICENSE.md +1 -1
  4. data/README.md +9 -12
  5. data/lib/twitter.rb +5 -0
  6. data/lib/twitter/client.rb +5 -5
  7. data/lib/twitter/creatable.rb +5 -1
  8. data/lib/twitter/cursor.rb +12 -2
  9. data/lib/twitter/direct_message.rb +2 -0
  10. data/lib/twitter/direct_message_event.rb +44 -0
  11. data/lib/twitter/direct_messages/welcome_message.rb +17 -0
  12. data/lib/twitter/direct_messages/welcome_message_rule.rb +12 -0
  13. data/lib/twitter/direct_messages/welcome_message_rule_wrapper.rb +36 -0
  14. data/lib/twitter/direct_messages/welcome_message_wrapper.rb +42 -0
  15. data/lib/twitter/enumerable.rb +14 -3
  16. data/lib/twitter/error.rb +48 -6
  17. data/lib/twitter/null_object.rb +15 -1
  18. data/lib/twitter/premium_search_results.rb +67 -0
  19. data/lib/twitter/profile.rb +2 -2
  20. data/lib/twitter/rate_limit.rb +2 -2
  21. data/lib/twitter/rest/account_activity.rb +99 -0
  22. data/lib/twitter/rest/api.rb +6 -0
  23. data/lib/twitter/rest/direct_messages.rb +131 -42
  24. data/lib/twitter/rest/direct_messages/welcome_messages.rb +90 -0
  25. data/lib/twitter/rest/favorites.rb +2 -2
  26. data/lib/twitter/rest/lists.rb +1 -0
  27. data/lib/twitter/rest/premium_search.rb +34 -0
  28. data/lib/twitter/rest/request.rb +39 -13
  29. data/lib/twitter/rest/search.rb +4 -3
  30. data/lib/twitter/rest/timelines.rb +1 -0
  31. data/lib/twitter/rest/tweets.rb +2 -31
  32. data/lib/twitter/rest/upload_utils.rb +68 -0
  33. data/lib/twitter/rest/utils.rb +24 -7
  34. data/lib/twitter/search_results.rb +4 -3
  35. data/lib/twitter/streaming/client.rb +5 -1
  36. data/lib/twitter/streaming/connection.rb +16 -3
  37. data/lib/twitter/streaming/message_parser.rb +1 -1
  38. data/lib/twitter/streaming/response.rb +1 -0
  39. data/lib/twitter/tweet.rb +10 -1
  40. data/lib/twitter/utils.rb +4 -2
  41. data/lib/twitter/version.rb +2 -2
  42. data/twitter.gemspec +3 -4
  43. metadata +16 -21
@@ -1,7 +1,7 @@
1
1
  require 'naught'
2
2
 
3
3
  module Twitter
4
- NullObject = Naught.build do |config|
4
+ NullObject = Naught.build do |config| # rubocop:disable Metrics/BlockLength
5
5
  include Comparable
6
6
 
7
7
  config.black_hole
@@ -19,11 +19,13 @@ module Twitter
19
19
 
20
20
  def instance_of?(klass)
21
21
  raise(TypeError, 'class or module required') unless klass.is_a?(Class)
22
+
22
23
  self.class == klass
23
24
  end
24
25
 
25
26
  def kind_of?(mod)
26
27
  raise(TypeError, 'class or module required') unless mod.is_a?(Module)
28
+
27
29
  self.class.ancestors.include?(mod)
28
30
  end
29
31
 
@@ -48,5 +50,17 @@ module Twitter
48
50
  def to_json(*args)
49
51
  nil.to_json(*args)
50
52
  end
53
+
54
+ def presence
55
+ nil
56
+ end
57
+
58
+ def blank?
59
+ true
60
+ end
61
+
62
+ def present?
63
+ false
64
+ end
51
65
  end
52
66
  end
@@ -0,0 +1,67 @@
1
+ require 'cgi'
2
+ require 'twitter/enumerable'
3
+ require 'twitter/rest/request'
4
+ require 'twitter/utils'
5
+ require 'uri'
6
+
7
+ module Twitter
8
+ class PremiumSearchResults
9
+ include Twitter::Enumerable
10
+ include Twitter::Utils
11
+ # @return [Hash]
12
+ attr_reader :attrs
13
+ alias to_h attrs
14
+ alias to_hash to_h
15
+
16
+ # Initializes a new SearchResults object
17
+ #
18
+ # @param request [Twitter::REST::Request]
19
+ # @return [Twitter::PremiumSearchResults]
20
+ def initialize(request, request_config = {})
21
+ @client = request.client
22
+ @request_method = request.verb
23
+ @path = request.path
24
+ @options = request.options
25
+ @request_config = request_config
26
+ @collection = []
27
+ self.attrs = request.perform
28
+ end
29
+
30
+ private
31
+
32
+ # @return [Boolean]
33
+ def last?
34
+ !next_page?
35
+ end
36
+
37
+ # @return [Boolean]
38
+ def next_page?
39
+ !!@attrs[:next]
40
+ end
41
+
42
+ # Returns a Hash of query parameters for the next result in the search
43
+ #
44
+ # @note Returned Hash can be merged into the previous search options list to easily access the next page.
45
+ # @return [Hash] The parameters needed to fetch the next page.
46
+ def next_page
47
+ {next: @attrs[:next]} if next_page?
48
+ end
49
+
50
+ # @return [Hash]
51
+ def fetch_next_page
52
+ request = @client.premium_search(@options[:query], (@options.reject { |k| k == :query } || {}).merge(next_page), @request_config)
53
+
54
+ self.attrs = request.attrs
55
+ end
56
+
57
+ # @param attrs [Hash]
58
+ # @return [Hash]
59
+ def attrs=(attrs)
60
+ @attrs = attrs
61
+ @attrs.fetch(:results, []).collect do |tweet|
62
+ @collection << Tweet.new(tweet)
63
+ end
64
+ @attrs
65
+ end
66
+ end
67
+ end
@@ -4,8 +4,8 @@ require 'memoizable'
4
4
 
5
5
  module Twitter
6
6
  module Profile
7
- PROFILE_IMAGE_SUFFIX_REGEX = /_normal(\.gif|\.jpe?g|\.png)$/i
8
- PREDICATE_URI_METHOD_REGEX = /_uri\?$/
7
+ PROFILE_IMAGE_SUFFIX_REGEX = /_normal(\.gif|\.jpe?g|\.png)$/i.freeze
8
+ PREDICATE_URI_METHOD_REGEX = /_uri\?$/.freeze
9
9
  include Memoizable
10
10
 
11
11
  class << self
@@ -7,14 +7,14 @@ module Twitter
7
7
  # @return [Integer]
8
8
  def limit
9
9
  limit = @attrs['x-rate-limit-limit']
10
- limit.to_i if limit
10
+ limit&.to_i
11
11
  end
12
12
  memoize :limit
13
13
 
14
14
  # @return [Integer]
15
15
  def remaining
16
16
  remaining = @attrs['x-rate-limit-remaining']
17
- remaining.to_i if remaining
17
+ remaining&.to_i
18
18
  end
19
19
  memoize :remaining
20
20
 
@@ -0,0 +1,99 @@
1
+ require 'twitter/rest/request'
2
+ require 'twitter/rest/utils'
3
+ require 'twitter/utils'
4
+
5
+ module Twitter
6
+ module REST
7
+ module AccountActivity
8
+ include Twitter::REST::Utils
9
+ include Twitter::Utils
10
+
11
+ # Registers a webhook URL for all event types. The URL will be validated via CRC request before saving. In case the validation failed, returns comprehensive error message to the requester.
12
+ # @see https://developer.twitter.com/en/docs/accounts-and-users/subscribe-account-activity/api-reference
13
+ # @see https://developer.twitter.com/en/docs/accounts-and-users/subscribe-account-activity/api-reference/aaa-premium#post-account-activity-all-env-name-webhooks
14
+ # @note Create a webhook
15
+ # @rate_limited Yes
16
+ # @authentication Requires user context - all consumer and access tokens
17
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
18
+ # @return [Hash]
19
+ # @param env_name [String] Environment Name.
20
+ # @param url [String] Encoded URL for the callback endpoint.
21
+ def create_webhook(env_name, url)
22
+ perform_request(:json_post, "/1.1/account_activity/all/#{env_name}/webhooks.json?url=#{url}")
23
+ end
24
+
25
+ # Returns all environments, webhook URLs and their statuses for the authenticating app. Currently, only one webhook URL can be registered to each environment.
26
+ # @see https://developer.twitter.com/en/docs/accounts-and-users/subscribe-account-activity/api-reference/aaa-premium#get-account-activity-all-webhooks
27
+ # @note List webhooks
28
+ # @rate_limited Yes
29
+ # @authentication Requires user context - all consumer and access tokens
30
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
31
+ # @return [Hash]
32
+ # @param env_name [String] Environment Name.
33
+ def list_webhooks(env_name)
34
+ perform_request(:get, "/1.1/account_activity/all/#{env_name}/webhooks.json")
35
+ end
36
+
37
+ # Removes the webhook from the provided application's all activities configuration. The webhook ID can be accessed by making a call to GET /1.1/account_activity/all/webhooks.
38
+ # @see https://developer.twitter.com/en/docs/accounts-and-users/subscribe-account-activity/api-reference/aaa-premium#delete-account-activity-all-env-name-webhooks-webhook-id
39
+ # @note Delete a webhook
40
+ # @authentication Requires user context - all consumer and access tokens
41
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
42
+ # @return [nil]
43
+ # @param env_name [String] Environment Name.
44
+ # @param webhook_id [String] Webhook ID.
45
+ def delete_webhook(env_name, webhook_id)
46
+ perform_request(:delete, "/1.1/account_activity/all/#{env_name}/webhooks/#{webhook_id}.json")
47
+ end
48
+
49
+ # Triggers the challenge response check (CRC) for the given enviroments webhook for all activites. If the check is successful, returns 204 and reenables the webhook by setting its status to valid.
50
+ # @see https://developer.twitter.com/en/docs/accounts-and-users/subscribe-account-activity/api-reference/aaa-premium#put-account-activity-all-env-name-webhooks-webhook-id
51
+ # @note Trigger CRC check to a webhook
52
+ # @rate_limited Yes
53
+ # @authentication Requires user context - all consumer and access tokens
54
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
55
+ # @return [nil]
56
+ # @param env_name [String] Environment Name.
57
+ # @param webhook_id [String] Webhook ID.
58
+ def trigger_crc_check(env_name, webhook_id)
59
+ perform_request(:json_put, "/1.1/account_activity/all/#{env_name}/webhooks/#{webhook_id}.json")
60
+ end
61
+
62
+ # Subscribes the provided application to all events for the provided environment for all message types. After activation, all events for the requesting user will be sent to the application's webhook via POST request.
63
+ # @see https://developer.twitter.com/en/docs/accounts-and-users/subscribe-account-activity/api-reference/aaa-premium#post-account-activity-all-env-name-subscriptions
64
+ # @note Subscribe the user(whose credentials are provided) to the app so that the webhook can receive all types of events from user
65
+ # @rate_limited Yes
66
+ # @authentication Requires user context - all consumer and access tokens
67
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
68
+ # @return [nil]
69
+ # @param env_name [String] Environment Name
70
+ def create_subscription(env_name)
71
+ perform_request(:json_post, "/1.1/account_activity/all/#{env_name}/subscriptions.json")
72
+ end
73
+
74
+ # Provides a way to determine if a webhook configuration is subscribed to the provided user's events. If the provided user context has an active subscription with provided application, returns 204 OK.
75
+ # @see https://developer.twitter.com/en/docs/accounts-and-users/subscribe-account-activity/api-reference/aaa-premium#get-account-activity-all-env-name-subscriptions
76
+ # @note Check if the user is subscribed to the given app
77
+ # @rate_limited Yes
78
+ # @authentication Requires user context - all consumer and access tokens
79
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
80
+ # @return [nil]
81
+ # @param env_name [String] Environment Name
82
+ def check_subscription(env_name)
83
+ perform_request(:get, "/1.1/account_activity/all/#{env_name}/subscriptions.json")
84
+ end
85
+
86
+ # Deactivates subscription(s) for the provided user context and application for all activities. After deactivation, all events for the requesting user will no longer be sent to the webhook URL.
87
+ # @see https://developer.twitter.com/en/docs/accounts-and-users/subscribe-account-activity/api-reference/aaa-premium#delete-account-activity-all-env-name-subscriptions
88
+ # @note Deactivate a subscription, Users events will not be sent to the app
89
+ # @rate_limited Yes
90
+ # @authentication Requires user context - all consumer and access tokens
91
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
92
+ # @return [nil]
93
+ # @param env_name [String] Environment Name
94
+ def deactivate_subscription(env_name)
95
+ perform_request(:delete, "/1.1/account_activity/all/#{env_name}/subscriptions.json")
96
+ end
97
+ end
98
+ end
99
+ end
@@ -1,4 +1,6 @@
1
+ require 'twitter/rest/account_activity'
1
2
  require 'twitter/rest/direct_messages'
3
+ require 'twitter/rest/direct_messages/welcome_messages'
2
4
  require 'twitter/rest/favorites'
3
5
  require 'twitter/rest/friends_and_followers'
4
6
  require 'twitter/rest/help'
@@ -7,6 +9,7 @@ require 'twitter/rest/oauth'
7
9
  require 'twitter/rest/places_and_geo'
8
10
  require 'twitter/rest/saved_searches'
9
11
  require 'twitter/rest/search'
12
+ require 'twitter/rest/premium_search'
10
13
  require 'twitter/rest/spam_reporting'
11
14
  require 'twitter/rest/suggested_users'
12
15
  require 'twitter/rest/timelines'
@@ -20,13 +23,16 @@ module Twitter
20
23
  # @note All methods have been separated into modules and follow the same grouping used in {http://dev.twitter.com/doc the Twitter API Documentation}.
21
24
  # @see https://dev.twitter.com/overview/general/things-every-developer-should-know
22
25
  module API
26
+ include Twitter::REST::AccountActivity
23
27
  include Twitter::REST::DirectMessages
28
+ include Twitter::REST::DirectMessages::WelcomeMessages
24
29
  include Twitter::REST::Favorites
25
30
  include Twitter::REST::FriendsAndFollowers
26
31
  include Twitter::REST::Help
27
32
  include Twitter::REST::Lists
28
33
  include Twitter::REST::OAuth
29
34
  include Twitter::REST::PlacesAndGeo
35
+ include Twitter::REST::PremiumSearch
30
36
  include Twitter::REST::SavedSearches
31
37
  include Twitter::REST::Search
32
38
  include Twitter::REST::SpamReporting
@@ -1,5 +1,7 @@
1
1
  require 'twitter/arguments'
2
2
  require 'twitter/direct_message'
3
+ require 'twitter/direct_message_event'
4
+ require 'twitter/rest/upload_utils'
3
5
  require 'twitter/rest/utils'
4
6
  require 'twitter/user'
5
7
  require 'twitter/utils'
@@ -7,59 +9,103 @@ require 'twitter/utils'
7
9
  module Twitter
8
10
  module REST
9
11
  module DirectMessages
12
+ include Twitter::REST::UploadUtils
10
13
  include Twitter::REST::Utils
11
14
  include Twitter::Utils
12
15
 
13
- # Returns the 20 most recent direct messages sent to the authenticating user
14
- #
15
- # @see https://dev.twitter.com/rest/reference/get/direct_messages
16
+ # Returns all Direct Message events for the authenticated user (both sent and received) within the last 30 days. Sorted in reverse-chronological order.
17
+ # @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/list-events
18
+ # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
19
+ # @rate_limited Yes
20
+ # @authentication Requires user context
21
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
22
+ # @return [Array<Twitter::DirectMessageEvent>] Direct message events sent by and received by the authenticating user.
23
+ # @param options [Hash] A customizable set of options.
24
+ # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 50. Default is 20
25
+ # @option options [String] :cursor Specifies the cursor position of results to retrieve.
26
+ def direct_messages_events(options = {})
27
+ limit = options.fetch(:count, 20)
28
+ perform_get_with_cursor('/1.1/direct_messages/events/list.json', options.merge!(no_default_cursor: true, count: 50, limit: limit), :events, Twitter::DirectMessageEvent)
29
+ end
30
+
31
+ # Returns all Direct Messages for the authenticated user (both sent and received) within the last 30 days. Sorted in reverse-chronological order.
32
+ # @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/list-events
33
+ # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
34
+ # @rate_limited Yes
35
+ # @authentication Requires user context
36
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
37
+ # @return [Array<Twitter::DirectMessage>] Direct messages sent by and received by the authenticating user.
38
+ # @param options [Hash] A customizable set of options.
39
+ # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 50. Default is 20
40
+ # @option options [String] :cursor Specifies the cursor position of results to retrieve.
41
+ def direct_messages_list(options = {})
42
+ direct_messages_events(options).collect(&:direct_message)
43
+ end
44
+
45
+ # Returns Direct Messages received by the authenticated user within the last 30 days. Sorted in reverse-chronological order.
46
+ # @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/list-events
16
47
  # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
17
48
  # @rate_limited Yes
18
49
  # @authentication Requires user context
19
50
  # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
20
- # @return [Array<Twitter::DirectMessage>] Direct messages sent to the authenticating user.
51
+ # @return [Array<Twitter::DirectMessage>] Direct messages received by the authenticating user.
21
52
  # @param options [Hash] A customizable set of options.
22
- # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
23
- # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
24
- # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
25
- # @option options [Integer] :page Specifies the page of results to retrieve.
53
+ # @option options [Integer] :count Specifies the number of records (sent and received dms) to retrieve. Must be less than or equal to 50. Default is 50
54
+ # this count does not directly correspond to the output, as we pull sent and received messages from twitter and only present received to the user
55
+ # @option options [String] :cursor Specifies the cursor position of results to retrieve.
26
56
  def direct_messages_received(options = {})
27
- perform_get_with_objects('/1.1/direct_messages.json', options, Twitter::DirectMessage)
57
+ limit = options.fetch(:count, 20)
58
+ direct_messages_list(options).select { |dm| dm.recipient_id == user_id }.first(limit)
28
59
  end
29
60
 
30
- # Returns the 20 most recent direct messages sent by the authenticating user
31
- #
32
- # @see https://dev.twitter.com/rest/reference/get/direct_messages/sent
61
+ # Returns Direct Messages sent by the authenticated user within the last 30 days. Sorted in reverse-chronological order.
62
+ # @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/list-events
33
63
  # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
34
64
  # @rate_limited Yes
35
65
  # @authentication Requires user context
36
66
  # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
37
67
  # @return [Array<Twitter::DirectMessage>] Direct messages sent by the authenticating user.
38
68
  # @param options [Hash] A customizable set of options.
39
- # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
40
- # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
41
- # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
42
- # @option options [Integer] :page Specifies the page of results to retrieve.
69
+ # @option options [Integer] :count Specifies the number of records (sent and received dms) to retrieve. Must be less than or equal to 50. Default is 50
70
+ # this count does not directly correspond to the output, as we pull sent and received messages from twitter and only present received to the user
71
+ # @option options [String] :cursor Specifies the cursor position of results to retrieve.
43
72
  def direct_messages_sent(options = {})
44
- perform_get_with_objects('/1.1/direct_messages/sent.json', options, Twitter::DirectMessage)
73
+ limit = options.fetch(:count, 20)
74
+ direct_messages_list(options).select { |dm| dm.sender_id == user_id }.first(limit)
45
75
  end
46
76
 
47
77
  # Returns a direct message
48
78
  #
49
- # @see https://dev.twitter.com/rest/reference/get/direct_messages/show
79
+ # @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/get-event
50
80
  # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
51
81
  # @rate_limited Yes
52
82
  # @authentication Requires user context
53
83
  # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
54
- # @return [Twitter::DirectMessage] The requested messages.
84
+ # @return [Twitter::DirectMessage] The requested message.
55
85
  # @param id [Integer] A direct message ID.
56
86
  # @param options [Hash] A customizable set of options.
87
+
57
88
  def direct_message(id, options = {})
89
+ direct_message_event(id, options).direct_message
90
+ end
91
+
92
+ # Returns a direct message event
93
+ #
94
+ # @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/get-event
95
+ # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
96
+ # @rate_limited Yes
97
+ # @authentication Requires user context
98
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
99
+ # @return [Twitter::DirectMessageEvent] The requested message.
100
+ # @param id [Integer] A direct message ID.
101
+ # @param options [Hash] A customizable set of options.
102
+ def direct_message_event(id, options = {})
58
103
  options = options.dup
59
104
  options[:id] = id
60
- perform_get_with_object('/1.1/direct_messages/show.json', options, Twitter::DirectMessage)
105
+ perform_get_with_object('/1.1/direct_messages/events/show.json', options, Twitter::DirectMessageEvent)
61
106
  end
62
107
 
108
+ # Returns direct messages specified in arguments, or, if no arguments are given, returns direct messages received by authenticating user
63
109
  # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
64
110
  # @rate_limited Yes
65
111
  # @authentication Requires user context
@@ -67,13 +113,12 @@ module Twitter
67
113
  # @return [Array<Twitter::DirectMessage>] The requested messages.
68
114
  # @overload direct_messages(options = {})
69
115
  # Returns the 20 most recent direct messages sent to the authenticating user
70
- #
71
- # @see https://dev.twitter.com/rest/reference/get/direct_messages
116
+
117
+ # @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/list-events
72
118
  # @param options [Hash] A customizable set of options.
73
- # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
74
- # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
75
- # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200.
76
- # @option options [Integer] :page Specifies the page of results to retrieve.
119
+ # @option options [Integer] :count Specifies the number of records (sent and received dms) to retrieve. Must be less than or equal to 50. Default is 50
120
+ # this count does not directly correspond to the output, as we pull sent and received messages from twitter and only present received to the user
121
+ # @option options [String] :cursor Specifies the cursor position of results to retrieve.
77
122
  # @overload direct_messages(*ids)
78
123
  # Returns direct messages
79
124
  #
@@ -98,40 +143,84 @@ module Twitter
98
143
 
99
144
  # Destroys direct messages
100
145
  #
101
- # @see https://dev.twitter.com/rest/reference/post/direct_messages/destroy
146
+ # @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/delete-message-event
102
147
  # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
103
- # @rate_limited No
148
+ # @rate_limited Yes
104
149
  # @authentication Requires user context
105
150
  # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
106
- # @return [Array<Twitter::DirectMessage>] Deleted direct message.
151
+ # @return [nil] Response body from Twitter is nil if successful
107
152
  # @overload destroy_direct_message(*ids)
108
153
  # @param ids [Enumerable<Integer>] A collection of direct message IDs.
109
- # @overload destroy_direct_message(*ids, options)
110
- # @param ids [Enumerable<Integer>] A collection of direct message IDs.
111
- # @param options [Hash] A customizable set of options.
112
- def destroy_direct_message(*args)
113
- parallel_objects_from_response(Twitter::DirectMessage, :post, '/1.1/direct_messages/destroy.json', args)
154
+ def destroy_direct_message(*ids)
155
+ pmap(ids) do |id|
156
+ perform_requests(:delete, '/1.1/direct_messages/events/destroy.json', id: id)
157
+ end
158
+ nil
114
159
  end
115
160
 
116
161
  # Sends a new direct message to the specified user from the authenticating user
117
162
  #
118
- # @see https://dev.twitter.com/rest/reference/post/direct_messages/new
119
- # @rate_limited No
163
+ # @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-event
164
+ # @rate_limited Yes
120
165
  # @authentication Requires user context
121
166
  # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
122
167
  # @return [Twitter::DirectMessage] The sent message.
123
- # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
168
+ # @param user [Integer, String, Twitter::User] A Twitter user ID
124
169
  # @param text [String] The text of your direct message, up to 10,000 characters.
125
170
  # @param options [Hash] A customizable set of options.
126
- def create_direct_message(user, text, options = {})
127
- options = options.dup
128
- merge_user!(options, user)
129
- options[:text] = text
130
- perform_post_with_object('/1.1/direct_messages/new.json', options, Twitter::DirectMessage)
171
+ def create_direct_message(user_id, text, options = {})
172
+ event = perform_request_with_object(:json_post, '/1.1/direct_messages/events/new.json', format_json_options(user_id, text, options), Twitter::DirectMessageEvent)
173
+ event.direct_message
131
174
  end
132
175
  alias d create_direct_message
133
176
  alias m create_direct_message
134
177
  alias dm create_direct_message
178
+
179
+ # Create a new direct message event to the specified user from the authenticating user
180
+ #
181
+ # @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-event
182
+ # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
183
+ # @rate_limited Yes
184
+ # @authentication Requires user context
185
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
186
+ # @return [Twitter::DirectMessageEvent] The created direct message event.
187
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
188
+ # @param text [String] The text of your direct message, up to 10,000 characters.
189
+ # @param options [Hash] A customizable set of options.
190
+ def create_direct_message_event(*args)
191
+ arguments = Twitter::Arguments.new(args)
192
+ options = arguments.options.dup
193
+ options[:event] = {type: 'message_create', message_create: {target: {recipient_id: extract_id(arguments[0])}, message_data: {text: arguments[1]}}} if arguments.length >= 2
194
+ response = Twitter::REST::Request.new(self, :json_post, '/1.1/direct_messages/events/new.json', options).perform
195
+ Twitter::DirectMessageEvent.new(response[:event])
196
+ end
197
+
198
+ # Create a new direct message event to the specified user from the authenticating user with media
199
+ #
200
+ # @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-event
201
+ # @see https://developer.twitter.com/en/docs/direct-messages/message-attachments/guides/attaching-media
202
+ # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.
203
+ # @rate_limited Yes
204
+ # @authentication Requires user context
205
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
206
+ # @return [Twitter::DirectMessageEvent] The created direct message event.
207
+ # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, URI, or object.
208
+ # @param text [String] The text of your direct message, up to 10,000 characters.
209
+ # @param media [File] A media file (PNG, JPEG, GIF or MP4).
210
+ # @param options [Hash] A customizable set of options.
211
+ def create_direct_message_event_with_media(user, text, media, options = {})
212
+ media_id = upload(media, media_category_prefix: 'dm')[:media_id]
213
+ options = options.dup
214
+ options[:event] = {type: 'message_create', message_create: {target: {recipient_id: extract_id(user)}, message_data: {text: text, attachment: {type: 'media', media: {id: media_id}}}}}
215
+ response = Twitter::REST::Request.new(self, :json_post, '/1.1/direct_messages/events/new.json', options).perform
216
+ Twitter::DirectMessageEvent.new(response[:event])
217
+ end
218
+
219
+ private
220
+
221
+ def format_json_options(user_id, text, options)
222
+ {'event': {'type': 'message_create', 'message_create': {'target': {'recipient_id': user_id}, 'message_data': {'text': text}.merge(options)}}}
223
+ end
135
224
  end
136
225
  end
137
226
  end