2020-02-28 10:01:48 -05:00
|
|
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
|
|
|
import User from "discourse/models/user";
|
|
|
|
import EmberObject from "@ember/object";
|
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: "user-card-directory",
|
2020-03-09 12:37:29 -04:00
|
|
|
initialize(container) {
|
|
|
|
// This component provides a responsive template
|
|
|
|
// Delete the core mobile one
|
|
|
|
delete Ember.TEMPLATES["mobile/users"];
|
|
|
|
|
2020-02-28 10:01:48 -05:00
|
|
|
withPluginApi("0.8.7", api => {
|
2020-03-09 12:37:29 -04:00
|
|
|
api.modifyClass("route:users", {
|
2020-03-10 06:09:30 -04:00
|
|
|
resetController(controller, isExiting) {
|
|
|
|
this._super(...arguments);
|
|
|
|
if (isExiting) {
|
2020-03-09 12:37:29 -04:00
|
|
|
controller.set("cachedUserCardInfo", {});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-02-28 10:01:48 -05:00
|
|
|
api.modifyClass("controller:users", {
|
2020-03-10 06:09:30 -04:00
|
|
|
cachedUserCardInfo: null,
|
|
|
|
|
|
|
|
init(){
|
|
|
|
this.set("cachedUserCardInfo", {});
|
|
|
|
this._super(...arguments);
|
|
|
|
},
|
2020-03-09 12:37:29 -04:00
|
|
|
|
|
|
|
stats: [
|
|
|
|
{ name: "likes_received", icon: "heart" },
|
|
|
|
{ name: "likes_given", icon: "heart" },
|
|
|
|
{ name: "topic_count" },
|
|
|
|
{ name: "post_count" },
|
|
|
|
{ name: "topics_entered" },
|
|
|
|
{ name: "posts_read" },
|
|
|
|
{ name: "days_visited" }
|
|
|
|
],
|
|
|
|
|
|
|
|
@discourseComputed("site.groups")
|
|
|
|
availableGroups(groups) {
|
|
|
|
return groups
|
|
|
|
.map(g => {
|
|
|
|
// prevents group "everyone" to be listed
|
|
|
|
if (g.id !== 0) {
|
|
|
|
return { name: g.name, value: g.name };
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter(Boolean);
|
|
|
|
},
|
|
|
|
|
2020-02-28 10:01:48 -05:00
|
|
|
@discourseComputed("model.content.@each")
|
|
|
|
userCards(allUsers) {
|
2020-06-19 16:24:06 -04:00
|
|
|
if (!allUsers) return [];
|
2020-03-09 12:37:29 -04:00
|
|
|
const toLoad = [];
|
|
|
|
const userCardInfos = allUsers.map(u => {
|
|
|
|
if (this.cachedUserCardInfo[u.id]) {
|
|
|
|
return this.cachedUserCardInfo[u.id];
|
|
|
|
}
|
2020-02-28 10:01:48 -05:00
|
|
|
|
2020-03-09 12:37:29 -04:00
|
|
|
const userCardInfo = (this.cachedUserCardInfo[
|
|
|
|
u.id
|
|
|
|
] = EmberObject.create({
|
|
|
|
user: User.create(u.user),
|
|
|
|
directoryItem: u,
|
|
|
|
loading: true
|
|
|
|
}));
|
2020-02-28 10:01:48 -05:00
|
|
|
|
2020-03-09 12:37:29 -04:00
|
|
|
toLoad.push(userCardInfo);
|
2020-02-28 10:01:48 -05:00
|
|
|
|
2020-03-09 12:37:29 -04:00
|
|
|
return userCardInfo;
|
|
|
|
});
|
2020-02-28 10:01:48 -05:00
|
|
|
|
2020-03-09 12:37:29 -04:00
|
|
|
const loadMax = 50;
|
2020-02-28 10:01:48 -05:00
|
|
|
|
2020-03-09 12:37:29 -04:00
|
|
|
while (toLoad.length > 0) {
|
|
|
|
const thisBatch = toLoad.splice(0, loadMax);
|
|
|
|
const promise = ajax("/user-cards.json", {
|
|
|
|
data: { user_ids: thisBatch.map(uc => uc.user.id).join(",") }
|
|
|
|
});
|
|
|
|
thisBatch.forEach(uc => {
|
2020-03-10 06:09:30 -04: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
|
2020-03-09 12:37:29 -04:00
|
|
|
const convertedPromise = promise.then(data => {
|
|
|
|
// 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, {
|
|
|
|
user: data.users.find(u => u.id === uc.user.id)
|
|
|
|
});
|
2020-02-28 10:01:48 -05:00
|
|
|
});
|
2020-03-09 12:37:29 -04:00
|
|
|
return uc.user
|
|
|
|
.findDetails({ existingRequest: convertedPromise })
|
2020-03-10 06:09:30 -04:00
|
|
|
.finally(() => uc.set("loading", false));
|
2020-03-09 12:37:29 -04:00
|
|
|
});
|
|
|
|
}
|
2020-02-28 10:01:48 -05:00
|
|
|
|
2020-03-09 12:37:29 -04:00
|
|
|
return userCardInfos;
|
2020-02-28 10:01:48 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-03-09 12:37:29 -04:00
|
|
|
};
|