From 8535a04728b38a45df27afd133dbe4079703f643 Mon Sep 17 00:00:00 2001 From: luren5 Date: Sat, 21 Jan 2017 20:30:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=9B=9E=E5=B8=96=E5=8F=91?= =?UTF-8?q?=E9=82=AE=E4=BB=B6=E6=8F=90=E9=86=92=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/at.js | 4 ++++ common/mail.js | 26 ++++++++++++++++++++++++++ controllers/reply.js | 6 ++++++ controllers/user.js | 7 +++++++ models/user.js | 4 ++-- proxy/user.js | 13 +++++++++++++ views/user/setting.html | 19 +++++++++++++++++++ 7 files changed, 77 insertions(+), 2 deletions(-) diff --git a/common/at.js b/common/at.js index 8dbda554c9..3b4bfd2b3e 100644 --- a/common/at.js +++ b/common/at.js @@ -13,6 +13,7 @@ var User = require('../proxy').User; var Message = require('./message'); var EventProxy = require('eventproxy'); var _ = require('lodash'); +var Mail = require('./mail'); /** * 从文本中提取出@username 标记的用户名数组 @@ -86,6 +87,9 @@ exports.sendMessageToMentionUsers = function (text, topicId, authorId, reply_id, users.forEach(function (user) { Message.sendAtMessage(user._id, authorId, topicId, reply_id, ep.done('sent')); + if(user.receive_at_mail && user.email) { + Mail.sendReplyMail(user.email, user.loginname, topicId, reply_id); + } }); }); }; diff --git a/common/mail.js b/common/mail.js index a6d3b73b0a..ce45401b60 100644 --- a/common/mail.js +++ b/common/mail.js @@ -82,3 +82,29 @@ exports.sendResetPassMail = function (who, token, name) { html: html }); }; + +/** + * 发送消息通知邮件 + * @param {String} who 接收人的邮件地址 + * @param {String} name 接收人的用户名 + * @param {String} topic_id 帖子ID + * @param {String} reply_id 回复ID + */ +exports.sendReplyMail = function (who, name, topic_id, reply_id) { + var from = util.format('%s <%s>', config.name, config.mail_opts.auth.user); + var to = who; + var subject = config.name + '社区消息提醒'; + var html = '

您好:' + name + '

' + + '

您在' + config.name + '社区有一条未读消息,请单击下面的链接查看:

' + + '消息提醒链接' + + '

惹您不希望收到消息提醒邮件,您可以在个人设置中心关闭邮件提醒

' + + '

若您没有在' + config.name + '社区填写过注册信息,说明有人滥用了您的电子邮箱,请删除此邮件,我们对给您造成的打扰感到抱歉。

' + + '

' + config.name + '社区 谨上。

'; + + exports.sendMail({ + from: from, + to: to, + subject: subject, + html: html + }); +}; diff --git a/controllers/reply.js b/controllers/reply.js index d41446f654..2787aedadc 100644 --- a/controllers/reply.js +++ b/controllers/reply.js @@ -7,6 +7,7 @@ var User = require('../proxy').User; var Topic = require('../proxy').Topic; var Reply = require('../proxy').Reply; var config = require('../config'); +var Mail = require('../common/mail'); /** * 添加回复 @@ -63,6 +64,11 @@ exports.add = function (req, res, next) { ep.all('reply_saved', 'topic', function (reply, topic) { if (topic.author_id.toString() !== req.session.user._id.toString()) { message.sendReplyMessage(topic.author_id, req.session.user._id, topic._id, reply._id); + User.getUserByQuery({'_id': topic.author_id, 'receive_reply_mail': true}, ep.done(function (user) { + if(user && user.email) { + Mail.sendReplyMail(user.email, user.loginname, topic._id, reply._id ); + } + })); } ep.emit('message_saved'); }); diff --git a/controllers/user.js b/controllers/user.js index ba997bff4c..c57e81b786 100644 --- a/controllers/user.js +++ b/controllers/user.js @@ -109,6 +109,8 @@ exports.setting = function (req, res, next) { signature: data.signature, weibo: data.weibo, accessToken: data.accessToken, + receive_reply_mail: data.receive_reply_mail, + receive_at_mail: data.receive_at_mail, }; if (isSuccess) { data2.success = msg; @@ -125,12 +127,17 @@ exports.setting = function (req, res, next) { var location = validator.trim(req.body.location); var weibo = validator.trim(req.body.weibo); var signature = validator.trim(req.body.signature); + var receive_reply_mail = validator.trim(validator.trim(req.body.receive_reply_mail)) == 1 ? true : false; + var receive_at_mail = validator.trim(validator.trim(req.body.receive_at_mail)) == 1 ? true : false; User.getUserById(req.session.user._id, ep.done(function (user) { user.url = url; user.location = location; user.signature = signature; user.weibo = weibo; + user.receive_reply_mail = receive_reply_mail; + user.receive_at_mail = receive_at_mail; + user.save(function (err) { if (err) { return next(err); diff --git a/models/user.js b/models/user.js index c5c66db372..d857b3b9ce 100644 --- a/models/user.js +++ b/models/user.js @@ -35,8 +35,8 @@ var UserSchema = new Schema({ level: { type: String }, active: { type: Boolean, default: false }, - receive_reply_mail: {type: Boolean, default: false }, - receive_at_mail: { type: Boolean, default: false }, + receive_reply_mail: {type: Boolean, default: true }, + receive_at_mail: { type: Boolean, default: true }, from_wp: { type: Boolean }, retrieve_time: {type: Number}, diff --git a/proxy/user.js b/proxy/user.js index 58b8a0a132..cbed39f2a0 100644 --- a/proxy/user.js +++ b/proxy/user.js @@ -82,6 +82,19 @@ exports.getUsersByQuery = function (query, opt, callback) { User.find(query, '', opt, callback); }; +/** + * 根据关键字,获取一个用户 + * Callback: + * - err, 数据库异常 + * - user, 用户信息 + * @param {String} query 关键字 + * @param {Function} callback 回调函数 + */ +exports.getUserByQuery = function (query, callback) { + User.findOne(query, callback); +}; + + /** * 根据查询条件,获取一个用户 * Callback: diff --git a/views/user/setting.html b/views/user/setting.html index a7343ac7ae..693ebafcfa 100644 --- a/views/user/setting.html +++ b/views/user/setting.html @@ -79,6 +79,25 @@ +
+ +
+ +
+
+
+ +
+ +
+
+