FEATURE: prevent chat emails for messages created via SDK (#27875)

This change allows us to distinguish between regular user generated chat messages and those created via the Chat SDK.

A new created_by_sdk boolean column is added to the Chat Messages table. When this value is true, we will not include the message in the user summary email that is sent to users.
This commit is contained in:
David Battersby
2024-07-12 10:57:14 +04:00
committed by GitHub
parent a0283305ca
commit 4a365bc4a2
7 changed files with 48 additions and 0 deletions

View File

@ -0,0 +1,6 @@
# frozen_string_literal: true
class AddCreatedBySdkToChatMessages < ActiveRecord::Migration[7.1]
def change
add_column :chat_messages, :created_by_sdk, :boolean
end
end

View File

@ -0,0 +1,24 @@
# frozen_string_literal: true
class UpdateChatMessagesCreatedBySdk < ActiveRecord::Migration[7.1]
def up
change_column_default :chat_messages, :created_by_sdk, false
if DB.query_single("SELECT 1 FROM chat_messages WHERE created_by_sdk IS NULL LIMIT 1").first
batch_size = 10_000
min_id = DB.query_single("SELECT MIN(id) FROM chat_messages").first.to_i
max_id = DB.query_single("SELECT MAX(id) FROM chat_messages").first.to_i
while max_id >= min_id
DB.exec(
"UPDATE chat_messages SET created_by_sdk = false WHERE id > #{max_id - batch_size} AND id <= #{max_id}",
)
max_id -= batch_size
end
end
change_column_null :chat_messages, :created_by_sdk, false
end
def down
raise ActiveRecord::IrreversibleMigration
end
end