2020-02-28 07:01:48 -08:00
|
|
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
|
|
|
import User from "discourse/models/user";
|
2022-10-17 02:06:20 -07:00
|
|
|
import EmberObject, { action } from "@ember/object";
|
2020-02-28 07:01:48 -08:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2022-10-17 02:06:20 -07:00
|
|
|
import DiscourseURL, { userPath } from "discourse/lib/url";
|
2020-02-28 07:01:48 -08:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: "user-card-directory",
|
2022-06-18 07:52:34 -07:00
|
|
|
initialize() {
|
2021-07-09 06:33:56 -07:00
|
|
|
withPluginApi("0.8.7", (api) => {
|
2020-03-09 09:37:29 -07:00
|
|
|
api.modifyClass("route:users", {
|
2022-06-18 07:52:34 -07:00
|
|
|
pluginId: "user-card-directory",
|
2024-01-09 04:44:56 -08:00
|
|
|
|
|
|
|
get templateName() {
|
|
|
|
if (this.modelFor("users")?.showAsCards) {
|
|
|
|
return "users-as-card-directory";
|
|
|
|
} else {
|
|
|
|
return "users";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-03-10 03:09:30 -07:00
|
|
|
resetController(controller, isExiting) {
|
|
|
|
this._super(...arguments);
|
|
|
|
if (isExiting) {
|
2020-03-09 09:37:29 -07:00
|
|
|
controller.set("cachedUserCardInfo", {});
|
|
|
|
}
|
2021-07-09 06:33:56 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
queryParams: {
|
|
|
|
cards: { refreshModel: true },
|
|
|
|
},
|
|
|
|
|
|
|
|
model(params) {
|
|
|
|
return this._super(params).then((model) => {
|
2024-01-09 04:44:56 -08:00
|
|
|
model.showAsCards =
|
|
|
|
params["cards"] === "yes" ||
|
|
|
|
(params["cards"] === undefined &&
|
|
|
|
settings.default_view === "cards");
|
2021-07-09 06:33:56 -07:00
|
|
|
return model;
|
|
|
|
});
|
|
|
|
},
|
2020-03-09 09:37:29 -07:00
|
|
|
});
|
|
|
|
|
2020-02-28 07:01:48 -08:00
|
|
|
api.modifyClass("controller:users", {
|
2022-06-18 07:52:34 -07:00
|
|
|
pluginId: "user-card-directory",
|
2020-03-10 03:09:30 -07:00
|
|
|
cachedUserCardInfo: null,
|
|
|
|
|
2022-06-18 07:52:34 -07:00
|
|
|
init() {
|
2020-03-10 03:09:30 -07:00
|
|
|
this.set("cachedUserCardInfo", {});
|
|
|
|
this._super(...arguments);
|
|
|
|
},
|
2020-03-09 09:37:29 -07:00
|
|
|
|
2020-02-28 07:01:48 -08:00
|
|
|
@discourseComputed("model.content.@each")
|
|
|
|
userCards(allUsers) {
|
2022-06-18 07:52:34 -07:00
|
|
|
if (!allUsers) {
|
|
|
|
return [];
|
|
|
|
}
|
2020-03-09 09:37:29 -07:00
|
|
|
const toLoad = [];
|
2020-11-24 12:08:20 -08:00
|
|
|
if (settings.hide_current_user && this.currentUser) {
|
2021-07-01 09:50:38 -07:00
|
|
|
allUsers = allUsers.filter((u) => u.id !== this.currentUser.id);
|
2020-11-24 12:08:20 -08:00
|
|
|
}
|
2024-02-21 17:48:36 -08:00
|
|
|
|
|
|
|
const filteredUsers = allUsers.filter(u => u.user.user_fields["3"] === "true");
|
|
|
|
const userCardInfos = filteredUsers.map((u) => {
|
2020-03-09 09:37:29 -07:00
|
|
|
if (this.cachedUserCardInfo[u.id]) {
|
|
|
|
return this.cachedUserCardInfo[u.id];
|
|
|
|
}
|
2020-02-28 07:01:48 -08:00
|
|
|
|
2022-06-18 07:52:34 -07:00
|
|
|
const userCardInfo = (this.cachedUserCardInfo[u.id] =
|
|
|
|
EmberObject.create({
|
|
|
|
user: User.create(u.user),
|
|
|
|
directoryItem: u,
|
|
|
|
loading: true,
|
|
|
|
}));
|
2020-02-28 07:01:48 -08:00
|
|
|
|
2020-03-09 09:37:29 -07:00
|
|
|
toLoad.push(userCardInfo);
|
2020-02-28 07:01:48 -08:00
|
|
|
|
2020-03-09 09:37:29 -07:00
|
|
|
return userCardInfo;
|
|
|
|
});
|
2020-02-28 07:01:48 -08:00
|
|
|
|
2020-03-09 09:37:29 -07:00
|
|
|
const loadMax = 50;
|
2020-02-28 07:01:48 -08:00
|
|
|
|
2020-03-09 09:37:29 -07:00
|
|
|
while (toLoad.length > 0) {
|
|
|
|
const thisBatch = toLoad.splice(0, loadMax);
|
|
|
|
const promise = ajax("/user-cards.json", {
|
2022-06-18 07:52:34 -07:00
|
|
|
data: { user_ids: thisBatch.map((uc) => uc.user.id).join(",") },
|
2020-03-09 09:37:29 -07:00
|
|
|
});
|
2022-06-18 07:52:34 -07:00
|
|
|
thisBatch.forEach((uc) => {
|
2020-03-10 03:09:30 -07:00
|
|
|
// Each user card expects its own promise
|
|
|
|
// Rather than making a separate AJAX request for each
|
|
|
|
// We re-use the `user-cards.json` promise, and manipulate the data
|
2022-06-18 07:52:34 -07:00
|
|
|
const convertedPromise = promise.then((data) => {
|
2020-03-09 09:37:29 -07:00
|
|
|
// Find the correct user from users, and put it in the user attribute
|
|
|
|
// Use Object.assign to avoid contaminating the source object
|
|
|
|
return Object.assign({}, data, {
|
2022-06-18 07:52:34 -07:00
|
|
|
user: data.users.find((u) => u.id === uc.user.id),
|
2020-03-09 09:37:29 -07:00
|
|
|
});
|
2020-02-28 07:01:48 -08:00
|
|
|
});
|
2020-03-09 09:37:29 -07:00
|
|
|
return uc.user
|
|
|
|
.findDetails({ existingRequest: convertedPromise })
|
2020-03-10 03:09:30 -07:00
|
|
|
.finally(() => uc.set("loading", false));
|
2020-03-09 09:37:29 -07:00
|
|
|
});
|
|
|
|
}
|
2020-02-28 07:01:48 -08:00
|
|
|
|
2024-02-21 20:37:45 -08:00
|
|
|
window.scrollBy(0, 5);
|
|
|
|
window.scrollBy(0, -5);
|
|
|
|
|
2020-03-09 09:37:29 -07:00
|
|
|
return userCardInfos;
|
2022-06-18 07:52:34 -07:00
|
|
|
},
|
2022-10-17 02:06:20 -07:00
|
|
|
|
|
|
|
@action
|
|
|
|
userCardShowUser(user) {
|
|
|
|
DiscourseURL.routeTo(userPath(user.username_lower));
|
|
|
|
},
|
2020-02-28 07:01:48 -08:00
|
|
|
});
|
|
|
|
});
|
2022-06-18 07:52:34 -07:00
|
|
|
},
|
2020-03-09 09:37:29 -07:00
|
|
|
};
|