discourse/plugins/chat/app/models/chat_thread.rb
Martin Brennan e7b39e2fc1
DEV: Add ChatThread model and DB table, and ChatMessage reference (#20106)
This new table will be used to automatically group replies
for messages into one place. In future additional functionality
will be built around the thread, like pinning messages, changing
the title, etc., the columns are just the main ones needed at first.
The columns are not prefixed with `chat_*` e.g. `chat_channel` since
this is redundant and just adds duplication everywhere, we want to
move away from this generally within chat.
2023-02-01 13:50:38 +10:00

45 lines
1.5 KiB
Ruby

# frozen_string_literal: true
class ChatThread < ActiveRecord::Base
belongs_to :channel, foreign_key: "channel_id", class_name: "ChatChannel"
belongs_to :original_message_user, foreign_key: "original_message_user_id", class_name: "User"
belongs_to :original_message, foreign_key: "original_message_id", class_name: "ChatMessage"
has_many :chat_messages,
-> { order("chat_messages.created_at ASC, chat_messages.id ASC") },
foreign_key: :thread_id,
primary_key: :id
enum :status, { open: 0, read_only: 1, closed: 2, archived: 3 }, scopes: false
def url
"#{channel.url}/t/#{self.id}"
end
def relative_url
"#{channel.relative_url}/t/#{self.id}"
end
end
# == Schema Information
#
# Table name: chat_threads
#
# id :bigint not null, primary key
# channel_id :integer not null
# original_message_id :integer not null
# original_message_user_id :integer not null
# status :integer default("open"), not null
# title :string
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_chat_threads_on_channel_id (channel_id)
# index_chat_threads_on_channel_id_and_status (channel_id,status)
# index_chat_threads_on_original_message_id (original_message_id)
# index_chat_threads_on_original_message_user_id (original_message_user_id)
# index_chat_threads_on_status (status)
#