FEATURE: add custom fields to chat (channel/message/thread) (#29504)

This allows various extensions to store extra information the 3 most popular
chat entities

Useful for certain plugins and migrations
This commit is contained in:
Sam
2024-11-01 09:12:19 +11:00
committed by GitHub
parent 29d3b9b03e
commit e7f62ab52b
10 changed files with 140 additions and 11 deletions

View File

@ -11,6 +11,14 @@ RSpec.describe Chat::Channel do
it { is_expected.to validate_length_of(:chatable_type).is_at_most(100) }
it { is_expected.to validate_length_of(:type).is_at_most(100) }
it "supports custom fields" do
channel.custom_fields["test"] = "test"
channel.save_custom_fields
loaded_channel = Chat::Channel.find(channel.id)
expect(loaded_channel.custom_fields["test"]).to eq("test")
expect(Chat::ChannelCustomField.first.channel.id).to eq(channel.id)
end
describe ".last_message" do
context "when there are no last message" do
it "returns an instance of NullMessage" do

View File

@ -5,6 +5,14 @@ describe Chat::Message do
it { is_expected.to have_many(:chat_mentions).dependent(:destroy) }
it "supports custom fields" do
message.custom_fields["test"] = "test"
message.save_custom_fields
loaded_message = Chat::Message.find(message.id)
expect(loaded_message.custom_fields["test"]).to eq("test")
expect(Chat::MessageCustomField.first.message.id).to eq(message.id)
end
describe "validations" do
subject(:message) { described_class.new(message: "") }
@ -509,7 +517,7 @@ describe Chat::Message do
it "destroys upload_references" do
message_1 = Fabricate(:chat_message)
upload_reference_1 = Fabricate(:upload_reference, target: message_1)
upload_1 = Fabricate(:upload)
_upload_1 = Fabricate(:upload)
message_1.destroy!

View File

@ -9,18 +9,16 @@ RSpec.describe Chat::Thread do
fab!(:thread_2) { Fabricate(:chat_thread, channel: channel) }
fab!(:thread_3) { Fabricate(:chat_thread, channel: channel) }
before do
Fabricate(:chat_message, chat_channel: channel, thread: thread_1)
Fabricate(:chat_message, chat_channel: channel, thread: thread_1)
Fabricate(:chat_message, chat_channel: channel, thread: thread_1)
fab!(:thread_1_message_1) { Fabricate(:chat_message, chat_channel: channel, thread: thread_1) }
fab!(:thread_1_message_2) { Fabricate(:chat_message, chat_channel: channel, thread: thread_1) }
fab!(:thread_1_message_3) { Fabricate(:chat_message, chat_channel: channel, thread: thread_1) }
Fabricate(:chat_message, chat_channel: channel, thread: thread_2)
Fabricate(:chat_message, chat_channel: channel, thread: thread_2)
Fabricate(:chat_message, chat_channel: channel, thread: thread_2)
Fabricate(:chat_message, chat_channel: channel, thread: thread_2)
fab!(:thread_2_message_1) { Fabricate(:chat_message, chat_channel: channel, thread: thread_2) }
fab!(:thread_2_message_2) { Fabricate(:chat_message, chat_channel: channel, thread: thread_2) }
fab!(:thread_2_message_3) { Fabricate(:chat_message, chat_channel: channel, thread: thread_2) }
fab!(:thread_2_message_4) { Fabricate(:chat_message, chat_channel: channel, thread: thread_2) }
Fabricate(:chat_message, chat_channel: channel, thread: thread_3)
end
fab!(:thread_3_message_1) { Fabricate(:chat_message, chat_channel: channel, thread: thread_3) }
describe "updating replies_count for all threads" do
it "counts correctly and does not include the original message" do
@ -233,4 +231,17 @@ RSpec.describe Chat::Thread do
expect(thread.latest_not_deleted_message_id).to eq(old_message.id)
end
end
describe "custom fields" do
fab!(:channel) { Fabricate(:category_channel) }
fab!(:thread) { Fabricate(:chat_thread, channel: channel) }
it "allows create and save" do
thread.custom_fields["test"] = "test"
thread.save_custom_fields
loaded_thread = Chat::Thread.find(thread.id)
expect(loaded_thread.custom_fields["test"]).to eq("test")
expect(Chat::ThreadCustomField.first.thread.id).to eq(thread.id)
end
end
end