1717sw = stopwords .words ('english' )
1818
1919# Configure logging for spam detection
20- logging . basicConfig ( level = logging . INFO )
20+ import logging
2121spam_logger = logging .getLogger ('spam_detection' )
2222
2323
@@ -113,7 +113,7 @@ def __init__(self):
113113 def extract_urls (self , text : str ):
114114 return re .findall (r'https?://[^\s)<>"]+' , text )
115115
116- def detect_spam (self , title : str , content : str , category : str = "" , tutorial : str = "" ) -> dict :
116+ def detect_spam (self ,user , question , title : str , content : str , category : str = "" , tutorial : str = "" ) -> dict :
117117 combined_text = " " .join (filter (None , [title , content , category , tutorial ])).lower ()
118118 spam_score = 0
119119 matches = []
@@ -160,8 +160,10 @@ def detect_spam(self, title: str, content: str, category: str = "", tutorial: st
160160 }
161161
162162 # debug log
163- spam_logger .info (f"SpamDetect result: score={ spam_score } action={ action } matches={ len (matches )} " )
164-
163+ spam_logger .info (
164+ "SpamDetect result: question_id=%s user_id=%s score=%s action=%s matches=%s" ,
165+ question .id , user .id , spam_score , action , len (matches )
166+ )
165167 return result
166168
167169
@@ -175,6 +177,8 @@ def handle_spam(question, user, delete_on_high=True, save_question_metadata_befo
175177 """
176178 detector = SpamQuestionDetector ()
177179 result = detector .detect_spam (
180+ user = user ,
181+ question = question ,
178182 title = getattr (question , 'title' , '' ) or '' ,
179183 content = getattr (question , 'body' , '' ) or '' ,
180184 category = getattr (question , 'category' , '' ) or '' ,
@@ -188,7 +192,7 @@ def handle_spam(question, user, delete_on_high=True, save_question_metadata_befo
188192
189193 # prepare log payload
190194 log_payload = {
191- # 'question_id': question.id,
195+ 'question_id' : question .id ,
192196 'user_id' : user .id if user else None ,
193197 'category' : getattr (question , 'category' , '' ) or '' ,
194198 'title' : getattr (question , 'title' , '' ) or '' ,
@@ -206,8 +210,8 @@ def handle_spam(question, user, delete_on_high=True, save_question_metadata_befo
206210
207211 if delete_on_high :
208212 # delete after logging
209- spam_logger .info (f"AUTO_DELETE : Question { question .id } by user { user .id } score={ spam_score } " )
210- question .delete ()
213+ spam_logger .info (f"MARK_INACTIVE : Question { question .id } by user { user .id } score={ spam_score } " )
214+ question .status = 0
211215 return 'AUTO_DELETE'
212216 else :
213217 # hide instead of delete
0 commit comments