Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加回帖发邮件提醒功能 #920

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions common/at.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 标记的用户名数组
Expand Down Expand Up @@ -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);
}
});
});
};
Expand Down
26 changes: 26 additions & 0 deletions common/mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<p>您好:' + name + '</p>' +
'<p>您在' + config.name + '社区有一条未读消息,请单击下面的链接查看:</p>' +
'<a href="' + SITE_ROOT_URL + '/topic/' + topic_id + '#' + reply_id + '">消息提醒链接</a>' +
'<p>惹您不希望收到消息提醒邮件,您可以在<a href="' + SITE_ROOT_URL + '/setting/' + '">个人设置中心</a>关闭邮件提醒</p>' +
'<p>若您没有在' + config.name + '社区填写过注册信息,说明有人滥用了您的电子邮箱,请删除此邮件,我们对给您造成的打扰感到抱歉。</p>' +
'<p>' + config.name + '社区 谨上。</p>';

exports.sendMail({
from: from,
to: to,
subject: subject,
html: html
});
};
6 changes: 6 additions & 0 deletions controllers/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

/**
* 添加回复
Expand Down Expand Up @@ -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');
});
Expand Down
7 changes: 7 additions & 0 deletions controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
13 changes: 13 additions & 0 deletions proxy/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
19 changes: 19 additions & 0 deletions views/user/setting.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@
<textarea class='input-xlarge' id='signature' name='signature' size='30'><%= typeof(signature) !== 'undefined' ? signature : "" %></textarea>
</div>
</div>
<div class='control-group'>
<label class='control-label' for='notice'>回复邮件提醒</label>
<div class='controls'>
<select name='receive_reply_mail'>
<option value=1 <% if (typeof(receive_reply_mail) !== 'undefined' && receive_reply_mail == true) { %> selected <% } %>>开启</option>
<option value=0 <% if (typeof(receive_reply_mail) !== 'undefined' && receive_reply_mail == false) { %> selected <% } %>>关闭</option>
</select>
</div>
</div>
<div class='control-group'>
<label class='control-label' for='notice'>@邮件提醒</label>
<div class='controls'>
<select name='receive_at_mail'>
<option value=1 <% if (typeof(receive_at_mail) !== 'undefined' && receive_at_mail == true) { %> selected <% } %>>开启</option>
<option value=0 <% if (typeof(receive_at_mail) !== 'undefined' && receive_at_mail == false) { %> selected <% } %>>关闭</option>
</select>
</div>
</div>

<input type='hidden' id='action' name='action' value='change_setting'/>
<input type='hidden' name='_csrf' value='<%= csrf %>'/>

Expand Down