Class: X::DirectMessage
- Extended by:
- DirectMessageConversations, Finders
- Defined in:
- x-objects/lib/x/objects/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>
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.
-
.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.
-
.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
197 |
# File 'x-objects/lib/x/objects/direct_message.rb', line 197 attribute :attachments, :object |
#created_at ⇒ Time? (readonly)
The time when the event occurred
154 |
# File 'x-objects/lib/x/objects/direct_message.rb', line 154 attribute :created_at, :time |
#dm_conversation_id ⇒ String? (readonly)
The identifier of the conversation
172 |
# File 'x-objects/lib/x/objects/direct_message.rb', line 172 attribute :dm_conversation_id, :conversation_id |
#entities ⇒ Hash? (readonly)
The entities found in the text: its URLs, hashtags, mentions, and cashtags
205 |
# File 'x-objects/lib/x/objects/direct_message.rb', line 205 attribute :entities, :object |
#event_type ⇒ String? (readonly)
The event type: MessageCreate, ParticipantsJoin, or ParticipantsLeave
146 |
# File 'x-objects/lib/x/objects/direct_message.rb', line 146 attribute :event_type |
#participant_ids ⇒ Array<Integer> (readonly)
The identifiers of the participants who joined or left
180 |
# File 'x-objects/lib/x/objects/direct_message.rb', line 180 attribute :participant_ids, :integers |
#referenced_posts ⇒ Array<Hash> (readonly)
The referenced posts with their identifiers
188 |
# File 'x-objects/lib/x/objects/direct_message.rb', line 188 attribute :referenced_posts, :objects, tweet_key: %w[referenced_tweets] |
#sender_id ⇒ Integer? (readonly)
The identifier of the sender
162 |
# File 'x-objects/lib/x/objects/direct_message.rb', line 162 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.
138 |
# File 'x-objects/lib/x/objects/direct_message.rb', line 138 attribute :text |
Class Method Details
.all(client:, **params) ⇒ Cursor
The most recent direct message events across every conversation
71 72 73 |
# File 'x-objects/lib/x/objects/direct_message.rb', line 71 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
107 108 109 110 |
# File 'x-objects/lib/x/objects/direct_message.rb', line 107 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 |
.default_params ⇒ Hash{String => Array<String>}
The default query parameters requesting every direct message field and expansion
58 59 60 61 |
# File 'x-objects/lib/x/objects/direct_message.rb', line 58 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
120 121 122 123 |
# File 'x-objects/lib/x/objects/direct_message.rb', line 120 def delete(, client:) body = client.delete("dm_events/#{Utils.id_of(, self)}", **Utils::JSON_CLASSES) body.to_h.dig("data", "deleted").eql?(true) end |
.with(user, client:, **params) ⇒ Cursor
The direct message events in the one-to-one conversation with a user
84 85 86 87 |
# File 'x-objects/lib/x/objects/direct_message.rb', line 84 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
307 308 309 |
# File 'x-objects/lib/x/objects/direct_message.rb', line 307 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.
257 |
# File 'x-objects/lib/x/objects/direct_message.rb', line 257 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.
270 271 272 273 |
# File 'x-objects/lib/x/objects/direct_message.rb', line 270 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
229 |
# File 'x-objects/lib/x/objects/direct_message.rb', line 229 references :media, :Media, key: %w[attachments media_keys] |
#participants ⇒ Array<User>
The participants who joined or left, from the includes or as stubs
221 |
# File 'x-objects/lib/x/objects/direct_message.rb', line 221 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.
292 293 294 295 296 297 298 299 |
# File 'x-objects/lib/x/objects/direct_message.rb', line 292 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
238 239 240 241 242 |
# File 'x-objects/lib/x/objects/direct_message.rb', line 238 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
213 |
# File 'x-objects/lib/x/objects/direct_message.rb', line 213 reference :sender, :User, key: %w[sender_id] |