Class: MatrixSdk::Room
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?
Gets the current name of the room, querying the API if necessary.
-
#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, **data) ⇒ Object
Add a tag to the room.
-
#all_members(**params) ⇒ Array(User)
Get all members (member events) in the room.
-
#allow_guests=(allow_guests) ⇒ Object
Sets if guests are allowed in the room.
-
#avatar_url ⇒ String?
Gets the avatar url of the room - if any.
-
#avatar_url=(avatar_url) ⇒ Object
Sets a new avatar URL for the room.
-
#backfill_messages(reverse = false, limit = 10) ⇒ Object
Backfills messages into the room history.
-
#ban_user(user_id, reason = '') ⇒ Boolean
Bans a user from the room.
-
#display_name ⇒ String
Gets a human-readable name for the room.
-
#get_account_data(type) ⇒ Hash
Retrieves a custom entry from the room-specific account data.
-
#guest_access? ⇒ Boolean
Checks if
guest_accessis set to:can_join. -
#initialize(client, room_id, data = {}) ⇒ Room
constructor
Create a new room instance.
-
#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
Sets if the room should be invite only or not.
-
#invite_only? ⇒ Boolean
Checks if
join_ruleis set to:invite. -
#invite_user(user_id) ⇒ Boolean
Invites a user into the room.
-
#joined_members ⇒ Array(User)
Populates and returns the #members array.
-
#kick_user(user_id, reason = '') ⇒ Boolean
Kicks a user from the room.
-
#leave ⇒ Boolean
Requests to be removed from the room.
-
#modify_required_power_levels(events = nil, params = {}) ⇒ Boolean
Modifies the required power levels for actions in the room.
-
#modify_user_power_levels(users = nil, users_default = nil) ⇒ Boolean
Modifies the power levels of the room.
-
#redact_message(event_id, reason = nil) ⇒ Object
Redacts a message from the room.
-
#reload! ⇒ Object
(also: #refresh!)
Refreshes the room state caches for name, topic, and aliases.
-
#reload_aliases! ⇒ Boolean
(also: #refresh_aliases!)
Reloads the list of aliases by an API query.
-
#reload_name! ⇒ Boolean
(also: #refresh_name!)
Reloads the name of the room.
-
#reload_topic! ⇒ Boolean
(also: #refresh_topic!)
Reloads the topic of the room.
-
#remove_tag(tag) ⇒ Object
Remove a tag from the room.
-
#report_message(event_id, reason:, score: -100)) ⇒ Object
Reports a message in the room.
-
#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, msgtype: nil, format: nil) ⇒ 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
Stores a custom entry into the room-specific account data.
-
#set_user_profile(display_name: nil, avatar_url: nil, reason: nil) ⇒ Object
Changes the room-specific user profile.
-
#tags ⇒ Response
Returns a list of the room tags.
-
#unban_user(user_id) ⇒ Boolean
Unbans a user from the room.
Methods included from Extensions
Methods included from Logging
Constructor Details
#initialize(client, room_id, data = {}) ⇒ Room
This method isn’t supposed to be used directly, rather rooms should be retrieved from the Client abstraction.
Create a new room instance
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/matrix_sdk/room.rb', line 82 def initialize(client, room_id, data = {}) raise ArgumentError, 'Must be given a Client instance' unless client.is_a? Client room_id = MXID.new room_id unless room_id.is_a?(MXID) raise ArgumentError, 'room_id must be a valid Room ID' unless room_id.room_id? event_initialize @name = nil @topic = nil @canonical_alias = nil @aliases = [] @join_rule = nil @guest_access = nil @world_readable = nil @members = [] @events = [] @members_loaded = false @event_history_limit = 10 @avatar_url = nil @prev_batch = nil data.each do |k, v| instance_variable_set("@#{k}", v) if instance_variable_defined? "@#{k}" end @client = client @id = room_id.to_s @name_checked = Time.new(0) logger.debug "Created room #{room_id}" end |
Instance Attribute Details
#aliases ⇒ Array(String) (readonly)
Returns a list of user-set aliases for the room.
43 |
# File 'lib/matrix_sdk/room.rb', line 43 attr_reader :id, :client, :topic, :aliases, :members, :events |
#canonical_alias ⇒ String?
Returns the canonical alias of the room.
15 16 17 |
# File 'lib/matrix_sdk/room.rb', line 15 def canonical_alias @canonical_alias end |
#client ⇒ Client (readonly)
Returns the client for the room.
43 |
# File 'lib/matrix_sdk/room.rb', line 43 attr_reader :id, :client, :topic, :aliases, :members, :events |
#event_history_limit ⇒ Fixnum
Returns the limit of events to keep in the event log.
15 |
# File 'lib/matrix_sdk/room.rb', line 15 attr_accessor :canonical_alias, :event_history_limit |
#events ⇒ Object (readonly)
Returns the value of attribute events.
43 |
# File 'lib/matrix_sdk/room.rb', line 43 attr_reader :id, :client, :topic, :aliases, :members, :events |
#guest_access ⇒ :can_join, :forbidden
Returns the guest access for the room - either :can_join or :forbidden.
43 |
# File 'lib/matrix_sdk/room.rb', line 43 attr_reader :id, :client, :topic, :aliases, :members, :events |
#id ⇒ String (readonly) Also known as: room_id
Returns the internal ID of the room.
43 44 45 |
# File 'lib/matrix_sdk/room.rb', line 43 def id @id end |
#join_rule ⇒ :invite, :public
Returns the join rule for the room - either :invite or :public.
43 |
# File 'lib/matrix_sdk/room.rb', line 43 attr_reader :id, :client, :topic, :aliases, :members, :events |
#members ⇒ Array(User) (readonly)
Returns the members of the room.
43 |
# File 'lib/matrix_sdk/room.rb', line 43 attr_reader :id, :client, :topic, :aliases, :members, :events |
#name ⇒ String?
Will cache the current name for 15 minutes
Gets the current name of the room, querying the API if necessary
43 |
# File 'lib/matrix_sdk/room.rb', line 43 attr_reader :id, :client, :topic, :aliases, :members, :events |
#on_ephemeral_event ⇒ EventHandlerArray (readonly)
Returns The list of event handlers for only ephemeral events.
51 |
# File 'lib/matrix_sdk/room.rb', line 51 events :event, :state_event, :ephemeral_event |
#on_event ⇒ EventHandlerArray (readonly)
Returns The list of event handlers for all events.
51 |
# File 'lib/matrix_sdk/room.rb', line 51 events :event, :state_event, :ephemeral_event |
#on_state_event ⇒ EventHandlerArray (readonly)
Returns The list of event handlers for only state events.
51 |
# File 'lib/matrix_sdk/room.rb', line 51 events :event, :state_event, :ephemeral_event |
#topic ⇒ String?
Returns the user-provided topic of the room.
43 |
# File 'lib/matrix_sdk/room.rb', line 43 attr_reader :id, :client, :topic, :aliases, :members, :events |
Instance Method Details
#add_alias(room_alias) ⇒ Boolean
Add an alias to the room
562 563 564 565 566 |
# File 'lib/matrix_sdk/room.rb', line 562 def add_alias(room_alias) client.api.set_room_alias(id, room_alias) @aliases << room_alias true end |
#add_tag(tag, **data) ⇒ Object
Add a tag to the room
495 496 497 498 |
# File 'lib/matrix_sdk/room.rb', line 495 def add_tag(tag, **data) client.api.add_user_tag(client.mxid, id, tag, data) true end |
#all_members(**params) ⇒ Array(User)
This will also count members who’ve knocked, been invited, have left, or have been banned.
Get all members (member events) in the room
167 168 169 |
# File 'lib/matrix_sdk/room.rb', line 167 def all_members(**params) client.api.get_room_members(id, **params)[:chunk].map { |ch| client.get_user(ch[:state_key]) } end |
#allow_guests=(allow_guests) ⇒ Object
Sets if guests are allowed in the room
611 612 613 614 |
# File 'lib/matrix_sdk/room.rb', line 611 def allow_guests=(allow_guests) self.guest_access = (allow_guests ? :can_join : :forbidden) @guest_access == :can_join end |
#avatar_url ⇒ String?
Gets the avatar url of the room - if any
189 190 191 192 193 194 |
# File 'lib/matrix_sdk/room.rb', line 189 def avatar_url @avatar_url ||= client.api.get_room_avatar(id).url rescue MatrixNotFoundError # No avatar has been set nil end |
#avatar_url=(avatar_url) ⇒ Object
Sets a new avatar URL for the room
627 628 629 630 631 632 633 |
# File 'lib/matrix_sdk/room.rb', line 627 def avatar_url=(avatar_url) avatar_url = URI(avatar_url) unless avatar_url.is_a? URI raise ArgumentError, 'Must be a valid MXC URL' unless avatar_url.is_a? URI::MATRIX client.api.set_room_avatar(id, avatar_url) @avatar_url = avatar_url 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
354 355 356 357 358 359 360 361 362 363 |
# File 'lib/matrix_sdk/room.rb', line 354 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 true end |
#ban_user(user_id, reason = '') ⇒ Boolean
Bans a user from the room
395 396 397 398 399 |
# File 'lib/matrix_sdk/room.rb', line 395 def ban_user(user_id, reason = '') user_id = user_id.id if user_id.is_a? MatrixSdk::User client.api.ban_user(id, user_id, reason: reason) true 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.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/matrix_sdk/room.rb', line 130 def display_name return name if name return canonical_alias if canonical_alias members = joined_members .reject { |m| m.user_id == client.mxid } .map(&: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) ⇒ Hash
Retrieves a custom entry from the room-specific account data
424 425 426 |
# File 'lib/matrix_sdk/room.rb', line 424 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
205 206 207 |
# File 'lib/matrix_sdk/room.rb', line 205 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.
56 57 |
# File 'lib/matrix_sdk/room.rb', line 56 ignore_inspect :client, :members, :events, :prev_batch, :logger, :on_event, :on_state_event, :on_ephemeral_event |
#invite_only=(invite_only) ⇒ Object
Sets if the room should be invite only or not
595 596 597 598 |
# File 'lib/matrix_sdk/room.rb', line 595 def invite_only=(invite_only) self.join_rule = invite_only ? :invite : :public @join_rule == :invite end |
#invite_only? ⇒ Boolean
Checks if join_rule is set to :invite
210 211 212 |
# File 'lib/matrix_sdk/room.rb', line 210 def invite_only? join_rule == :invite end |
#invite_user(user_id) ⇒ Boolean
Invites a user into the room
373 374 375 376 377 |
# File 'lib/matrix_sdk/room.rb', line 373 def invite_user(user_id) user_id = user_id.id if user_id.is_a? MatrixSdk::User client.api.invite_user(id, user_id) true end |
#joined_members ⇒ Array(User)
Populates and returns the #members array
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/matrix_sdk/room.rb', line 148 def joined_members return members if @members_loaded && !members.empty? client.api.get_room_joined_members(id)[:joined].each do |mxid, data| ensure_member(User.new(client, mxid.to_s, display_name: data.fetch(:display_name, nil), avatar_url: data.fetch(:avatar_url, nil))) end @members_loaded = true members end |
#kick_user(user_id, reason = '') ⇒ Boolean
Kicks a user from the room
384 385 386 387 388 |
# File 'lib/matrix_sdk/room.rb', line 384 def kick_user(user_id, reason = '') user_id = user_id.id if user_id.is_a? MatrixSdk::User client.api.kick_user(id, user_id, reason: reason) true end |
#leave ⇒ Boolean
Requests to be removed from the room
414 415 416 417 418 |
# File 'lib/matrix_sdk/room.rb', line 414 def leave client.api.leave_room(id) client.instance_variable_get(:@rooms).delete id true end |
#modify_required_power_levels(events = nil, params = {}) ⇒ Boolean
Modifies the required power levels for actions in the room
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 |
# File 'lib/matrix_sdk/room.rb', line 661 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) true end |
#modify_user_power_levels(users = nil, users_default = nil) ⇒ Boolean
Modifies the power levels of the room
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 |
# File 'lib/matrix_sdk/room.rb', line 640 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 end |
#redact_message(event_id, reason = nil) ⇒ Object
Redacts a message from the room
334 335 336 337 |
# File 'lib/matrix_sdk/room.rb', line 334 def (event_id, reason = nil) client.api.redact_event(id, event_id, reason: reason) true end |
#reload! ⇒ Object Also known as: refresh!
Refreshes the room state caches for name, topic, and aliases
505 506 507 508 509 510 |
# File 'lib/matrix_sdk/room.rb', line 505 def reload! reload_name! reload_topic! reload_aliases! true end |
#reload_aliases! ⇒ Boolean Also known as: refresh_aliases!
The list of aliases is not sorted, ordering changes will result in alias list updates.
Reloads the list of aliases by an API query
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 |
# File 'lib/matrix_sdk/room.rb', line 573 def reload_aliases! begin new_aliases = client.api.get_room_aliases(id).aliases rescue MatrixNotFoundError data = client.api.get_room_state_all(id) new_aliases = data.select { |chunk| chunk[:type] == 'm.room.aliases' && chunk.key?(:content) && chunk[:content].key?(:aliases) } .map { |chunk| chunk[:content][:aliases] } .flatten .compact end return false if new_aliases.nil? changed = new_aliases != aliases @aliases = new_aliases if changed changed end |
#reload_name! ⇒ Boolean Also known as: refresh_name!
Reloads the name of the room
524 525 526 527 528 529 530 531 532 533 |
# File 'lib/matrix_sdk/room.rb', line 524 def reload_name! data = begin client.api.get_room_name(id) rescue MatrixNotFoundError nil end changed = data[:name] != @name @name = data[:name] if changed changed end |
#reload_topic! ⇒ Boolean Also known as: refresh_topic!
Reloads the topic of the room
547 548 549 550 551 552 553 554 555 556 |
# File 'lib/matrix_sdk/room.rb', line 547 def reload_topic! data = begin client.api.get_room_topic(id) rescue MatrixNotFoundError nil end changed = data[:topic] != @topic @topic = data[:topic] if changed changed end |
#remove_tag(tag) ⇒ Object
Remove a tag from the room
486 487 488 489 |
# File 'lib/matrix_sdk/room.rb', line 486 def remove_tag(tag) client.api.remove_user_tag(client.mxid, id, tag) true end |
#report_message(event_id, reason:, score: -100)) ⇒ Object
Reports a message in the room
344 345 346 347 |
# File 'lib/matrix_sdk/room.rb', line 344 def (event_id, reason:, score: -100) client.api.report_event(id, event_id, reason: reason, score: score) true 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
326 327 328 |
# File 'lib/matrix_sdk/room.rb', line 326 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
248 249 250 |
# File 'lib/matrix_sdk/room.rb', line 248 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
262 263 264 |
# File 'lib/matrix_sdk/room.rb', line 262 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, msgtype: nil, format: nil) ⇒ Object
Sends a custom HTML message to the room
234 235 236 237 238 239 240 241 242 243 |
# File 'lib/matrix_sdk/room.rb', line 234 def send_html(html, body = nil, msgtype: nil, format: nil) content = { body: body || html.gsub(/<\/?[^>]*>/, ''), msgtype: msgtype || 'm.text', format: 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
285 286 287 |
# File 'lib/matrix_sdk/room.rb', line 285 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
296 297 298 |
# File 'lib/matrix_sdk/room.rb', line 296 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
269 270 271 |
# File 'lib/matrix_sdk/room.rb', line 269 def send_notice(text) client.api.send_notice(id, text) end |
#send_text(text) ⇒ Object
Sends a plain-text message to the room
221 222 223 |
# File 'lib/matrix_sdk/room.rb', line 221 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
313 314 315 |
# File 'lib/matrix_sdk/room.rb', line 313 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
Stores a custom entry into the room-specific account data
432 433 434 435 |
# File 'lib/matrix_sdk/room.rb', line 432 def set_account_data(type, account_data) client.api.set_room_account_data(client.mxid, id, type, account_data) true end |
#set_user_profile(display_name: nil, avatar_url: nil, reason: nil) ⇒ Object
the avatar URL should be a mxc:// URI
Changes the room-specific user profile
442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/matrix_sdk/room.rb', line 442 def set_user_profile(display_name: nil, avatar_url: nil, reason: nil) return nil unless display_name || 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] = display_name unless display_name.nil? data[:avatar_url] = avatar_url unless avatar_url.nil? client.api.set_membership(id, client.mxid, 'join', reason || 'Updating room profile information', data) true end |
#tags ⇒ Response
Returns a list of the room tags
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
# File 'lib/matrix_sdk/room.rb', line 465 def client.api.(client.mxid, id)[:tags].tap do |tag_obj| tag_obj.instance_variable_set(:@room, self) tag_obj.define_singleton_method(:room) do @room end tag_obj.define_singleton_method(:add) do |tag, **data| @room.add_tag(tag.to_s.to_sym, data) self[tag.to_s.to_sym] = data self end tag_obj.define_singleton_method(:remove) do |tag| @room.remove_tag(tag.to_s.to_sym) delete tag.to_s.to_sym end end end |
#unban_user(user_id) ⇒ Boolean
Unbans a user from the room
405 406 407 408 409 |
# File 'lib/matrix_sdk/room.rb', line 405 def unban_user(user_id) user_id = user_id.id if user_id.is_a? MatrixSdk::User client.api.unban_user(id, user_id) true end |