Skip to content

A platform for community discussion. Free, open, simple.

UX: Hide "My Threads" when user has no threads (#36931)

Régis Hanol24bf2c4e7293
diff --git a/plugins/chat/app/models/chat/thread.rb b/plugins/chat/app/models/chat/thread.rb
index 6b2253bd39b..a8da3ae1039 100644
--- a/plugins/chat/app/models/chat/thread.rb
+++ b/plugins/chat/app/models/chat/thread.rb
@@ -38,28 +38,62 @@ module Chat
validates :title, length: { maximum: Chat::Thread::MAX_TITLE_LENGTH }
+ scope :viewable_by_user,
+ ->(user) do
+ joins(:user_chat_thread_memberships, :channel)
+ .joins(<<~SQL)
+ INNER JOIN chat_messages viewable_om
+ ON viewable_om.id = chat_threads.original_message_id
+ AND viewable_om.chat_channel_id = chat_threads.channel_id
+ AND viewable_om.deleted_at IS NULL
+ INNER JOIN chat_messages viewable_lm
+ ON viewable_lm.id = chat_threads.last_message_id
+ AND viewable_lm.chat_channel_id = chat_threads.channel_id
+ AND viewable_lm.deleted_at IS NULL
+ INNER JOIN user_chat_channel_memberships
+ ON user_chat_channel_memberships.chat_channel_id = chat_channels.id
+ AND user_chat_channel_memberships.user_id = #{user.id.to_i}
+ AND user_chat_channel_memberships.following
+ SQL
+ .where(user_chat_thread_memberships: { user_id: user.id })
+ .where.not(
+ user_chat_thread_memberships: {
+ notification_level: Chat::NotificationLevels.all[:muted],
+ },
+ )
+ .where(chat_channels: { threading_enabled: true, status: :open })
+ .where("chat_threads.replies_count > 0")
+ end
+
# Since the `replies` for the thread can all be deleted, to avoid errors
# in lists and previews of the thread, we can consider the original message
# as the last message in this case as a fallback.
before_create { self.last_message_id = self.original_message_id }
def add(user, notification_level: Chat::NotificationLevels.all[:tracking])
- membership = Chat::UserChatThreadMembership.find_by(user: user, thread: self)
+ membership = Chat::UserChatThreadMembership.find_by(user:, thread: self)
return membership if membership
- Chat::UserChatThreadMembership.create!(
- user: user,
- thread: self,
- notification_level: notification_level,
- )
+ is_first = channel.threading_enabled && !Chat::Thread.viewable_by_user(user).exists?
+
+ membership = Chat::UserChatThreadMembership.create!(user:, thread: self, notification_level:)
+
+ if is_first && !membership.muted?
+ # This is called from Chat::CreateMessage's post_process_thread step,
+ # which runs inside a transaction block. We use after_commit to ensure
+ # the MessageBus publish only happens after the transaction commits.
+ DB.after_commit { Chat::Publisher.publish_user_has_threads!(user) }
+ end
+
+ membership
end
def remove(user)
- Chat::UserChatThreadMembership.find_by(user: user, thread: self)&.destroy
+ Chat::UserChatThreadMembership.find_by(user:, thread: self)&.destroy
end
def membership_for(user)
- user_chat_thread_memberships.find_by(user: user)
+ user_chat_thread_memberships.find_by(user:)
end
def replies
diff --git a/plugins/chat/app/serializers/chat/structured_channel_serializer.rb b/plugins/chat/app/serializers/chat/structured_channel_serializer.rb
index 03993c942a0..47c3832ed63 100644
--- a/plugins/chat/app/serializers/chat/structured_channel_serializer.rb
+++ b/plugins/chat/app/serializers/chat/structured_channel_serializer.rb
@@ -2,7 +2,16 @@
module Chat
class StructuredChannelSerializer < ApplicationSerializer
- attributes :public_channels, :direct_message_channels, :tracking, :meta, :unread_thread_overview
+ attributes :public_channels,
+ :direct_message_channels,
+ :tracking,
+ :meta,
+ :unread_thread_overview,
+ :has_threads
+
+ def has_threads
+ object[:has_threads]
+ end
def tracking
object[:tracking]
@@ -78,6 +87,13 @@ module Chat
]
last_ids[:user_tracking_state] = user_tracking_state_last_id if user_tracking_state_last_id
+
+ user_has_threads_last_id =
+ chat_message_bus_last_ids[
+ Chat::Publisher.user_has_threads_message_bus_channel(scope.user.id)
+ ]
+
+ last_ids[:user_has_threads] = user_has_threads_last_id if user_has_threads_last_id
end
{ message_bus_last_ids: last_ids }
@@ -99,6 +115,7 @@ module Chat
if !scope.anonymous?
message_bus_channels.push(
Chat::Publisher.user_tracking_state_message_bus_channel(scope.user.id),
+ Chat::Publisher.user_has_threads_message_bus_channel(scope.user.id),
)
end
diff --git a/plugins/chat/app/services/chat/list_user_channels.rb b/plugins/chat/app/services/chat/list_user_channels.rb
index 5e7f58aa4c0..9848702314d 100644
--- a/plugins/chat/app/services/chat/list_user_channels.rb
+++ b/plugins/chat/app/services/chat/list_user_channels.rb
@@ -15,6 +15,7 @@ module Chat
model :structured
step :inject_unread_thread_overview
+ step :inject_has_threads
model :post_allowed_category_ids, optional: true
private
@@ -23,6 +24,10 @@ module Chat
::Chat::ChannelFetcher.structured(guardian)
end
+ def inject_has_threads(structured:, guardian:)
+ structured[:has_threads] = ::Chat::Thread.viewable_by_user(guardian.user).exists?
+ end
+
def inject_unread_thread_overview(structured:, guardian:)
channel_ids =
structured[:public_channels].map(&:id) + structured[:direct_message_channels].map(&:id)
diff --git a/plugins/chat/app/services/chat/lookup_user_threads.rb b/plugins/chat/app/services/chat/lookup_user_threads.rb
index a78a41672cd..e13744781ed 100644
--- a/plugins/chat/app/services/chat/lookup_user_threads.rb
+++ b/plugins/chat/app/services/chat/lookup_user_threads.rb
@@ -41,6 +41,7 @@ module Chat
def fetch_threads(guardian:, params:)
::Chat::Thread
+ .viewable_by_user(guardian.user)
.includes(
:channel,
:user_chat_thread_memberships,
@@ -49,55 +50,23 @@ module Chat
:uploads,
:chat_webhook_event,
:chat_channel,
- user_mentions: {
- user: :user_status,
- },
- user: :user_status,
+ { user_mentions: { user: :user_status } },
+ { user: :user_status },
],
original_message: [
:uploads,
:chat_webhook_event,
:chat_channel,
- user_mentions: {
- user: :user_status,
- },
- user: :user_status,
+ { user_mentions: { user: :user_status } },
+ { user: :user_status },
],
)
- .joins(
- "INNER JOIN user_chat_thread_memberships ON chat_threads.id = user_chat_thread_memberships.thread_id",
- )
- .joins(
- "LEFT JOIN chat_messages AS last_message ON chat_threads.last_message_id = last_message.id",
- )
- .joins(
- "INNER JOIN chat_messages AS original_message ON chat_threads.original_message_id = original_message.id",
- )
- .where(
- channel_id:
- ::Chat::Channel
- .joins(:user_chat_channel_memberships)
- .where(user_chat_channel_memberships: { user_id: guardian.user.id, following: true })
- .where({ threading_enabled: true, status: ::Chat::Channel.statuses[:open] })
- .select(:id),
- )
- .where("original_message.chat_channel_id = chat_threads.channel_id")
- .where("original_message.deleted_at IS NULL")
- .where("last_message.chat_channel_id = chat_threads.channel_id")
- .where("last_message.deleted_at IS NULL")
- .where("chat_threads.replies_count > 0")
- .where("user_chat_thread_memberships.user_id = ?", guardian.user.id)
- .where(
- "user_chat_thread_memberships.notification_level IN (?)",
- [
- ::Chat::UserChatThreadMembership.notification_levels[:normal],
- ::Chat::UserChatThreadMembership.notification_levels[:watching],
- ::Chat::UserChatThreadMembership.notification_levels[:tracking],
- ],
- )
- .order(
- "CASE WHEN user_chat_thread_memberships.last_read_message_id IS NULL OR user_chat_thread_memberships.last_read_message_id < chat_threads.last_message_id THEN true ELSE false END DESC, last_message.created_at DESC",
- )
+ .order(<<~SQL)
+ CASE WHEN user_chat_thread_memberships.last_read_message_id IS NULL
+ OR user_chat_thread_memberships.last_read_message_id < chat_threads.last_message_id
+ THEN 1 ELSE 0 END DESC,
+ viewable_lm.created_at DESC
+ SQL
.limit(params.limit)
.offset(params.offset)
end
diff --git a/plugins/chat/app/services/chat/publisher.rb b/plugins/chat/app/services/chat/publisher.rb
index 1ce5135c05a..5d368bb86a9 100644
--- a/plugins/chat/app/services/chat/publisher.rb
+++ b/plugins/chat/app/services/chat/publisher.rb
@@ -106,6 +106,18 @@ module Chat
)
end
+ def self.user_has_threads_message_bus_channel(user_id)
+ "/chat/user-has-threads/#{user_id}"
+ end
+
+ def self.publish_user_has_threads!(user)
+ MessageBus.publish(
+ user_has_threads_message_bus_channel(user.id),
+ { has_threads: true },
+ user_ids: [user.id],
+ )
+ end
+
def self.publish_processed!(chat_message)
chat_channel = chat_message.chat_channel
message_bus_targets = calculate_publish_targets(chat_channel, chat_message)
diff --git a/plugins/chat/assets/javascripts/discourse/components/channels-list-public.gjs b/plugins/chat/assets/javascripts/discourse/components/channels-list-public.gjs
index 5256050fbe9..b5c7d0ec246 100644
--- a/plugins/chat/assets/javascripts/discourse/components/channels-list-public.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/channels-list-public.gjs
@@ -28,8 +28,8 @@ export default class ChannelsListPublic extends Component {
return this.chatTrackingStateManager.hasUnreadThreads;
}
- get hasThreadedChannels() {
- return this.chatChannelsManager.hasThreadedChannels;
+ get shouldShowMyThreads() {
+ return this.chatChannelsManager.shouldShowMyThreads;
}
get channelList() {
@@ -51,7 +51,7 @@ export default class ChannelsListPublic extends Component {
}
<template>
- {{#if (and this.site.desktopView this.inSidebar this.hasThreadedChannels)}}
+ {{#if (and this.site.desktopView this.inSidebar this.shouldShowMyThreads)}}
<LinkTo @route="chat.threads" class="chat-channel-row --threads">
<span class="chat-channel-title">
{{icon "discourse-threads" class="chat-user-threads__icon"}}
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-footer.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-footer.gjs
index 283572e4087..df5ff870ee7 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat-footer.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/chat-footer.gjs
@@ -27,7 +27,7 @@ export default class ChatFooter extends Component {
if (!this.siteSettings.chat_threads_enabled) {
return false;
}
- return this.chatChannelsManager.hasThreadedChannels;
+ return this.chatChannelsManager.shouldShowMyThreads;
}
get directMessagesEnabled() {
diff --git a/plugins/chat/assets/javascripts/discourse/initializers/chat-sidebar.js b/plugins/chat/assets/javascripts/discourse/initializers/chat-sidebar.js
index 1d071233c6f..bbe9e01bfd2 100644
--- a/plugins/chat/assets/javascripts/discourse/initializers/chat-sidebar.js
+++ b/plugins/chat/assets/javascripts/discourse/initializers/chat-sidebar.js
@@ -355,7 +355,7 @@ export default {
}
get displaySection() {
- return this.chatChannelsManager.hasThreadedChannels;
+ return this.chatChannelsManager.shouldShowMyThreads;
}
};
diff --git a/plugins/chat/assets/javascripts/discourse/routes/chat/index.js b/plugins/chat/assets/javascripts/discourse/routes/chat/index.js
index 02aac33363d..bf30cdab604 100644
--- a/plugins/chat/assets/javascripts/discourse/routes/chat/index.js
+++ b/plugins/chat/assets/javascripts/discourse/routes/chat/index.js
@@ -12,7 +12,7 @@ export default class ChatIndexRoute extends DiscourseRoute {
return false;
}
- return this.chatChannelsManager.hasThreadedChannels;
+ return this.chatChannelsManager.shouldShowMyThreads;
}
get hasDirectMessages() {
diff --git a/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.js b/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.js
index 1f49bb48544..a78f11d3f66 100644
--- a/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.js
+++ b/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.js
@@ -23,6 +23,7 @@ export default class ChatChannelsManager extends Service {
@service chatDraftsManager;
@service siteSettings;
+ @tracked userHasThreads = false;
@tracked _cached = new TrackedObject();
async find(id, options = { fetchIfNotFound: true }) {
@@ -150,6 +151,10 @@ export default class ChatChannelsManager extends Service {
return this.allChannels?.some((channel) => channel.threadingEnabled);
}
+ get shouldShowMyThreads() {
+ return this.hasThreadedChannels && this.userHasThreads;
+ }
+
get allChannels() {
return [...this.publicMessageChannels, ...this.directMessageChannels].sort(
(a, b) => {
diff --git a/plugins/chat/assets/javascripts/discourse/services/chat-drawer-router.js b/plugins/chat/assets/javascripts/discourse/services/chat-drawer-router.js
index b6cbe681ef1..dd74ce0ed55 100644
--- a/plugins/chat/assets/javascripts/discourse/services/chat-drawer-router.js
+++ b/plugins/chat/assets/javascripts/discourse/services/chat-drawer-router.js
@@ -290,7 +290,7 @@ export default class ChatDrawerRouter extends Service {
return false;
}
- return this.chatChannelsManager.hasThreadedChannels;
+ return this.chatChannelsManager.shouldShowMyThreads;
}
get hasDirectMessages() {
diff --git a/plugins/chat/assets/javascripts/discourse/services/chat-subscriptions-manager.js b/plugins/chat/assets/javascripts/discourse/services/chat-subscriptions-manager.js
index fd77dc2dbf7..529697a524d 100644
--- a/plugins/chat/assets/javascripts/discourse/services/chat-subscriptions-manager.js
+++ b/plugins/chat/assets/javascripts/discourse/services/chat-subscriptions-manager.js
@@ -56,6 +56,7 @@ export default class ChatSubscriptionsManager extends Service {
this._startNewChannelSubscription(messageBusIds.new_channel);
this._startChannelArchiveStatusSubscription(messageBusIds.archive_status);
this._startUserTrackingStateSubscription(messageBusIds.user_tracking_state);
+ this._startUserHasThreadsSubscription(messageBusIds.user_has_threads);
this._startChannelsEditsSubscription(messageBusIds.channel_edits);
this._startChannelsStatusChangesSubscription(messageBusIds.channel_status);
this._startChannelsMetadataChangesSubscription(
@@ -67,6 +68,7 @@ export default class ChatSubscriptionsManager extends Service {
this._stopNewChannelSubscription();
this._stopChannelArchiveStatusSubscription();
this._stopUserTrackingStateSubscription();
+ this._stopUserHasThreadsSubscription();
this._stopChannelsEditsSubscription();
this._stopChannelsStatusChangesSubscription();
this._stopChannelsMetadataChangesSubscription();
@@ -326,6 +328,36 @@ export default class ChatSubscriptionsManager extends Service {
);
}
+ _startUserHasThreadsSubscription(lastId) {
+ if (!this.currentUser) {
+ return;
+ }
+
+ this.messageBus.subscribe(
+ `/chat/user-has-threads/${this.currentUser.id}`,
+ this._onUserHasThreads,
+ lastId
+ );
+ }
+
+ _stopUserHasThreadsSubscription() {
+ if (!this.currentUser) {
+ return;
+ }
+
+ this.messageBus.unsubscribe(
+ `/chat/user-has-threads/${this.currentUser.id}`,
+ this._onUserHasThreads
+ );
+ }
+
+ @bind
+ _onUserHasThreads(busData) {
+ if (busData.has_threads) {
+ this.chatChannelsManager.userHasThreads = true;
+ }
+ }
+
@bind
_onBulkUserTrackingStateUpdate(busData) {
Object.keys(busData).forEach((channelId) => {
diff --git a/plugins/chat/assets/javascripts/discourse/services/chat.js b/plugins/chat/assets/javascripts/discourse/services/chat.js
index 1e802d50f6a..16553378838 100644
--- a/plugins/chat/assets/javascripts/discourse/services/chat.js
+++ b/plugins/chat/assets/javascripts/discourse/services/chat.js
@@ -125,6 +125,9 @@ export default class Chat extends Service {
channelsView.meta.message_bus_last_ids
);
+ this.chatChannelsManager.userHasThreads =
+ channelsView.has_threads ?? false;
+
[
...channelsView.public_channels,
...channelsView.direct_message_channels,
@@ -215,6 +218,8 @@ export default class Chat extends Service {
);
this.presenceChannel.subscribe(channelsView.global_presence_channel_state);
+ this.chatChannelsManager.userHasThreads = channelsView.has_threads ?? false;
+
[
...channelsView.public_channels,
...channelsView.direct_message_channels,
diff --git a/plugins/chat/spec/serializer/chat/structured_channel_serializer_spec.rb b/plugins/chat/spec/serializer/chat/structured_channel_serializer_spec.rb
index f6f458ceda8..678d2292a1f 100644
--- a/plugins/chat/spec/serializer/chat/structured_channel_serializer_spec.rb
+++ b/plugins/chat/spec/serializer/chat/structured_channel_serializer_spec.rb
@@ -126,6 +126,7 @@ RSpec.describe Chat::StructuredChannelSerializer do
new_channel
archive_status
user_tracking_state
+ user_has_threads
],
)
end
@@ -141,6 +142,7 @@ RSpec.describe Chat::StructuredChannelSerializer do
Chat::Publisher::NEW_CHANNEL_MESSAGE_BUS_CHANNEL,
Chat::Publisher::CHANNEL_ARCHIVE_STATUS_MESSAGE_BUS_CHANNEL,
Chat::Publisher.user_tracking_state_message_bus_channel(user1.id),
+ Chat::Publisher.user_has_threads_message_bus_channel(user1.id),
Chat::Publisher.new_messages_message_bus_channel(channel1.id),
Chat::Publisher.new_mentions_message_bus_channel(channel1.id),
Chat::Publisher.kick_users_message_bus_channel(channel1.id),
diff --git a/plugins/chat/spec/system/chat_footer_spec.rb b/plugins/chat/spec/system/chat_footer_spec.rb
index 33cfa48830e..d820b93be80 100644
--- a/plugins/chat/spec/system/chat_footer_spec.rb
+++ b/plugins/chat/spec/system/chat_footer_spec.rb
@@ -40,10 +40,16 @@ RSpec.describe "Mobile Chat footer", type: :system, mobile: true do
expect(page).to have_current_path("/chat/channels")
end
- context "when user is a member of at least one channel with threads" do
- it "shows threads tab when user has threads" do
+ context "when user has viewable threads" do
+ before do
SiteSetting.chat_threads_enabled = true
+ thread = Fabricate(:chat_thread, channel: channel, original_message: message)
+ thread.add(current_user)
+ Fabricate(:chat_message, chat_channel: channel, thread: thread, user: other_user)
+ thread.set_replies_count_cache(1, update_db: true)
+ end
+ it "shows threads tab when user has threads" do
visit("/")
chat_page.open_from_header
@@ -173,7 +179,11 @@ RSpec.describe "Mobile Chat footer", type: :system, mobile: true do
fab!(:thread) { Fabricate(:chat_thread, channel: channel, original_message: message) }
fab!(:thread_message) { Fabricate(:chat_message, chat_channel: channel, thread: thread) }
- before { SiteSetting.chat_threads_enabled = true }
+ before do
+ SiteSetting.chat_threads_enabled = true
+ thread.add(current_user)
+ thread.set_replies_count_cache(1, update_db: true)
+ end
it "is unread" do
visit("/")
@@ -219,7 +229,11 @@ RSpec.describe "Mobile Chat footer", type: :system, mobile: true do
Fabricate(:chat_message, chat_channel: dm_channel, thread: dm_thread, user: other_user)
end
- before { SiteSetting.chat_threads_enabled = true }
+ before do
+ SiteSetting.chat_threads_enabled = true
+ dm_thread.add(current_user)
+ dm_thread.set_replies_count_cache(1, update_db: true)
+ end
it "is unread" do
visit("/")
diff --git a/plugins/chat/spec/system/drawer/threads_spec.rb b/plugins/chat/spec/system/drawer/threads_spec.rb
index c3cc465fff2..21a3ee3d1bc 100644
--- a/plugins/chat/spec/system/drawer/threads_spec.rb
+++ b/plugins/chat/spec/system/drawer/threads_spec.rb
@@ -2,6 +2,7 @@
RSpec.describe "Drawer - threads", type: :system do
fab!(:current_user, :user)
+ fab!(:other_user, :user)
fab!(:channel_1) { Fabricate(:chat_channel, threading_enabled: true) }
let(:drawer_page) { PageObjects::Pages::ChatDrawer.new }
@@ -9,14 +10,25 @@ RSpec.describe "Drawer - threads", type: :system do
before do
SiteSetting.chat_threads_enabled = true
channel_1.add(current_user)
+ channel_1.add(other_user)
chat_system_bootstrap
sign_in(current_user)
end
- it "shows a button for chat search" do
- drawer_page.visit_user_threads
- drawer_page.open_chat_search
+ context "when user has viewable threads" do
+ before do
+ message = Fabricate(:chat_message, chat_channel: channel_1, user: current_user)
+ thread = Fabricate(:chat_thread, channel: channel_1, original_message: message)
+ thread.add(current_user)
+ Fabricate(:chat_message, chat_channel: channel_1, thread: thread, user: other_user)
+ thread.set_replies_count_cache(1, update_db: true)
+ end
- expect(drawer_page).to have_open_chat_search
+ it "shows a button for chat search" do
+ drawer_page.visit_user_threads
+ drawer_page.open_chat_search
+
+ expect(drawer_page).to have_open_chat_search
+ end
end
end
diff --git a/plugins/chat/spec/system/drawer_spec.rb b/plugins/chat/spec/system/drawer_spec.rb
index 82ffea9348f..505579531ce 100644
--- a/plugins/chat/spec/system/drawer_spec.rb
+++ b/plugins/chat/spec/system/drawer_spec.rb
@@ -275,10 +275,12 @@ RSpec.describe "Drawer", type: :system do
context "when clicking footer nav items" do
fab!(:channel) { Fabricate(:chat_channel, threading_enabled: true) }
+ fab!(:other_user, :user)
before do
SiteSetting.chat_threads_enabled = true
channel.add(current_user)
+ channel.add(other_user)
end
it "shows active state" do
@@ -289,18 +291,28 @@ RSpec.describe "Drawer", type: :system do
expect(page).to have_css("#c-footer-direct-messages.--active")
end
- it "redirects to correct route" do
- visit("/")
- chat_page.open_from_header
+ context "with viewable threads" do
+ before do
+ message = Fabricate(:chat_message, chat_channel: channel, user: current_user)
+ thread = Fabricate(:chat_thread, channel: channel, original_message: message)
+ thread.add(current_user)
+ Fabricate(:chat_message, chat_channel: channel, thread: thread, user: other_user)
+ thread.set_replies_count_cache(1, update_db: true)
+ end
- drawer_page.click_direct_messages
- expect(drawer_page).to have_open_direct_messages
+ it "redirects to correct route" do
+ visit("/")
+ chat_page.open_from_header
+
+ drawer_page.click_direct_messages
+ expect(drawer_page).to have_open_direct_messages
- drawer_page.click_channels
- expect(drawer_page).to have_open_channels
+ drawer_page.click_channels
+ expect(drawer_page).to have_open_channels
- drawer_page.click_user_threads
- expect(drawer_page).to have_open_user_threads
+ drawer_page.click_user_threads
+ expect(drawer_page).to have_open_user_threads
+ end
end
end
end
diff --git a/plugins/chat/spec/system/list_channels/mobile_spec.rb b/plugins/chat/spec/system/list_channels/mobile_spec.rb
index 0d19197e12c..0aab0eee6ce 100644
--- a/plugins/chat/spec/system/list_channels/mobile_spec.rb
+++ b/plugins/chat/spec/system/list_channels/mobile_spec.rb
@@ -235,16 +235,37 @@ RSpec.describe "List channels | mobile", type: :system, mobile: true do
SiteSetting.chat_preferred_index = "my_threads"
end
- it "redirects to threads" do
- channel = Fabricate(:chat_channel, threading_enabled: true)
- channel.add(current_user)
+ context "when user has viewable threads" do
+ it "redirects to threads" do
+ channel = Fabricate(:chat_channel, threading_enabled: true)
+ channel.add(current_user)
+ other_user = Fabricate(:user)
+ channel.add(other_user)
+
+ message = Fabricate(:chat_message, chat_channel: channel, user: current_user)
+ thread = Fabricate(:chat_thread, channel: channel, original_message: message)
+ thread.add(current_user)
+ Fabricate(:chat_message, chat_channel: channel, thread: thread, user: other_user)
+ thread.set_replies_count_cache(1, update_db: true)
- visit("/chat")
+ visit("/chat")
+
+ expect(page).to have_current_path("/chat/threads")
+ end
+ end
- expect(page).to have_current_path("/chat/threads")
+ context "when user has no viewable threads" do
+ it "redirects to browse" do
+ channel = Fabricate(:chat_channel, threading_enabled: true)
+ channel.add(current_user)
+
+ visit("/chat")
+
+ expect(page).to have_current_path("/chat/browse/open")
+ end
end
- context "when no threads" do
+ context "when threads feature is disabled" do
before { SiteSetting.chat_threads_enabled = false }
it "redirects to browse" do
diff --git a/plugins/chat/spec/system/user_threads_spec.rb b/plugins/chat/spec/system/user_threads_spec.rb
index 4c3a4ed4c32..8522b008ddd 100644
--- a/plugins/chat/spec/system/user_threads_spec.rb
+++ b/plugins/chat/spec/system/user_threads_spec.rb
@@ -17,8 +17,10 @@ RSpec.describe "User threads", type: :system do
end
context "when in sidebar" do
- context "when user is a member of at least one channel with threads" do
- before { channel_1.add(current_user) }
+ context "when user has viewable threads" do
+ before do
+ chat_thread_chain_bootstrap(channel: channel_1, users: [current_user, Fabricate(:user)])
+ end
it "shows a link to user threads" do
visit("/")
@@ -27,11 +29,8 @@ RSpec.describe "User threads", type: :system do
end
end
- context "when user is not a member of any channel with threads" do
- before do
- channel_1.update!(threading_enabled: false)
- channel_1.add(current_user)
- end
+ context "when user has no viewable threads" do
+ before { channel_1.add(current_user) }
it "does not show a link to user threads" do
visit("/")
@@ -135,8 +134,10 @@ RSpec.describe "User threads", type: :system do
context "when in drawer" do
before { SiteSetting.chat_threads_enabled = true }
- context "when user is a member of at least one channel with threads" do
- before { channel_1.add(current_user) }
+ context "when user has viewable threads" do
+ before do
+ chat_thread_chain_bootstrap(channel: channel_1, users: [current_user, Fabricate(:user)])
+ end
it "shows the user threads tab" do
visit("/")
@@ -145,11 +146,8 @@ RSpec.describe "User threads", type: :system do
end
end
- context "when user is not a member of any channel with threads" do
- before do
- channel_1.update!(threading_enabled: false)
- channel_1.add(current_user)
- end
+ context "when user has no viewable threads" do
+ before { channel_1.add(current_user) }
it "does not show a link to user threads" do
visit("/")