FIX: allow scanning of trashed posts and deleted users for test (#1024)
When a post is trashed we should still be allowed to scan it post.topic will be nil for a trashed topic even if post is trashed
This commit is contained in:
parent
47f5da7e42
commit
47c1ea337e
|
@ -124,7 +124,7 @@ module DiscourseAi
|
||||||
llm_model = llm_id ? LlmModel.find(llm_id) : settings.llm_model
|
llm_model = llm_id ? LlmModel.find(llm_id) : settings.llm_model
|
||||||
llm = llm_model.to_llm
|
llm = llm_model.to_llm
|
||||||
custom_instructions = custom_instructions || settings.custom_instructions.presence
|
custom_instructions = custom_instructions || settings.custom_instructions.presence
|
||||||
context = build_context(post)
|
context = build_context(post, post.topic || Topic.with_deleted.find_by(id: post.topic_id))
|
||||||
prompt = completion_prompt(post, context: context, custom_instructions: custom_instructions)
|
prompt = completion_prompt(post, context: context, custom_instructions: custom_instructions)
|
||||||
|
|
||||||
result =
|
result =
|
||||||
|
@ -247,34 +247,39 @@ module DiscourseAi
|
||||||
(result.present? && result.strip.downcase.start_with?("spam"))
|
(result.present? && result.strip.downcase.start_with?("spam"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.build_context(post)
|
def self.build_context(post, topic = nil)
|
||||||
|
topic ||= post.topic
|
||||||
context = []
|
context = []
|
||||||
|
|
||||||
# Clear distinction between reply and new topic
|
# Clear distinction between reply and new topic
|
||||||
if post.is_first_post?
|
if post.is_first_post?
|
||||||
context << "NEW TOPIC POST ANALYSIS"
|
context << "NEW TOPIC POST ANALYSIS"
|
||||||
context << "- Topic title: #{post.topic.title}"
|
context << "- Topic title: #{topic.title}"
|
||||||
context << "- Category: #{post.topic.category&.name}"
|
context << "- Category: #{topic.category&.name}"
|
||||||
else
|
else
|
||||||
context << "REPLY POST ANALYSIS"
|
context << "REPLY POST ANALYSIS"
|
||||||
context << "- In topic: #{post.topic.title}"
|
context << "- In topic: #{topic.title}"
|
||||||
context << "- Category: #{post.topic.category&.name}"
|
context << "- Category: #{topic.category&.name}"
|
||||||
context << "- Topic started by: #{post.topic.user.username}"
|
context << "- Topic started by: #{topic.user&.username}"
|
||||||
|
|
||||||
# Include parent post context for replies
|
if post.reply_to_post_number.present?
|
||||||
if post.reply_to_post.present?
|
parent =
|
||||||
parent = post.reply_to_post
|
Post.with_deleted.find_by(topic_id: topic.id, post_number: post.reply_to_post_number)
|
||||||
context << "\nReplying to #{parent.user.username}'s post:"
|
if parent
|
||||||
context << "#{parent.raw[0..500]}..." if parent.raw.length > 500
|
context << "\nReplying to #{parent.user&.username}'s post:"
|
||||||
context << parent.raw if parent.raw.length <= 500
|
context << "#{parent.raw[0..500]}..." if parent.raw.length > 500
|
||||||
|
context << parent.raw if parent.raw.length <= 500
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context << "\nPost Author Information:"
|
context << "\nPost Author Information:"
|
||||||
context << "- Username: #{post.user.username}"
|
if post.user # during test we may not have a user
|
||||||
context << "- Account age: #{(Time.current - post.user.created_at).to_i / 86_400} days"
|
context << "- Username: #{post.user.username}"
|
||||||
context << "- Total posts: #{post.user.post_count}"
|
context << "- Account age: #{(Time.current - post.user.created_at).to_i / 86_400} days"
|
||||||
context << "- Trust level: #{post.user.trust_level}"
|
context << "- Total posts: #{post.user.post_count}"
|
||||||
|
context << "- Trust level: #{post.user.trust_level}"
|
||||||
|
end
|
||||||
|
|
||||||
context << "\nPost Content (first #{MAX_RAW_SCAN_LENGTH} chars):\n"
|
context << "\nPost Content (first #{MAX_RAW_SCAN_LENGTH} chars):\n"
|
||||||
context << post.raw[0..MAX_RAW_SCAN_LENGTH]
|
context << post.raw[0..MAX_RAW_SCAN_LENGTH]
|
||||||
|
|
|
@ -120,7 +120,11 @@ RSpec.describe DiscourseAi::Admin::AiSpamController do
|
||||||
|
|
||||||
before { sign_in(admin) }
|
before { sign_in(admin) }
|
||||||
|
|
||||||
it "can scan using post url" do
|
it "can scan using post url (even when trashed and user deleted)" do
|
||||||
|
User.where(id: spam_post2.user_id).delete_all
|
||||||
|
spam_post2.topic.trash!
|
||||||
|
spam_post2.trash!
|
||||||
|
|
||||||
llm2 = Fabricate(:llm_model, name: "DiffLLM")
|
llm2 = Fabricate(:llm_model, name: "DiffLLM")
|
||||||
|
|
||||||
DiscourseAi::Completions::Llm.with_prepared_responses(["spam", "just because"]) do
|
DiscourseAi::Completions::Llm.with_prepared_responses(["spam", "just because"]) do
|
||||||
|
|
Loading…
Reference in New Issue