DEV: Rename direct message related models

This is a followup of the previous refactor where we created two new
models to handle all the dedicated logic that was present in the
`ChatChannel` model.

For the sake of consistency, `DMChannel` has been renamed to
`DirectMessageChannel` and the previous `DirectMessageChannel` model is
now named `DirectMessage`. This should help reasoning about direct
messages.
This commit is contained in:
Loïc Guitaut
2022-11-02 15:53:36 +01:00
committed by Loïc Guitaut
parent 7e992cb299
commit abcaa1a961
57 changed files with 378 additions and 455 deletions

View File

@ -1,66 +0,0 @@
# frozen_string_literal: true
RSpec.describe DMChannel do
subject(:channel) { Fabricate.build(:dm_channel) }
it_behaves_like "a chat channel model"
it { is_expected.to delegate_method(:allowed_user_ids).to(:direct_message_channel).as(:user_ids) }
describe "#category_channel?" do
it "always returns false" do
expect(channel).not_to be_a_category_channel
end
end
describe "#public_channel?" do
it "always returns false" do
expect(channel).not_to be_a_public_channel
end
end
describe "#chatable_has_custom_fields?" do
it "always returns false" do
expect(channel).not_to be_a_chatable_has_custom_fields
end
end
describe "#direct_message_channel?" do
it "always returns true" do
expect(channel).to be_a_direct_message_channel
end
end
describe "#read_restricted?" do
it "always returns true" do
expect(channel).to be_read_restricted
end
end
describe "#allowed_group_ids" do
it "always returns nothing" do
expect(channel.allowed_group_ids).to be_nil
end
end
describe "#chatable_url" do
it "always returns nothing" do
expect(channel.chatable_url).to be_nil
end
end
describe "#title" do
subject(:title) { channel.title(user) }
let(:user) { stub }
let(:direct_message_channel) { channel.direct_message_channel }
it "delegates to direct_message_channel" do
direct_message_channel
.expects(:chat_channel_title_for_user)
.with(channel, user)
.returns("something")
expect(title).to eq("something")
end
end
end

View File

@ -1,73 +1,63 @@
# frozen_string_literal: true
require "rails_helper"
RSpec.describe DirectMessageChannel do
subject(:channel) { Fabricate.build(:direct_message_channel) }
describe DirectMessageChannel do
fab!(:user1) { Fabricate(:user, username: "chatdmfellow1") }
fab!(:user2) { Fabricate(:user, username: "chatdmuser") }
fab!(:chat_channel) { Fabricate(:chat_channel) }
it_behaves_like "a chat channel model"
it_behaves_like "a chatable model" do
fab!(:chatable) { Fabricate(:direct_message_channel) }
let(:channel_class) { DMChannel }
it { is_expected.to delegate_method(:allowed_user_ids).to(:direct_message).as(:user_ids) }
describe "#category_channel?" do
it "always returns false" do
expect(channel).not_to be_a_category_channel
end
end
describe "#chat_channel_title_for_user" do
it "returns a nicely formatted name if it's more than one user" do
user3 = Fabricate.build(:user, username: "chatdmregent")
direct_message_channel = Fabricate(:direct_message_channel, users: [user1, user2, user3])
expect(direct_message_channel.chat_channel_title_for_user(chat_channel, user1)).to eq(
I18n.t(
"chat.channel.dm_title.multi_user",
users: [user3, user2].map { |u| "@#{u.username}" }.join(", "),
),
)
describe "#public_channel?" do
it "always returns false" do
expect(channel).not_to be_a_public_channel
end
end
it "returns a nicely formatted truncated name if it's more than 5 users" do
user3 = Fabricate.build(:user, username: "chatdmregent")
users = [user1, user2, user3].concat(
5.times.map.with_index { |i| Fabricate(:user, username: "chatdmuser#{i}") },
)
direct_message_channel = Fabricate(:direct_message_channel, users: users)
expect(direct_message_channel.chat_channel_title_for_user(chat_channel, user1)).to eq(
I18n.t(
"chat.channel.dm_title.multi_user_truncated",
users: users[1..5].sort_by(&:username).map { |u| "@#{u.username}" }.join(", "),
leftover: 2,
),
)
describe "#chatable_has_custom_fields?" do
it "always returns false" do
expect(channel).not_to be_a_chatable_has_custom_fields
end
end
it "returns the other user's username if it's a dm to that user" do
direct_message_channel = Fabricate(:direct_message_channel, users: [user1, user2])
expect(direct_message_channel.chat_channel_title_for_user(chat_channel, user1)).to eq(
I18n.t("chat.channel.dm_title.single_user", user: "@#{user2.username}"),
)
describe "#direct_message_channel?" do
it "always returns true" do
expect(channel).to be_a_direct_message_channel
end
end
it "returns the current user's username if it's a dm to self" do
direct_message_channel = Fabricate(:direct_message_channel, users: [user1])
expect(direct_message_channel.chat_channel_title_for_user(chat_channel, user1)).to eq(
I18n.t("chat.channel.dm_title.single_user", user: "@#{user1.username}"),
)
describe "#read_restricted?" do
it "always returns true" do
expect(channel).to be_read_restricted
end
end
context "when user is deleted" do
it "returns a placeholder username" do
direct_message_channel = Fabricate(:direct_message_channel, users: [user1, user2])
user2.destroy!
direct_message_channel.reload
describe "#allowed_group_ids" do
it "always returns nothing" do
expect(channel.allowed_group_ids).to be_nil
end
end
expect(direct_message_channel.chat_channel_title_for_user(chat_channel, user1)).to eq(
"@#{I18n.t("chat.deleted_chat_username")}",
)
end
describe "#chatable_url" do
it "always returns nothing" do
expect(channel.chatable_url).to be_nil
end
end
describe "#title" do
subject(:title) { channel.title(user) }
let(:user) { stub }
let(:direct_message) { channel.direct_message }
it "delegates to direct_message" do
direct_message.expects(:chat_channel_title_for_user).with(channel, user).returns("something")
expect(title).to eq("something")
end
end
end

