Module: ActiveMatrix::Protocols::CS::MessageRelationships
- Defined in:
- lib/active_matrix/protocols/cs/message_relationships.rb
Overview
Handles message relationships (replies, edits, reactions, threads)
Instance Method Summary collapse
-
#edit_message(room_id, event_id, new_content, msgtype: 'm.text', **params) ⇒ Response
Edit an existing message.
-
#event_edited?(event) ⇒ Boolean
Check if an event has been edited.
-
#get_aggregated_relations(room_id, event_ids, rel_type: nil, event_type: nil, **params) ⇒ Response
Get aggregated relations for multiple events.
-
#get_edit_history(room_id, event_id, **params) ⇒ Response
Get the edit history of an event.
-
#get_latest_edit_content(event) ⇒ Hash?
Get the latest edit content for an event.
-
#get_reactions(room_id, event_id, **params) ⇒ Response
Get all reactions for an event.
-
#get_relations(room_id, event_id, rel_type: nil, event_type: nil, from: nil, to: nil, limit: nil, direction: 'b', **params) ⇒ Response
Get related events for a given event.
-
#get_thread_messages(room_id, thread_root_id, **params) ⇒ Response
Get thread messages for a root event.
-
#get_thread_root_id(event) ⇒ String?
Get the thread root ID for an event.
-
#in_thread?(event) ⇒ Boolean
Check if an event is part of a thread.
-
#remove_reaction(room_id, reaction_event_id, reason: nil, **params) ⇒ Response
Remove a reaction from an event.
-
#reply_to(room_id, event_id, content, msgtype: 'm.text', **params) ⇒ Response
Send a message as a reply to another event.
-
#send_reaction(room_id, event_id, key, **params) ⇒ Response
Send a reaction to an event.
-
#send_reference(room_id, event_id, content, msgtype: 'm.text', **params) ⇒ Response
Send a reference to another event.
-
#send_threaded_message(room_id, thread_root_id, content, msgtype: 'm.text', latest_event_id: nil, include_fallback: false, **params) ⇒ Response
Send a threaded message.
Instance Method Details
#edit_message(room_id, event_id, new_content, msgtype: 'm.text', **params) ⇒ Response
Edit an existing message
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/active_matrix/protocols/cs/message_relationships.rb', line 94 def (room_id, event_id, new_content, msgtype: 'm.text', **params) new_content = { body: new_content } if new_content.is_a?(String) new_content[:msgtype] ||= msgtype # Build the edit event content content = { body: "* #{new_content[:body]}", # Fallback with asterisk prefix msgtype: msgtype, 'm.new_content' => new_content, 'm.relates_to' => { rel_type: 'm.replace', event_id: event_id } } # Copy format fields to top level if present if new_content[:format] content[:format] = new_content[:format] content[:formatted_body] = "* #{new_content[:formatted_body]}" end (room_id, 'm.room.message', content, **params) end |
#event_edited?(event) ⇒ Boolean
Check if an event has been edited
282 283 284 |
# File 'lib/active_matrix/protocols/cs/message_relationships.rb', line 282 def event_edited?(event) event.dig(:unsigned, :'m.relations', :'m.replace').present? end |
#get_aggregated_relations(room_id, event_ids, rel_type: nil, event_type: nil, **params) ⇒ Response
Get aggregated relations for multiple events
229 230 231 232 233 234 235 236 237 |
# File 'lib/active_matrix/protocols/cs/message_relationships.rb', line 229 def get_aggregated_relations(room_id, event_ids, rel_type: nil, event_type: nil, **params) body = { event_ids: event_ids, rel_type: rel_type, event_type: event_type }.merge(params).compact request(:post, client_api_latest, "/rooms/#{room_id}/aggregations", body: body) end |
#get_edit_history(room_id, event_id, **params) ⇒ Response
Get the edit history of an event
248 249 250 |
# File 'lib/active_matrix/protocols/cs/message_relationships.rb', line 248 def get_edit_history(room_id, event_id, **params) get_relations(room_id, event_id, rel_type: 'm.replace', event_type: 'm.room.message', **params) end |
#get_latest_edit_content(event) ⇒ Hash?
Get the latest edit content for an event
290 291 292 293 294 295 296 |
# File 'lib/active_matrix/protocols/cs/message_relationships.rb', line 290 def get_latest_edit_content(event) edit_event = event.dig(:unsigned, :'m.relations', :'m.replace') return nil unless edit_event # Return the m.new_content if available, otherwise the content edit_event.dig(:content, :'m.new_content') || edit_event[:content] end |
#get_reactions(room_id, event_id, **params) ⇒ Response
Get all reactions for an event
261 262 263 |
# File 'lib/active_matrix/protocols/cs/message_relationships.rb', line 261 def get_reactions(room_id, event_id, **params) get_relations(room_id, event_id, rel_type: 'm.annotation', event_type: 'm.reaction', **params) end |
#get_relations(room_id, event_id, rel_type: nil, event_type: nil, from: nil, to: nil, limit: nil, direction: 'b', **params) ⇒ Response
Get related events for a given event
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/active_matrix/protocols/cs/message_relationships.rb', line 201 def get_relations(room_id, event_id, rel_type: nil, event_type: nil, from: nil, to: nil, limit: nil, direction: 'b', **params) query = { from: from, to: to, limit: limit, dir: direction }.merge(params).compact # Build the appropriate endpoint based on filters endpoint = "/rooms/#{room_id}/relations/#{event_id}" endpoint += "/#{rel_type}" if rel_type endpoint += "/#{event_type}" if rel_type && event_type request(:get, client_api_latest, endpoint, query: query) end |
#get_thread_messages(room_id, thread_root_id, **params) ⇒ Response
Get thread messages for a root event
274 275 276 |
# File 'lib/active_matrix/protocols/cs/message_relationships.rb', line 274 def (room_id, thread_root_id, **params) get_relations(room_id, thread_root_id, rel_type: 'm.thread', **params) end |
#get_thread_root_id(event) ⇒ String?
Get the thread root ID for an event
311 312 313 314 |
# File 'lib/active_matrix/protocols/cs/message_relationships.rb', line 311 def get_thread_root_id(event) return nil unless in_thread?(event) event.dig(:content, :'m.relates_to', :event_id) end |
#in_thread?(event) ⇒ Boolean
Check if an event is part of a thread
302 303 304 305 |
# File 'lib/active_matrix/protocols/cs/message_relationships.rb', line 302 def in_thread?(event) rel_type = event.dig(:content, :'m.relates_to', :rel_type) rel_type == 'm.thread' end |
#remove_reaction(room_id, reaction_event_id, reason: nil, **params) ⇒ Response
Remove a reaction from an event
153 154 155 |
# File 'lib/active_matrix/protocols/cs/message_relationships.rb', line 153 def remove_reaction(room_id, reaction_event_id, reason: nil, **params) redact_event(room_id, reaction_event_id, reason: reason, **params) end |
#reply_to(room_id, event_id, content, msgtype: 'm.text', **params) ⇒ Response
Send a message as a reply to another event
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/active_matrix/protocols/cs/message_relationships.rb', line 23 def reply_to(room_id, event_id, content, msgtype: 'm.text', **params) content = { body: content } if content.is_a?(String) content[:msgtype] ||= msgtype # Add the reply relationship content[:'m.relates_to'] = { 'm.in_reply_to' => { event_id: event_id } } (room_id, 'm.room.message', content, **params) end |
#send_reaction(room_id, event_id, key, **params) ⇒ Response
Send a reaction to an event
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/active_matrix/protocols/cs/message_relationships.rb', line 131 def send_reaction(room_id, event_id, key, **params) content = { 'm.relates_to' => { rel_type: 'm.annotation', event_id: event_id, key: key } } (room_id, 'm.reaction', content, **params) end |
#send_reference(room_id, event_id, content, msgtype: 'm.text', **params) ⇒ Response
Send a reference to another event
168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/active_matrix/protocols/cs/message_relationships.rb', line 168 def send_reference(room_id, event_id, content, msgtype: 'm.text', **params) content = { body: content } if content.is_a?(String) content[:msgtype] ||= msgtype content[:'m.relates_to'] = { rel_type: 'm.reference', event_id: event_id } (room_id, 'm.room.message', content, **params) end |
#send_threaded_message(room_id, thread_root_id, content, msgtype: 'm.text', latest_event_id: nil, include_fallback: false, **params) ⇒ Response
Send a threaded message
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/active_matrix/protocols/cs/message_relationships.rb', line 53 def (room_id, thread_root_id, content, msgtype: 'm.text', latest_event_id: nil, include_fallback: false, **params) content = { body: content } if content.is_a?(String) content[:msgtype] ||= msgtype # Build the thread relationship relates_to = { rel_type: 'm.thread', event_id: thread_root_id } # Add fallback for older clients if requested if include_fallback && latest_event_id relates_to[:'m.in_reply_to'] = { event_id: latest_event_id } relates_to[:is_falling_back] = true end content[:'m.relates_to'] = relates_to (room_id, 'm.room.message', content, **params) end |