Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions lib/ai_moderation/spam_scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,18 +288,50 @@ def self.build_context(post, topic = nil)
end

context << "\nPost Author Information:"
if post.user # during test we may not have a user
context << "- Username: #{post.user.username}"
context << "- Account age: #{(Time.current - post.user.created_at).to_i / 86_400} days"
context << "- Total posts: #{post.user.post_count}"
context << "- Trust level: #{post.user.trust_level}"
if user = post.user # during test we may not have a user
context << "- Username: #{user.username}\n"
context << "- Email: #{user.email}\n"
context << "- Account age: #{(Time.current - user.created_at).to_i / 86_400} days\n"
context << "- Total posts: #{user.post_count}\n"
context << "- Trust level: #{user.trust_level}\n"
if info = location_info(user)
context << "- Registration Location: #{info[:registration]}\n" if info[:registration]
context << "- Last Location: #{info[:last]}\n" if info[:last]
end
end

context << "\nPost Content (first #{MAX_RAW_SCAN_LENGTH} chars):\n"
context << post.raw[0..MAX_RAW_SCAN_LENGTH]
context.join("\n")
end

def self.location_info(user)
registration, last = nil
if user.ip_address.present?
info = DiscourseIpInfo.get(user.ip_address, resolve_hostname: true)
last = "#{info[:location]} (#{info[:organization]})" if info && info[:location].present?
end
if user.registration_ip_address.present?
info = DiscourseIpInfo.get(user.registration_ip_address, resolve_hostname: true)
registration = "#{info[:location]} (#{info[:organization]})" if info &&
info[:location].present?
end

rval = nil
if registration || last
rval = { registration: registration } if registration
if last && last != registration
rval ||= {}
rval[:last] = last
end
end

rval
rescue => e
Discourse.warn_exception(e, message: "Failed to lookup location info")
nil
end

def self.build_system_prompt(custom_instructions)
base_prompt = +<<~PROMPT
You are a spam detection system. Analyze the following post content and context.
Expand Down
26 changes: 26 additions & 0 deletions spec/lib/modules/ai_moderation/spam_scanner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,30 @@
expect(post.user.reload.silenced?).to eq(false)
end
end

it "includes location information and email in context" do
user.update!(ip_address: "1.2.3.4", registration_ip_address: "5.6.7.8")

ip_info_registration = { location: "New York", organization: "ISP1" }
ip_info_last = { location: "London", organization: "ISP2" }

DiscourseIpInfo
.stubs(:get)
.with("5.6.7.8", resolve_hostname: true)
.returns(ip_info_registration)
DiscourseIpInfo.stubs(:get).with("1.2.3.4", resolve_hostname: true).returns(ip_info_last)

prompts = nil
DiscourseAi::Completions::Llm.with_prepared_responses(
["spam", "just because"],
) do |_, _, _prompts|
prompts = _prompts
described_class.test_post(post)
end

context = prompts.first.messages[1][:content]
expect(context).to include("Registration Location: New York (ISP1)")
expect(context).to include("Last Location: London (ISP2)")
expect(context).to include(user.email)
end
end
Loading