190 lines
6.4 KiB
JavaScript
190 lines
6.4 KiB
JavaScript
const superagent = require('superagent');
|
|
const socket_io = require('socket.io-client');
|
|
|
|
const _ = require('./I18n.js');
|
|
const Util = require('./Util.js');
|
|
|
|
var Store = module.exports = {
|
|
data: {
|
|
layout: 'message-on-right',
|
|
quickReply: true,
|
|
msg: null,
|
|
threads: false,
|
|
accounts: [],
|
|
listGroups: [],
|
|
messages: []
|
|
},
|
|
|
|
listeners: [],
|
|
on: function(cb)
|
|
{
|
|
this.listeners.push(cb);
|
|
},
|
|
un: function(cb)
|
|
{
|
|
for (var i = this.listeners.length; i >= 0; i--)
|
|
if (this.listeners[i] == cb)
|
|
this.listeners.splice(i, 1);
|
|
},
|
|
get: function(k)
|
|
{
|
|
return this.data[k];
|
|
},
|
|
set: function(k, v)
|
|
{
|
|
this.data[k] = v;
|
|
(this.listeners || []).map(i => i());
|
|
},
|
|
setAll: function(obj)
|
|
{
|
|
for (var k in obj)
|
|
this.data[k] = obj[k];
|
|
(this.listeners || []).map(i => i());
|
|
},
|
|
|
|
startIo: function()
|
|
{
|
|
var self = this;
|
|
this.io = socket_io('', { path: window.location.pathname.replace(/[^\/]+$/, 'backend/socket.io') });
|
|
this.io.on('sync', function(params)
|
|
{
|
|
if (params.state == 'start')
|
|
{
|
|
self.setAll({ progressText: 'Syncing '+params.email+' / '+params.folder, progressPercent: 0 });
|
|
}
|
|
else if (params.state == 'progress')
|
|
{
|
|
self.setAll({ progressPercent: Math.round(100*params.done/(params.total||1)) });
|
|
}
|
|
else if (params.state == 'finish-box')
|
|
{
|
|
self.setAll({ progressPercent: 100 });
|
|
}
|
|
else if (params.state == 'complete')
|
|
{
|
|
self.setAll({ progressText: '', progressPercent: 0 });
|
|
}
|
|
self.set('sync', params.progress);
|
|
});
|
|
},
|
|
|
|
loadAccounts: function()
|
|
{
|
|
superagent.get('backend/folders').end(function(err, res)
|
|
{
|
|
var ixOfAll = {
|
|
received: 1,
|
|
outbox: 3,
|
|
sent: 4,
|
|
drafts: 5,
|
|
spam: 6,
|
|
trash: 7
|
|
};
|
|
var accounts = [ {
|
|
name: _('All Messages'),
|
|
accountId: null,
|
|
unreadCount: 0,
|
|
folders: [
|
|
{ name: _('Unread'), icon: 'mail_unread', unreadCount: 0, type: 'unread' },
|
|
{ name: _('Received'), icon: 'mail_received', unreadCount: 0, type: 'inbox' },
|
|
{ name: _('Pinned'), icon: 'mail_pinned', unreadCount: 0, type: 'pinned' },
|
|
{ name: _('Outbox'), icon: 'mail_outbox', unreadCount: 0, type: 'outbox' },
|
|
{ name: _('Sent'), icon: 'mail_sent', unreadCount: 0, type: 'sent' },
|
|
{ name: _('Drafts'), icon: 'mail_drafts', unreadCount: 0, type: 'drafts' },
|
|
{ name: _('Spam'), icon: 'mail_spam', unreadCount: 0, type: 'spam' },
|
|
{ name: _('Trash'), icon: 'mail_trash', unreadCount: 0, type: 'trash' },
|
|
],
|
|
} ];
|
|
for (let a of res.body.accounts)
|
|
{
|
|
let account = {
|
|
name: a.name,
|
|
email: a.email,
|
|
accountId: a.id,
|
|
unreadCount: 0,
|
|
warning: false,
|
|
folders: [
|
|
{ name: _('Unread'), icon: 'mail_unread', unreadCount: 0, type: 'unread' },
|
|
{ name: _('Pinned'), icon: 'mail_pinned', unreadCount: a.pinned_unread_count, type: 'pinned' },
|
|
],
|
|
folderMap: a.foldermap,
|
|
folderTypes: {}
|
|
};
|
|
if (!account.folderMap.received)
|
|
{
|
|
account.folderMap.received = 'INBOX';
|
|
}
|
|
for (let f in account.folderMap)
|
|
{
|
|
account.folderTypes[account.folderMap[f]] = f;
|
|
}
|
|
for (let f of a.folders)
|
|
{
|
|
let icon = (account.folderTypes[f.name] ? 'mail_'+account.folderTypes[f.name] : 'folder');
|
|
account.folders.push({ name: f.name, icon: icon, unreadCount: f.unread_count-0, folderId: f.id });
|
|
account.folders[0].unreadCount -= -f.unread_count;
|
|
if (account.folderTypes[f.name])
|
|
{
|
|
accounts[0].folders[ixOfAll[account.folderTypes[f.name]]].unreadCount -= -f.unread_count;
|
|
}
|
|
}
|
|
accounts.push(account);
|
|
accounts[0].unreadCount += account.unreadCount;
|
|
accounts[0].folders[0].unreadCount += account.unreadCount;
|
|
accounts[0].folders[2].unreadCount += account.folders[1].unreadCount;
|
|
}
|
|
Store.set('accounts', accounts);
|
|
});
|
|
},
|
|
|
|
loadFolder: function(folderId)
|
|
{
|
|
superagent.get('backend/groups').query({ folderId: folderId }).end(function(err, res)
|
|
{
|
|
var groups = res.body.groups.map(g => { return { name: Util.getGroupName(g.name), messageCount: g.count-0, start: 0 } });
|
|
var start = 0;
|
|
for (var i = 0; i < groups.length; i++)
|
|
{
|
|
groups[i].start = start;
|
|
start += groups[i].messageCount;
|
|
}
|
|
Store.setAll({
|
|
folderId: folderId,
|
|
listGroups: groups,
|
|
messages: []
|
|
});
|
|
});
|
|
},
|
|
|
|
loadMessages: function(start, count)
|
|
{
|
|
console.log('load '+start+'..'+count);
|
|
superagent.get('backend/messages').query({ folderId: Store.get('folderId'), limit: count, offset: start }).end(function(err, res)
|
|
{
|
|
var msgs = Store.get('messages').slice(0);
|
|
var par = res.body.messages;
|
|
par.unshift(par.length);
|
|
par.unshift(start);
|
|
msgs.splice.apply(msgs, par);
|
|
Store.set('messages', msgs);
|
|
});
|
|
},
|
|
|
|
loadMessage: function(msgId, callback)
|
|
{
|
|
superagent.get('backend/message').query({ msgId: msgId }).end(function(err, res)
|
|
{
|
|
callback(res.body.msg);
|
|
});
|
|
},
|
|
|
|
startResync: function()
|
|
{
|
|
superagent.post('backend/sync').send().end(function(err, res)
|
|
{
|
|
});
|
|
}
|
|
};
|
|
|
|
Store.startIo();
|