Class: MatrixSdk::Room
- Inherits:
-
Object
- Object
- MatrixSdk::Room
- Defined in:
- lib/matrix_sdk/room.rb
Overview
A class for tracking the information about a room on Matrix
Instance Attribute Summary collapse
-
#aliases ⇒ Array(String)
readonly
A list of user-set aliases for the room.
-
#canonical_alias ⇒ String?
The canonical alias of the room.
-
#client ⇒ Client
readonly
The client for the room.
-
#event_history_limit ⇒ Fixnum
The limit of events to keep in the event log.
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#guest_access ⇒ :can_join, :forbidden
The guest access for the room - either
:can_joinor:forbidden. -
#id ⇒ String
(also: #room_id)
readonly
The internal ID of the room.
-
#join_rule ⇒ :invite, :public
The join rule for the room - either
:inviteor:public. -
#members ⇒ Array(User)
readonly
The members of the room.
-
#name ⇒ String?
The user-provided name of the room.
-
#on_ephemeral_event ⇒ EventHandlerArray
readonly
The list of event handlers for only ephemeral events.
-
#on_event ⇒ EventHandlerArray
readonly
The list of event handlers for all events.
-
#on_state_event ⇒ EventHandlerArray
readonly
The list of event handlers for only state events.
-
#topic ⇒ String?
The user-provided topic of the room.
Instance Method Summary collapse
-
#add_alias(room_alias) ⇒ Boolean
Add an alias to the room.
- #add_tag(tag, params = {}) ⇒ Object
- #allow_guests=(allow_guests) ⇒ Object
-
#backfill_messages(reverse = false, limit = 10) ⇒ Object
Backfills messages into the room history.
- #ban_user(user_id, reason = '') ⇒ Object
-
#display_name ⇒ String
Gets a human-readable name for the room.
- #get_account_data(type) ⇒ Object
-
#guest_access? ⇒ Boolean
Checks if
guest_accessis set to:can_join. -
#initialize(client, room_id, data = {}) ⇒ Room
constructor
A new instance of Room.
-
#inspect ⇒ String
An inspect method that skips a handful of instance variables to avoid flooding the terminal with debug data.
- #invite_only=(invite_only) ⇒ Object
-
#invite_only? ⇒ Boolean
Checks if
join_ruleis set to:invite. -
#invite_user(user_id) ⇒ Object
User Management.
-
#joined_members ⇒ Object
Populates and returns the #members array.
- #kick_user(user_id, reason = '') ⇒ Object
- #leave ⇒ Object
- #logger ⇒ Object
- #modify_required_power_levels(events = nil, params = {}) ⇒ Object
- #modify_user_power_levels(users = nil, users_default = nil) ⇒ Object
-
#redact_message(event_id, reason = nil) ⇒ Object
Redacts a message from the room.
-
#reload_aliases! ⇒ Boolean
Reloads the list of aliases by an API query.
- #reload_name! ⇒ Object
- #reload_topic! ⇒ Object
- #remove_tag(tag) ⇒ Object
-
#send_audio(url, name, audio_info = {}) ⇒ Object
Sends a link to an audio clip to the room.
-
#send_emote(text) ⇒ Object
Sends an emote (/me) message to the room.
-
#send_file(url, name, file_info = {}) ⇒ Object
Sends a link to a generic file to the room.
-
#send_html(html, body = nil, msg_type = 'm.text') ⇒ Object
Sends a custom HTML message to the room.
-
#send_image(url, name, image_info = {}) ⇒ Object
Sends a link to an image to the room.
-
#send_location(geo_uri, name, thumbnail_url = nil, thumbnail_info = {}) ⇒ Object
Sends a location object to the room.
-
#send_notice(text) ⇒ Object
Sends a notice (bot) message to the room.
-
#send_text(text) ⇒ Object
Sends a plain-text message to the room.
-
#send_video(url, name, video_info = {}) ⇒ Object
Sends a link to a video to the room.
- #set_account_data(type, account_data) ⇒ Object
- #set_user_profile(params = {}) ⇒ Object
- #tags ⇒ Object
- #unban_user(user_id) ⇒ Object
Constructor Details
#initialize(client, room_id, data = {}) ⇒ Room
Returns a new instance of Room.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/matrix_sdk/room.rb', line 56 def initialize(client, room_id, data = {}) event_initialize @client = client @id = room_id.to_s @name = nil @topic = nil @canonical_alias = nil @aliases = [] @join_rule = nil @guest_access = nil @members = [] @events = [] @event_history_limit = 10 @prev_batch = nil data.each do |k, v| instance_variable_set("@#{k}", v) if instance_variable_defined? "@#{k}" end logger.debug "Created room #{room_id}" end |
Instance Attribute Details
#aliases ⇒ Array(String) (readonly)
Returns a list of user-set aliases for the room.
38 |
# File 'lib/matrix_sdk/room.rb', line 38 attr_reader :id, :client, :name, :topic, :aliases, :join_rule, :guest_access, :members, :events |
#canonical_alias ⇒ String?
Returns the canonical alias of the room.
10 11 12 |
# File 'lib/matrix_sdk/room.rb', line 10 def canonical_alias @canonical_alias end |
#client ⇒ Client (readonly)
Returns the client for the room.
38 |
# File 'lib/matrix_sdk/room.rb', line 38 attr_reader :id, :client, :name, :topic, :aliases, :join_rule, :guest_access, :members, :events |
#event_history_limit ⇒ Fixnum
Returns the limit of events to keep in the event log.
10 |
# File 'lib/matrix_sdk/room.rb', line 10 attr_accessor :canonical_alias, :event_history_limit |
#events ⇒ Object (readonly)
Returns the value of attribute events.
38 |
# File 'lib/matrix_sdk/room.rb', line 38 attr_reader :id, :client, :name, :topic, :aliases, :join_rule, :guest_access, :members, :events |
#guest_access ⇒ :can_join, :forbidden
Returns the guest access for the room - either :can_join or :forbidden.
38 |
# File 'lib/matrix_sdk/room.rb', line 38 attr_reader :id, :client, :name, :topic, :aliases, :join_rule, :guest_access, :members, :events |
#id ⇒ String (readonly) Also known as: room_id
Returns the internal ID of the room.
38 39 40 |
# File 'lib/matrix_sdk/room.rb', line 38 def id @id end |
#join_rule ⇒ :invite, :public
Returns the join rule for the room - either :invite or :public.
38 |
# File 'lib/matrix_sdk/room.rb', line 38 attr_reader :id, :client, :name, :topic, :aliases, :join_rule, :guest_access, :members, :events |
#members ⇒ Array(User) (readonly)
Returns the members of the room.
38 |
# File 'lib/matrix_sdk/room.rb', line 38 attr_reader :id, :client, :name, :topic, :aliases, :join_rule, :guest_access, :members, :events |
#name ⇒ String?
Returns the user-provided name of the room.
38 |
# File 'lib/matrix_sdk/room.rb', line 38 attr_reader :id, :client, :name, :topic, :aliases, :join_rule, :guest_access, :members, :events |
#on_ephemeral_event ⇒ EventHandlerArray (readonly)
Returns The list of event handlers for only ephemeral events.
46 |
# File 'lib/matrix_sdk/room.rb', line 46 events :event, :state_event, :ephemeral_event |
#on_event ⇒ EventHandlerArray (readonly)
Returns The list of event handlers for all events.
46 |
# File 'lib/matrix_sdk/room.rb', line 46 events :event, :state_event, :ephemeral_event |
#on_state_event ⇒ EventHandlerArray (readonly)
Returns The list of event handlers for only state events.
46 |
# File 'lib/matrix_sdk/room.rb', line 46 events :event, :state_event, :ephemeral_event |
#topic ⇒ String?
Returns the user-provided topic of the room.
38 |
# File 'lib/matrix_sdk/room.rb', line 38 attr_reader :id, :client, :name, :topic, :aliases, :join_rule, :guest_access, :members, :events |
Instance Method Details
#add_alias(room_alias) ⇒ Boolean
Add an alias to the room
369 370 371 372 373 374 375 |
# File 'lib/matrix_sdk/room.rb', line 369 def add_alias(room_alias) client.api.set_room_alias(id, room_alias) @aliases << room_alias true rescue MatrixError false end |
#add_tag(tag, params = {}) ⇒ Object
327 328 329 |
# File 'lib/matrix_sdk/room.rb', line 327 def add_tag(tag, params = {}) client.api.add_user_tag(client.mxid, id, tag, params) end |
#allow_guests=(allow_guests) ⇒ Object
408 409 410 411 |
# File 'lib/matrix_sdk/room.rb', line 408 def allow_guests=(allow_guests) self.guest_access = (allow_guests ? :can_join : :forbidden) @guest_access == :can_join # rubocop:disable Lint/Void end |
#backfill_messages(reverse = false, limit = 10) ⇒ Object
This will trigger the ‘on_event` events as messages are added
Backfills messages into the room history
250 251 252 253 254 255 256 257 258 |
# File 'lib/matrix_sdk/room.rb', line 250 def (reverse = false, limit = 10) data = client.api.(id, @prev_batch, direction: :b, limit: limit) events = data[:chunk] events.reverse! unless reverse events.each do |ev| put_event(ev) end end |
#ban_user(user_id, reason = '') ⇒ Object
278 279 280 281 282 283 |
# File 'lib/matrix_sdk/room.rb', line 278 def ban_user(user_id, reason = '') client.api.ban_user(id, user_id, reason: reason) true rescue MatrixError false end |
#display_name ⇒ String
This method will populate the #members list if it has to fall back to the member name generation.
Gets a human-readable name for the room
This will return #name or #canonical_alias if they’ve been set, otherwise it will query the API for members and generate a string from a subset of their names.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/matrix_sdk/room.rb', line 97 def display_name return name if name return canonical_alias if canonical_alias members = joined_members .reject { |m| m.user_id == client.mxid } .map(&:get_display_name) return members.first if members.one? return "#{members.first} and #{members.last}" if members.count == 2 return "#{members.first} and #{members.count - 1} others" if members.count > 2 'Empty Room' end |
#get_account_data(type) ⇒ Object
300 301 302 |
# File 'lib/matrix_sdk/room.rb', line 300 def get_account_data(type) client.api.get_room_account_data(client.mxid, id, type) end |
#guest_access? ⇒ Boolean
Checks if guest_access is set to :can_join
124 125 126 |
# File 'lib/matrix_sdk/room.rb', line 124 def guest_access? guest_access == :can_join end |
#inspect ⇒ String
An inspect method that skips a handful of instance variables to avoid flooding the terminal with debug data.
51 52 |
# File 'lib/matrix_sdk/room.rb', line 51 ignore_inspect :client, :members, :events, :prev_batch, :on_event, :on_state_event, :on_ephemeral_event |
#invite_only=(invite_only) ⇒ Object
396 397 398 399 |
# File 'lib/matrix_sdk/room.rb', line 396 def invite_only=(invite_only) self.join_rule = invite_only ? :invite : :public @join_rule == :invite # rubocop:disable Lint/Void end |
#invite_only? ⇒ Boolean
Checks if join_rule is set to :invite
129 130 131 |
# File 'lib/matrix_sdk/room.rb', line 129 def invite_only? join_rule == :invite end |
#invite_user(user_id) ⇒ Object
User Management
264 265 266 267 268 269 |
# File 'lib/matrix_sdk/room.rb', line 264 def invite_user(user_id) client.api.invite_user(id, user_id) true rescue MatrixError false end |
#joined_members ⇒ Object
Populates and returns the #members array
113 114 115 116 117 118 119 120 121 |
# File 'lib/matrix_sdk/room.rb', line 113 def joined_members return members unless members.empty? client.api.get_room_members(id)[:chunk].each do |chunk| next unless chunk [:content][:membership] == 'join' ensure_member(User.new(client, chunk[:state_key], display_name: chunk[:content].fetch(:displayname))) end members end |
#kick_user(user_id, reason = '') ⇒ Object
271 272 273 274 275 276 |
# File 'lib/matrix_sdk/room.rb', line 271 def kick_user(user_id, reason = '') client.api.kick_user(id, user_id, reason: reason) true rescue MatrixError false end |
#leave ⇒ Object
292 293 294 295 296 297 298 |
# File 'lib/matrix_sdk/room.rb', line 292 def leave client.api.leave_room(id) client.rooms.delete id true rescue MatrixError false end |
#logger ⇒ Object
80 81 82 |
# File 'lib/matrix_sdk/room.rb', line 80 def logger Logging.logger[self.class.name] end |
#modify_required_power_levels(events = nil, params = {}) ⇒ Object
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
# File 'lib/matrix_sdk/room.rb', line 437 def modify_required_power_levels(events = nil, params = {}) return false if events.nil? && (params.nil? || params.empty?) data = client.api.get_power_levels(id) data.merge!(params) data.delete_if { |_k, v| v.nil? } if events data[:events] = {} unless data.key? :events data[:events].merge!(events) data[:events].delete_if { |_k, v| v.nil? } end client.api.set_power.levels(id, data) rescue MatrixError false end |
#modify_user_power_levels(users = nil, users_default = nil) ⇒ Object
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/matrix_sdk/room.rb', line 420 def modify_user_power_levels(users = nil, users_default = nil) return false if users.nil? && users_default.nil? data = client.api.get_power_levels(id) data[:users_default] = users_default unless users_default.nil? if users data[:users] = {} unless data.key? :users data[:users].merge!(users) data[:users].delete_if { |_k, v| v.nil? } end client.api.set_power_levels(id, data) true rescue MatrixError false end |
#redact_message(event_id, reason = nil) ⇒ Object
Redacts a message from the room
242 243 244 |
# File 'lib/matrix_sdk/room.rb', line 242 def (event_id, reason = nil) client.api.redact_event(id, event_id, reason: reason) end |
#reload_aliases! ⇒ Boolean
The list of aliases is not sorted, ordering changes will result in alias list updates.
Reloads the list of aliases by an API query
381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/matrix_sdk/room.rb', line 381 def reload_aliases! data = client.api.get_room_state(id) new_aliases = data.select { |chunk| chunk.key?(:content) && chunk[:content].key?(:aliases) } .map { |chunk| chunk[:content][:aliases] } .flatten .reject(&:nil?) return false if new_aliases.nil? changed = new_aliases != aliases @aliases = new_aliases if changed changed rescue MatrixError false end |
#reload_name! ⇒ Object
342 343 344 345 346 347 348 349 |
# File 'lib/matrix_sdk/room.rb', line 342 def reload_name! data = client.api.get_room_name(id) changed = data[:name] != name @name = data[:name] if changed changed rescue MatrixError false end |
#reload_topic! ⇒ Object
358 359 360 361 362 363 364 365 |
# File 'lib/matrix_sdk/room.rb', line 358 def reload_topic! data = client.api.get_room_topic(id) changed = data[:topic] != topic @topic = data[:topic] if changed changed rescue MatrixError false end |
#remove_tag(tag) ⇒ Object
323 324 325 |
# File 'lib/matrix_sdk/room.rb', line 323 def remove_tag(tag) client.api.remove_user_tag(client.mxid, id, tag) end |
#send_audio(url, name, audio_info = {}) ⇒ Object
The URLs should all be of the ‘mxc://’ schema
Sends a link to an audio clip to the room
235 236 237 |
# File 'lib/matrix_sdk/room.rb', line 235 def send_audio(url, name, audio_info = {}) client.api.send_content(id, url, name, 'm.audio', extra_information: audio_info) end |
#send_emote(text) ⇒ Object
Sends an emote (/me) message to the room
163 164 165 |
# File 'lib/matrix_sdk/room.rb', line 163 def send_emote(text) client.api.send_emote(id, text) end |
#send_file(url, name, file_info = {}) ⇒ Object
The URLs should all be of the ‘mxc://’ schema
Sends a link to a generic file to the room
176 177 178 |
# File 'lib/matrix_sdk/room.rb', line 176 def send_file(url, name, file_info = {}) client.api.send_content(id, url, name, 'm.file', extra_information: file_info) end |
#send_html(html, body = nil, msg_type = 'm.text') ⇒ Object
Sends a custom HTML message to the room
150 151 152 153 154 155 156 157 158 159 |
# File 'lib/matrix_sdk/room.rb', line 150 def send_html(html, body = nil, msg_type = 'm.text') content = { body: body ? body : html.gsub(/<\/?[^>]*>/, ''), msgtype: msg_type, format: 'org.matrix.custom.html', formatted_body: html } client.api.(id, 'm.room.message', content) end |
#send_image(url, name, image_info = {}) ⇒ Object
The URLs should all be of the ‘mxc://’ schema
Sends a link to an image to the room
197 198 199 |
# File 'lib/matrix_sdk/room.rb', line 197 def send_image(url, name, image_info = {}) client.api.send_content(id, url, name, 'm.image', extra_information: image_info) end |
#send_location(geo_uri, name, thumbnail_url = nil, thumbnail_info = {}) ⇒ Object
The thumbnail URL should be of the ‘mxc://’ schema
Sends a location object to the room
207 208 209 |
# File 'lib/matrix_sdk/room.rb', line 207 def send_location(geo_uri, name, thumbnail_url = nil, thumbnail_info = {}) client.api.send_location(id, geo_uri, name, thumbnail_url: thumbnail_url, thumbnail_info: thumbnail_info) end |
#send_notice(text) ⇒ Object
Sends a notice (bot) message to the room
182 183 184 |
# File 'lib/matrix_sdk/room.rb', line 182 def send_notice(text) client.api.send_notice(id, text) end |
#send_text(text) ⇒ Object
Sends a plain-text message to the room
139 140 141 |
# File 'lib/matrix_sdk/room.rb', line 139 def send_text(text) client.api.(id, text) end |
#send_video(url, name, video_info = {}) ⇒ Object
The URLs should all be of the ‘mxc://’ schema
Sends a link to a video to the room
223 224 225 |
# File 'lib/matrix_sdk/room.rb', line 223 def send_video(url, name, video_info = {}) client.api.send_content(id, url, name, 'm.video', extra_information: video_info) end |
#set_account_data(type, account_data) ⇒ Object
304 305 306 |
# File 'lib/matrix_sdk/room.rb', line 304 def set_account_data(type, account_data) client.api.set_room_account_data(client.mxid, id, type, account_data) end |
#set_user_profile(params = {}) ⇒ Object
308 309 310 311 312 313 314 315 316 317 |
# File 'lib/matrix_sdk/room.rb', line 308 def set_user_profile(params = {}) return nil unless params[:display_name] || params[:avatar_url] data = client.api.get_membership(id, client.mxid) raise "Can't set profile if you haven't joined the room" unless data[:membership] == 'join' data[:displayname] = params[:display_name] unless params[:display_name].nil? data[:avatar_url] = params[:avatar_url] unless params[:avatar_url].nil? client.api.set_membership(id, client.mxid, 'join', params.fetch(:reason, 'Updating room profile information'), data) end |
#tags ⇒ Object
319 320 321 |
# File 'lib/matrix_sdk/room.rb', line 319 def client.api.(client.mxid, id) end |
#unban_user(user_id) ⇒ Object
285 286 287 288 289 290 |
# File 'lib/matrix_sdk/room.rb', line 285 def unban_user(user_id) client.api.unban_user(id, user_id) true rescue MatrixError false end |