DEV: Implement "My Posts" section link for experimental sidebar (#17008)

This commit is contained in:
Alan Guo Xiang Tan 2022-06-07 10:52:54 +08:00 committed by GitHub
parent a258630790
commit 7da074d591
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 165 additions and 23 deletions

View File

@ -6,6 +6,7 @@ import { customSectionLinks } from "discourse/lib/sidebar/custom-topics-section-
import EverythingSectionLink from "discourse/lib/sidebar/topics-section/everything-section-link";
import TrackedSectionLink from "discourse/lib/sidebar/topics-section/tracked-section-link";
import BookmarkedSectionLink from "discourse/lib/sidebar/topics-section/bookmarked-section-link";
import MyPostsSectionLink from "discourse/lib/sidebar/topics-section/my-posts-section-link";
import { action } from "@ember/object";
import { next } from "@ember/runloop";
@ -14,6 +15,7 @@ const DEFAULT_SECTION_LINKS = [
EverythingSectionLink,
TrackedSectionLink,
BookmarkedSectionLink,
MyPostsSectionLink,
];
export default class SidebarTopicsSection extends GlimmerComponent {
@ -23,6 +25,7 @@ export default class SidebarTopicsSection extends GlimmerComponent {
return new sectionLinkClass({
topicTrackingState: this.topicTrackingState,
currentUser: this.currentUser,
appEvents: this.appEvents,
});
});

View File

@ -1,15 +0,0 @@
export default {
name: "subscribe-user-changes",
after: "message-bus",
initialize(container) {
const user = container.lookup("current-user:main");
if (user) {
const bus = container.lookup("message-bus:main");
bus.subscribe("/user", (data) => {
user.setProperties(data);
});
}
},
};

View File

@ -108,6 +108,10 @@ export default {
user.notification_channel_position
);
bus.subscribe(`/user-drafts/${user.id}`, (data) => {
user.updateDraftProperties(data);
});
bus.subscribe(`/do-not-disturb/${user.get("id")}`, (data) => {
user.updateDoNotDisturbStatus(data.ends_at);
});

View File

@ -2,9 +2,10 @@
* Base class representing a sidebar topics section link interface.
*/
export default class BaseSectionLink {
constructor({ topicTrackingState, currentUser } = {}) {
constructor({ topicTrackingState, currentUser, appEvents } = {}) {
this.topicTrackingState = topicTrackingState;
this.currentUser = currentUser;
this.appEvents = appEvents;
}
/**

View File

@ -0,0 +1,65 @@
import I18n from "I18n";
import { tracked } from "@glimmer/tracking";
import BaseSectionLink from "discourse/lib/sidebar/topics-section/base-section-link";
const USER_DRAFTS_CHANGED_EVENT = "user-drafts:changed";
export default class MyPostsSectionLink extends BaseSectionLink {
@tracked draftCount = this.currentUser.draft_count;
constructor() {
super(...arguments);
this.appEvents.on(USER_DRAFTS_CHANGED_EVENT, this, this._updateDraftCount);
}
teardown() {
this.appEvents.off(USER_DRAFTS_CHANGED_EVENT, this, this._updateDraftCount);
}
_updateDraftCount() {
this.draftCount = this.currentUser.draft_count;
}
get name() {
return "my-posts";
}
get route() {
if (this._hasDraft) {
return "userActivity.drafts";
} else {
return "userActivity.index";
}
}
get currentWhen() {
if (this._hasDraft) {
return "userActivity.index userActivity.drafts";
}
}
get model() {
return this.currentUser;
}
get title() {
return I18n.t("sidebar.sections.topics.links.my_posts.title");
}
get text() {
return I18n.t("sidebar.sections.topics.links.my_posts.content");
}
get badgeText() {
if (this._hasDraft) {
return I18n.t("sidebar.sections.topics.links.my_posts.draft_count", {
count: this.draftCount,
});
}
}
get _hasDraft() {
return this.draftCount > 0;
}
}

View File

@ -1002,6 +1002,11 @@ const User = RestModel.extend({
this.appEvents.trigger("do-not-disturb:changed", this.do_not_disturb_until);
},
updateDraftProperties(properties) {
this.setProperties(properties);
this.appEvents.trigger("user-drafts:changed");
},
isInDoNotDisturb() {
return (
this.do_not_disturb_until &&

View File

@ -203,6 +203,79 @@ acceptance("Sidebar - Topics Section", function (needs) {
}
);
conditionalTest(
"clicking on my posts link",
!isLegacyEmber(),
async function (assert) {
await visit("/t/280");
await click(".sidebar-section-topics .sidebar-section-link-my-posts");
assert.strictEqual(
currentURL(),
`/u/${loggedInUser().username}/activity`,
"it should transistion to the user's activity url"
);
assert.strictEqual(
queryAll(".sidebar-section-topics .sidebar-section-link.active").length,
1,
"only one link is marked as active"
);
assert.ok(
exists(".sidebar-section-topics .sidebar-section-link-my-posts.active"),
"the my posts link is marked as active"
);
await visit(`/u/${loggedInUser().username}/activity/drafts`);
assert.notOk(
exists(".sidebar-section-topics .sidebar-section-link-my-posts.active"),
"the my posts link is not marked as active when user has no drafts and visiting the user activity drafts URL"
);
}
);
conditionalTest(
"clicking on my posts link when user has a draft",
!isLegacyEmber(),
async function (assert) {
await visit("/t/280");
publishToMessageBus(`/user-drafts/${loggedInUser().id}`, {
draft_count: 1,
});
await settled();
await click(".sidebar-section-topics .sidebar-section-link-my-posts");
assert.strictEqual(
currentURL(),
`/u/${loggedInUser().username}/activity/drafts`,
"it transistions to the user's activity drafts url"
);
assert.strictEqual(
queryAll(".sidebar-section-topics .sidebar-section-link.active").length,
1,
"only one link is marked as active"
);
assert.ok(
exists(".sidebar-section-topics .sidebar-section-link-my-posts.active"),
"the my posts link is marked as active"
);
await visit(`/u/${loggedInUser().username}/activity`);
assert.ok(
exists(".sidebar-section-topics .sidebar-section-link-my-posts.active"),
"the my posts link is marked as active"
);
}
);
conditionalTest(
"visiting top route",
!isLegacyEmber(),

View File

@ -215,7 +215,7 @@ class UserStat < ActiveRecord::Base
SQL
MessageBus.publish(
'/user',
"/user-drafts/#{user_id}",
{
draft_count: draft_count,
has_topic_draft: !!has_topic_draft

View File

@ -4058,6 +4058,12 @@ en:
bookmarked:
content: "Bookmarked"
title: "All bookmarked topics"
my_posts:
content: "My Posts"
title: "My posts"
draft_count:
one: "%{count} draft"
other: "%{count} drafts"
# This section is exported to the javascript for i18n in the admin section
admin_js:

View File

@ -174,9 +174,9 @@ describe PostCreator do
"/latest",
"/topic/#{created_post.topic_id}",
"/topic/#{created_post.topic_id}",
"/user",
"/user",
"/user"
"/user-drafts/#{admin.id}",
"/user-drafts/#{admin.id}",
"/user-drafts/#{admin.id}",
].sort
)
@ -205,7 +205,7 @@ describe PostCreator do
user_action = messages.find { |m| m.channel == "/u/#{p.user.username}" }
expect(user_action).not_to eq(nil)
draft_count = messages.find { |m| m.channel == "/user" }
draft_count = messages.find { |m| m.channel == "/user-drafts/#{p.user_id}" }
expect(draft_count).not_to eq(nil)
expect(messages.filter { |m| m.channel != "/distributed_hash" }.length).to eq(7)

View File

@ -179,14 +179,14 @@ describe Draft do
it 'updates draft count when a draft is created or destroyed' do
Draft.set(Fabricate(:user), Draft::NEW_TOPIC, 0, "data")
messages = MessageBus.track_publish("/user") do
messages = MessageBus.track_publish("/user-drafts/#{user.id}") do
Draft.set(user, Draft::NEW_TOPIC, 0, "data")
end
expect(messages.first.data[:draft_count]).to eq(1)
expect(messages.first.data[:has_topic_draft]).to eq(true)
messages = MessageBus.track_publish("/user") do
messages = MessageBus.track_publish("/user-drafts/#{user.id}") do
Draft.where(user: user).destroy_all
end