Class: XNM::Telegram::User

Inherits:
Chat
  • Object
show all
Defined in:
lib/xnm/telegram/User.rb

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

Attributes inherited from Chat

#chat_id, #chat_obj, #on_telegram_event, #str_id

Instance Method Summary collapse

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, )
	super(handler, );

	@username   = [:username]
	@first_name = [:first_name]
	@last_name  = [:last_name]

	@casual_name = [:first_name]

	@permissions = [:permissions] || []
	@perm_state  = [:perm_state] || {}
end

Instance Attribute Details

#casual_nameObject (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_nameObject (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_nameObject (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_stateObject (readonly)

TODO:

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

#permissionsObject (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
  @permissions
end

#temp_stateObject

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

#usernameObject (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 add_permissions(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

Note:

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.

Returns:

  • (Boolean)


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 has_permission?(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.permissions_list[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

Returns:

  • (Boolean)


96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/xnm/telegram/User.rb', line 96

def has_permissions?(*targets)
	targets = targets.flatten

	return true if targets.nil?
	return true if @permissions.include? :sudo

	targets.each do |perm|
		return false unless has_permission? 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 take_permissions(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