/
reviewable-item.js
331 lines (285 loc) · 9.45 KB
/
reviewable-item.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import { getOwner } from "@ember/application";
import Component from "@ember/component";
import { action, set } from "@ember/object";
import { inject as service } from "@ember/service";
import { classify, dasherize } from "@ember/string";
import ExplainReviewableModal from "discourse/components/modal/explain-reviewable";
import ReviseAndRejectPostReviewable from "discourse/components/modal/revise-and-reject-post-reviewable";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import optionalService from "discourse/lib/optional-service";
import showModal from "discourse/lib/show-modal";
import Category from "discourse/models/category";
import discourseComputed, { bind } from "discourse-common/utils/decorators";
import I18n from "I18n";
let _components = {};
const pluginReviewableParams = {};
// The mappings defined here are default core mappings, and cannot be overridden
// by plugins.
const defaultActionModalClassMap = {
revise_and_reject_post: ReviseAndRejectPostReviewable,
};
const actionModalClassMap = { ...defaultActionModalClassMap };
export function addPluginReviewableParam(reviewableType, param) {
pluginReviewableParams[reviewableType]
? pluginReviewableParams[reviewableType].push(param)
: (pluginReviewableParams[reviewableType] = [param]);
}
export function registerReviewableActionModal(actionName, modalClass) {
if (Object.keys(defaultActionModalClassMap).includes(actionName)) {
throw new Error(
`Cannot override default action modal class for ${actionName} (mapped to ${defaultActionModalClassMap[actionName].name})!`
);
}
actionModalClassMap[actionName] = modalClass;
}
export default Component.extend({
adminTools: optionalService(),
dialog: service(),
modal: service(),
siteSettings: service(),
currentUser: service(),
tagName: "",
updating: null,
editing: false,
_updates: null,
@discourseComputed(
"reviewable.type",
"reviewable.last_performing_username",
"siteSettings.blur_tl0_flagged_posts_media",
"reviewable.target_created_by_trust_level"
)
customClasses(type, lastPerformingUsername, blurEnabled, trustLevel) {
let classes = dasherize(type);
if (lastPerformingUsername) {
classes = `${classes} reviewable-stale`;
}
if (blurEnabled && trustLevel === 0) {
classes = `${classes} blur-images`;
}
return classes;
},
@discourseComputed("reviewable.created_from_flag", "reviewable.status")
displayContextQuestion(createdFromFlag, status) {
return createdFromFlag && status === 0;
},
@discourseComputed(
"reviewable.topic",
"reviewable.topic_id",
"reviewable.removed_topic_id"
)
topicId(topic, topicId, removedTopicId) {
return (topic && topic.id) || topicId || removedTopicId;
},
@discourseComputed("siteSettings.reviewable_claiming", "topicId")
claimEnabled(claimMode, topicId) {
return claimMode !== "disabled" && !!topicId;
},
@discourseComputed(
"claimEnabled",
"siteSettings.reviewable_claiming",
"reviewable.claimed_by"
)
canPerform(claimEnabled, claimMode, claimedBy) {
if (!claimEnabled) {
return true;
}
if (claimedBy) {
return claimedBy.id === this.currentUser.id;
}
return claimMode !== "required";
},
@discourseComputed(
"siteSettings.reviewable_claiming",
"reviewable.claimed_by"
)
claimHelp(claimMode, claimedBy) {
if (claimedBy) {
return claimedBy.id === this.currentUser.id
? I18n.t("review.claim_help.claimed_by_you")
: I18n.t("review.claim_help.claimed_by_other", {
username: claimedBy.username,
});
}
return claimMode === "optional"
? I18n.t("review.claim_help.optional")
: I18n.t("review.claim_help.required");
},
// Find a component to render, if one exists. For example:
// `ReviewableUser` will return `reviewable-user`
@discourseComputed("reviewable.type")
reviewableComponent(type) {
if (_components[type] !== undefined) {
return _components[type];
}
const dasherized = dasherize(type);
const owner = getOwner(this);
const componentExists =
owner.hasRegistration(`component:${dasherized}`) ||
owner.hasRegistration(`template:components/${dasherized}`);
_components[type] = componentExists ? dasherized : null;
return _components[type];
},
@discourseComputed("_updates.category_id", "reviewable.category.id")
tagCategoryId(updatedCategoryId, categoryId) {
return updatedCategoryId || categoryId;
},
@bind
_performConfirmed(performableAction, additionalData = {}) {
let reviewable = this.reviewable;
let performAction = () => {
let version = reviewable.get("version");
this.set("updating", true);
const data = {
send_email: reviewable.sendEmail,
reject_reason: reviewable.rejectReason,
...additionalData,
};
(pluginReviewableParams[reviewable.type] || []).forEach((param) => {
if (reviewable[param]) {
data[param] = reviewable[param];
}
});
return ajax(
`/review/${reviewable.id}/perform/${performableAction.server_action}?version=${version}`,
{
type: "PUT",
data,
}
)
.then((result) => {
let performResult = result.reviewable_perform_result;
// "fast track" to update the current user's reviewable count before the message bus finds out.
if (performResult.reviewable_count !== undefined) {
this.currentUser.updateReviewableCount(
performResult.reviewable_count
);
}
if (performResult.unseen_reviewable_count !== undefined) {
this.currentUser.set(
"unseen_reviewable_count",
performResult.unseen_reviewable_count
);
}
if (this.attrs.remove) {
this.attrs.remove(performResult.remove_reviewable_ids);
} else {
return this.store.find("reviewable", reviewable.id);
}
})
.catch(popupAjaxError)
.finally(() => this.set("updating", false));
};
if (performableAction.client_action) {
let actionMethod =
this[`client${classify(performableAction.client_action)}`];
if (actionMethod) {
return actionMethod.call(this, reviewable, performAction);
} else {
// eslint-disable-next-line no-console
console.error(
`No handler for ${performableAction.client_action} found`
);
return;
}
} else {
return performAction();
}
},
clientSuspend(reviewable, performAction) {
this._penalize("showSuspendModal", reviewable, performAction);
},
clientSilence(reviewable, performAction) {
this._penalize("showSilenceModal", reviewable, performAction);
},
_penalize(adminToolMethod, reviewable, performAction) {
let adminTools = this.adminTools;
if (adminTools) {
let createdBy = reviewable.get("target_created_by");
let postId = reviewable.get("post_id");
let postEdit = reviewable.get("raw");
return adminTools[adminToolMethod](createdBy, {
postId,
postEdit,
before: performAction,
});
}
},
@action
explainReviewable(reviewable, event) {
event.preventDefault();
this.modal.show(ExplainReviewableModal, {
model: { reviewable },
});
},
actions: {
edit() {
this.set("editing", true);
this.set("_updates", { payload: {} });
},
cancelEdit() {
this.set("editing", false);
},
saveEdit() {
let updates = this._updates;
// Remove empty objects
Object.keys(updates).forEach((name) => {
let attr = updates[name];
if (typeof attr === "object" && Object.keys(attr).length === 0) {
delete updates[name];
}
});
this.set("updating", true);
return this.reviewable
.update(updates)
.then(() => this.set("editing", false))
.catch(popupAjaxError)
.finally(() => this.set("updating", false));
},
categoryChanged(categoryId) {
let category = Category.findById(categoryId);
if (!category) {
category = Category.findUncategorized();
}
set(this._updates, "category_id", category.id);
},
valueChanged(fieldId, event) {
set(this._updates, fieldId, event.target.value);
},
perform(performableAction) {
if (this.updating) {
return;
}
const message = performableAction.get("confirm_message");
const requireRejectReason = performableAction.get(
"require_reject_reason"
);
const actionModalClass =
actionModalClassMap[performableAction.server_action];
if (message) {
this.dialog.confirm({
message,
didConfirm: () => this._performConfirmed(performableAction),
});
} else if (requireRejectReason) {
showModal("reject-reason-reviewable", {
title: "review.reject_reason.title",
model: this.reviewable,
}).setProperties({
performConfirmed: this._performConfirmed,
action: performableAction,
});
} else if (actionModalClass) {
this.modal.show(actionModalClass, {
model: {
reviewable: this.reviewable,
performConfirmed: this._performConfirmed,
action: performableAction,
},
});
} else {
return this._performConfirmed(performableAction);
}
},
},
});