Skip to content

Commit 1e4f05f

Browse files
committed
IMPROVE: initialise Chatbot quota values for those adding plugin for first time
1 parent bb9892b commit 1e4f05f

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

plugin.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22
# name: discourse-chatbot
33
# about: a plugin that allows you to have a conversation with a configurable chatbot in Discourse Chat, Topics and Private Messages
4-
# version: 1.4.5
4+
# version: 1.4.6
55
# authors: merefield
66
# url: https://github.com/merefield/discourse-chatbot
77

@@ -144,6 +144,16 @@ def progress_debug_message(message)
144144
register_user_custom_field_type(::DiscourseChatbot::CHATBOT_REMAINING_QUOTA_TOKENS_CUSTOM_FIELD, :integer)
145145
register_user_custom_field_type(::DiscourseChatbot::CHATBOT_QUERIES_QUOTA_REACH_ESCALATION_DATE_CUSTOM_FIELD, :date)
146146

147+
# Initialize Chatbot Quotas for all users as required
148+
user_count = User.count
149+
queries_field_count = UserCustomField.where(name: ::DiscourseChatbot::CHATBOT_REMAINING_QUOTA_QUERIES_CUSTOM_FIELD).count
150+
token_field_count = UserCustomField.where(name: ::DiscourseChatbot::CHATBOT_REMAINING_QUOTA_TOKENS_CUSTOM_FIELD).count
151+
pp "CHATBOT: Checking presence of Chatbot Custom Fields"
152+
if user_count > queries_field_count * 2 || user_count > token_field_count * 2
153+
pp "CHATBOT: Resetting Chatbot Quotas for all users as many users without required Chatbot Custom Fields"
154+
::DiscourseChatbot::Bot.new.reset_all_quotas
155+
end
156+
147157
add_to_serializer(:current_user, :chatbot_access) do
148158
!::DiscourseChatbot::EventEvaluation.new.trust_level(object.id).blank?
149159
end

0 commit comments

Comments
 (0)