FEATURE: Expand messages filter links when viewing private messages. (#17106)

This commit is contained in:
Alan Guo Xiang Tan 2022-06-20 11:43:01 +08:00 committed by GitHub
parent 2f66eb59c2
commit 986060a850
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 177 additions and 61 deletions

View File

@ -1,5 +1,6 @@
import { cached } from "@glimmer/tracking"; import { cached } from "@glimmer/tracking";
import { getOwner } from "discourse-common/lib/get-owner";
import GlimmerComponent from "discourse/components/glimmer"; import GlimmerComponent from "discourse/components/glimmer";
import GroupMessageSectionLink from "discourse/lib/sidebar/messages-section/group-message-section-link"; import GroupMessageSectionLink from "discourse/lib/sidebar/messages-section/group-message-section-link";
import PersonalMessageSectionLink from "discourse/lib/sidebar/messages-section/personal-message-section-link"; import PersonalMessageSectionLink from "discourse/lib/sidebar/messages-section/personal-message-section-link";
@ -10,8 +11,15 @@ const SENT = "sent";
const NEW = "new"; const NEW = "new";
const ARCHIVE = "archive"; const ARCHIVE = "archive";
export const PERSONAL_MESSAGES_INBOXES = [INBOX, UNREAD, NEW, SENT, ARCHIVE]; export const PERSONAL_MESSAGES_INBOX_FILTERS = [
export const GROUP_MESSAGES_INBOXES = [INBOX, UNREAD, NEW, ARCHIVE]; INBOX,
NEW,
UNREAD,
SENT,
ARCHIVE,
];
export const GROUP_MESSAGES_INBOX_FILTERS = [INBOX, NEW, UNREAD, ARCHIVE];
export default class SidebarMessagesSection extends GlimmerComponent { export default class SidebarMessagesSection extends GlimmerComponent {
constructor() { constructor() {
@ -37,19 +45,30 @@ export default class SidebarMessagesSection extends GlimmerComponent {
currentRouteParentName, currentRouteParentName,
currentRouteParams, currentRouteParams,
}) { }) {
const sectionLinks = [ if (
...this.personalMessagesSectionLinks, currentRouteParentName !== "userPrivateMessages" &&
...this.groupMessagesSectionLinks, currentRouteParentName !== "topic"
]; ) {
for (const sectionLink of this.allSectionLinks) {
if (currentRouteParentName !== "userPrivateMessages") {
sectionLinks.forEach((sectionLink) => {
sectionLink.collapse(); sectionLink.collapse();
}); }
} else { } else {
sectionLinks.forEach((sectionLink) => { const attrs = {
sectionLink.pageChanged(currentRouteName, currentRouteParams); currentRouteName,
}); currentRouteParams,
};
if (currentRouteParentName === "topic") {
const topicController = getOwner(this).lookup("controller:topic");
if (topicController.model.isPrivateMessage) {
attrs.privateMessageTopic = topicController.model;
}
}
for (const sectionLink of this.allSectionLinks) {
sectionLink.pageChanged(attrs);
}
} }
} }
@ -57,7 +76,7 @@ export default class SidebarMessagesSection extends GlimmerComponent {
get personalMessagesSectionLinks() { get personalMessagesSectionLinks() {
const links = []; const links = [];
PERSONAL_MESSAGES_INBOXES.forEach((type) => { PERSONAL_MESSAGES_INBOX_FILTERS.forEach((type) => {
links.push( links.push(
new PersonalMessageSectionLink({ new PersonalMessageSectionLink({
currentUser: this.currentUser, currentUser: this.currentUser,
@ -74,7 +93,7 @@ export default class SidebarMessagesSection extends GlimmerComponent {
const links = []; const links = [];
this.currentUser.groupsWithMessages.forEach((group) => { this.currentUser.groupsWithMessages.forEach((group) => {
GROUP_MESSAGES_INBOXES.forEach((groupMessageLink) => { GROUP_MESSAGES_INBOX_FILTERS.forEach((groupMessageLink) => {
links.push( links.push(
new GroupMessageSectionLink({ new GroupMessageSectionLink({
group, group,
@ -87,4 +106,11 @@ export default class SidebarMessagesSection extends GlimmerComponent {
return links; return links;
} }
get allSectionLinks() {
return [
...this.groupMessagesSectionLinks,
...this.personalMessagesSectionLinks,
];
}
} }

View File

@ -1,13 +1,9 @@
import I18n from "I18n"; import I18n from "I18n";
import { tracked } from "@glimmer/tracking";
import { capitalize } from "@ember/string"; import { capitalize } from "@ember/string";
import MessageSectionLink from "discourse/lib/sidebar/messages-section/message-section-link";
import { INBOX } from "discourse/components/sidebar/messages-section"; export default class GroupMessageSectionLink extends MessageSectionLink {
export default class GroupMessageSectionLink {
@tracked shouldDisplay = this._isInbox;
routeNames = new Set([ routeNames = new Set([
"userPrivateMessages.group", "userPrivateMessages.group",
"userPrivateMessages.groupUnread", "userPrivateMessages.groupUnread",
@ -15,13 +11,6 @@ export default class GroupMessageSectionLink {
"userPrivateMessages.groupArchive", "userPrivateMessages.groupArchive",
]); ]);
constructor({ group, type, currentUser, router }) {
this.group = group;
this.type = type;
this.currentUser = currentUser;
this.router = router;
}
get name() { get name() {
return `group-messages-${this.type}`; return `group-messages-${this.type}`;
} }
@ -56,25 +45,22 @@ export default class GroupMessageSectionLink {
} }
} }
collapse() { pageChanged({ currentRouteName, currentRouteParams, privateMessageTopic }) {
if (this._isInbox) { if (this._isInbox) {
return; return;
} }
this.shouldDisplay = false; if (
} privateMessageTopic?.allowedGroups?.some(
(g) => g.name === this.group.name
pageChanged(currentRouteName, currentRouteParams) { )
if (this._isInbox) { ) {
this.setDisplayState = true;
return; return;
} }
this.shouldDisplay = this.setDisplayState =
this.routeNames.has(currentRouteName) && this.routeNames.has(currentRouteName) &&
currentRouteParams.name.toLowerCase() === this.group.name.toLowerCase(); currentRouteParams.name.toLowerCase() === this.group.name.toLowerCase();
} }
get _isInbox() {
return this.type === INBOX;
}
} }

View File

@ -0,0 +1,46 @@
import { tracked } from "@glimmer/tracking";
import { INBOX } from "discourse/components/sidebar/messages-section";
export default class MessageSectionLink {
@tracked shouldDisplay = this._isInbox;
constructor({ group, currentUser, type }) {
this.group = group;
this.currentUser = currentUser;
this.type = type;
}
set setDisplayState(value) {
this.shouldDisplay = value;
}
get inboxFilter() {
throw "not implemented";
}
expand() {
if (this._isInbox) {
return;
}
this.setDisplayState = true;
}
collapse() {
if (this._isInbox) {
return;
}
this.setDisplayState = false;
}
// eslint-disable-next-line no-unused-vars
pageChanged({ currentRouteName, currentRouteParams, privateMessageTopic }) {
throw "not implemented";
}
get _isInbox() {
return this.type === INBOX;
}
}

View File

@ -1,11 +1,8 @@
import I18n from "I18n"; import I18n from "I18n";
import { tracked } from "@glimmer/tracking"; import MessageSectionLink from "discourse/lib/sidebar/messages-section/message-section-link";
import { INBOX } from "discourse/components/sidebar/messages-section";
export default class PersonalMessageSectionLink {
@tracked shouldDisplay = this._isInbox;
export default class PersonalMessageSectionLink extends MessageSectionLink {
routeNames = new Set([ routeNames = new Set([
"userPrivateMessages.index", "userPrivateMessages.index",
"userPrivateMessages.unread", "userPrivateMessages.unread",
@ -14,16 +11,14 @@ export default class PersonalMessageSectionLink {
"userPrivateMessages.archive", "userPrivateMessages.archive",
]); ]);
constructor({ currentUser, type, router }) {
this.currentUser = currentUser;
this.type = type;
this.router = router;
}
get name() { get name() {
return `personal-messages-${this.type}`; return `personal-messages-${this.type}`;
} }
get class() {
return `personal-messages`;
}
get route() { get route() {
if (this._isInbox) { if (this._isInbox) {
return "userPrivateMessages.index"; return "userPrivateMessages.index";
@ -46,23 +41,16 @@ export default class PersonalMessageSectionLink {
return I18n.t(`sidebar.sections.messages.links.${this.type}`); return I18n.t(`sidebar.sections.messages.links.${this.type}`);
} }
collapse() { pageChanged({ currentRouteName, privateMessageTopic }) {
if (this._isInbox) { if (this._isInbox) {
return; return;
} }
this.shouldDisplay = false; if (privateMessageTopic?.allowedGroups?.length === 0) {
} this.setDisplayState = true;
pageChanged(currentRouteName) {
if (this._isInbox) {
return; return;
} }
this.shouldDisplay = this.routeNames.has(currentRouteName); this.setDisplayState = this.routeNames.has(currentRouteName);
}
get _isInbox() {
return this.type === INBOX;
} }
} }

View File

@ -70,6 +70,7 @@ const Topic = RestModel.extend({
lastPosterUser: alias("lastPoster.user"), lastPosterUser: alias("lastPoster.user"),
lastPosterGroup: alias("lastPoster.primary_group"), lastPosterGroup: alias("lastPoster.primary_group"),
allowedGroups: alias("details.allowed_groups"),
@discourseComputed("posters.[]", "participants.[]", "allowed_user_count") @discourseComputed("posters.[]", "participants.[]", "allowed_user_count")
featuredUsers(posters, participants, allowedUserCount) { featuredUsers(posters, participants, allowedUserCount) {

View File

@ -11,6 +11,7 @@
{{#if personalMessageSectionLink.shouldDisplay}} {{#if personalMessageSectionLink.shouldDisplay}}
<Sidebar::SectionLink <Sidebar::SectionLink
@linkName={{personalMessageSectionLink.name}} @linkName={{personalMessageSectionLink.name}}
@class={{personalMessageSectionLink.class}}
@route={{personalMessageSectionLink.route}} @route={{personalMessageSectionLink.route}}
@model={{personalMessageSectionLink.model}} @model={{personalMessageSectionLink.model}}
@currentWhen={{personalMessageSectionLink.currentWhen}} @currentWhen={{personalMessageSectionLink.currentWhen}}

View File

@ -287,5 +287,73 @@ acceptance(
); );
}); });
}); });
test("viewing personal message topic with a group the user is a part of", async function (assert) {
updateCurrentUser({
groups: [
{
name: "foo_group", // based on fixtures
has_messages: true,
},
],
});
await visit("/t/130");
assert.strictEqual(
queryAll(".sidebar-section-messages .sidebar-section-link").length,
5,
"5 section links are displayed"
);
assert.strictEqual(
queryAll(
".sidebar-section-messages .sidebar-section-link.personal-messages"
).length,
1,
"personal messages inbox filter links are not shown"
);
assert.strictEqual(
queryAll(".sidebar-section-messages .sidebar-section-link.foo_group")
.length,
4,
"foo_group messages inbox filter links are shown"
);
});
test("viewing personal message topic", async function (assert) {
updateCurrentUser({
groups: [
{
name: "foo_group", // based on fixtures
has_messages: true,
},
],
});
await visit("/t/34");
assert.strictEqual(
queryAll(".sidebar-section-messages .sidebar-section-link").length,
6,
"6 section links are displayed"
);
assert.strictEqual(
queryAll(
".sidebar-section-messages .sidebar-section-link.personal-messages"
).length,
5,
"personal messages inbox filter links are shown"
);
assert.strictEqual(
queryAll(".sidebar-section-messages .sidebar-section-link.foo_group")
.length,
1,
"foo_group messages inbox filter links are not shown"
);
});
} }
); );