-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
edit.js
144 lines (122 loc) · 3.93 KB
/
edit.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
'use strict';
var validator = require('validator');
var _ = require('lodash');
var db = require('../database');
var meta = require('../meta');
var topics = require('../topics');
var user = require('../user');
var privileges = require('../privileges');
var plugins = require('../plugins');
var pubsub = require('../pubsub');
var utils = require('../utils');
var translator = require('../translator');
module.exports = function (Posts) {
pubsub.on('post:edit', function (pid) {
require('./cache').del(pid);
});
Posts.edit = async function (data) {
const canEdit = await privileges.posts.canEdit(data.pid, data.uid);
if (!canEdit.flag) {
throw new Error(canEdit.message);
}
let postData = await db.getObject('post:' + data.pid);
if (!postData) {
throw new Error('[[error:no-post]]');
}
const oldContent = postData.content; // for diffing purposes
postData.content = data.content;
postData.edited = Date.now();
postData.editor = data.uid;
if (data.handle) {
postData.handle = data.handle;
}
const result = await plugins.fireHook('filter:post.edit', { req: data.req, post: postData, data: data, uid: data.uid });
postData = result.post;
const [editor, topic] = await Promise.all([
user.getUserFields(data.uid, ['username', 'userslug']),
editMainPost(data, postData),
]);
await Posts.setPostFields(data.pid, postData);
if (meta.config.enablePostHistory === 1) {
await Posts.diffs.save({
pid: data.pid,
uid: data.uid,
oldContent: oldContent,
newContent: data.content,
});
}
await Posts.uploads.sync(data.pid);
postData.cid = topic.cid;
postData.topic = topic;
await topics.notifyFollowers(postData, data.uid, {
type: 'post-edit',
bodyShort: translator.compile('notifications:user_edited_post', editor.username, postData.topic.title),
nid: 'edit_post:' + postData.pid + ':uid:' + data.uid,
});
plugins.fireHook('action:post.edit', { post: _.clone(postData), data: data, uid: data.uid });
require('./cache').del(String(postData.pid));
pubsub.publish('post:edit', String(postData.pid));
postData = await Posts.parsePost(postData);
return {
topic: topic,
editor: editor,
post: postData,
};
};
async function editMainPost(data, postData) {
const tid = postData.tid;
const title = data.title ? data.title.trim() : '';
const [topicData, isMain] = await Promise.all([
topics.getTopicFields(tid, ['cid', 'title', 'timestamp']),
Posts.isMain(data.pid),
]);
if (!isMain) {
return {
tid: tid,
cid: topicData.cid,
title: validator.escape(String(topicData.title)),
isMainPost: false,
renamed: false,
};
}
const newTopicData = {
tid: tid,
cid: topicData.cid,
uid: postData.uid,
mainPid: data.pid,
};
if (title) {
newTopicData.title = title;
newTopicData.slug = tid + '/' + (utils.slugify(title) || 'topic');
}
newTopicData.thumb = data.thumb || '';
data.tags = data.tags || [];
if (data.tags.length) {
const canTag = await privileges.categories.can('topics:tag', topicData.cid, data.uid);
if (!canTag) {
throw new Error('[[error:no-privileges]]');
}
}
await topics.validateTags(data.tags, topicData.cid);
const results = await plugins.fireHook('filter:topic.edit', { req: data.req, topic: newTopicData, data: data });
await db.setObject('topic:' + tid, results.topic);
await topics.updateTopicTags(tid, data.tags);
const tags = await topics.getTopicTagsObjects(tid);
newTopicData.tags = data.tags;
newTopicData.oldTitle = topicData.title;
newTopicData.timestamp = topicData.timestamp;
const renamed = translator.escape(validator.escape(String(title))) !== topicData.title;
plugins.fireHook('action:topic.edit', { topic: newTopicData, uid: data.uid });
return {
tid: tid,
cid: newTopicData.cid,
uid: postData.uid,
title: validator.escape(String(title)),
oldTitle: topicData.title,
slug: newTopicData.slug,
isMainPost: true,
renamed: renamed,
tags: tags,
};
}
};