/
post-manager.js
145 lines (136 loc) · 4.73 KB
/
post-manager.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
var Topic = require('../models/Topic.js');
var Forum = require('../models/Forum.js');
var Post = require('../models/Post.js');
var Category = require('../models/Category.js');
var CM = require('./category-manager.js');
var FM = require('./forum-manager.js');
var TM = require('./topic-manager.js');
var mongoose = require('mongoose');
var moment = require('moment');
var ObjectId = mongoose.Types.ObjectId;
var akismet = require('akismet').client({ blog: 'http://xforum.slashmanx.com', apiKey: '0cfa41b5611f' });
var PM = {};
module.exports = PM;
// record insertion, update & deletion methods //
PM.create = function(newData, callback)
{
// TODO : Check if previous post in this topic was by the same author, and within the past 10 mins (settings)
Topic.findById(newData.topic).populate('post', 'author').populate('replies', 'author postedOn', null, {sort: {postedOn: -1}, limit: 1}).exec(function(err, t) {
var tmp = t.toObject();
var prevAuthor = tmp.post.author;
var postID = tmp.post;
if(tmp.replies.length)
{
prevAuthor = tmp.replies[0].author;
postID = t.replies[0];
lastPosted = t.replies[0].postedOn;
}
akismet.checkSpam({
user_ip: '1.1.1.1',
comment_author: newData.author,
comment_content: newData.body
}, function(err, spam) {
if(spam)
newData.isSpam = true;
else
newData.isSpam = false;
});
console.log(moment().diff(lastPosted, 'minutes'));
if(""+prevAuthor == ""+newData.author && moment().diff(lastPosted, 'minutes') < 10)
{
t.lastPost = moment().format();
t.markModified('lastPost');
t.save();
Post.findById(postID).exec(function(perr, p) {
p.body = p.body + "<br/><br/>"+ newData.body;
p.postedOn = moment().format();
p.markModified('body');
p.markModified('postedOn');
p.save(function(nerr, post){
Post.findById(post._id).populate('author').populate('topic').exec(function (err, cbPost) {
callback(cbPost, true);
});
})
});
}
else {
p = new Post(newData);
p.save(function(err, thepost) {
Topic.findByIdAndUpdate(newData.topic, {lastPost: moment().format(), $push : { replies : thepost._id }}, function(err, t) {
Post.findById(p._id).populate('author').populate('topic').exec(function (err, cbPost) {
callback(cbPost);
});
});
});
}
});
};
PM.update = function(data, callback) {
Post.findByIdAndUpdate(data.id, {body: data.post}).exec(function(err, post) {
if(err)
console.log(err);
else
callback(post);
})
};
PM.edit = function(data, callback) {
Post.findOne({_id: data.id}).exec(function(err, post) {
if(err)
console.log(err);
else {
post.editHistory.push({editedBy: data.editor, dateEdited: moment().format(), prevBody: post.body, newBody: data.post});
post.body = data.post;
post.save(function(error, p){
if(error) console.log(error);
else
callback(p);
})
}
})
};
PM.delete = function(id, callback) {
Post.findByIdAndRemove(id).populate('topic').exec(function(err, p) {
p = p.toObject();
p.topic.replies.splice(p.topic.replies.indexOf(id));
if(err)
callback(err);
else
{
Topic.findByIdAndUpdate(p.topic._id, { $pull : { 'replies' :p._id } }, function(terr, t){
callback(terr, p);
});
}
});
}
PM.getPostInfo = function(pid, callback)
{
Post.findById(pid).populate('author').exec(function(e, postInfo){
callback(postInfo);
})
}
PM.getPostEditHistory = function(pid, callback)
{
Post.findById(pid).select('editHistory').populate('editHistory.editedBy').exec(function(e, postInfo){
if(e) callback(e)
else callback(null, postInfo.editHistory);
})
}
PM.getTopic = function(tid, callback)
{
Post.find({topic: tid}).populate('author').populate('editHistory.editedBy').sort({postedOn: 1}).exec(function(e, posts) {
if (e) callback(e)
else {
callback(null, posts)
}
});
}
PM.getIDFromSlug = function(slug, callback)
{
Post.findOne({slug: slug}).select('_id').exec(function (err, p) {
if(p) callback(p._id)
})
}
PM.delAllRecords = function()
{
Post.remove({}, function() {}); // reset accounts collection for testing //
}