Skip to content

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

FIX: fully hides section if user can't send dms (#35595)

Joffrey JAFFEUX02ba427c5272
diff --git a/plugins/chat/assets/javascripts/discourse/initializers/chat-sidebar.js b/plugins/chat/assets/javascripts/discourse/initializers/chat-sidebar.js
index c941c00831e..b17fa642955 100644
--- a/plugins/chat/assets/javascripts/discourse/initializers/chat-sidebar.js
+++ b/plugins/chat/assets/javascripts/discourse/initializers/chat-sidebar.js
@@ -321,271 +321,269 @@ export default {
);
}
- api.addSidebarSection(
- (BaseCustomSidebarSection, BaseCustomSidebarSectionLink) => {
- const SidebarChatNewDirectMessagesSectionLink = class extends BaseCustomSidebarSectionLink {
- route = "chat.new-message";
- name = "new-chat-dm";
- title = i18n("sidebar.start_new_dm.title");
- text = i18n("sidebar.start_new_dm.text");
- prefixType = "icon";
- prefixValue = "plus";
- };
+ if (this.chatService.userCanDirectMessage) {
+ api.addSidebarSection(
+ (BaseCustomSidebarSection, BaseCustomSidebarSectionLink) => {
+ const SidebarChatNewDirectMessagesSectionLink = class extends BaseCustomSidebarSectionLink {
+ route = "chat.new-message";
+ name = "new-chat-dm";
+ title = i18n("sidebar.start_new_dm.title");
+ text = i18n("sidebar.start_new_dm.text");
+ prefixType = "icon";
+ prefixValue = "plus";
+ };
- const SidebarChatDirectMessagesSectionLink = class extends BaseCustomSidebarSectionLink {
- route = "chat.channel";
- suffixType = "icon";
- hoverType = "icon";
- hoverValue = "xmark";
- hoverTitle = i18n("chat.direct_messages.close");
+ const SidebarChatDirectMessagesSectionLink = class extends BaseCustomSidebarSectionLink {
+ route = "chat.channel";
+ suffixType = "icon";
+ hoverType = "icon";
+ hoverValue = "xmark";
+ hoverTitle = i18n("chat.direct_messages.close");
- constructor({ channel, chatService, currentUser }) {
- super(...arguments);
- this.channel = channel;
- this.chatService = chatService;
- this.currentUser = currentUser;
- this.chatStateManager = chatStateManager;
+ constructor({ channel, chatService, currentUser }) {
+ super(...arguments);
+ this.channel = channel;
+ this.chatService = chatService;
+ this.currentUser = currentUser;
+ this.chatStateManager = chatStateManager;
- if (this.oneOnOneMessage) {
- const user = this.channel.chatable.users[0];
- if (user.username !== i18n("chat.deleted_chat_username")) {
- user.statusManager.trackStatus();
+ if (this.oneOnOneMessage) {
+ const user = this.channel.chatable.users[0];
+ if (user.username !== i18n("chat.deleted_chat_username")) {
+ user.statusManager.trackStatus();
+ }
}
}
- }
- @bind
- willDestroy() {
- if (this.oneOnOneMessage) {
- this.channel.chatable.users[0].statusManager.stopTrackingStatus();
+ @bind
+ willDestroy() {
+ if (this.oneOnOneMessage) {
+ this.channel.chatable.users[0].statusManager.stopTrackingStatus();
+ }
}
- }
-
- get oneOnOneMessage() {
- return this.channel.chatable.users.length === 1;
- }
- get contentComponentArgs() {
- return this.channel.chatable.users[0].get("status");
- }
-
- get contentComponent() {
- if (this.oneOnOneMessage) {
- return UserStatusMessage;
+ get oneOnOneMessage() {
+ return this.channel.chatable.users.length === 1;
}
- }
-
- get name() {
- return this.channel.slugifiedTitle;
- }
- get classNames() {
- const classes = [];
+ get contentComponentArgs() {
+ return this.channel.chatable.users[0].get("status");
+ }
- if (this.channel.currentUserMembership.muted) {
- classes.push("sidebar-section-link--muted");
+ get contentComponent() {
+ if (this.oneOnOneMessage) {
+ return UserStatusMessage;
+ }
}
- if (
- this.channel.id === this.chatService.activeChannel?.id &&
- (this.chatStateManager.isDrawerExpanded ||
- this.chatStateManager.isFullPageActive)
- ) {
- classes.push("sidebar-section-link--active");
+ get name() {
+ return this.channel.slugifiedTitle;
}
- classes.push(`channel-${this.channel.id}`);
+ get classNames() {
+ const classes = [];
- return classes.join(" ");
- }
+ if (this.channel.currentUserMembership.muted) {
+ classes.push("sidebar-section-link--muted");
+ }
- get models() {
- return this.channel.routeModels;
- }
+ if (
+ this.channel.id === this.chatService.activeChannel?.id &&
+ (this.chatStateManager.isDrawerExpanded ||
+ this.chatStateManager.isFullPageActive)
+ ) {
+ classes.push("sidebar-section-link--active");
+ }
- get title() {
- if (this.channel.chatable.group) {
- return i18n("chat.placeholder_channel", {
- channelName: this.channel.escapedTitle,
- });
- } else {
- return i18n("chat.placeholder_users", {
- commaSeparatedNames: this.channel.escapedTitle,
- });
- }
- }
+ classes.push(`channel-${this.channel.id}`);
- get text() {
- if (this.channel.chatable.group) {
- return this.channel.title;
- } else {
- const username = this.channel.escapedTitle.replaceAll("@", "");
- return htmlSafe(
- `${escapeExpression(username)}${decorateUsername(
- escapeExpression(username)
- )}`
- );
+ return classes.join(" ");
}
- }
- get prefixType() {
- if (this.channel.iconUploadUrl) {
- return "image";
- } else if (this.channel.chatable.group) {
- return "text";
- } else {
- return "image";
+ get models() {
+ return this.channel.routeModels;
}
- }
- get prefixValue() {
- if (this.channel.iconUploadUrl) {
- return this.channel.iconUploadUrl;
- } else if (this.channel.chatable.group) {
- return this.channel.membershipsCount;
- } else {
- return avatarUrl(
- this.channel.chatable.users[0].avatar_template,
- "tiny"
- );
+ get title() {
+ if (this.channel.chatable.group) {
+ return i18n("chat.placeholder_channel", {
+ channelName: this.channel.escapedTitle,
+ });
+ } else {
+ return i18n("chat.placeholder_users", {
+ commaSeparatedNames: this.channel.escapedTitle,
+ });
+ }
}
- }
-
- get prefixCSSClass() {
- const activeUsers = this.chatService.presenceChannel.users;
- const user = this.channel.chatable.users[0];
- if (
- !!activeUsers?.find((item) => get(item, "id") === user?.id) ||
- !!activeUsers?.find(
- (item) => get(item, "username") === user?.username
- )
- ) {
- return "active";
+ get text() {
+ if (this.channel.chatable.group) {
+ return this.channel.title;
+ } else {
+ const username = this.channel.escapedTitle.replaceAll(
+ "@",
+ ""
+ );
+ return htmlSafe(
+ `${escapeExpression(username)}${decorateUsername(
+ escapeExpression(username)
+ )}`
+ );
+ }
}
- return "";
- }
- get suffixValue() {
- return this.channel.tracking.unreadCount > 0 ||
- this.channel.unreadThreadsCountSinceLastViewed > 0
- ? "circle"
- : "";
- }
+ get prefixType() {
+ if (this.channel.iconUploadUrl) {
+ return "image";
+ } else if (this.channel.chatable.group) {
+ return "text";
+ } else {
+ return "image";
+ }
+ }
- get suffixCSSClass() {
- return this.channel.tracking.unreadCount > 0 ||
- this.channel.tracking.mentionCount > 0 ||
- this.channel.tracking.watchedThreadsUnreadCount > 0
- ? "urgent"
- : "unread";
- }
+ get prefixValue() {
+ if (this.channel.iconUploadUrl) {
+ return this.channel.iconUploadUrl;
+ } else if (this.channel.chatable.group) {
+ return this.channel.membershipsCount;
+ } else {
+ return avatarUrl(
+ this.channel.chatable.users[0].avatar_template,
+ "tiny"
+ );
+ }
+ }
- get hoverAction() {
- return (event) => {
- event.stopPropagation();
- event.preventDefault();
- this.chatService.unfollowChannel(this.channel);
- };
- }
- };
+ get prefixCSSClass() {
+ const activeUsers = this.chatService.presenceChannel.users;
+ const user = this.channel.chatable.users[0];
- const SidebarChatDirectMessagesSection = class extends BaseCustomSidebarSection {
- @service site;
- @service modal;
- @service router;
- @service currentUser;
- @service chatStateManager;
+ if (
+ !!activeUsers?.find((item) => get(item, "id") === user?.id) ||
+ !!activeUsers?.find(
+ (item) => get(item, "username") === user?.username
+ )
+ ) {
+ return "active";
+ }
+ return "";
+ }
- @tracked
- userCanDirectMessage = this.chatService.userCanDirectMessage;
+ get suffixValue() {
+ return this.channel.tracking.unreadCount > 0 ||
+ this.channel.unreadThreadsCountSinceLastViewed > 0
+ ? "circle"
+ : "";
+ }
- constructor() {
- super(...arguments);
+ get suffixCSSClass() {
+ return this.channel.tracking.unreadCount > 0 ||
+ this.channel.tracking.mentionCount > 0 ||
+ this.channel.tracking.watchedThreadsUnreadCount > 0
+ ? "urgent"
+ : "unread";
+ }
- if (container.isDestroyed) {
- return;
+ get hoverAction() {
+ return (event) => {
+ event.stopPropagation();
+ event.preventDefault();
+ this.chatService.unfollowChannel(this.channel);
+ };
}
+ };
- this.chatService = container.lookup("service:chat");
- this.chatChannelsManager = container.lookup(
- "service:chat-channels-manager"
- );
- }
+ const SidebarChatDirectMessagesSection = class extends BaseCustomSidebarSection {
+ @service site;
+ @service modal;
+ @service router;
+ @service currentUser;
+ @service chatStateManager;
- get hideSectionHeader() {
- return (
- this.chatChannelsManager.truncatedDirectMessageChannels
- .length === 0
- );
- }
+ constructor() {
+ super(...arguments);
- get sectionLinks() {
- const channels =
- this.chatChannelsManager.truncatedDirectMessageChannels;
+ if (container.isDestroyed) {
+ return;
+ }
- if (channels.length > 0) {
- return channels.map(
- (channel) =>
- new SidebarChatDirectMessagesSectionLink({
- channel,
- chatService: this.chatService,
- currentUser: this.currentUser,
- })
+ this.chatService = container.lookup("service:chat");
+ this.chatChannelsManager = container.lookup(
+ "service:chat-channels-manager"
);
- } else if (this.currentUser.can_direct_message) {
- return [new SidebarChatNewDirectMessagesSectionLink()];
}
- }
- get name() {
- return "chat-dms";
- }
+ get hideSectionHeader() {
+ return (
+ this.chatChannelsManager.truncatedDirectMessageChannels
+ .length === 0
+ );
+ }
- get title() {
- return i18n("chat.direct_messages.title");
- }
+ get sectionLinks() {
+ const channels =
+ this.chatChannelsManager.truncatedDirectMessageChannels;
+
+ if (channels.length > 0) {
+ return channels.map(
+ (channel) =>
+ new SidebarChatDirectMessagesSectionLink({
+ channel,
+ chatService: this.chatService,
+ currentUser: this.currentUser,
+ })
+ );
+ } else {
+ return [new SidebarChatNewDirectMessagesSectionLink()];
+ }
+ }
- get text() {
- return i18n("chat.direct_messages.title");
- }
+ get name() {
+ return "chat-dms";
+ }
+
+ get title() {
+ return i18n("chat.direct_messages.title");
+ }
- get actions() {
- if (!this.userCanDirectMessage) {
- return [];
+ get text() {
+ return i18n("chat.direct_messages.title");
}
- return [
- {
- id: "startDm",
- title: i18n("chat.direct_messages.new"),
- action: () => {
- this.modal.show(ChatModalNewMessage);
+ get actions() {
+ return [
+ {
+ id: "startDm",
+ title: i18n("chat.direct_messages.new"),
+ action: () => {
+ this.modal.show(ChatModalNewMessage);
+ },
},
- },
- ];
- }
+ ];
+ }
- get actionsIcon() {
- return "plus";
- }
+ get actionsIcon() {
+ return "plus";
+ }
- get links() {
- return this.sectionLinks;
- }
+ get links() {
+ return this.sectionLinks;
+ }
- get displaySection() {
- return (
- this.chatStateManager.hasPreloadedChannels &&
- (this.sectionLinks?.length > 0 || this.userCanDirectMessage)
- );
- }
- };
+ get displaySection() {
+ return (
+ this.chatStateManager.hasPreloadedChannels &&
+ this.sectionLinks?.length > 0
+ );
+ }
+ };
- return SidebarChatDirectMessagesSection;
- },
- "chat"
- );
+ return SidebarChatDirectMessagesSection;
+ },
+ "chat"
+ );
+ }
});
},
};
diff --git a/plugins/chat/spec/system/list_channels/sidebar_spec.rb b/plugins/chat/spec/system/list_channels/sidebar_spec.rb
index 08fc154ee94..37b1e197587 100644
--- a/plugins/chat/spec/system/list_channels/sidebar_spec.rb
+++ b/plugins/chat/spec/system/list_channels/sidebar_spec.rb
@@ -147,10 +147,10 @@ RSpec.describe "List channels | sidebar", type: :system do
before { SiteSetting.direct_message_enabled_groups = group.id }
- it "doesn't show the start new dm button" do
+ it "doesn't show the section" do
visit("/")
- expect(chat_sidebar).to have_no_start_new_dm
+ expect(chat_sidebar).to have_no_dm_section
end
end
end
diff --git a/plugins/chat/spec/system/page_objects/chat/components/sidebar.rb b/plugins/chat/spec/system/page_objects/chat/components/sidebar.rb
index 3f86b80656f..ad0a28f414f 100644
--- a/plugins/chat/spec/system/page_objects/chat/components/sidebar.rb
+++ b/plugins/chat/spec/system/page_objects/chat/components/sidebar.rb
@@ -38,6 +38,10 @@ module PageObjects
selector
end
+ def has_no_dm_section?
+ has_no_selector?(".sidebar-section[data-section-name='chat-dms']")
+ end
+
NEW_START_DM_SELECTOR = ".sidebar-section-link[data-link-name='new-chat-dm']"
def has_no_start_new_dm?