View File

@ -0,0 +1,73 @@
# frozen_string_literal: true
require "rails_helper"
describe DirectMessage do
fab!(:user1) { Fabricate(:user, username: "chatdmfellow1") }
fab!(:user2) { Fabricate(:user, username: "chatdmuser") }
fab!(:chat_channel) { Fabricate(:direct_message_channel) }
it_behaves_like "a chatable model" do
fab!(:chatable) { Fabricate(:direct_message) }
let(:channel_class) { DirectMessageChannel }
end
describe "#chat_channel_title_for_user" do
it "returns a nicely formatted name if it's more than one user" do
user3 = Fabricate.build(:user, username: "chatdmregent")
direct_message = Fabricate(:direct_message, users: [user1, user2, user3])
expect(direct_message.chat_channel_title_for_user(chat_channel, user1)).to eq(
I18n.t(
"chat.channel.dm_title.multi_user",
users: [user3, user2].map { |u| "@#{u.username}" }.join(", "),
),
)
end
it "returns a nicely formatted truncated name if it's more than 5 users" do
user3 = Fabricate.build(:user, username: "chatdmregent")
users = [user1, user2, user3].concat(
5.times.map.with_index { |i| Fabricate(:user, username: "chatdmuser#{i}") },
)
direct_message = Fabricate(:direct_message, users: users)
expect(direct_message.chat_channel_title_for_user(chat_channel, user1)).to eq(
I18n.t(
"chat.channel.dm_title.multi_user_truncated",
users: users[1..5].sort_by(&:username).map { |u| "@#{u.username}" }.join(", "),
leftover: 2,
),
)
end
it "returns the other user's username if it's a dm to that user" do
direct_message = Fabricate(:direct_message, users: [user1, user2])
expect(direct_message.chat_channel_title_for_user(chat_channel, user1)).to eq(
I18n.t("chat.channel.dm_title.single_user", user: "@#{user2.username}"),
)
end
it "returns the current user's username if it's a dm to self" do
direct_message = Fabricate(:direct_message, users: [user1])
expect(direct_message.chat_channel_title_for_user(chat_channel, user1)).to eq(
I18n.t("chat.channel.dm_title.single_user", user: "@#{user1.username}"),
)
end
context "when user is deleted" do
it "returns a placeholder username" do
direct_message = Fabricate(:direct_message, users: [user1, user2])
user2.destroy!
direct_message.reload
expect(direct_message.chat_channel_title_for_user(chat_channel, user1)).to eq(
"@#{I18n.t("chat.deleted_chat_username")}",
)
end
end
end
end