Skip to content

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

DEV: Replace legacy admin user list state with tracked properties

Régis Hanol8839c5bca34a
diff --git a/frontend/discourse/admin/controllers/admin-users-list/show.js b/frontend/discourse/admin/controllers/admin-users-list/show.js
index e5ee6a5d042..ece475443ad 100644
--- a/frontend/discourse/admin/controllers/admin-users-list/show.js
+++ b/frontend/discourse/admin/controllers/admin-users-list/show.js
@@ -1,7 +1,6 @@
import { tracked } from "@glimmer/tracking";
import Controller from "@ember/controller";
-import { action, computed } from "@ember/object";
-import { dependentKeyCompat } from "@ember/object/compat";
+import { action } from "@ember/object";
import { trackedArray } from "@ember/reactive/collections";
import { service } from "@ember/service";
import BulkUserDeleteConfirmation from "discourse/admin/components/bulk-user-delete-confirmation";
@@ -29,36 +28,25 @@ export default class AdminUsersListShowController extends Controller {
@tracked listFilter = null;
@tracked initialFilter = null;
- query = null;
- order = null;
- asc = null;
- showEmails = false;
- lastSelected = null;
-
- _page = 1;
- _results = trackedArray();
- _canLoadMore = true;
+ @tracked query = null;
+ @tracked order = null;
+ @tracked asc = null;
+ @tracked showEmails = false;
- @computed("siteSettings.moderators_view_emails")
- get canModeratorsViewEmails() {
- return this.siteSettings.moderators_view_emails;
- }
+ lastSelected = null;
- @dependentKeyCompat
- get searchHint() {
- return i18n(`search_hint`);
- }
+ #page = 1;
+ #results = trackedArray();
+ #canLoadMore = true;
get users() {
- return this._results.flat();
+ return this.#results.flat();
}
- @computed("query")
get title() {
return i18n("admin.users.titles." + this.query);
}
- @computed("showEmails")
get columnCount() {
let colCount = 7; // note that the first column is hardcoded in the template
@@ -73,35 +61,22 @@ export default class AdminUsersListShowController extends Controller {
return colCount;
}
- @computed("model.id", "currentUser.id")
get canCheckEmails() {
return new CanCheckEmailsHelper(
this.model?.id,
- this.canModeratorsViewEmails,
+ this.siteSettings.moderators_view_emails,
this.currentUser
).canCheckEmails;
}
- @computed("model.id", "currentUser.id")
- get canAdminCheckEmails() {
- return new CanCheckEmailsHelper(
- this.model?.id,
- this.canModeratorsViewEmails,
- this.currentUser
- ).canAdminCheckEmails;
- }
-
- @computed("query")
get showSilenceReason() {
return this.query === "silenced";
}
- @computed("query")
get showSuspendReason() {
return this.query === "suspended";
}
- @computed("query")
get showActivationFilter() {
return this.query === "new";
}
@@ -120,10 +95,10 @@ export default class AdminUsersListShowController extends Controller {
}
resetFilters() {
- this._page = 1;
- this._results.length = 0;
- this._canLoadMore = true;
- return this._refreshUsers();
+ this.#page = 1;
+ this.#results.length = 0;
+ this.#canLoadMore = true;
+ return this.#refreshUsers();
}
stripHtml(html) {
@@ -157,13 +132,13 @@ export default class AdminUsersListShowController extends Controller {
if (this.refreshing) {
return;
}
- this._page += 1;
- this._refreshUsers();
+ this.#page += 1;
+ this.#refreshUsers();
}
@action
toggleEmailVisibility() {
- this.toggleProperty("showEmails");
+ this.showEmails = !this.showEmails;
this.resetFilters();
}
@@ -323,13 +298,13 @@ export default class AdminUsersListShowController extends Controller {
});
}
- _refreshUsers() {
- if (!this._canLoadMore) {
+ #refreshUsers() {
+ if (!this.#canLoadMore) {
return;
}
- const page = this._page;
- this.set("refreshing", true);
+ const page = this.#page;
+ this.refreshing = true;
return AdminUser.findAll(this.query, {
filter: this.listFilter,
@@ -340,13 +315,13 @@ export default class AdminUsersListShowController extends Controller {
page,
})
.then((result) => {
- this._results[page] = result;
+ this.#results[page] = result;
if (result.length === 0) {
- this._canLoadMore = false;
+ this.#canLoadMore = false;
}
})
.finally(() => {
- this.set("refreshing", false);
+ this.refreshing = false;
});
}
}
diff --git a/frontend/discourse/admin/routes/admin-users-list/show.js b/frontend/discourse/admin/routes/admin-users-list/show.js
index 6bbe889a0f2..59d73b64fd0 100644
--- a/frontend/discourse/admin/routes/admin-users-list/show.js
+++ b/frontend/discourse/admin/routes/admin-users-list/show.js
@@ -7,43 +7,24 @@ export default class AdminUsersListShowRoute extends DiscourseRoute {
username: { refreshModel: true },
};
- // TODO: this has been introduced to fix a bug in admin-users-list-show
- // loading AdminUser model multiple times without refactoring the controller
- beforeModel(transition) {
- const routeName = "adminUsersList.show";
+ setupController(controller, model, transition) {
+ super.setupController(...arguments);
- if (transition.targetName === routeName) {
- const params = transition.routeInfos.find(
- (a) => a.name === routeName
- ).params;
- const controller = this.controllerFor(routeName);
- if (controller) {
- controller.setProperties({
- order: transition.to.queryParams.order,
- asc: transition.to.queryParams.asc,
- // `filter` is deliberately not a registered query param (its name
- // would clash with the :filter segment); `username` is kept as a
- // legacy fallback for old links. `initialFilter` seeds the filter
- // controls input; `listFilter` is the live value sent to the server.
- listFilter:
- transition.to.queryParams.filter ??
- transition.to.queryParams.username,
- initialFilter:
- transition.to.queryParams.filter ??
- transition.to.queryParams.username,
- query: params.filter,
- activation:
- params.filter === "new"
- ? transition.to.queryParams.activation
- : null,
- refreshing: false,
- bulkSelectedUsersMap: {},
- bulkSelectedUserIdsSet: new Set(),
- displayBulkActions: false,
- });
+ const query = transition.to.params.filter;
+ const { queryParams } = transition.to;
+ const filter = queryParams.filter ?? queryParams.username;
- controller.resetFilters();
- }
- }
+ controller.setProperties({
+ order: queryParams.order,
+ asc: queryParams.asc,
+ listFilter: filter,
+ initialFilter: filter,
+ query,
+ activation: query === "new" ? queryParams.activation : null,
+ bulkSelectedUsersMap: {},
+ displayBulkActions: false,
+ });
+
+ controller.resetFilters();
}
}
diff --git a/frontend/discourse/admin/templates/admin-users-list/show.gjs b/frontend/discourse/admin/templates/admin-users-list/show.gjs
index 34183b73166..a6572a6f014 100644
--- a/frontend/discourse/admin/templates/admin-users-list/show.gjs
+++ b/frontend/discourse/admin/templates/admin-users-list/show.gjs
@@ -67,7 +67,7 @@ export default <template>
}}
@dropdownValue={{or @controller.activation "all"}}
@initialTextFilter={{@controller.initialFilter}}
- @inputPlaceholder={{@controller.searchHint}}
+ @inputPlaceholder={{i18n "search_hint"}}
@loading={{@controller.refreshing}}
@noResultsMessage={{i18n "search.no_results"}}
@onDropdownFilterChange={{@controller.onActivationChange}}
diff --git a/frontend/discourse/tests/acceptance/admin-users-list-test.js b/frontend/discourse/tests/acceptance/admin-users-list-test.js
index eebc0ced66c..e725a1d8675 100644
--- a/frontend/discourse/tests/acceptance/admin-users-list-test.js
+++ b/frontend/discourse/tests/acceptance/admin-users-list-test.js
@@ -185,6 +185,26 @@ acceptance("Admin - Users List", function (needs) {
});
});
+acceptance("Admin - Users List - email permissions", function (needs) {
+ needs.user({ admin: false, moderator: true });
+ needs.settings({ moderators_view_emails: false });
+
+ test("shows the email action only when moderators can view emails", async function (assert) {
+ await visit("/admin/users/list/active");
+
+ assert
+ .dom(".admin-users__subheader-show-emails")
+ .doesNotExist("hides the unauthorized action");
+
+ this.siteSettings.moderators_view_emails = true;
+ await visit("/admin/users/list/new");
+
+ assert
+ .dom(".admin-users__subheader-show-emails")
+ .exists("shows the permitted action");
+ });
+});
+
acceptance("Admin - Users List - bulk search", function (needs) {
needs.user();