Class: ActiveMatrix::Room
Overview
A class for tracking the information about a room on Matrix
Direct Known Subclasses
Constant Summary
Constants included from Util::Tinycache
Instance Attribute Summary collapse
-
#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.
-
#id ⇒ String
(also: #room_id)
readonly
The internal ID of the room.
- #on_account_data ⇒ Object readonly
- #on_ephemeral_event ⇒ Object readonly
- #on_event ⇒ Object readonly
- #on_state_event ⇒ Object readonly
Instance Method Summary collapse
- #account_data ⇒ Object
-
#add_alias(room_alias) ⇒ Boolean
Add an alias to the room.
-
#add_tag(tag, **data) ⇒ Object
Add a tag to the room.
-
#admin!(user, level: 100) ⇒ Object
Make a user an admin in the room.
-
#admin?(user, target_level: 100) ⇒ Boolean
Check if a user is an admin in the room.
-
#aliases(canonical_only: true) ⇒ Array[String]
Gets the room aliases.
-
#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(*args, reverse: false, limit: 10) ⇒ Object
Backfills messages into the room history.
-
#ban_user(user_id, reason = '') ⇒ Boolean
Bans a user from the room.
-
#canonical_alias ⇒ String?
The canonical alias of the room.
-
#creation_info ⇒ Response
Gets the room creation information.
-
#display_name ⇒ String
Gets a human-readable name for the room.
-
#dm=(direct) ⇒ Object
Mark a room as a direct (1:1) message Room.
-
#dm?(members_only: false) ⇒ Boolean
Checks if the room is a direct message / 1:1 room.
-
#get_account_data(type) ⇒ Hash
Retrieves a custom entry from the room-specific account data.
-
#get_state(type, state_key: nil) ⇒ Object
Gets a state object in the room.
-
#guest_access ⇒ :can_join, :forbidden
Gets the guest access rights for the room.
-
#guest_access=(guest_access) ⇒ Object
Sets the guest access status for the room.
-
#guest_access? ⇒ Boolean
Checks if
guest_accessis set to:can_join. -
#history_visibility ⇒ :invited, ...
Gets the history visibility of the room.
-
#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.
-
#join_rule ⇒ :public, ...
Gets the join rule for the room.
-
#join_rule=(join_rule) ⇒ Object
Sets the join rule of the room.
-
#joined_members ⇒ Array(User)
Populates and returns the #members array.
-
#kick_user(user_id, reason = '') ⇒ Boolean
Kicks a user from the room.
-
#knock_only? ⇒ Boolean
Checks if
join_ruleis set to:knock. -
#leave ⇒ Boolean
Requests to be removed from the room.
- #members ⇒ Object
-
#moderator!(user, level: 50) ⇒ Object
Make a user a moderator in the room.
-
#moderator?(user, target_level: 50) ⇒ Boolean
Check if a user is a moderator in 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.
-
#name ⇒ String?
Gets the current name of the room, querying the API if necessary.
-
#name=(name) ⇒ Object
Sets a new name on the room.
-
#power_levels ⇒ Hash
Get 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.
- #room_state ⇒ Object
-
#room_type ⇒ 'm.space', ...
Retrieves the type of the room.
-
#room_version ⇒ String
Retrieves the room version.
-
#send_audio(url, name, audio_info = {}) ⇒ Object
Sends a link to an audio clip to the room.
-
#send_custom_message(body, content = {}, msgtype: nil) ⇒ Object
Sends a customized message to the Room.
-
#send_emote(text) ⇒ Object
Sends an emote (/me) message to the room.
-
#send_event(type, content = {}) ⇒ Object
Sends a custom timeline event 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_state(type, data, state_key: nil) ⇒ Object
Sets a state object in the room.
-
#set_user_profile(display_name: nil, avatar_url: nil, reason: nil) ⇒ Object
Changes the room-specific user profile.
-
#space? ⇒ Boolean?
Checks if the room is a Matrix Space.
-
#tags ⇒ Response
Returns a list of the room tags.
- #to_s ⇒ Object
-
#to_space ⇒ Object
Casting operators.
-
#topic ⇒ String?
Gets the room topic - if any.
-
#topic=(topic) ⇒ Object
Sets a new topic on the room.
-
#unban_user(user_id) ⇒ Boolean
Unbans a user from the room.
-
#user_can_send?(user, event, state: false) ⇒ Boolean
Checks if a user can send a given event type in the room.
-
#user_powerlevel(user, use_default: true) ⇒ Integer?
Gets the power level of a user in the room.
-
#world_readable? ⇒ Boolean
(also: #world_readable)
Checks if the room history is world readable.
Methods included from Extensions
Methods included from Util::Tinycache
adapter, adapter=, cached, extended, tinycache_adapter_config
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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/active_matrix/room.rb', line 60 def initialize(client, room_id, data = {}) if client.is_a? Room copy = client client = copy.client room_id = copy.id # data = copy.attributes end raise ArgumentError, 'Must be given a Client instance' unless client.is_a? Client @client = 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? @events = [] @event_history_limit = 10 @room_type = nil @prev_batch = nil i[name topic canonical_alias avatar_url].each do |type| room_state.tinycache_adapter.write("m.room.#{type}", { type => data.delete(type) }) if data.key? type end room_state.tinycache_adapter.write('m.room.join_rules', { join_rule: data.delete(:join_rule) }) if data.key? :join_rule room_state.tinycache_adapter.write('m.room.history_visibility', { history_visibility: data.delete(:world_readable) ? :world_readable : nil }) if data.key? :world_readable data.each do |k, v| next if i[client].include? k if respond_to?(:"#{k}_cached?") && send(:"#{k}_cached?") tinycache_adapter.write(k, v) elsif instance_variable_defined? "@#{k}" instance_variable_set("@#{k}", v) end end @id = room_id.to_s logger.debug "Created room #{room_id}" end |
Instance Attribute Details
#client ⇒ Client (readonly)
Returns the client for the room.
21 |
# File 'lib/active_matrix/room.rb', line 21 attr_reader :id, :client, :events |
#event_history_limit ⇒ Fixnum
Returns the limit of events to keep in the event log.
12 13 14 |
# File 'lib/active_matrix/room.rb', line 12 def event_history_limit @event_history_limit end |
#events ⇒ Object (readonly)
Returns the value of attribute events.
21 |
# File 'lib/active_matrix/room.rb', line 21 attr_reader :id, :client, :events |
#id ⇒ String (readonly) Also known as: room_id
Returns the internal ID of the room.
21 22 23 |
# File 'lib/active_matrix/room.rb', line 21 def id @id end |
#on_account_data ⇒ Object (readonly)
130 131 132 |
# File 'lib/active_matrix/room.rb', line 130 def on_account_data ensure_room_handlers[:account_data] end |
#on_ephemeral_event ⇒ Object (readonly)
142 143 144 |
# File 'lib/active_matrix/room.rb', line 142 def on_ephemeral_event ensure_room_handlers[:ephemeral_event] end |
#on_event ⇒ Object (readonly)
124 125 126 |
# File 'lib/active_matrix/room.rb', line 124 def on_event ensure_room_handlers[:event] end |
#on_state_event ⇒ Object (readonly)
136 137 138 |
# File 'lib/active_matrix/room.rb', line 136 def on_state_event ensure_room_handlers[:state_event] end |
Instance Method Details
#account_data ⇒ Object
570 571 572 573 574 |
# File 'lib/active_matrix/room.rb', line 570 def account_data return ActiveMatrix::Util::AccountDataCache.new client, room: self if client.cache == :none @account_data ||= ActiveMatrix::Util::AccountDataCache.new client, room: self end |
#add_alias(room_alias) ⇒ Boolean
Add an alias to the room
736 737 738 739 740 |
# File 'lib/active_matrix/room.rb', line 736 def add_alias(room_alias) client.api.set_room_alias(id, room_alias) tinycache_adapter.read(:aliases) << room_alias if tinycache_adapter.exist?(:aliases) true end |
#add_tag(tag, **data) ⇒ Object
Add a tag to the room
683 684 685 686 |
# File 'lib/active_matrix/room.rb', line 683 def add_tag(tag, **data) client.api.add_user_tag(client.mxid, id, tag, data) true end |
#admin!(user, level: 100) ⇒ Object
Make a user an admin in the room
853 854 855 856 857 858 859 860 861 |
# File 'lib/active_matrix/room.rb', line 853 def admin!(user, level: 100) return true if admin?(user, target_level: level) user = user.id if user.is_a? User user = MXID.new(user.to_s) unless user.is_a? MXID raise ArgumentError, 'Must provide a valid user or MXID' unless user.user? modify_user_power_levels({ user.to_s.to_sym => level }) end |
#admin?(user, target_level: 100) ⇒ Boolean
Check if a user is an admin in the room
841 842 843 844 845 846 |
# File 'lib/active_matrix/room.rb', line 841 def admin?(user, target_level: 100) level = user_powerlevel(user, use_default: false) return false unless level level >= target_level end |
#aliases(canonical_only: true) ⇒ Array[String]
Gets the room aliases
325 326 327 328 329 330 331 |
# File 'lib/active_matrix/room.rb', line 325 def aliases(canonical_only: true) canonical = get_state('m.room.canonical_alias') rescue {} aliases = ([canonical[:alias]].compact + (canonical[:alt_aliases] || [])).uniq.sort return aliases if canonical_only (aliases + client.api.get_room_aliases(id).aliases).uniq.sort 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
199 200 201 |
# File 'lib/active_matrix/room.rb', line 199 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
772 773 774 775 |
# File 'lib/active_matrix/room.rb', line 772 def allow_guests=(allow_guests) self.guest_access = (allow_guests ? :can_join : :forbidden) allow_guests end |
#avatar_url ⇒ String?
Gets the avatar url of the room - if any
244 245 246 247 248 249 |
# File 'lib/active_matrix/room.rb', line 244 def avatar_url get_state('m.room.avatar_url')[:url] rescue MatrixNotFoundError # No avatar has been set nil end |
#avatar_url=(avatar_url) ⇒ Object
Sets a new avatar URL for the room
788 789 790 791 792 793 794 |
# File 'lib/active_matrix/room.rb', line 788 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::MXC room_state['m.room.avatar_url'] = { avatar_url: avatar_url } avatar_url end |
#backfill_messages(*args, reverse: false, limit: 10) ⇒ Object
This will trigger the ‘on_event` events as messages are added
Backfills messages into the room history
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 |
# File 'lib/active_matrix/room.rb', line 498 def (*args, reverse: false, limit: 10) # To be backwards-compatible if args.length == 2 reverse = args.first limit = args.last end 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
545 546 547 548 549 |
# File 'lib/active_matrix/room.rb', line 545 def ban_user(user_id, reason = '') user_id = user_id.id if user_id.is_a? ActiveMatrix::User client.api.ban_user(id, user_id, reason: reason) true end |
#canonical_alias ⇒ String?
Returns the canonical alias of the room.
175 176 177 178 179 |
# File 'lib/active_matrix/room.rb', line 175 def canonical_alias get_state('m.room.canonical_alias')[:alias] rescue ActiveMatrix::MatrixNotFoundError nil end |
#creation_info ⇒ Response
Gets the room creation information
614 615 616 |
# File 'lib/active_matrix/room.rb', line 614 def creation_info room_state['m.room.create'] 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.
159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/active_matrix/room.rb', line 159 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 |
#dm=(direct) ⇒ Object
Mark a room as a direct (1:1) message Room
226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/active_matrix/room.rb', line 226 def dm=(direct) rooms = client.direct_rooms dirty = false list_for_room = (rooms[id.to_s] ||= []) if direct && !list_for_room.include?(id.to_s) list_for_room << id.to_s dirty = true elsif !direct && list_for_room.include?(id.to_s) list_for_room.delete id.to_s rooms.delete id.to_s if list_for_room.empty? dirty = true end client.account_data['m.direct'] = rooms if dirty end |
#dm?(members_only: false) ⇒ Boolean
Checks if the room is a direct message / 1:1 room
219 220 221 222 223 |
# File 'lib/active_matrix/room.rb', line 219 def dm?(members_only: false) return true if !members_only && client.direct_rooms.any? { |_uid, rooms| rooms.include? id.to_s } joined_members.count <= 2 end |
#get_account_data(type) ⇒ Hash
Retrieves a custom entry from the room-specific account data
580 581 582 |
# File 'lib/active_matrix/room.rb', line 580 def get_account_data(type) account_data[type] end |
#get_state(type, state_key: nil) ⇒ Object
Gets a state object in the room
297 298 299 |
# File 'lib/active_matrix/room.rb', line 297 def get_state(type, state_key: nil) room_state[type, state_key] end |
#guest_access ⇒ :can_join, :forbidden
Gets the guest access rights for the room
264 265 266 |
# File 'lib/active_matrix/room.rb', line 264 def guest_access get_state('m.room.guest_access')[:guest_access]&.to_sym end |
#guest_access=(guest_access) ⇒ Object
Sets the guest access status for the room
780 781 782 783 |
# File 'lib/active_matrix/room.rb', line 780 def guest_access=(guest_access) room_state['m.room.guest_access'] = { guest_access: guest_access } guest_access end |
#guest_access? ⇒ Boolean
Checks if guest_access is set to :can_join
276 277 278 |
# File 'lib/active_matrix/room.rb', line 276 def guest_access? guest_access == :can_join end |
#history_visibility ⇒ :invited, ...
Gets the history visibility of the room
309 310 311 |
# File 'lib/active_matrix/room.rb', line 309 def history_visibility get_state('m.room.history_visibility')[:history_visibility]&.to_sym end |
#inspect ⇒ String
An inspect method that skips a handful of instance variables to avoid flooding the terminal with debug data.
27 |
# File 'lib/active_matrix/room.rb', line 27 ignore_inspect :client, :events, :prev_batch, :logger, :tinycache_adapter |
#invite_only=(invite_only) ⇒ Object
Sets if the room should be invite only or not
756 757 758 759 |
# File 'lib/active_matrix/room.rb', line 756 def invite_only=(invite_only) self.join_rule = invite_only ? :invite : :public invite_only end |
#invite_only? ⇒ Boolean
Checks if join_rule is set to :invite
281 282 283 |
# File 'lib/active_matrix/room.rb', line 281 def invite_only? join_rule == :invite end |
#invite_user(user_id) ⇒ Boolean
Invites a user into the room
523 524 525 526 527 |
# File 'lib/active_matrix/room.rb', line 523 def invite_user(user_id) user_id = user_id.id if user_id.is_a? ActiveMatrix::User client.api.invite_user(id, user_id) true end |
#join_rule ⇒ :public, ...
Gets the join rule for the room
271 272 273 |
# File 'lib/active_matrix/room.rb', line 271 def join_rule get_state('m.room.join_rules')[:join_rule]&.to_sym end |
#join_rule=(join_rule) ⇒ Object
Sets the join rule of the room
764 765 766 767 |
# File 'lib/active_matrix/room.rb', line 764 def join_rule=(join_rule) room_state['m.room.join_rules'] = { join_rule: join_rule } join_rule end |
#joined_members ⇒ Array(User)
Populates and returns the #members array
184 185 186 187 188 189 190 |
# File 'lib/active_matrix/room.rb', line 184 def joined_members client.api.get_room_joined_members(id)[:joined].map do |mxid, data| User.new(client, mxid.to_s, display_name: data.fetch(:display_name, nil), avatar_url: data.fetch(:avatar_url, nil)) end end |
#kick_user(user_id, reason = '') ⇒ Boolean
Kicks a user from the room
534 535 536 537 538 |
# File 'lib/active_matrix/room.rb', line 534 def kick_user(user_id, reason = '') user_id = user_id.id if user_id.is_a? ActiveMatrix::User client.api.kick_user(id, user_id, reason: reason) true end |
#knock_only? ⇒ Boolean
Checks if join_rule is set to :knock
286 287 288 |
# File 'lib/active_matrix/room.rb', line 286 def knock_only? join_rule == :knock end |
#leave ⇒ Boolean
Requests to be removed from the room
564 565 566 567 568 |
# File 'lib/active_matrix/room.rb', line 564 def leave client.api.leave_room(id) client.instance_variable_get(:@rooms).delete id true end |
#members ⇒ Object
37 |
# File 'lib/active_matrix/room.rb', line 37 alias members joined_members |
#moderator!(user, level: 50) ⇒ Object
Make a user a moderator in the room
881 882 883 884 885 886 887 888 889 |
# File 'lib/active_matrix/room.rb', line 881 def moderator!(user, level: 50) return true if moderator?(user, target_level: level) user = user.id if user.is_a? User user = MXID.new(user.to_s) unless user.is_a? MXID raise ArgumentError, 'Must provide a valid user or MXID' unless user.user? modify_user_power_levels({ user.to_s.to_sym => level }) end |
#moderator?(user, target_level: 50) ⇒ Boolean
Check if a user is a moderator in the room
869 870 871 872 873 874 |
# File 'lib/active_matrix/room.rb', line 869 def moderator?(user, target_level: 50) level = user_powerlevel(user, use_default: false) return false unless level level >= target_level end |
#modify_required_power_levels(events = nil, params = {}) ⇒ Boolean
Modifies the required power levels for actions in the room
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 |
# File 'lib/active_matrix/room.rb', line 928 def modify_required_power_levels(events = nil, params = {}) return false if events.nil? && (params.nil? || params.empty?) room_state.tinycache_adapter.expire 'm.room.power_levels' data = power_levels 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 room_state['m.room.power_levels'] = data true end |
#modify_user_power_levels(users = nil, users_default = nil) ⇒ Boolean
Modifies the power levels of the room
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 |
# File 'lib/active_matrix/room.rb', line 896 def modify_user_power_levels(users = nil, users_default = nil) return false if users.nil? && users_default.nil? room_state.tinycache_adapter.expire 'm.room.power_levels' data = power_levels data[:users_default] = users_default unless users_default.nil? if users data[:users] = {} unless data.key? :users users.each do |user, level| user = user.id if user.is_a? User user = MXID.new(user.to_s) unless user.is_a? MXID raise ArgumentError, 'Must provide a valid user or MXID' unless user.user? if level.nil? data[:users].delete(user.to_s.to_sym) else data[:users][user.to_s.to_sym] = level end end end room_state['m.room.power_levels'] = data true end |
#name ⇒ String?
Will cache the current name for 15 minutes
Gets the current name of the room, querying the API if necessary
208 209 210 211 212 213 |
# File 'lib/active_matrix/room.rb', line 208 def name get_state('m.room.name')[:name] rescue MatrixNotFoundError # No room name has been specified nil end |
#name=(name) ⇒ Object
Sets a new name on the room
704 705 706 707 |
# File 'lib/active_matrix/room.rb', line 704 def name=(name) room_state['m.room.name'] = { name: name } name end |
#power_levels ⇒ Hash
The returned power levels are cached for a minute
Get the power levels of the room
801 802 803 |
# File 'lib/active_matrix/room.rb', line 801 def power_levels get_state('m.room.power_levels') end |
#redact_message(event_id, reason = nil) ⇒ Object
Redacts a message from the room
478 479 480 481 |
# File 'lib/active_matrix/room.rb', line 478 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
693 694 695 696 697 698 |
# File 'lib/active_matrix/room.rb', line 693 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
747 748 749 750 |
# File 'lib/active_matrix/room.rb', line 747 def reload_aliases! room_state.expire('m.room.canonical_alias') clear_aliases_cache end |
#reload_name! ⇒ Boolean Also known as: refresh_name!
Reloads the name of the room
712 713 714 |
# File 'lib/active_matrix/room.rb', line 712 def reload_name! room_state.expire('m.room.name') end |
#reload_topic! ⇒ Boolean Also known as: refresh_topic!
Reloads the topic of the room
728 729 730 |
# File 'lib/active_matrix/room.rb', line 728 def reload_topic! room_state.expire('m.room.topic') end |
#remove_tag(tag) ⇒ Object
Remove a tag from the room
674 675 676 677 |
# File 'lib/active_matrix/room.rb', line 674 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
488 489 490 491 |
# File 'lib/active_matrix/room.rb', line 488 def (event_id, reason:, score: -100) client.api.report_event(id, event_id, reason: reason, score: score) true end |
#room_state ⇒ Object
290 291 292 293 294 |
# File 'lib/active_matrix/room.rb', line 290 def room_state return ActiveMatrix::Util::StateEventCache.new self if client.cache == :none @room_state ||= ActiveMatrix::Util::StateEventCache.new self end |
#room_type ⇒ 'm.space', ...
Retrieves the type of the room
621 622 623 624 |
# File 'lib/active_matrix/room.rb', line 621 def room_type # Can't change, so a permanent cache is ok @room_type ||= creation_info[:type] end |
#room_version ⇒ String
Retrieves the room version
629 630 631 632 |
# File 'lib/active_matrix/room.rb', line 629 def room_version # Can't change, so a permanent cache is ok @room_version ||= creation_info[:room_version] 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
445 446 447 |
# File 'lib/active_matrix/room.rb', line 445 def send_audio(url, name, audio_info = {}) client.api.send_content(id, url, name, 'm.audio', extra_information: audio_info) end |
#send_custom_message(body, content = {}, msgtype: nil) ⇒ Object
Sends a customized message to the Room
454 455 456 457 458 459 460 461 |
# File 'lib/active_matrix/room.rb', line 454 def (body, content = {}, msgtype: nil) content.merge!( body: body, msgtype: msgtype || 'm.text' ) client.api.(id, 'm.room.message', content) end |
#send_emote(text) ⇒ Object
Sends an emote (/me) message to the room
367 368 369 |
# File 'lib/active_matrix/room.rb', line 367 def send_emote(text) client.api.send_emote(id, text) end |
#send_event(type, content = {}) ⇒ Object
Sends a custom timeline event to the Room
470 471 472 |
# File 'lib/active_matrix/room.rb', line 470 def send_event(type, content = {}) client.api.(id, type, content) 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
381 382 383 |
# File 'lib/active_matrix/room.rb', line 381 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
353 354 355 356 357 358 359 360 361 362 |
# File 'lib/active_matrix/room.rb', line 353 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
404 405 406 |
# File 'lib/active_matrix/room.rb', line 404 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
415 416 417 |
# File 'lib/active_matrix/room.rb', line 415 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
388 389 390 |
# File 'lib/active_matrix/room.rb', line 388 def send_notice(text) client.api.send_notice(id, text) end |
#send_text(text) ⇒ Object
Sends a plain-text message to the room
340 341 342 |
# File 'lib/active_matrix/room.rb', line 340 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
432 433 434 |
# File 'lib/active_matrix/room.rb', line 432 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
588 589 590 591 |
# File 'lib/active_matrix/room.rb', line 588 def set_account_data(type, account_data) self.account_data[type] = account_data true end |
#set_state(type, data, state_key: nil) ⇒ Object
Sets a state object in the room
302 303 304 |
# File 'lib/active_matrix/room.rb', line 302 def set_state(type, data, state_key: nil) room_state[type, state_key] = data 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
598 599 600 601 602 603 604 605 606 607 608 609 |
# File 'lib/active_matrix/room.rb', line 598 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 |
#space? ⇒ Boolean?
Checks if the room is a Matrix Space
637 638 639 640 641 |
# File 'lib/active_matrix/room.rb', line 637 def space? room_type == 'm.space' rescue ActiveMatrix::MatrixForbiddenError, ActiveMatrix::MatrixNotFoundError nil end |
#tags ⇒ Response
Returns a list of the room tags
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 |
# File 'lib/active_matrix/room.rb', line 653 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 |
#to_s ⇒ Object
111 112 113 114 115 116 |
# File 'lib/active_matrix/room.rb', line 111 def to_s prefix = canonical_alias || id return "#{prefix} | #{name}" unless name.nil? prefix end |
#to_space ⇒ Object
Casting operators
105 106 107 108 109 |
# File 'lib/active_matrix/room.rb', line 105 def to_space return nil unless space? Rooms::Space.new self, nil end |
#topic ⇒ String?
Gets the room topic - if any
254 255 256 257 258 259 |
# File 'lib/active_matrix/room.rb', line 254 def topic get_state('m.room.topic')[:topic] rescue MatrixNotFoundError # No room name has been specified nil end |
#topic=(topic) ⇒ Object
Sets a new topic on the room
720 721 722 723 |
# File 'lib/active_matrix/room.rb', line 720 def topic=(topic) room_state['m.room.topic'] = { topic: topic } topic end |
#unban_user(user_id) ⇒ Boolean
Unbans a user from the room
555 556 557 558 559 |
# File 'lib/active_matrix/room.rb', line 555 def unban_user(user_id) user_id = user_id.id if user_id.is_a? ActiveMatrix::User client.api.unban_user(id, user_id) true end |
#user_can_send?(user, event, state: false) ⇒ Boolean
Checks if a user can send a given event type in the room
827 828 829 830 831 832 833 |
# File 'lib/active_matrix/room.rb', line 827 def user_can_send?(user, event, state: false) user_pl = user_powerlevel(user) event_pl = power_levels.dig(:events, event.to_s.to_sym) event_pl ||= state ? (power_levels[:state_default] || 50) : (power_levels[:events_default] || 0) user_pl >= event_pl end |
#user_powerlevel(user, use_default: true) ⇒ Integer?
Gets the power level of a user in the room
811 812 813 814 815 816 817 818 819 |
# File 'lib/active_matrix/room.rb', line 811 def user_powerlevel(user, use_default: true) user = user.id if user.is_a? User user = MXID.new(user.to_s) unless user.is_a? MXID raise ArgumentError, 'Must provide a valid User or MXID' unless user.user? level = power_levels.dig(:users, user.to_s.to_sym) level ||= power_levels[:users_default] || 0 if use_default level end |
#world_readable? ⇒ Boolean Also known as: world_readable
Checks if the room history is world readable
316 317 318 |
# File 'lib/active_matrix/room.rb', line 316 def world_readable? history_visibility == :world_readable end |