Class: Chatrix::Api::RoomActions
- Inherits:
-
ApiComponent
- Object
- ApiComponent
- Chatrix::Api::RoomActions
- Defined in:
- lib/chatrix/api/room_actions.rb
Overview
Contains methods for performing actions on rooms.
Instance Method Summary collapse
-
#ban(room, user, reason) ⇒ Boolean
Kicks and bans a user from a room.
-
#create(data = {}) ⇒ String
Creates a new room on the server.
-
#forget(room) ⇒ Boolean
Forgets about a room.
-
#initialize(matrix) ⇒ RoomActions
constructor
Initializes a new RoomActions instance.
- #invite(room, data) ⇒ Object
-
#join(room, third_party_signed = nil) ⇒ String
Joins a room on the homeserver.
-
#kick(room, user, reason) ⇒ Boolean
Kicks a user from a room.
-
#leave(room) ⇒ Boolean
Leaves a room (but does not forget about it).
-
#redact(room, event, reason) ⇒ String
Redacts a room event from the server.
-
#send_html(room, html, clean = nil) ⇒ String
Sends a message formatted using HTML markup.
-
#send_message(room, content, type = 'm.text') ⇒ String
A helper method to send a simple message construct.
-
#send_message_raw(room, content, type = 'm.room.message') ⇒ String
Sends a message object to a room.
-
#send_typing(room, user, typing = true, duration = 30_000) ⇒ Boolean
Sends a message to the server informing it about a user having started or stopped typing.
-
#set_receipt(room, event, type = 'm.read', data = {}) ⇒ Boolean
Updates the marker for the given receipt type to point to the specified event.
-
#unban(room, user) ⇒ Boolean
Unbans a user from a room.
Methods inherited from ApiComponent
Constructor Details
#initialize(matrix) ⇒ RoomActions
Initializes a new RoomActions instance.
10 11 12 13 |
# File 'lib/chatrix/api/room_actions.rb', line 10 def initialize(matrix) super @transaction_id = 0 end |
Instance Method Details
#ban(room, user, reason) ⇒ Boolean
Kicks and bans a user from a room.
146 147 148 149 150 151 152 |
# File 'lib/chatrix/api/room_actions.rb', line 146 def ban(room, user, reason) make_request( :post, "/rooms/#{room}/ban", content: { reason: reason, user_id: user } ).code == 200 end |
#create(data = {}) ⇒ String
Creates a new room on the server.
57 58 59 |
# File 'lib/chatrix/api/room_actions.rb', line 57 def create(data = {}) make_request(:post, '/createRoom', content: data)['room_id'] end |
#forget(room) ⇒ Boolean
Forgets about a room.
111 112 113 |
# File 'lib/chatrix/api/room_actions.rb', line 111 def forget(room) make_request(:post, "/rooms/#{room}/forget").code == 200 end |
#invite(room, user) ⇒ Boolean #invite(room, data) ⇒ Boolean
92 93 94 95 |
# File 'lib/chatrix/api/room_actions.rb', line 92 def invite(room, data) data = { user_id: data } if data.is_a? String make_request(:post, "/rooms/#{room}/invite", content: data).code == 200 end |
#join(room, third_party_signed = nil) ⇒ String
Joins a room on the homeserver.
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/chatrix/api/room_actions.rb', line 68 def join(room, third_party_signed = nil) if third_party_signed make_request( :post, "/join/#{room}", content: { third_party_signed: third_party_signed } )['room_id'] else make_request(:post, "/join/#{room}")['room_id'] end end |
#kick(room, user, reason) ⇒ Boolean
Kicks a user from a room.
This does not ban the user, they can rejoin unless the room is invite-only, in which case they need a new invite to join back.
128 129 130 131 132 133 134 |
# File 'lib/chatrix/api/room_actions.rb', line 128 def kick(room, user, reason) make_request( :post, "/rooms/#{room}/kick", content: { reason: reason, user_id: user } ).code == 200 end |
#leave(room) ⇒ Boolean
Leaves a room (but does not forget about it).
102 103 104 |
# File 'lib/chatrix/api/room_actions.rb', line 102 def leave(room) make_request(:post, "/rooms/#{room}/leave").code == 200 end |
#redact(room, event, reason) ⇒ String
Redacts a room event from the server.
265 266 267 268 269 270 271 |
# File 'lib/chatrix/api/room_actions.rb', line 265 def redact(room, event, reason) make_request( :put, "/rooms/#{room}/redact/#{event}/#{@transaction_id += 1}", content: { reason: reason } )['event_id'] end |
#send_html(room, html, clean = nil) ⇒ String
Sends a message formatted using HTML markup.
The body
field in the content will have the HTML stripped out, and is
usually presented in clients that don't support the formatting.
The formatted_body
field in the content will contain the actual HTML
formatted message (as passed to the html
parameter).
214 215 216 217 218 219 220 221 222 |
# File 'lib/chatrix/api/room_actions.rb', line 214 def send_html(room, html, clean = nil) ( room, msgtype: 'm.text', format: 'org.matrix.custom.html', body: clean || html.gsub(%r{</?[^>]*?>}, ''), # TODO: Make this better formatted_body: html ) end |
#send_message(room, content, type = 'm.text') ⇒ String
A helper method to send a simple message construct.
191 192 193 |
# File 'lib/chatrix/api/room_actions.rb', line 191 def (room, content, type = 'm.text') room, msgtype: type, body: content end |
#send_message_raw(room, content, type = 'm.room.message') ⇒ String
Sends a message object to a room.
176 177 178 179 180 181 182 |
# File 'lib/chatrix/api/room_actions.rb', line 176 def (room, content, type = 'm.room.message') make_request( :put, "/rooms/#{room}/send/#{type}/#{@transaction_id += 1}", content: content )['event_id'] end |
#send_typing(room, user, typing = true, duration = 30_000) ⇒ Boolean
Sends a message to the server informing it about a user having started or stopped typing.
234 235 236 237 238 239 240 |
# File 'lib/chatrix/api/room_actions.rb', line 234 def send_typing(room, user, typing = true, duration = 30_000) make_request( :put, "/rooms/#{room}/typing/#{user}", content: { typingState: { typing: typing, timeout: duration } } ).code == 200 end |
#set_receipt(room, event, type = 'm.read', data = {}) ⇒ Boolean
Updates the marker for the given receipt type to point to the specified event.
251 252 253 254 255 256 257 |
# File 'lib/chatrix/api/room_actions.rb', line 251 def set_receipt(room, event, type = 'm.read', data = {}) make_request( :post, "/rooms/#{room}/receipt/#{type}/#{event}", content: data ).code == 200 end |
#unban(room, user) ⇒ Boolean
Unbans a user from a room.
160 161 162 163 |
# File 'lib/chatrix/api/room_actions.rb', line 160 def unban(room, user) make_request(:post, "/rooms/#{room}/unban", content: { user_id: user }) .code == 200 end |