Skip to content
Browse files

add unseen module & fix some bugs

  • Loading branch information...
1 parent 275a25e commit 48845dcd528fc3b7107306df2bfb10a9b00d28dd @nomospace committed
View
78 controllers/inbox.js
@@ -19,15 +19,10 @@ emitter.on('messages', function(res) {
status: 'success',
data: mailObject
});
- console.log('Done fetching all messages!');
+// console.log('Done fetching all messages!');
// imap.logout(cb);
});
-//emitter.on('boxes', function(res, boxes) {
-// res.json(boxes);
-// console.log('Done fetching all boxes!');
-//});
-
exports.index = function(req, res) {
if (req.session.user) {
var page = inboxPage = req.params.page || 1;
@@ -38,8 +33,17 @@ exports.index = function(req, res) {
}
};
-exports.getList = function(req, res) {
- _getMail(req, res);
+exports.unseen = function(req, res) {
+ res.locals({'tag': 'unseen'});
+ res.render('mail/list.html');
+};
+
+exports.getAll = function(req, res) {
+ _getMail(req, res, 'ALL');
+};
+
+exports.getUnseen = function(req, res) {
+ _getMail(req, res, 'UNSEEN');
};
exports.getById = function(req, res) {
@@ -51,7 +55,7 @@ exports.getById = function(req, res) {
res.locals({
'id': id,
'page': page,
- 'tag': 'index',
+ 'tag': 'mail',
'moment': moment,
'data': mail.data
});
@@ -76,10 +80,13 @@ exports.getHtml = function(req, res) {
}
};
-function _getMail(req, res) {
+function _getMail(req, res, type) {
var user = req.session.user;
- if (!user) return;
- if (!imap) {
+ if (!user) {
+ res.redirect('/signin');
+ return;
+ }
+ if (!imap || inboxPage > 1 || type != 'ALL') {
// TODO 离开页面时,需要 abort 掉 imap 连接
imap = mailUtil.connection(user);
imap.on('error', function(err) {
@@ -89,14 +96,17 @@ function _getMail(req, res) {
_connect,
_getBoxes,
_openBox,
- _search,
+ function(results) {
+ _search(results, type);
+ },
function(results) {
_fetch(results, req, res);
}
]);
cb();
} else {
- mailUtil.getMailList(function(list) {
+ mailUtil.getMailListByPage(1, function(list) {
+ mailObject.msgs = [];
list.forEach(function(l) {
mailObject.msgs.push(l.data);
});
@@ -114,32 +124,24 @@ function _connect(/*fn*/) {
}
function _getBoxes() {
- if (!cache.get('boxes') || !cache.get('boxes').length) {
- imap.getBoxes(function(err, boxes) {
- cache.set('boxes', boxes);
- if (err) throw err;
- mailObject.boxes = [];
-// console.log(boxes);
- for (var key in boxes) {
- key && mailObject.boxes.push(key);
-// console.log('status: ' + key);
-// imap.status(key, function(err, box) {
-// console.log(key, err, box);
-// });
- }
- cb();
- });
- }
+ mailUtil.getBoxes(imap, function(boxes) {
+ mailObject.boxes = boxes;
+ cb();
+ });
}
function _openBox() {
imap.openBox('INBOX', false, cb);
}
-function _search(results) {
+function _search(results, type) {
mailObject.messages = results.messages;
// node-imap 不支持几天时间段内的查询
- imap.search(['ALL', ['SINCE', moment().subtract('weeks', inboxPage)]], cb);
+ if (type == 'UNSEEN') {
+ imap.search([type], cb);
+ } else {
+ imap.search([type, ['SINCE', moment().subtract('weeks', inboxPage)]], cb);
+ }
}
function _fetch(results, req, res) {
@@ -161,12 +163,12 @@ function _fetch(results, req, res) {
});
// req.session 过大时,页面响应速度会明显变慢,估计是频繁调用 JSON.stringify(session) 导致的计算效率下降
- var msgs = {};
+// var msgs = {};
console.log('total:', msgLength);
fetch.on('message', function(msg) {
- // var fileName = 'msg-' + msg.seqno + '-raw.txt';
+ // var fileName = 'msg-' + msg.uid + '-raw.txt';
// fileStream = fs.createWriteStream(fileName);
msg.on('data', function(chunk) {
// fileStream.write(chunk);
@@ -188,15 +190,17 @@ function _fetch(results, req, res) {
'mail': mail
};
- msgs[msg.seqno] = data;
+// msgs[msg.uid] = data;
mailObject.msgs.push(data);
// 持久化至本地数据库
(function(data) {
- var id = msg.seqno;
+ var id = msg.uid;
mailUtil.getMailById(id, function(mail) {
if (!mail) {
- mailUtil.saveMail({seqno: id, data: data, username: req.session.user.name});
+ mailUtil.saveMail({id: id, page: inboxPage, data: data, username: req.session.user.name});
+// } else if (mail.page != inboxPage) {
+// mailUtil.updateMail({id: id, page: inboxPage});
}
});
})(data);
View
45 libs/mail-util.js
@@ -58,7 +58,7 @@ exports.createTransport = function(req) {
};
exports.getMailById = function(id, cb) {
- Mail.findOne({seqno: id, username: USER.name}, function(err, mail) {
+ Mail.findOne({id: id, username: USER.name}, function(err, mail) {
if (err) throw err;
cb(mail);
});
@@ -66,7 +66,8 @@ exports.getMailById = function(id, cb) {
exports.saveMail = function(options) {
var mail = new Mail();
- mail.seqno = options.seqno;
+ mail.id = options.id;
+ mail.page = options.page;
mail.data = options.data;
mail.username = USER.name;
mail.save(function(err) {
@@ -89,14 +90,46 @@ exports.saveMail = function(options) {
// });
//};
-exports.getMailList = function(cb) {
- Mail.find({username: USER.name}, 'data', function(err, list) {
- // TODO list 数据对象不仅仅包含纯 data 字段
+exports.getMailListByPage = function(page, cb) {
+ Mail.find({page: page, username: USER.name}, 'data',
+ function(err, list) {
+ // TODO list 数据对象不仅仅包含纯 data 字段
+ // 数据按时间对象排序
+ if (err) throw err;
+ cb(list);
+ });
+};
+
+exports.updateMail = function(options) {
+ Mail.update({id: options.id, page: options.page, username: USER.name}, function(err, mail) {
+ if (err) throw err;
+ });
+};
+
+exports.getUnseenMail = function(imap, cb) {
+ imap.search(['UNSEEN'], cb);
+};
+
+exports.getBoxes = function(imap, cb) {
+ imap.getBoxes(function(err, boxes) {
if (err) throw err;
- cb(list);
+ var bs = [], key;
+// console.log(boxes);
+ for (key in boxes) {
+ key && bs.push(key);
+// console.log('status: ' + key);
+// imap.status(key, function(err, box) {
+// console.log(key, err, box);
+// });
+ }
+ cb(bs);
});
};
+exports.saveBoxes = function() {
+
+};
+
function die(err) {
console.log('Uh oh: ' + err);
// process.exit(1);
View
3 models/mail.js
@@ -3,8 +3,9 @@ var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var MailSchema = new Schema({
- seqno: {type: String},
+ id: {type: String},
username: {type: String},
+ page: {type: Number},
data: {type: Object},
createAt: {type: Date, default: Date.now}
});
View
4 public/javascripts/inbox.js
@@ -1,12 +1,11 @@
$(function() {
-
'use strict';
var inboxTpl = $('#inbox_tpl').html();
var sideItemTpl = $('#side_item_tpl').html();
function _getInbox() {
- $.getJSON('/ajax/mail/inbox').done(function(result) {
+ $.getJSON('/ajax/mail/' + TAG).done(function(result) {
console.log(result);
var context = result.data;
@@ -19,7 +18,6 @@ $(function() {
// });
$('#J_content').html(Handlebars.compile(inboxTpl)(context));
-
$('#J_side_list').html(Handlebars.compile(sideItemTpl)(context));
});
}
View
1 public/javascripts/mail.js
@@ -1,6 +1,7 @@
$(function() {
$('iframe').iframeAutoHeight();
+ $('#J_toolbar').stickyPanel({topPadding: 50, savePanelSpace: true});
var editor = 'J_editor';
if (window.tinyMCE) {
View
4 routes.js
@@ -19,6 +19,7 @@ module.exports = function(app) {
app.post('/signin', sign.login);
app.get('/mail', mail.index);
app.get('/mail/inbox', inbox.index);
+ app.get('/mail/unseen', inbox.unseen);
app.get('/mail/inbox/page/:page', inbox.index);
app.get('/mail/inbox/:id', inbox.getById);
app.get('/mail/inbox/:id/html', inbox.getHtml);
@@ -27,7 +28,8 @@ module.exports = function(app) {
// ajax
- app.get('/ajax/mail/inbox', inbox.getList);
+ app.get('/ajax/mail/inbox', inbox.getAll);
+ app.get('/ajax/mail/unseen', inbox.getUnseen);
app.get('/ajax/mail/boxes', inbox.getBoxes);
app.post('/ajax/mail/send', compose.send);
View
5 views/mail/compose.html
@@ -1,7 +1,7 @@
<%- include side.html %>
-<div class="main-body mail-page" id='J_content'>
- <div class="btn-groups clearfix">
+<div class="main-body mail-page" id="J_content">
+ <div class="btn-groups clearfix" id="J_toolbar">
<div class="btn-group">
<button id="J_send" class="btn btn-primary">发送 </button>
</div>
@@ -39,4 +39,5 @@
</div>
<script type="text/javascript" src="/libs/tiny-mce/tiny_mce_dev.js"></script>
+<script type="text/javascript" src="/libs/sticky-panel/stickypanel.js"></script>
<script type="text/javascript" src="/javascripts/mail.js"></script>
View
2 views/mail/content.html
@@ -1 +1 @@
-<%-html%>
+<%-html%>
View
2 views/mail/index.html
@@ -1,6 +1,6 @@
<%- include side.html %>
-<div class="main-body" id='J_content'>
+<div class="main-body" id="J_content">
<pre>
<%- include ../../README.md %>
</pre>
View
28 views/mail/list.html
@@ -1,13 +1,15 @@
<%- include side.html %>
-<div class="main-body" id='J_content'>
+<div class="main-body" id="J_content">
正在获取数据...
</div>
<script id="inbox_tpl" type="tpl/cache">
+ <%if(tag!=='unseen'){%>
{{#messages}}
- <p><strong>{{total}}</strong> 封,其中 <a href="javascript:;">未读邮件</a> <strong>{{new}}</strong><a href="javascript:;">全部设为已读</a></p>
+ <p><strong>{{total}}</strong> 封,其中 <a href="/mail/unseen">未读邮件</a> <strong>{{new}}</strong><!--<a href="javascript:;">全部设为已读</a>--></p>
{{/messages}}
+ <%}%>
<table class="table table-striped">
<tr>
<th>发件人</th>
@@ -16,22 +18,11 @@
</tr>
{{#each msgs}}
<tr>
- <!--div>{{chunk}}</div-->
{{#mail}}
- <!--p><strong>flags: </strong>{{#each flags}}{{.}}{{/each}}</p-->
- <!--p><strong>date: </strong>{{dateFormat date}}</p-->
{{#headers}}
<td>{{from}}</td>
- <td><a href="/mail/inbox/{{../../msg/seqno}}">{{#each subject}}{{.}}{{/each}}</a></td>
+ <td><a href="/mail/inbox/{{../../msg/uid}}">{{#each subject}}{{.}}{{/each}}</a></td>
<td>{{dateFormat date}}</td>
- <!--p><strong>content-type: </strong>{{#each content-type}}{{.}}{{/each}}</p>
- <p><strong>message-id: </strong>{{#each message-id}}{{.}}{{/each}}</p>
- <p><strong>mime-version: </strong>{{#each mime-version}}{{.}}{{/each}}</p>
- <p><strong>received: </strong>{{#each received}}{{.}}{{/each}}</p>
- <p><strong>to: </strong>{{#each to}}{{.}}{{/each}}</p>
- <p><strong>x-cm-transid: </strong>{{#each x-cm-transid}}{{.}}{{/each}}</p>
- <p><strong>x-coremail-antispam: </strong>{{#each x-coremail-antispam}}{{.}}{{/each}}</p>
- <p><strong>x-originating-ip: </strong>{{#each x-originating-ip}}{{.}}{{/each}}</p-->
{{/headers}}
</tr>
{{/mail}}
@@ -44,6 +35,7 @@
</tr>
{{/msgs}}
</table>
+ <%if(tag!=='unseen'){%>
<ul class="pager">
<li class="previous">
<a href="/mail/inbox/page/<%=parseInt(page)+1%>">&larr; <%=parseInt(page)+1%>周前</a>
@@ -54,7 +46,9 @@
</li>
<%}%>
</ul>
+ <%}%>
</script>
-<script type="text/javascript" src="/libs/moment/moment.js"></script>
-<script type="text/javascript" src="/libs/moment/lang/zh-cn.js"></script>
-<script type="text/javascript" src="/javascripts/inbox.js"></script>
+<script src="/libs/moment/moment.js"></script>
+<script src="/libs/moment/lang/zh-cn.js"></script>
+<script>TAG='<%=tag||"inbox"%>';</script>
+<script src="/javascripts/inbox.js"></script>
View
12 views/mail/mail.html
@@ -1,14 +1,13 @@
<%- include side.html %>
-<div class="main-body mail-page" id='J_content'>
- <!-- <code><%=JSON.stringify(data)%></code> -->
- <div class="btn-groups clearfix">
+<div class="main-body mail-page" id="J_content">
+ <div class="btn-groups clearfix" id="J_toolbar">
<div class="btn-group">
<button class="btn"><a href="/mail/inbox">返回</a> </button>
</div>
- <div class="btn-group">
+ <div class="btn-group">
<button class="btn"><a href="/mail/inbox/<%=id%>/compose">回复</a> </button>
- </div>
+ </div>
<div class="btn-group">
<button class="btn">转发 </button>
</div>
@@ -25,7 +24,7 @@
<li><a href="#">垃圾邮件</a></li>
</ul>
</div>
- </div>
+ </div>
<div class="mail-body">
<div><span class="label">主 题</span><%=data.mail.headers.subject%></span></div>
<div><span class="label">发件人</span><%=data.mail.headers.from%></div>
@@ -43,4 +42,5 @@
<iframe src="/mail/inbox/<%=id%>/html" class="mail-body-iframe"></iframe>
</div>
+<script type="text/javascript" src="/libs/sticky-panel/stickypanel.js"></script>
<script type="text/javascript" src="/javascripts/mail.js"></script>

0 comments on commit 48845dc

Please sign in to comment.
Something went wrong with that request. Please try again.