class Chatrix::Components::Permissions

Helper for parsing permissions in a room.

Public Class Methods

new(room) click to toggle source

Initializes a new Permissions instance. @param room [Room] The room that this permissions set belongs to.

# File lib/chatrix/components/permissions.rb, line 14
def initialize(room)
  @room = room
  @actions = {}
  @events = {}
end

Public Instance Methods

can?(user, action) click to toggle source

Check if a user can perform an action.

@param user [User] The user to test. @param action [Symbol] The action to check. @return [Boolean] `true` if the user can perform the action,

otherwise `false`.
# File lib/chatrix/components/permissions.rb, line 41
def can?(user, action)
  return false unless @actions.key? action
  user.power_in(@room) >= @actions[action]
end
can_set?(user, event) click to toggle source

Check if a user can set an event.

@param user [User] The user to test. @param event [Symbol] The event to check. @return [Boolean] `true` if the user can set the event,

otherwise `false`.
# File lib/chatrix/components/permissions.rb, line 52
def can_set?(user, event)
  return false unless @events.key? event
  user.power_in(@room) >= @events[event]
end
update(content) click to toggle source

Updates permission data. @param content [Hash] New permission data.

# File lib/chatrix/components/permissions.rb, line 22
def update(content)
  @actions[:ban] = content['ban']
  @actions[:kick] = content['kick']
  @actions[:invite] = content['invite']
  @actions[:redact] = content['redact']

  content['events'].each do |event, level|
    @events[event.match(/\w+$/).to_s.to_sym] = level
  end

  broadcast :update, @room, self
end