FEATURE: automatically delete replies on a topic after N days. (#9209)

This commit is contained in:
Vinoth Kannan
2020-03-19 21:06:31 +05:30
committed by GitHub
parent 0cd502a558
commit aad12822b7
16 changed files with 241 additions and 125 deletions
@@ -1,18 +1,15 @@
import { isEmpty } from "@ember/utils";
import { equal, or, readOnly } from "@ember/object/computed";
import { schedule } from "@ember/runloop";
import Component from "@ember/component";
import discourseComputed, {
observes,
on
} from "discourse-common/utils/decorators";
import discourseComputed, { observes } from "discourse-common/utils/decorators";
import {
PUBLISH_TO_CATEGORY_STATUS_TYPE,
OPEN_STATUS_TYPE,
DELETE_STATUS_TYPE,
REMINDER_TYPE,
CLOSE_STATUS_TYPE,
BUMP_TYPE
BUMP_TYPE,
DELETE_REPLIES_TYPE
} from "discourse/controllers/edit-topic-timer";
export default Component.extend({
@@ -23,15 +20,18 @@ export default Component.extend({
autoBump: equal("selection", BUMP_TYPE),
publishToCategory: equal("selection", PUBLISH_TO_CATEGORY_STATUS_TYPE),
reminder: equal("selection", REMINDER_TYPE),
autoDeleteReplies: equal("selection", DELETE_REPLIES_TYPE),
showTimeOnly: or("autoOpen", "autoDelete", "reminder", "autoBump"),
@discourseComputed(
"topicTimer.updateTime",
showFutureDateInput: or(
"showTimeOnly",
"publishToCategory",
"topicTimer.category_id"
)
saveDisabled(updateTime, publishToCategory, topicTimerCategoryId) {
return isEmpty(updateTime) || (publishToCategory && !topicTimerCategoryId);
"autoClose",
"autoDeleteReplies"
),
@discourseComputed("autoDeleteReplies")
durationType(autoDeleteReplies) {
return autoDeleteReplies ? "days" : "hours";
},
@discourseComputed("topic.visible")
@@ -39,25 +39,6 @@ export default Component.extend({
if (visible) return this.get("topic.category_id");
},
@on("init")
@observes("topicTimer", "topicTimer.execute_at", "topicTimer.duration")
_setUpdateTime() {
let time = null;
const executeAt = this.get("topicTimer.execute_at");
if (executeAt && this.get("topicTimer.based_on_last_post")) {
time = this.get("topicTimer.duration");
} else if (executeAt) {
const closeTime = moment(executeAt);
if (closeTime > moment()) {
time = closeTime.format("YYYY-MM-DD HH:mm");
}
}
this.set("topicTimer.updateTime", time);
},
@observes("selection")
_updateBasedOnLastPost() {
if (!this.autoClose) {
@@ -79,11 +60,5 @@ export default Component.extend({
);
}
});
},
actions: {
onChangeTimerType(value) {
this.set("topicTimer.status_type", value);
}
}
});
@@ -1,5 +1,5 @@
import { isEmpty } from "@ember/utils";
import { equal, and, empty } from "@ember/object/computed";
import { equal, and, empty, or } from "@ember/object/computed";
import Component from "@ember/component";
import discourseComputed, { observes } from "discourse-common/utils/decorators";
import { FORMAT } from "select-kit/components/future-date-input-selector";
@@ -10,10 +10,13 @@ export default Component.extend({
date: null,
time: null,
includeDateTime: true,
duration: null,
durationType: "hours",
isCustom: equal("selection", "pick_date_and_time"),
isBasedOnLastPost: equal("selection", "set_based_on_last_post"),
displayDateAndTimePicker: and("includeDateTime", "isCustom"),
displayLabel: null,
displayNumberInput: or("isBasedOnLastPost", "isBasedOnDuration"),
init() {
this._super(...arguments);
@@ -21,6 +24,8 @@ export default Component.extend({
if (this.input) {
if (this.basedOnLastPost) {
this.set("selection", "set_based_on_last_post");
} else if (this.isBasedOnDuration) {
this.set("selection", null);
} else {
const datetime = moment(this.input);
this.setProperties({
@@ -57,28 +62,44 @@ export default Component.extend({
this.set("basedOnLastPost", this.isBasedOnLastPost);
},
@discourseComputed("input", "isBasedOnLastPost")
duration(input, isBasedOnLastPost) {
const now = moment();
if (isBasedOnLastPost) {
return parseFloat(input);
} else {
return moment(input) - now;
}
@observes("duration")
_updateDuration() {
this.attrs.onChangeDuration &&
this.attrs.onChangeDuration(parseInt(this.duration, 0));
},
@discourseComputed("input", "isBasedOnLastPost")
executeAt(input, isBasedOnLastPost) {
if (isBasedOnLastPost) {
return moment()
.add(input, "hours")
@discourseComputed(
"input",
"duration",
"isBasedOnLastPost",
"isBasedOnDuration",
"durationType"
)
executeAt(
input,
duration,
isBasedOnLastPost,
isBasedOnDuration,
durationType
) {
if (isBasedOnLastPost || isBasedOnDuration) {
return moment(input)
.add(parseInt(duration, 0), durationType)
.format(FORMAT);
} else {
return input;
}
},
@discourseComputed("durationType")
durationLabel(durationType) {
return I18n.t(
`topic.topic_status_update.num_of_${
durationType === "hours" ? "hours" : "days"
}`
);
},
didReceiveAttrs() {
this._super(...arguments);
@@ -92,7 +113,9 @@ export default Component.extend({
"date",
"time",
"willCloseImmediately",
"categoryId"
"categoryId",
"displayNumberInput",
"duration"
)
showTopicStatusInfo(
statusType,
@@ -101,7 +124,9 @@ export default Component.extend({
date,
time,
willCloseImmediately,
categoryId
categoryId,
displayNumberInput,
duration
) {
if (!statusType || willCloseImmediately) return false;
@@ -114,6 +139,8 @@ export default Component.extend({
return moment(`${date}${time ? " " + time : ""}`).isAfter(moment());
}
return time;
} else if (displayNumberInput) {
return duration;
} else {
return input;
}
@@ -3,7 +3,10 @@ import { cancel, later } from "@ember/runloop";
import Component from "@ember/component";
import { iconHTML } from "discourse-common/lib/icon-library";
import Category from "discourse/models/category";
import { REMINDER_TYPE } from "discourse/controllers/edit-topic-timer";
import {
REMINDER_TYPE,
DELETE_REPLIES_TYPE
} from "discourse/controllers/edit-topic-timer";
import ENV from "discourse-common/config/environment";
export default Component.extend({
@@ -28,7 +31,13 @@ export default Component.extend({
},
renderTopicTimer() {
if (!this.executeAt || this.executeAt < moment()) {
const isDeleteRepliesType = this.statusType === DELETE_REPLIES_TYPE;
if (
!isDeleteRepliesType &&
!this.basedOnLastPost &&
(!this.executeAt || this.executeAt < moment())
) {
this.set("showTopicTimer", null);
return;
}
@@ -40,7 +49,7 @@ export default Component.extend({
const statusUpdateAt = moment(this.executeAt);
const duration = moment.duration(statusUpdateAt - moment());
const minutesLeft = duration.asMinutes();
if (minutesLeft > 0) {
if (minutesLeft > 0 || isDeleteRepliesType || this.basedOnLastPost) {
let rerenderDelay = 1000;
if (minutesLeft > 2160) {
rerenderDelay = 12 * 60 * 60000;
@@ -51,11 +60,15 @@ export default Component.extend({
} else if (minutesLeft > 2) {
rerenderDelay = 60000;
}
let autoCloseHours = this.duration || 0;
let durationHours = parseInt(this.duration, 0) || 0;
if (isDeleteRepliesType) {
durationHours *= 24;
}
let options = {
timeLeft: duration.humanize(true),
duration: moment.duration(autoCloseHours, "hours").humanize()
duration: moment.duration(durationHours, "hours").humanize()
};
const categoryId = this.categoryId;