discourse/app/assets/javascripts/admin/models/flagged-post.js.es6

179 lines
5.1 KiB
Plaintext
Raw Normal View History

2016-07-01 01:55:44 +08:00
import { ajax } from 'discourse/lib/ajax';
import AdminUser from 'admin/models/admin-user';
import Topic from 'discourse/models/topic';
import Post from 'discourse/models/post';
import { iconHTML } from 'discourse-common/lib/icon-library';
2017-09-06 22:21:07 +08:00
import computed from 'ember-addons/ember-computed-decorators';
const FlaggedPost = Post.extend({
2017-09-06 22:21:07 +08:00
@computed
summary() {
2013-06-17 15:15:56 +08:00
return _(this.post_actions)
.groupBy(function (a) { return a.post_action_type_id; })
.map(function (v,k) { return I18n.t('admin.flags.summary.action_type_' + k, { count: v.length }); })
.join(',');
2017-09-06 22:21:07 +08:00
},
2017-09-06 22:21:07 +08:00
@computed
flaggers() {
return this.post_actions.map(postAction => {
return {
user: this.userLookup[postAction.user_id],
topic: this.topicLookup[postAction.topic_id],
flagType: I18n.t('admin.flags.summary.action_type_' + postAction.post_action_type_id, { count: 1 }),
flaggedAt: postAction.created_at,
2017-09-06 22:21:07 +08:00
disposedBy: postAction.disposed_by_id ? this.userLookup[postAction.disposed_by_id] : null,
disposedAt: postAction.disposed_at,
2017-09-06 22:21:07 +08:00
dispositionIcon: this.dispositionIcon(postAction.disposition),
tookAction: postAction.staff_took_action
2017-09-09 04:27:07 +08:00
};
});
2017-09-06 22:21:07 +08:00
},
2017-09-06 22:21:07 +08:00
dispositionIcon(disposition) {
if (!disposition) { return null; }
let icon;
let title = 'admin.flags.dispositions.' + disposition;
switch (disposition) {
case "deferred": { icon = "external-link"; break; }
case "agreed": { icon = "thumbs-o-up"; break; }
case "disagreed": { icon = "thumbs-o-down"; break; }
}
return iconHTML(icon, { title });
},
2017-09-06 22:21:07 +08:00
@computed('last_revised_at', 'post_actions.@each.created_at')
wasEdited(lastRevisedAt) {
if (Ember.isEmpty(this.get("last_revised_at"))) { return false; }
2017-09-06 22:21:07 +08:00
lastRevisedAt = Date.parse(lastRevisedAt);
return _.some(this.get("post_actions"), function (postAction) {
return Date.parse(postAction.created_at) < lastRevisedAt;
});
2017-09-06 22:21:07 +08:00
},
2017-09-06 22:21:07 +08:00
@computed
conversations() {
let conversations = [];
2017-09-06 22:21:07 +08:00
this.post_actions.forEach(postAction => {
if (postAction.conversation) {
2017-09-06 22:21:07 +08:00
let conversation = {
permalink: postAction.permalink,
hasMore: postAction.conversation.has_more,
response: {
excerpt: postAction.conversation.response.excerpt,
2017-09-06 22:21:07 +08:00
user: this.userLookup[postAction.conversation.response.user_id]
}
};
if (postAction.conversation.reply) {
2017-09-06 22:21:07 +08:00
conversation.reply = {
excerpt: postAction.conversation.reply.excerpt,
2017-09-06 22:21:07 +08:00
user: this.userLookup[postAction.conversation.reply.user_id]
};
}
conversations.push(conversation);
}
});
return conversations;
2017-09-06 22:21:07 +08:00
},
2017-09-06 22:21:07 +08:00
@computed
user() {
return this.userLookup[this.user_id];
2017-09-06 22:21:07 +08:00
},
2017-09-06 22:21:07 +08:00
@computed
topic() {
return this.topicLookup[this.topic_id];
2017-09-06 22:21:07 +08:00
},
2017-09-06 22:21:07 +08:00
@computed('post_actions.@each.name_key')
flaggedForSpam() {
2017-09-11 22:31:38 +08:00
return this.get('post_actions').every(action => action.name_key === 'spam');
2017-09-06 22:21:07 +08:00
},
2017-09-06 22:21:07 +08:00
@computed('post_actions.@each.targets_topic')
topicFlagged() {
2014-02-06 06:54:16 +08:00
return _.any(this.get('post_actions'), function(action) { return action.targets_topic; });
2017-09-06 22:21:07 +08:00
},
2014-02-06 06:54:16 +08:00
2017-09-06 22:21:07 +08:00
@computed('post_actions.@each.targets_topic')
postAuthorFlagged() {
2014-02-06 06:54:16 +08:00
return _.any(this.get('post_actions'), function(action) { return !action.targets_topic; });
2017-09-06 22:21:07 +08:00
},
2014-02-06 06:54:16 +08:00
2017-09-11 22:31:38 +08:00
@computed('flaggedForSpam')
2017-09-06 22:21:07 +08:00
canDeleteAsSpammer(flaggedForSpam) {
2017-09-11 22:31:38 +08:00
return flaggedForSpam &&
this.get('user.can_delete_all_posts') &&
this.get('user.can_be_deleted');
2017-09-06 22:21:07 +08:00
},
2017-09-06 22:21:07 +08:00
deletePost() {
if (this.get('post_number') === 1) {
2016-07-01 01:55:44 +08:00
return ajax('/t/' + this.topic_id, { type: 'DELETE', cache: false });
} else {
2016-07-01 01:55:44 +08:00
return ajax('/posts/' + this.id, { type: 'DELETE', cache: false });
}
},
2017-09-06 22:21:07 +08:00
disagreeFlags() {
2016-07-01 01:55:44 +08:00
return ajax('/admin/flags/disagree/' + this.id, { type: 'POST', cache: false });
},
2017-09-06 22:21:07 +08:00
deferFlags(deletePost) {
2016-07-01 01:55:44 +08:00
return ajax('/admin/flags/defer/' + this.id, { type: 'POST', cache: false, data: { delete_post: deletePost } });
},
2017-09-06 22:21:07 +08:00
agreeFlags(actionOnPost) {
2016-07-01 01:55:44 +08:00
return ajax('/admin/flags/agree/' + this.id, { type: 'POST', cache: false, data: { action_on_post: actionOnPost } });
},
2017-09-06 22:21:07 +08:00
postHidden: Ember.computed.alias('hidden'),
2017-09-12 02:01:59 +08:00
deleted: Ember.computed.or('deleted_at', 'topic_deleted_at'),
});
FlaggedPost.reopenClass({
2017-09-06 22:21:07 +08:00
findAll(args) {
2017-09-09 04:27:07 +08:00
let { filter } = args;
2017-09-06 22:21:07 +08:00
let result = [];
result.set('loading', true);
2017-09-09 04:27:07 +08:00
let data = {};
if (args.topic_id) {
data.topic_id = args.topic_id;
}
if (args.offset) {
data.offset = args.offset;
}
return ajax(`/admin/flags/${filter}.json`, { data }).then(response => {
// users
2017-09-06 22:21:07 +08:00
let userLookup = {};
2017-09-09 04:27:07 +08:00
response.users.forEach(user => userLookup[user.id] = AdminUser.create(user));
// topics
2017-09-06 22:21:07 +08:00
let topicLookup = {};
2017-09-09 04:27:07 +08:00
response.topics.forEach(topic => topicLookup[topic.id] = Topic.create(topic));
// posts
2017-09-09 04:27:07 +08:00
response.posts.forEach(post => {
2017-09-06 22:21:07 +08:00
let f = FlaggedPost.create(post);
f.userLookup = userLookup;
f.topicLookup = topicLookup;
result.pushObject(f);
});
result.set('loading', false);
return result;
});
}
});
export default FlaggedPost;