mirror of
https://github.com/discourse/discourse.git
synced 2026-08-09 21:45:25 +08:00
AI API audit logs could be left behind when posts, topics, or users were deleted when an admin needs to delete a user. This PR will remove those logs through the existing destroy hooks, so batched post deletion and user deletion remove the matching audit records. Note: This delete can be expensive because user_id is not the leading column of an index. Given user deletion is rare and this table is very large, I'm not adding an index just for this unless we have evidence it hurts in production. It may be worth accepting the occasional scan. /399458/42
28 lines
927 B
Ruby
Vendored
28 lines
927 B
Ruby
Vendored
# frozen_string_literal: true
|
|
|
|
module DiscourseAi
|
|
class AiApiAuditLogCleaner
|
|
def self.delete_for_post(post_id)
|
|
AiApiAuditLog.where(post_id:).delete_all
|
|
end
|
|
|
|
def self.delete_for_topic(topic_id)
|
|
AiApiAuditLog.where(topic_id:).delete_all
|
|
end
|
|
|
|
def self.delete_for_user(user_id)
|
|
AiApiAuditLog.where(user_id:).delete_all
|
|
end
|
|
|
|
# logs reference the content they were generated against, so when a user's
|
|
# posts and topics are removed alongside the account we clear those too.
|
|
# with_deleted covers content already soft-deleted (e.g. "delete all posts")
|
|
# before the account itself is deleted.
|
|
def self.delete_for_user_content(user)
|
|
AiApiAuditLog.where(post_id: Post.with_deleted.where(user_id: user.id).select(:id)).delete_all
|
|
AiApiAuditLog.where(
|
|
topic_id: Topic.with_deleted.where(user_id: user.id).select(:id),
|
|
).delete_all
|
|
end
|
|
end
|
|
end
|