Merge remote-tracking branch 'origin/support' into notifications

This commit is contained in:
ClemDee
2019-06-25 16:44:55 +02:00
14 changed files with 562 additions and 11 deletions

View File

@@ -17,6 +17,6 @@ define(function () {
// Sub
plan: 'CryptPad_plan',
// Apps
criticalApps: ['profile', 'settings', 'debug', 'admin', 'notifications']
criticalApps: ['profile', 'settings', 'debug', 'admin', 'support', 'notifications']
};
});

View File

@@ -85,6 +85,11 @@ define([
return id;
};
Hash.getChannelIdFromKey = function (publicKey) {
if (!publicKey) { return; }
return uint8ArrayToHex(Hash.decodeBase64(publicKey).subarray(0,16));
};
Hash.createRandomHash = function (type, password) {
var cryptor;
if (type === 'file') {

View File

@@ -478,6 +478,7 @@ define([
settings: store.proxy.settings,
thumbnails: disableThumbnails === false,
isDriveOwned: Boolean(Util.find(store, ['driveMetadata', 'owners'])),
support: Util.find(store.proxy, ['mailboxes', 'support', 'channel']),
pendingFriends: store.proxy.friends_pending || {}
}
};

View File

@@ -10,6 +10,7 @@ define([
var TYPES = [
'notifications',
'support'
];
var BLOCKING_TYPES = [
];
@@ -25,6 +26,16 @@ define([
if (res.error) { console.error(res); }
});
}
if (!mailboxes['support']) {
mailboxes.support = {
channel: Hash.createChannelId(),
lastKnownHash: '',
viewed: []
};
ctx.pinPads([mailboxes.support.channel], function (res) {
if (res.error) { console.error(res); }
});
}
};
/*
@@ -76,15 +87,30 @@ proxy.mailboxes = {
var crypto = Crypto.Mailbox.createEncryptor(keys);
var network = ctx.store.network;
var ciphertext = crypto.encrypt(JSON.stringify({
var text = JSON.stringify({
type: type,
content: msg
}), user.curvePublic);
});
var ciphertext = crypto.encrypt(text, user.curvePublic);
network.join(user.channel).then(function (wc) {
wc.bcast(ciphertext).then(function () {
cb();
wc.leave();
// If we've just sent a message to one of our mailboxes, we have to trigger the handler manually
// (the server won't send back our message to us)
var box;
if (Object.keys(ctx.boxes).some(function (t) {
var _box = ctx.boxes[t];
if (_box.channel === user.channel) {
box = _box;
return true;
}
})) {
var hash = ciphertext.slice(0, 64);
box.onMessage(text, null, null, null, hash, user.curvePublic);
}
});
}, function (err) {
cb({error: err});
@@ -157,6 +183,7 @@ proxy.mailboxes = {
var openChannel = function (ctx, type, m, onReady) {
var box = ctx.boxes[type] = {
channel: m.channel,
queue: [], // Store the messages to send when the channel is ready
history: [], // All the hashes loaded from the server in corretc order
content: {}, // Content of the messages that should be displayed
@@ -213,7 +240,7 @@ proxy.mailboxes = {
});
box.queue = [];
};
cfg.onMessage = function (msg, user, vKey, isCp, hash, author) {
box.onMessage = cfg.onMessage = function (msg, user, vKey, isCp, hash, author) {
if (hash === m.lastKnownHash) { return; }
try {
msg = JSON.parse(msg);

View File

@@ -90,8 +90,11 @@ define([
var pushMessage = function (data, handler) {
var todo = function (f) {
try {
var el = createElement(data);
Notifications.add(Common, data, el);
var el;
if (data.type === 'notifications') {
el = createElement(data);
Notifications.add(Common, data, el);
}
f(data, el);
} catch (e) {
console.error(e);
@@ -108,7 +111,9 @@ define([
onViewedHandlers.forEach(function (f) {
try {
f(data);
Notifications.remove(Common, data);
if (data.type === 'notifications') {
Notifications.remove(Common, data);
}
} catch (e) {
console.error(e);
}
@@ -139,18 +144,25 @@ define([
var subscribed = false;
// Get all existing notifications + the new ones when they come
mailbox.subscribe = function (cfg) {
mailbox.subscribe = function (types, cfg) {
if (!subscribed) {
execCommand('SUBSCRIBE', null, function () {});
subscribed = true;
}
if (typeof(cfg.onViewed) === "function") {
onViewedHandlers.push(cfg.onViewed);
onViewedHandlers.push(function (data) {
if (types.indexOf(data.type) === -1) { return; }
cfg.onViewed(data);
});
}
if (typeof(cfg.onMessage) === "function") {
onMessageHandlers.push(cfg.onMessage);
onMessageHandlers.push(function (data, el) {
if (types.indexOf(data.type) === -1) { return; }
cfg.onMessage(data, el);
});
}
Object.keys(history).forEach(function (type) {
if (types.indexOf(type) === -1) { return; }
history[type].forEach(function (data) {
pushMessage({
type: type,

View File

@@ -983,7 +983,7 @@ MessengerUI, Messages) {
$button.addClass('fa-bell');
};
Common.mailbox.subscribe({
Common.mailbox.subscribe(['notifications'], {
onMessage: function (data, el) {
if (el) {
div.appendChild(el);