2019-11-08 05:38:28 +08:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2019-10-30 01:31:44 +08:00
|
|
|
import { inject } from "@ember/controller";
|
2019-10-24 01:06:54 +08:00
|
|
|
import Controller from "@ember/controller";
|
2019-04-01 18:39:49 +08:00
|
|
|
import { setting } from "discourse/lib/computed";
|
|
|
|
import AdminDashboard from "admin/models/admin-dashboard";
|
|
|
|
import VersionCheck from "admin/models/version-check";
|
2016-08-04 03:36:41 +08:00
|
|
|
|
2019-04-01 18:39:49 +08:00
|
|
|
const PROBLEMS_CHECK_MINUTES = 1;
|
2013-02-21 02:15:50 +08:00
|
|
|
|
2019-10-24 01:06:54 +08:00
|
|
|
export default Controller.extend({
|
2019-04-01 18:39:49 +08:00
|
|
|
isLoading: false,
|
2016-08-04 03:36:41 +08:00
|
|
|
dashboardFetchedAt: null,
|
2019-10-30 01:13:31 +08:00
|
|
|
exceptionController: inject("exception"),
|
2019-04-01 18:39:49 +08:00
|
|
|
showVersionChecks: setting("version_checks"),
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("problems.length")
|
2019-04-01 18:39:49 +08:00
|
|
|
foundProblems(problemsLength) {
|
|
|
|
return this.currentUser.get("admin") && (problemsLength || 0) > 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
fetchProblems() {
|
2019-05-27 16:15:39 +08:00
|
|
|
if (this.isLoadingProblems) return;
|
2019-04-01 18:39:49 +08:00
|
|
|
|
|
|
|
if (
|
2019-05-27 16:15:39 +08:00
|
|
|
!this.problemsFetchedAt ||
|
2019-04-01 18:39:49 +08:00
|
|
|
moment()
|
|
|
|
.subtract(PROBLEMS_CHECK_MINUTES, "minutes")
|
2019-05-27 16:15:39 +08:00
|
|
|
.toDate() > this.problemsFetchedAt
|
2019-04-01 18:39:49 +08:00
|
|
|
) {
|
|
|
|
this._loadProblems();
|
|
|
|
}
|
|
|
|
},
|
2014-09-12 04:30:47 +08:00
|
|
|
|
2016-08-04 03:36:41 +08:00
|
|
|
fetchDashboard() {
|
2019-04-01 18:39:49 +08:00
|
|
|
const versionChecks = this.siteSettings.version_checks;
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
if (this.isLoading || !versionChecks) return;
|
2019-04-01 18:39:49 +08:00
|
|
|
|
2016-08-04 03:36:41 +08:00
|
|
|
if (
|
2019-05-27 16:15:39 +08:00
|
|
|
!this.dashboardFetchedAt ||
|
2016-08-04 03:36:41 +08:00
|
|
|
moment()
|
|
|
|
.subtract(30, "minutes")
|
2019-05-27 16:15:39 +08:00
|
|
|
.toDate() > this.dashboardFetchedAt
|
2016-08-04 03:36:41 +08:00
|
|
|
) {
|
2019-04-01 18:39:49 +08:00
|
|
|
this.set("isLoading", true);
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2019-04-01 18:39:49 +08:00
|
|
|
AdminDashboard.fetch()
|
|
|
|
.then(model => {
|
|
|
|
let properties = {
|
|
|
|
dashboardFetchedAt: new Date()
|
|
|
|
};
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2019-04-01 18:39:49 +08:00
|
|
|
if (versionChecks) {
|
|
|
|
properties.versionCheck = VersionCheck.create(model.version_check);
|
2016-08-04 03:36:41 +08:00
|
|
|
}
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2019-04-01 18:39:49 +08:00
|
|
|
this.setProperties(properties);
|
2018-03-30 02:12:29 +08:00
|
|
|
})
|
|
|
|
.catch(e => {
|
2019-05-27 16:15:39 +08:00
|
|
|
this.exceptionController.set("thrown", e.jqXHR);
|
2018-03-30 02:12:29 +08:00
|
|
|
this.replaceRoute("exception");
|
|
|
|
})
|
|
|
|
.finally(() => {
|
2019-04-01 18:39:49 +08:00
|
|
|
this.set("isLoading", false);
|
2016-08-04 03:36:41 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-04-01 18:39:49 +08:00
|
|
|
_loadProblems() {
|
|
|
|
this.setProperties({
|
|
|
|
loadingProblems: true,
|
|
|
|
problemsFetchedAt: new Date()
|
|
|
|
});
|
|
|
|
|
|
|
|
AdminDashboard.fetchProblems()
|
|
|
|
.then(model => this.set("problems", model.problems))
|
|
|
|
.finally(() => this.set("loadingProblems", false));
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("problemsFetchedAt")
|
2019-04-01 18:39:49 +08:00
|
|
|
problemsTimestamp(problemsFetchedAt) {
|
|
|
|
return moment(problemsFetchedAt)
|
|
|
|
.locale("en")
|
|
|
|
.format("LLL");
|
2016-08-04 03:36:41 +08:00
|
|
|
},
|
2013-09-17 02:08:55 +08:00
|
|
|
|
|
|
|
actions: {
|
2019-04-01 18:39:49 +08:00
|
|
|
refreshProblems() {
|
|
|
|
this._loadProblems();
|
2013-09-17 02:08:55 +08:00
|
|
|
}
|
|
|
|
}
|
2013-02-23 04:41:12 +08:00
|
|
|
});
|