Class: Chatrix::Components::Permissions

Inherits:
Object
  • Object
show all
Includes:
Wisper::Publisher
Defined in:
lib/chatrix/components/permissions.rb

Overview

Helper for parsing permissions in a room.

Instance Method Summary collapse

Constructor Details

#initialize(room) ⇒ Permissions

Initializes a new Permissions instance.

Parameters:

  • room (Room)

    The room that this permissions set belongs to.



14
15
16
17
18
# File 'lib/chatrix/components/permissions.rb', line 14

def initialize(room)
  @room = room
  @actions = {}
  @events = {}
end

Instance Method Details

#can?(user, action) ⇒ Boolean

Check if a user can perform an action.

Parameters:

  • user (User)

    The user to test.

  • action (Symbol)

    The action to check.

Returns:

  • (Boolean)

    true if the user can perform the action, otherwise false.



41
42
43
44
# 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) ⇒ Boolean

Check if a user can set an event.

Parameters:

  • user (User)

    The user to test.

  • event (Symbol)

    The event to check.

Returns:

  • (Boolean)

    true if the user can set the event, otherwise false.



52
53
54
55
# 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) ⇒ Object

Updates permission data.

Parameters:

  • content (Hash)

    New permission data.



22
23
24
25
26
27
28
29
30
31
32
33
# 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