Skip to content

Commit

Permalink
Merge pull request #94 from merefield/discard_if_interrupted_in_private
Browse files Browse the repository at this point in the history
FEATURE: Discard messages if interrupted in private
  • Loading branch information
merefield authored Jun 4, 2024
2 parents 8ea3300 + 8114fa2 commit 835fed6
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lib/discourse_chatbot/message/message_evaluation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def on_submission(submission)
if mentions_bot_name && !bot_chat_channel
bot_user.user_chat_channel_memberships.create!(chat_channel: channel, following: true)
Jobs::Chat::UpdateChannelUserCount.new.execute(chat_channel_id: channel.id)
bot_chat_channel = true
channel.reload
::DiscourseChatbot.progress_debug_message("2.6 added bot to channel")
end
Expand All @@ -60,6 +61,7 @@ def on_submission(submission)
topic_or_channel_id: channel_id,
over_quota: over_quota,
trust_level: trust_level(user.id),
human_participants_count: bot_chat_channel ? direct_message_channel_user_count - 1 : direct_message_channel_user_count,
message_body: message_contents.gsub(bot_username.downcase, '').gsub(bot_username, '')
}

Expand Down
11 changes: 11 additions & 0 deletions lib/discourse_chatbot/message/message_reply_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ def initialize(options = {})
def create
::DiscourseChatbot.progress_debug_message("5. Creating a new Chat Nessage...")
begin
if @private && @human_participants_count == 1
# latest_message_id = ::Topic.find(@topic_or_channel_id).posts.order('created_at DESC').first.id
latest_message_id = ::Chat::Message.where(chat_channel_id: @topic_or_channel_id, deleted_at: nil).order('created_at DESC').first.id

if @reply_to != latest_message_id
::DiscourseChatbot.progress_debug_message("7. The Message was discarded as there is a newer human message")
# do not create a new response if the message is not the latest
return
end
end

Chat::CreateMessage.call(
chat_channel_id: @topic_or_channel_id,
guardian: @guardian,
Expand Down
9 changes: 5 additions & 4 deletions lib/discourse_chatbot/post/post_evaluation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ def trigger_response(submission)
mentions_bot_name = post_contents.downcase =~ /@#{bot_username.downcase}\b/

explicit_reply_to_bot = false
last_post_was_bot = false
prior_user_was_bot = false

if post.post_number > 1
prior_post = ::Post.where(topic_id: topic.id).second_to_last
last_post_was_bot = prior_post.user_id == bot_user.id
last_other_posting_user_id = ::Post.where(topic_id: topic.id).order(created_at: :desc).limit(5).where.not(user_id: user.id).first.user_id
prior_user_was_bot = last_other_posting_user_id == bot_user.id

explicit_reply_to_bot = post.reply_to_user_id == bot_user.id
else
Expand All @@ -57,7 +57,7 @@ def trigger_response(submission)

::DiscourseChatbot.progress_debug_message("humans found in this convo: #{human_participants_count}")

if bot_user && (user.id > 0) && (mentions_bot_name || explicit_reply_to_bot || (last_post_was_bot && human_participants_count == 1))
if bot_user && (user.id > 0) && (mentions_bot_name || explicit_reply_to_bot ||(prior_user_was_bot && human_participants_count == 1))
opts = {
type: POST,
private: topic.archetype == Archetype.private_message,
Expand All @@ -69,6 +69,7 @@ def trigger_response(submission)
category_id: category_id,
over_quota: over_quota(user.id),
trust_level: trust_level(user.id),
human_participants_count: human_participants_count,
message_body: post_contents.gsub(bot_username.downcase, '').gsub(bot_username, '')
}
else
Expand Down
10 changes: 10 additions & 0 deletions lib/discourse_chatbot/post/post_reply_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ def create
skip_validations: true
}

if @is_private_msg && @human_participants_count == 1
latest_post_id = ::Topic.find(@topic_or_channel_id).posts.order('created_at DESC').first.id

if @reply_to != latest_post_id
::DiscourseChatbot.progress_debug_message("7. The Post was discarded as there is a newer human message")
# do not create a new response if the message is not the latest
return
end
end

if @chatbot_bot_type == "RAG" && SiteSetting.chatbot_include_inner_thoughts_in_private_messages && @is_private_msg
default_opts.merge!(raw: '[details="Inner Thoughts"]<br/>' + @inner_thoughts + '<br/>[/details]')

Expand Down
2 changes: 2 additions & 0 deletions lib/discourse_chatbot/reply_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def initialize(options = {})
@topic_or_channel_id = options[:topic_or_channel_id]
@message_body = options[:reply]
@is_private_msg = options[:is_private_msg]
@private = options[:private]
@human_participants_count = options[:human_participants_count]
@inner_thoughts = options[:inner_thoughts]
@trust_level = options[:trust_level]
@chatbot_bot_type = options[:chatbot_bot_type]
Expand Down
2 changes: 1 addition & 1 deletion plugin.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
# name: discourse-chatbot
# about: a plugin that allows you to have a conversation with a configurable chatbot in Discourse Chat, Topics and Private Messages
# version: 0.9.22
# version: 0.9.23
# authors: merefield
# url: https://github.com/merefield/discourse-chatbot

Expand Down

0 comments on commit 835fed6

Please sign in to comment.