[new]垃圾评论检测,邮件模板,自唤醒,通知邮件补发

dependabot/npm_and_yarn/mixin-deep-1.3.2
panjunwen 6 years ago
parent 3df22e91dd
commit 99488531de
  1. 37
      cloud.js
  2. 30
      routes/comments.js
  3. 91
      utilities/check-spam.js
  4. 9
      views/comments.ejs

@ -2,25 +2,29 @@ const AV = require('leanengine');
const mail = require('./utilities/send-mail');
const Comment = AV.Object.extend('Comment');
const request = require('request');
const spam = require('./utilities/check-spam');
function sendNotification(currentComment) {
function sendNotification(currentComment, defaultIp) {
// 发送博主通知邮件
if (currentComment.get('mail') !== process.env.BLOGGER_EMAIL) {
mail.notice(currentComment);
}
let ip = currentComment.get('ip') || defaultIp;
console.log('IP: %s', ip);
spam.checkSpam(currentComment, ip);
// AT评论通知
// 获取评论内容
var comm = currentComment.get('comment');
// 从评论内容中提取出a标签的href属性值
var h = comm.match(/<a.*?href?\s*=\s*[\'|\"]+?(.*?)[\'|\"]+?/i);
if (!h) {
console.log('没有@任何人,结束!');
let rid =currentComment.get('pid') || currentComment.get('rid');
if (!rid) {
console.log("这条评论没有 @ 任何人");
return;
} else if (currentComment.get('isSpam')) {
console.log('检测到垃圾评论,通知邮件取消发送');
return;
}
// 替换掉#号,即为rid
let rid = h[1].replace(/#/,"");
// 将rid存入数据库,以供管理页面使用。
currentComment.set('rid', rid);
let query = new AV.Query('Comment');
query.get(rid).then(function (parentComment) {
if (parentComment.get('mail') && parentComment.get('mail') !== process.env.BLOGGER_EMAIL) {
@ -34,12 +38,13 @@ function sendNotification(currentComment) {
});
}
AV.Cloud.afterSave('Comment', function (request) {
let currentComment = request.object;
return sendNotification(currentComment);
AV.Cloud.afterSave('Comment', function (req) {
let currentComment = req.object;
// 检查垃圾评论
return sendNotification(currentComment, req.meta.remoteAddress);
});
AV.Cloud.define('resend-mails', function(request) {
AV.Cloud.define('resend-mails', function(req) {
let query = new AV.Query(Comment);
query.greaterThanOrEqualTo('createdAt', new Date(new Date().getTime() - 24*60*60*1000));
query.notEqualTo('isNotified', true);
@ -49,7 +54,7 @@ AV.Cloud.define('resend-mails', function(request) {
new Promise((resolve, reject)=>{
count = results.length;
for (var i = 0; i < results.length; i++ ) {
sendNotification(results[i]);
sendNotification(results[i], req.meta.remoteAddress);
}
resolve(count);
}).then((count)=>{

@ -2,6 +2,7 @@
const router = require('express').Router();
const AV = require('leanengine');
const mail = require('../utilities/send-mail');
const spam = require('../utilities/check-spam');
const Comment = AV.Object.extend('Comment');
@ -61,4 +62,33 @@ router.get('/delete', function (req, res, next) {
}
});
router.get('/not-spam', function (req, res, next) {
if (req.currentUser) {
let query = new AV.Query(Comment);
query.get(req.query.id).then(function (object) {
object.set('isSpam', false);
object.save();
spam.submitHam(object);
res.redirect('/comments')
}, function (err) {
}).catch(next);
} else {
res.redirect('/');
}
});
router.get('/mark-spam', function (req, res, next) {
if (req.currentUser) {
let query = new AV.Query(Comment);
query.get(req.query.id).then(function (object) {
object.set('isSpam', true);
object.save();
spam.submitSpam(object);
res.redirect('/comments')
}, function (err) {
}).catch(next);
} else {
res.redirect('/');
}
});
module.exports = router;

@ -0,0 +1,91 @@
'use strict';
const akismet = require('akismet-api');
const akismetClient = akismet.client({
key : process.env.AKISMET_KEY,
blog : process.env.SITE_URL
});
exports.checkSpam = (comment, ip)=> {
akismetClient.verifyKey(function(err, valid) {
if (err) console.log('Akismet key 异常:', err.message);
if (valid) {
// TODO(1) 这里有缺陷
comment.set('ip', ip);
akismetClient.checkSpam({
user_ip : ip,
user_agent : comment.get('ua'),
referrer : process.env.SITE_URL + comment.get('url'),
permalink : process.env.SITE_URL + comment.get('url'),
comment_type : 'comment',
comment_author : comment.get('nick'),
comment_author_email : comment.get('mail'),
comment_author_url : comment.get('link'),
comment_content : comment.get('comment'),
// is_test : true // Default value is false
}, function(err, spam) {
if (err) console.log (`垃圾评论检测出错!${err}`);
if (spam) {
console.log('逮到一只垃圾评论,烧死它!用文火~');
comment.set('isSpam', true);
comment.save();
// comment.destroy();
} else {
comment.set('isSpam', false);
comment.save();
console.log('垃圾评论检测完成,放行~');
}
});
}
else console.log('Akismet key 异常!');
});
};
exports.submitSpam = (comment)=> {
akismetClient.verifyKey(function(err, valid) {
if (err) console.log('Akismet key 异常:', err.message);
if (valid) {
let ipAddr = comment.get('ip');
akismetClient.submitSpam({
user_ip : ipAddr,
user_agent : comment.get('ua'),
referrer : process.env.SITE_URL + comment.get('url'),
permalink : process.env.SITE_URL + comment.get('url'),
comment_type : 'comment',
comment_author : comment.get('nick'),
comment_author_email : comment.get('mail'),
comment_author_url : comment.get('link'),
comment_content : comment.get('comment'),
// is_test : true // Default value is false
}, function(err) {
if (!err) {
console.log('垃圾评论已经提交!');
}
});
}
else console.log('Akismet key 异常!');
});
};
exports.submitHam = (comment)=> {
akismetClient.verifyKey(function(err, valid) {
if (err) console.log('Akismet key 异常:', err.message);
if (valid) {
let ipAddr = comment.get('ip');
akismetClient.submitHam({
user_ip : ipAddr,
user_agent : comment.get('ua'),
referrer : process.env.SITE_URL + comment.get('url'),
permalink : process.env.SITE_URL + comment.get('url'),
comment_type : 'comment',
comment_author : comment.get('nick'),
comment_author_email : comment.get('mail'),
comment_author_url : comment.get('link'),
comment_content : comment.get('comment'),
// is_test : true // Default value is false
}, function(err) {
if (!err) {
console.log('评论已经标记为非垃圾!');
}
});
}
else console.log('Akismet key 异常!');
});
};

@ -26,8 +26,15 @@
<%- comment_list[i].get('comment') %>
</div>
<div class="check">
<% if(comment_list[i].get('isSpam')) { %>
<a href="/comments/not-spam?id=<%= comment_list[i].get('objectId') %>" rel="nofollow">这不是垃圾评论</a>
<% } else { %>
<a href="/comments/mark-spam?id=<%= comment_list[i].get('objectId') %>" rel="nofollow">标记为垃圾评论</a>
<% } %>
<span class="spacer">•</span>
<a class="red" href="/comments/delete?id=<%= comment_list[i].get('objectId') %>" rel="nofollow">删除</a>
<a class="blue" href="<%= process.env.SITE_URL+comment_list[i].get('url') %>" rel="nofollow">查看文章</a>
<span class="spacer">•</span>
<a class="blue" href="<%= process.env.SITE_URL+comment_list[i].get('url')+'#' + comment_list[i].get('objectId') %>" rel="nofollow">查看文章</a>
<% if (comment_list[i].get('rid')) { %>
<% if(comment_list[i].get('isNotified')) { %>
<span class="spacer">•</span><span class="vtime">通知已送达</span>

Loading…
Cancel
Save