forked from tyrbo/discourse-change-username-cooldown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.rb
65 lines (48 loc) · 2.12 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# name: discourse-change-username-cooldown
# about: Allows username change on a cooldown.
# version: 0.0.1
enabled_site_setting :change_username_enabled
after_initialize do
UsernameChanger.class_eval do
original_update_username = singleton_method(:update_username)
define_singleton_method(:update_username) do |attrs|
return original_update_username.call(**attrs) unless SiteSetting.change_username_enabled
user = User.find(attrs[:user_id])
user.user_custom_fields.find_or_initialize_by(name: 'username_changed_at').tap do |custom_field|
custom_field.value = Time.current.iso8601
custom_field.save
end
original_update_username.call(**attrs)
end
end
UserGuardian.module_eval do
original_can_edit_username = instance_method(:can_edit_username?)
define_method(:can_edit_username?) do |user|
original_result = original_can_edit_username.bind(self).(user)
return original_result unless SiteSetting.change_username_enabled
unless original_result
return false unless is_me?(user)
last_changed = user.user_custom_fields.find_by(name: 'username_changed_at')&.value
if last_changed.nil?
return true
else
last_changed = DateTime.parse(last_changed)
cooldown = SiteSetting.change_username_cooldown.days
return Time.current >= last_changed + cooldown
end
end
original_result
end
end
add_to_serializer(:user, :username_changed_at) do
object.user_custom_fields.find_by(name: 'username_changed_at')&.value
end
add_to_serializer(:user, :username_change_available_on) do
last_changed = object.user_custom_fields.find_by(name: 'username_changed_at')&.value
unless last_changed.nil?
last_changed = DateTime.parse(last_changed)
cooldown = SiteSetting.change_username_cooldown.days
last_changed + cooldown
end
end
end