2021-05-25 12:37:32 -04:00
|
|
|
import Controller, { inject as controller } from "@ember/controller";
|
2019-04-01 06:39:49 -04:00
|
|
|
import AdminDashboard from "admin/models/admin-dashboard";
|
|
|
|
import VersionCheck from "admin/models/version-check";
|
2020-04-30 11:31:04 -04:00
|
|
|
import { computed } from "@ember/object";
|
2019-11-07 16:38:28 -05:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2019-04-01 06:39:49 -04:00
|
|
|
import { setting } from "discourse/lib/computed";
|
2016-08-03 15:36:41 -04:00
|
|
|
|
2019-04-01 06:39:49 -04:00
|
|
|
const PROBLEMS_CHECK_MINUTES = 1;
|
2013-02-20 13:15:50 -05:00
|
|
|
|
2019-10-23 13:06:54 -04:00
|
|
|
export default Controller.extend({
|
2019-04-01 06:39:49 -04:00
|
|
|
isLoading: false,
|
2016-08-03 15:36:41 -04:00
|
|
|
dashboardFetchedAt: null,
|
2021-05-25 12:37:32 -04:00
|
|
|
exceptionController: controller("exception"),
|
2019-04-01 06:39:49 -04:00
|
|
|
showVersionChecks: setting("version_checks"),
|
|
|
|
|
2022-01-03 19:14:33 -05:00
|
|
|
@discourseComputed(
|
|
|
|
"lowPriorityProblems.length",
|
|
|
|
"highPriorityProblems.length"
|
|
|
|
)
|
|
|
|
foundProblems(lowPriorityProblemsLength, highPriorityProblemsLength) {
|
|
|
|
const problemsLength =
|
|
|
|
lowPriorityProblemsLength + highPriorityProblemsLength;
|
|
|
|
return this.currentUser.admin && problemsLength > 0;
|
2019-04-01 06:39:49 -04:00
|
|
|
},
|
|
|
|
|
2020-04-30 11:31:04 -04:00
|
|
|
visibleTabs: computed("siteSettings.dashboard_visible_tabs", function () {
|
|
|
|
return (this.siteSettings.dashboard_visible_tabs || "")
|
|
|
|
.split("|")
|
|
|
|
.filter(Boolean);
|
|
|
|
}),
|
|
|
|
|
|
|
|
isModerationTabVisible: computed("visibleTabs", function () {
|
|
|
|
return this.visibleTabs.includes("moderation");
|
|
|
|
}),
|
|
|
|
|
|
|
|
isSecurityTabVisible: computed("visibleTabs", function () {
|
|
|
|
return this.visibleTabs.includes("security");
|
|
|
|
}),
|
|
|
|
|
|
|
|
isReportsTabVisible: computed("visibleTabs", function () {
|
|
|
|
return this.visibleTabs.includes("reports");
|
|
|
|
}),
|
|
|
|
|
2019-04-01 06:39:49 -04:00
|
|
|
fetchProblems() {
|
2020-09-22 10:28:28 -04:00
|
|
|
if (this.isLoadingProblems) {
|
|
|
|
return;
|
|
|
|
}
|
2019-04-01 06:39:49 -04:00
|
|
|
|
|
|
|
if (
|
2019-05-27 04:15:39 -04:00
|
|
|
!this.problemsFetchedAt ||
|
2019-04-01 06:39:49 -04:00
|
|
|
moment().subtract(PROBLEMS_CHECK_MINUTES, "minutes").toDate() >
|
2019-05-27 04:15:39 -04:00
|
|
|
this.problemsFetchedAt
|
2019-04-01 06:39:49 -04:00
|
|
|
) {
|
|
|
|
this._loadProblems();
|
|
|
|
}
|
|
|
|
},
|
2014-09-11 16:30:47 -04:00
|
|
|
|
2016-08-03 15:36:41 -04:00
|
|
|
fetchDashboard() {
|
2019-04-01 06:39:49 -04:00
|
|
|
const versionChecks = this.siteSettings.version_checks;
|
|
|
|
|
2020-09-22 10:28:28 -04:00
|
|
|
if (this.isLoading || !versionChecks) {
|
|
|
|
return;
|
|
|
|
}
|
2019-04-01 06:39:49 -04:00
|
|
|
|
2016-08-03 15:36:41 -04:00
|
|
|
if (
|
2019-05-27 04:15:39 -04:00
|
|
|
!this.dashboardFetchedAt ||
|
|
|
|
moment().subtract(30, "minutes").toDate() > this.dashboardFetchedAt
|
2016-08-03 15:36:41 -04:00
|
|
|
) {
|
2019-04-01 06:39:49 -04:00
|
|
|
this.set("isLoading", true);
|
2018-06-15 11:03:24 -04:00
|
|
|
|
2019-04-01 06:39:49 -04:00
|
|
|
AdminDashboard.fetch()
|
|
|
|
.then((model) => {
|
|
|
|
let properties = {
|
|
|
|
dashboardFetchedAt: new Date(),
|
|
|
|
};
|
2018-06-15 11:03:24 -04:00
|
|
|
|
2019-04-01 06:39:49 -04:00
|
|
|
if (versionChecks) {
|
|
|
|
properties.versionCheck = VersionCheck.create(model.version_check);
|
2016-08-03 15:36:41 -04:00
|
|
|
}
|
2018-06-15 11:03:24 -04:00
|
|
|
|
2019-04-01 06:39:49 -04:00
|
|
|
this.setProperties(properties);
|
2018-03-29 14:12:29 -04:00
|
|
|
})
|
|
|
|
.catch((e) => {
|
2019-05-27 04:15:39 -04:00
|
|
|
this.exceptionController.set("thrown", e.jqXHR);
|
2018-03-29 14:12:29 -04:00
|
|
|
this.replaceRoute("exception");
|
|
|
|
})
|
|
|
|
.finally(() => {
|
2019-04-01 06:39:49 -04:00
|
|
|
this.set("isLoading", false);
|
2016-08-03 15:36:41 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-04-01 06:39:49 -04:00
|
|
|
_loadProblems() {
|
|
|
|
this.setProperties({
|
|
|
|
loadingProblems: true,
|
|
|
|
problemsFetchedAt: new Date(),
|
|
|
|
});
|
|
|
|
|
|
|
|
AdminDashboard.fetchProblems()
|
2022-01-03 19:14:33 -05:00
|
|
|
.then((model) => {
|
|
|
|
this.set(
|
|
|
|
"highPriorityProblems",
|
|
|
|
model.problems.filterBy("priority", "high")
|
|
|
|
);
|
|
|
|
this.set(
|
|
|
|
"lowPriorityProblems",
|
|
|
|
model.problems.filterBy("priority", "low")
|
|
|
|
);
|
|
|
|
})
|
2019-04-01 06:39:49 -04:00
|
|
|
.finally(() => this.set("loadingProblems", false));
|
|
|
|
},
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("problemsFetchedAt")
|
2019-04-01 06:39:49 -04:00
|
|
|
problemsTimestamp(problemsFetchedAt) {
|
|
|
|
return moment(problemsFetchedAt).locale("en").format("LLL");
|
2016-08-03 15:36:41 -04:00
|
|
|
},
|
2013-09-16 14:08:55 -04:00
|
|
|
|
|
|
|
actions: {
|
2019-04-01 06:39:49 -04:00
|
|
|
refreshProblems() {
|
|
|
|
this._loadProblems();
|
2013-09-16 14:08:55 -04:00
|
|
|
},
|
|
|
|
},
|
2013-02-22 15:41:12 -05:00
|
|
|
});
|