|
|
@ -1,26 +1,33 @@
|
|
|
|
// jshint ignore: start
|
|
|
|
|
|
|
|
define([
|
|
|
|
define([
|
|
|
|
'/common/common-util.js',
|
|
|
|
'/common/common-util.js',
|
|
|
|
'/common/common-constants.js',
|
|
|
|
'/common/common-hash.js',
|
|
|
|
'/common/common-realtime.js',
|
|
|
|
'/common/common-realtime.js',
|
|
|
|
'/customize/messages.js',
|
|
|
|
|
|
|
|
'/bower_components/chainpad-netflux/chainpad-netflux.js',
|
|
|
|
'/bower_components/chainpad-netflux/chainpad-netflux.js',
|
|
|
|
'/bower_components/chainpad-crypto/crypto.js',
|
|
|
|
'/bower_components/chainpad-crypto/crypto.js',
|
|
|
|
], function (Util, Constants, Realtime, Messages, CpNetflux, Crypto) {
|
|
|
|
], function (Util, Hash, Realtime, CpNetflux, Crypto) {
|
|
|
|
var Mailbox = {};
|
|
|
|
var Mailbox = {};
|
|
|
|
|
|
|
|
|
|
|
|
var TYPES = [
|
|
|
|
var TYPES = [
|
|
|
|
'notifications'
|
|
|
|
'notifications',
|
|
|
|
|
|
|
|
'test'
|
|
|
|
];
|
|
|
|
];
|
|
|
|
var BLOCKING_TYPES = [
|
|
|
|
var BLOCKING_TYPES = [
|
|
|
|
];
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var initializeMailboxes = function (mailboxes) {
|
|
|
|
|
|
|
|
if (!mailboxes['notifications']) {
|
|
|
|
|
|
|
|
mailboxes.notifications = {
|
|
|
|
|
|
|
|
channel: Hash.createChannelId(),
|
|
|
|
|
|
|
|
lastKnownHash: '',
|
|
|
|
|
|
|
|
viewed: []
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
proxy.mailboxes = {
|
|
|
|
proxy.mailboxes = {
|
|
|
|
friends: {
|
|
|
|
friends: {
|
|
|
|
keys: '',
|
|
|
|
|
|
|
|
channel: '',
|
|
|
|
channel: '',
|
|
|
|
hash: '',
|
|
|
|
|
|
|
|
lastKnownHash: '',
|
|
|
|
lastKnownHash: '',
|
|
|
|
viewed: []
|
|
|
|
viewed: []
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -41,8 +48,9 @@ proxy.mailboxes = {
|
|
|
|
|
|
|
|
|
|
|
|
var openChannel = function (ctx, type, m, onReady) {
|
|
|
|
var openChannel = function (ctx, type, m, onReady) {
|
|
|
|
var box = ctx.boxes[type] = {
|
|
|
|
var box = ctx.boxes[type] = {
|
|
|
|
queue: [],
|
|
|
|
queue: [], // Store the messages to send when the channel is ready
|
|
|
|
history: [],
|
|
|
|
history: [], // All the hashes loaded from the server in corretc order
|
|
|
|
|
|
|
|
content: {}, // Content of the messages that should be displayed
|
|
|
|
sendMessage: function (msg) { // To send a message to our box
|
|
|
|
sendMessage: function (msg) { // To send a message to our box
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
msg = JSON.stringify(msg);
|
|
|
|
msg = JSON.stringify(msg);
|
|
|
@ -52,6 +60,7 @@ proxy.mailboxes = {
|
|
|
|
box.queue.push(msg);
|
|
|
|
box.queue.push(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
Crypto = Crypto;
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
// XXX
|
|
|
|
// XXX
|
|
|
|
if (!Crypto.Mailbox) {
|
|
|
|
if (!Crypto.Mailbox) {
|
|
|
@ -90,37 +99,61 @@ proxy.mailboxes = {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
box.queue = [];
|
|
|
|
box.queue = [];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
cfg.onMessage = function (msg, user, vKey, isCpi, hash) {
|
|
|
|
cfg.onMessage = function (msg, user, vKey, isCp, hash) {
|
|
|
|
// TODO
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
msg = JSON.parse(msg);
|
|
|
|
msg = JSON.parse(msg);
|
|
|
|
} catch (e) {
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
box.history.push(hash);
|
|
|
|
if (isMessageNew(hash, m)) {
|
|
|
|
if (isMessageNew(hash, m)) {
|
|
|
|
// Message should be displayed
|
|
|
|
// Message should be displayed
|
|
|
|
var message = {
|
|
|
|
var message = {
|
|
|
|
msg: msg,
|
|
|
|
msg: msg,
|
|
|
|
hash: hash
|
|
|
|
hash: hash
|
|
|
|
};
|
|
|
|
};
|
|
|
|
box.history.push(message);
|
|
|
|
box.content[hash] = msg;
|
|
|
|
showMessage(ctx, type, message);
|
|
|
|
showMessage(ctx, type, message);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// Message has already been viewer by the user
|
|
|
|
// Message has already been viewed by the user
|
|
|
|
if (history.length === 0) {
|
|
|
|
if (Object.keys(box.content).length === 0) {
|
|
|
|
|
|
|
|
// If nothing is displayed yet, we can bump our lastKnownHash and remove this hash
|
|
|
|
|
|
|
|
// from our "viewed" array
|
|
|
|
m.lastKnownHash = hash;
|
|
|
|
m.lastKnownHash = hash;
|
|
|
|
|
|
|
|
box.history = [];
|
|
|
|
|
|
|
|
var idxViewed = m.viewed.indexOf(hash);
|
|
|
|
|
|
|
|
if (idxViewed !== -1) { m.viewed.splice(idxViewed, 1); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log(hash + ' is not new');
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
cfg.onReady = function () {
|
|
|
|
cfg.onReady = function () {
|
|
|
|
|
|
|
|
// Clean the "viewed" array: make sure all the "viewed" hashes are
|
|
|
|
|
|
|
|
// in history
|
|
|
|
|
|
|
|
var toClean = [];
|
|
|
|
|
|
|
|
m.viewed.forEach(function (h, i) {
|
|
|
|
|
|
|
|
if (box.history.indexOf(h) === -1) {
|
|
|
|
|
|
|
|
toClean.push(i);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
for (var i = toClean.length-1; i>=0; i--) {
|
|
|
|
|
|
|
|
m.viewed.splice(toClean[i], 1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continue
|
|
|
|
onReady();
|
|
|
|
onReady();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
CpNetflux.start(cfg);
|
|
|
|
CpNetflux.start(cfg);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Send a message to someone else
|
|
|
|
// Send a message to someone else
|
|
|
|
var sendTo = function () {
|
|
|
|
/*var sendTo = function () {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
};*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var updateLastKnownHash = function (ctx, type) {
|
|
|
|
|
|
|
|
var m = Util.find(ctx, ['store', 'proxy', 'mailboxes', type]);
|
|
|
|
|
|
|
|
if (!m) { return; }
|
|
|
|
|
|
|
|
var box = ctx.boxes[type];
|
|
|
|
|
|
|
|
if (!box) { return; }
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -138,32 +171,46 @@ proxy.mailboxes = {
|
|
|
|
// - otherwise, just push to view
|
|
|
|
// - otherwise, just push to view
|
|
|
|
var idx;
|
|
|
|
var idx;
|
|
|
|
if (box.history.some(function (el, i) {
|
|
|
|
if (box.history.some(function (el, i) {
|
|
|
|
if (hash === el.hash) {
|
|
|
|
if (hash === el) {
|
|
|
|
idx = i;
|
|
|
|
idx = i;
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})) {
|
|
|
|
})) {
|
|
|
|
if (idx === 0) {
|
|
|
|
if (idx === 0) {
|
|
|
|
m.lastKnownHash = hash;
|
|
|
|
m.lastKnownHash = hash;
|
|
|
|
|
|
|
|
box.history.shift();
|
|
|
|
|
|
|
|
delete box.content[hash];
|
|
|
|
} else if (m.viewed.indexOf(hash) === -1) {
|
|
|
|
} else if (m.viewed.indexOf(hash) === -1) {
|
|
|
|
m.viewed.push(hash);
|
|
|
|
m.viewed.push(hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Clear data in memory if needed
|
|
|
|
// Check the "viewed" array to see if we're able to bump lastKnownhash more
|
|
|
|
// Check the "viewed" array to see if we're able to bump lastKnownhash more
|
|
|
|
var sliceIdx;
|
|
|
|
var sliceIdx;
|
|
|
|
box.history.some(function (el, i) {
|
|
|
|
var lastKnownHash;
|
|
|
|
var isViewed = m.viewed.indexOf(el.hash);
|
|
|
|
box.history.some(function (hash, i) {
|
|
|
|
|
|
|
|
var isViewed = m.viewed.indexOf(hash);
|
|
|
|
if (isViewed !== -1) {
|
|
|
|
if (isViewed !== -1) {
|
|
|
|
sliceIdx = i + 1;
|
|
|
|
sliceIdx = i + 1;
|
|
|
|
m.viewed.splice(isViewed, 1);
|
|
|
|
m.viewed.splice(isViewed, 1);
|
|
|
|
|
|
|
|
lastKnownHash = hash;
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (sliceIdx) {
|
|
|
|
if (sliceIdx) {
|
|
|
|
box.history = box.history.slice(sliceIdx);
|
|
|
|
box.history = box.history.slice(sliceIdx);
|
|
|
|
|
|
|
|
m.lastKnownHash = lastKnownHash;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Make sure we remove data about dismissed messages
|
|
|
|
|
|
|
|
Object.keys(box.content).forEach(function (h) {
|
|
|
|
|
|
|
|
if (box.history.indexOf(h) === -1 || m.viewed.indexOf(h) !== -1) {
|
|
|
|
|
|
|
|
delete box.content[h];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
Realtime.whenRealtimeSyncs(ctx.store.realtime, function () {
|
|
|
|
Realtime.whenRealtimeSyncs(ctx.store.realtime, function () {
|
|
|
|
cb();
|
|
|
|
cb();
|
|
|
@ -173,8 +220,12 @@ proxy.mailboxes = {
|
|
|
|
var subscribe = function (ctx, data, cId, cb) {
|
|
|
|
var subscribe = function (ctx, data, cId, cb) {
|
|
|
|
// Get existing notifications
|
|
|
|
// Get existing notifications
|
|
|
|
Object.keys(ctx.boxes).forEach(function (type) {
|
|
|
|
Object.keys(ctx.boxes).forEach(function (type) {
|
|
|
|
ctx.boxes[type].history.forEach(function (obj) {
|
|
|
|
Object.keys(ctx.boxes[type].content).forEach(function (h) {
|
|
|
|
showMessage(ctx, type, obj, cId);
|
|
|
|
var message = {
|
|
|
|
|
|
|
|
msg: ctx.boxes[type].content[h],
|
|
|
|
|
|
|
|
hash: h
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
showMessage(ctx, type, message, cId);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
// Subscribe to new notifications
|
|
|
|
// Subscribe to new notifications
|
|
|
@ -199,13 +250,17 @@ proxy.mailboxes = {
|
|
|
|
boxes: {}
|
|
|
|
boxes: {}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var mailboxes = store.proxy.mailboxes || {};
|
|
|
|
var mailboxes = store.proxy.mailboxes = store.proxy.mailboxes || {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initializeMailboxes(mailboxes);
|
|
|
|
|
|
|
|
|
|
|
|
Object.keys(mailboxes).forEach(function (key) {
|
|
|
|
Object.keys(mailboxes).forEach(function (key) {
|
|
|
|
if (TYPES.indexOf(key) === -1) { return; }
|
|
|
|
if (TYPES.indexOf(key) === -1) { return; }
|
|
|
|
var m = mailboxes[key];
|
|
|
|
var m = mailboxes[key];
|
|
|
|
|
|
|
|
|
|
|
|
if (BLOCKING_TYPES.indexOf(key) === -1) {
|
|
|
|
if (BLOCKING_TYPES.indexOf(key) === -1) {
|
|
|
|
openChannel(ctx, key, m, function () {
|
|
|
|
openChannel(ctx, key, m, function () {
|
|
|
|
|
|
|
|
updateLastKnownHash(ctx, key);
|
|
|
|
console.log(key + ' mailbox is ready');
|
|
|
|
console.log(key + ' mailbox is ready');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|