Class: X::DirectMessage
- Extended by:
- DirectMessageConversations, Finders, Resources::DirectMessageConversations, Resources::Finders
- Defined in:
- x-resources/lib/x/resources/direct_message.rb
Overview
A direct message event
Constant Summary collapse
- FIELDS =
The direct message event fields the object layer requests; the sender, the participants, and the posts a message refers to come with their expansions
A minor release may add to it the fields the API adds, so that a lookup asks for them too; see Resource#hydrated? for what that means for a resource looked up with a list of fields of its own.
%w[attachments created_at dm_conversation_id entities event_type id text].freeze
- EXPANSIONS =
Every expansion available on direct message endpoints
A minor release may add to it the expansions the API adds, so that a lookup asks for them too; see Resource#hydrated? for what that means for a resource looked up with a list of expansions of its own.
%w[attachments.media_keys participant_ids referenced_posts sender_id].freeze
Instance Attribute Summary collapse
-
#attachments ⇒ Hash?
readonly
The attachment keys.
-
#created_at ⇒ Time?
readonly
The time when the event occurred.
-
#dm_conversation_id ⇒ String?
readonly
The identifier of the conversation.
-
#entities ⇒ Hash?
readonly
The entities found in the text: its URLs, hashtags, mentions, and cashtags.
-
#event_type ⇒ String?
readonly
The event type: MessageCreate, ParticipantsJoin, or ParticipantsLeave.
-
#participant_ids ⇒ Array<Integer>
readonly
The identifiers of the participants who joined or left.
-
#referenced_posts ⇒ Array<Hash>
(also: #referenced_tweets)
readonly
The referenced posts with their identifiers.
-
#sender_id ⇒ Integer?
readonly
The identifier of the sender.
-
#text ⇒ String?
readonly
The text.
Class Method Summary collapse
-
.all(client:, **params) ⇒ Cursor
The most recent direct message events across every conversation.
-
.create(user, text = nil, client:, media_ids: nil, **params) ⇒ DirectMessage
Send a direct message to a user as the authenticated user.
-
.create_group(users, text = nil, client:, media_ids: nil, **params) ⇒ DirectMessage
extended
from Resources::DirectMessageConversations
Start a group conversation, sending its first message as the authenticated user.
-
.create_in(conversation, text = nil, client:, media_ids: nil, **params) ⇒ DirectMessage
extended
from Resources::DirectMessageConversations
Send a direct message to a conversation as the authenticated user.
-
.default_params ⇒ Hash{String => Array<String>}
The default query parameters requesting every direct message field and expansion.
-
.delete(message, client:) ⇒ Boolean
Delete a direct message event as the authenticated user.
-
.find(id, client:, **params) {|problem| ... } ⇒ Resource?
extended
from Resources::Finders
Look up a resource by identifier.
-
.find!(id, client:, **params) ⇒ Resource
extended
from Resources::Finders
Look up a resource by identifier, which must exist.
-
.in(conversation, client:, **params) ⇒ Cursor
extended
from Resources::DirectMessageConversations
The direct message events of a conversation, one-to-one or group.
-
.with(user, client:, **params) ⇒ Cursor
The direct message events in the one-to-one conversation with a user.
Instance Method Summary collapse
-
#delete ⇒ Boolean
Delete this direct message event as the authenticated user.
-
#from?(user) ⇒ Boolean
Check whether a user sent this message.
-
#group? ⇒ Boolean
Check whether the message belongs to a group conversation.
-
#media ⇒ Array<Media>
The attached media, from the includes or as stubs holding only their keys.
-
#participants ⇒ Array<User>
The participants who joined or left, from the includes or as stubs.
-
#peer(user) ⇒ User?
The other participant of a one-to-one conversation, as seen by a user.
-
#references ⇒ Array<Post>
The referenced posts, resolved from the includes or built as stubs.
-
#sender ⇒ User?
The sender, resolved from the includes or as a stub holding only its identifier.
Constructor Details
This class inherits a constructor from X::Resource
Instance Attribute Details
#attachments ⇒ Hash? (readonly)
The attachment keys
204 |
# File 'x-resources/lib/x/resources/direct_message.rb', line 204 attribute :attachments, :object |
#created_at ⇒ Time? (readonly)
The time when the event occurred
161 |
# File 'x-resources/lib/x/resources/direct_message.rb', line 161 attribute :created_at, :time |
#dm_conversation_id ⇒ String? (readonly)
The identifier of the conversation
179 |
# File 'x-resources/lib/x/resources/direct_message.rb', line 179 attribute :dm_conversation_id, :conversation_id |
#entities ⇒ Hash? (readonly)
The entities found in the text: its URLs, hashtags, mentions, and cashtags
212 |
# File 'x-resources/lib/x/resources/direct_message.rb', line 212 attribute :entities, :object |
#event_type ⇒ String? (readonly)
The event type: MessageCreate, ParticipantsJoin, or ParticipantsLeave
153 |
# File 'x-resources/lib/x/resources/direct_message.rb', line 153 attribute :event_type |
#participant_ids ⇒ Array<Integer> (readonly)
The identifiers of the participants who joined or left
187 |
# File 'x-resources/lib/x/resources/direct_message.rb', line 187 attribute :participant_ids, :integers |
#referenced_posts ⇒ Array<Hash> (readonly) Also known as: referenced_tweets
The referenced posts with their identifiers
195 |
# File 'x-resources/lib/x/resources/direct_message.rb', line 195 attribute :referenced_posts, :objects, tweet_key: %w[referenced_tweets] |
#sender_id ⇒ Integer? (readonly)
The identifier of the sender
169 |
# File 'x-resources/lib/x/resources/direct_message.rb', line 169 attribute :sender_id, :integer |
#text ⇒ String? (readonly)
The text
It is the text as the API sends it, which escapes &, <, and > as &, <, and >, and it stays so throughout 1.x, so unescape it to display it.
145 |
# File 'x-resources/lib/x/resources/direct_message.rb', line 145 attribute :text |
Class Method Details
.all(client:, **params) ⇒ Cursor
The most recent direct message events across every conversation
78 79 80 |
# File 'x-resources/lib/x/resources/direct_message.rb', line 78 def all(client:, **params) Cursor.__send__(:build, self, "dm_events", client:, params: {max_results: MAX_RESULTS}.merge(params)) end |
.create(user, text = nil, client:, media_ids: nil, **params) ⇒ DirectMessage
Send a direct message to a user as the authenticated user
114 115 116 117 |
# File 'x-resources/lib/x/resources/direct_message.rb', line 114 def create(user, text = nil, client:, media_ids: nil, **params) path = "dm_conversations/with/#{Utils.id_of(user, User)}/messages" sent(client.post(path, (text, params, media_ids), **Utils::JSON_CLASSES), path, client:) end |
.create_group(users, text = nil, client:, media_ids: nil, **params) ⇒ DirectMessage Originally defined in module Resources::DirectMessageConversations
Start a group conversation, sending its first message as the authenticated user
.create_in(conversation, text = nil, client:, media_ids: nil, **params) ⇒ DirectMessage Originally defined in module Resources::DirectMessageConversations
Send a direct message to a conversation as the authenticated user
.default_params ⇒ Hash{String => Array<String>}
The default query parameters requesting every direct message field and expansion
65 66 67 68 |
# File 'x-resources/lib/x/resources/direct_message.rb', line 65 def default_params {"dm_event.fields" => FIELDS, "user.fields" => User::FIELDS, "post.fields" => Post::FIELDS, "media.fields" => Media::FIELDS, "expansions" => EXPANSIONS} end |
.delete(message, client:) ⇒ Boolean
Delete a direct message event as the authenticated user
127 128 129 130 |
# File 'x-resources/lib/x/resources/direct_message.rb', line 127 def delete(, client:) body = client.delete("dm_events/#{Utils.id_of(, self)}", **Utils::JSON_CLASSES) Utils.written(body, "deleted").eql?(true) end |
.find(id, client:, **params) {|problem| ... } ⇒ Resource? Originally defined in module Resources::Finders
Look up a resource by identifier
.find!(id, client:, **params) ⇒ Resource Originally defined in module Resources::Finders
Look up a resource by identifier, which must exist
The error it raises names the identifier looked up, whether the identifier or a resource was given. Its cause is the X::NotFound of a lookup the API answered with a 404 that reports the resource as not found.
.in(conversation, client:, **params) ⇒ Cursor Originally defined in module Resources::DirectMessageConversations
The direct message events of a conversation, one-to-one or group
.with(user, client:, **params) ⇒ Cursor
The direct message events in the one-to-one conversation with a user
91 92 93 94 |
# File 'x-resources/lib/x/resources/direct_message.rb', line 91 def with(user, client:, **params) path = "dm_conversations/with/#{Utils.id_of(user, User)}/dm_events" Cursor.__send__(:build, self, path, client:, params: {max_results: MAX_RESULTS}.merge(params)) end |
Instance Method Details
#delete ⇒ Boolean
Delete this direct message event as the authenticated user
314 315 316 |
# File 'x-resources/lib/x/resources/direct_message.rb', line 314 def delete self.class.delete(self, client: client!) end |
#from?(user) ⇒ Boolean
Check whether a user sent this message
A message that does not name its sender, as the message a new direct message returns does not, or one fetched with dm_event.fields that leave sender_id out, cannot say the user sent it, so it answers false; its sender_id is nil.
264 |
# File 'x-resources/lib/x/resources/direct_message.rb', line 264 def from?(user) = sender_id.to_s.eql?(Utils.id_of(user, User)) |
#group? ⇒ Boolean
Check whether the message belongs to a group conversation
The identifier of a one-to-one conversation joins the identifiers of its two participants with a hyphen, and the identifier of a group conversation is a number of its own.
277 278 279 280 |
# File 'x-resources/lib/x/resources/direct_message.rb', line 277 def group? conversation_id = dm_conversation_id !conversation_id.nil? && !conversation_id.include?("-") end |
#media ⇒ Array<Media>
The attached media, from the includes or as stubs holding only their keys
236 |
# File 'x-resources/lib/x/resources/direct_message.rb', line 236 references :media, :Media, key: %w[attachments media_keys] |
#participants ⇒ Array<User>
The participants who joined or left, from the includes or as stubs
228 |
# File 'x-resources/lib/x/resources/direct_message.rb', line 228 references :participants, :User, key: %w[participant_ids] |
#peer(user) ⇒ User?
The other participant of a one-to-one conversation, as seen by a user
The sender, when the user did not send the message, and otherwise the other member of the conversation, from the includes or as a stub holding only its identifier. A message that does not name its sender, as the message a new direct message returns does not, is read by its conversation alone, whose other member is the peer of a user who is one of its two. A message whose conversation the user is not one of the two members of has no peer for that user, even one another user sent, so neither has a message of a group conversation, whose identifier names none of its members.
299 300 301 302 303 304 305 306 |
# File 'x-resources/lib/x/resources/direct_message.rb', line 299 def peer(user) user_id = Utils.id_of(user, User) members = dm_conversation_id.to_s.split("-") return unless members.empty? || members.include?(user_id) return sender unless sender_id.nil? || from?(user_id) resolve(User, members.find { |id| !id.eql?(user_id) }) #: User? end |
#references ⇒ Array<Post>
The referenced posts, resolved from the includes or built as stubs
245 246 247 248 249 |
# File 'x-resources/lib/x/resources/direct_message.rb', line 245 def references referenced_posts.filter_map do |reference| resolve(Post, reference["id"]) #: Post? end.freeze end |
#sender ⇒ User?
The sender, resolved from the includes or as a stub holding only its identifier
220 |
# File 'x-resources/lib/x/resources/direct_message.rb', line 220 reference :sender, :User, key: %w[sender_id] |