Class: XNM::Telegram::User
Overview
Telegram User class.
This class is an extension of a Telegram Chat object that represents a DM with a User, and represents the user himself. This is because Telegram’s User ID is equivalent to the Chat ID of the DM with the User.
Instance Attribute Summary collapse
-
#casual_name ⇒ Object
readonly
A human readable name.
-
#first_name ⇒ Object
readonly
First name, always guaranteed to be set.
-
#last_name ⇒ Object
readonly
Last name, may not be set.
-
#perm_state ⇒ Object
readonly
Permanent user state.
-
#permissions ⇒ Object
readonly
List of permissions this user has.
-
#temp_state ⇒ Object
Temporary state.
-
#username ⇒ Object
readonly
Username, without the @.
Attributes inherited from Chat
#chat_id, #chat_obj, #on_telegram_event, #str_id
Instance Method Summary collapse
- #add_permissions(list) ⇒ Object (also: #add_permission)
-
#has_permission?(target) ⇒ Boolean
Check whether a user has a given permission.
-
#has_permissions?(*targets) ⇒ Boolean
Check if a user has all given permissions.
-
#initialize(handler, user_info) ⇒ User
constructor
Initialize a new user object.
- #take_permissions(list) ⇒ Object (also: #take_permission)
Methods inherited from Chat
#on_command, #on_message, #send_message, #tg_mention, #to_i, #to_s
Constructor Details
#initialize(handler, user_info) ⇒ User
Initialize a new user object.
Pass the handler used for this User as well as the Hash containing Telegram’s “User” Object.
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/xnm/telegram/User.rb', line 50 def initialize(handler, user_info) super(handler, user_info); @username = user_info[:username] @first_name = user_info[:first_name] @last_name = user_info[:last_name] @casual_name = user_info[:first_name] @permissions = user_info[:permissions] || [] @perm_state = user_info[:perm_state] || {} end |
Instance Attribute Details
#casual_name ⇒ Object (readonly)
A human readable name. Overrides Chat#casual_name
22 23 24 |
# File 'lib/xnm/telegram/User.rb', line 22 def casual_name @casual_name end |
#first_name ⇒ Object (readonly)
First name, always guaranteed to be set.
16 17 18 |
# File 'lib/xnm/telegram/User.rb', line 16 def first_name @first_name end |
#last_name ⇒ Object (readonly)
Last name, may not be set.
18 19 20 |
# File 'lib/xnm/telegram/User.rb', line 18 def last_name @last_name end |
#perm_state ⇒ Object (readonly)
Actually save to disk.
Permanent user state. Will be saved to disk.
40 41 42 |
# File 'lib/xnm/telegram/User.rb', line 40 def perm_state @perm_state end |
#permissions ⇒ Object (readonly)
List of permissions this user has. This Array of Strings will be used to check against a executed command, to see if the User has appropriate rights to run said command.
Note that the Handler#permissions_list will be used to expand this list, i.e. if the permissions list is: ‘{ ’admin’ => [‘basic_rights’] }‘ And the user has the ’admin’ permission, he will also have basic_rights *without them being listed in #permissions*
Use #has_permissions? to check if a user has a certain permission.
36 37 38 |
# File 'lib/xnm/telegram/User.rb', line 36 def @permissions end |
#temp_state ⇒ Object
Temporary state. Will be lost of restart, should be cleaned out and only used for intermediary work.
44 45 46 |
# File 'lib/xnm/telegram/User.rb', line 44 def temp_state @temp_state end |
#username ⇒ Object (readonly)
Username, without the @
14 15 16 |
# File 'lib/xnm/telegram/User.rb', line 14 def username @username end |
Instance Method Details
#add_permissions(list) ⇒ Object Also known as: add_permission
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/xnm/telegram/User.rb', line 63 def (list) list = [list].flatten list.each do |perm| next if perm.is_a? Symbol next if perm.is_a? String raise ArgumentError, "Permission must be String or Symbol!" end @permissions = (@permissions + list).uniq end |
#has_permission?(target) ⇒ Boolean
This will always return true if the user has the :sudo permission, use only for full admin access!
Check whether a user has a given permission.
This function will check if the given permission is in the User’s #permissions. It will also use the Handler#permissions_list to expand the user’s permissions, i.e. if the permissions list is: ‘{ ’admin’ => [‘basic_rights’] }‘ And the user has the ’admin’ permission, he will also have basic_rights *without them being listed in #permissions*
nil will return true.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/xnm/telegram/User.rb', line 123 def (target) return true if @permissions.include? :sudo return true if target.nil? unchecked = @permissions.dup checked = {} while perm = unchecked.pop next if checked[perm] return true if perm == target unchecked += @handler.[perm] || [] checked[perm] = true end false end |
#has_permissions?(*targets) ⇒ Boolean
Check if a user has all given permissions. Will run #has_permission against every permission in the list, returns false if any one permission is not met.
An empty list counts as true
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/xnm/telegram/User.rb', line 96 def (*targets) targets = targets.flatten return true if targets.nil? return true if @permissions.include? :sudo targets.each do |perm| return false unless perm end true end |
#take_permissions(list) ⇒ Object Also known as: take_permission
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/xnm/telegram/User.rb', line 77 def (list) list = [list].flatten list.each do |perm| next if perm.is_a? Symbol next if perm.is_a? String raise ArgumentError, "Permission must be String or Symbol!" end @permissions -= list; end |