@@ -124,7 +124,7 @@ def self.test_post(post, custom_instructions: nil, llm_id: nil)
124124 llm_model = llm_id ? LlmModel . find ( llm_id ) : settings . llm_model
125125 llm = llm_model . to_llm
126126 custom_instructions = custom_instructions || settings . custom_instructions . presence
127- context = build_context ( post )
127+ context = build_context ( post , post . topic || Topic . with_deleted . find_by ( id : post . topic_id ) )
128128 prompt = completion_prompt ( post , context : context , custom_instructions : custom_instructions )
129129
130130 result =
@@ -247,34 +247,39 @@ def self.check_if_spam(result)
247247 ( result . present? && result . strip . downcase . start_with? ( "spam" ) )
248248 end
249249
250- def self . build_context ( post )
250+ def self . build_context ( post , topic = nil )
251+ topic ||= post . topic
251252 context = [ ]
252253
253254 # Clear distinction between reply and new topic
254255 if post . is_first_post?
255256 context << "NEW TOPIC POST ANALYSIS"
256- context << "- Topic title: #{ post . topic . title } "
257- context << "- Category: #{ post . topic . category &.name } "
257+ context << "- Topic title: #{ topic . title } "
258+ context << "- Category: #{ topic . category &.name } "
258259 else
259260 context << "REPLY POST ANALYSIS"
260- context << "- In topic: #{ post . topic . title } "
261- context << "- Category: #{ post . topic . category &.name } "
262- context << "- Topic started by: #{ post . topic . user . username } "
263-
264- # Include parent post context for replies
265- if post . reply_to_post . present?
266- parent = post . reply_to_post
267- context << "\n Replying to #{ parent . user . username } 's post:"
268- context << "#{ parent . raw [ 0 ..500 ] } ..." if parent . raw . length > 500
269- context << parent . raw if parent . raw . length <= 500
261+ context << "- In topic: #{ topic . title } "
262+ context << "- Category: #{ topic . category &.name } "
263+ context << "- Topic started by: #{ topic . user &.username } "
264+
265+ if post . reply_to_post_number . present?
266+ parent =
267+ Post . with_deleted . find_by ( topic_id : topic . id , post_number : post . reply_to_post_number )
268+ if parent
269+ context << "\n Replying to #{ parent . user &.username } 's post:"
270+ context << "#{ parent . raw [ 0 ..500 ] } ..." if parent . raw . length > 500
271+ context << parent . raw if parent . raw . length <= 500
272+ end
270273 end
271274 end
272275
273276 context << "\n Post Author Information:"
274- context << "- Username: #{ post . user . username } "
275- context << "- Account age: #{ ( Time . current - post . user . created_at ) . to_i / 86_400 } days"
276- context << "- Total posts: #{ post . user . post_count } "
277- context << "- Trust level: #{ post . user . trust_level } "
277+ if post . user # during test we may not have a user
278+ context << "- Username: #{ post . user . username } "
279+ context << "- Account age: #{ ( Time . current - post . user . created_at ) . to_i / 86_400 } days"
280+ context << "- Total posts: #{ post . user . post_count } "
281+ context << "- Trust level: #{ post . user . trust_level } "
282+ end
278283
279284 context << "\n Post Content (first #{ MAX_RAW_SCAN_LENGTH } chars):\n "
280285 context << post . raw [ 0 ..MAX_RAW_SCAN_LENGTH ]
0 commit comments