cryptpad/www/common/sframe-common-mailbox.js

275 lines
9.6 KiB
JavaScript
Raw Normal View History

2019-05-15 16:22:39 +00:00
define([
'jquery',
'/common/common-util.js',
2020-02-05 11:16:04 +00:00
'/common/common-hash.js',
2019-05-15 16:22:39 +00:00
'/common/common-interface.js',
'/common/common-ui-elements.js',
2019-05-22 16:03:52 +00:00
'/common/notifications.js',
2019-05-17 14:19:41 +00:00
'/common/hyperscript.js',
'/customize/messages.js',
2020-02-05 11:16:04 +00:00
], function ($, Util, Hash, UI, UIElements, Notifications, h, Messages) {
2019-05-15 16:22:39 +00:00
var Mailbox = {};
Mailbox.create = function (Common) {
2019-05-17 14:19:41 +00:00
var mailbox = Common.mailbox;
2019-05-15 16:22:39 +00:00
var sframeChan = Common.getSframeChannel();
var execCommand = function (cmd, data, cb) {
sframeChan.query('Q_MAILBOX_COMMAND', {
cmd: cmd,
data: data
}, function (err, obj) {
if (err) { return void cb({error: err}); }
cb(obj);
});
};
var history = {};
var removeFromHistory = function (type, hash) {
2019-05-27 12:03:15 +00:00
if (!history[type]) { return; }
2019-05-15 16:22:39 +00:00
history[type] = history[type].filter(function (obj) {
return obj.hash !== hash;
});
};
mailbox.sendTo = function (type, content, user, cb) {
cb = cb || function () {};
execCommand('SENDTO', {
type: type,
msg: content,
user: user
}, function (err, obj) {
cb(err || (obj && obj.error), obj);
if (err || (obj && obj.error)) {
return void console.error(err || obj.error);
}
});
2019-05-15 16:22:39 +00:00
};
// UI
2019-05-17 14:19:41 +00:00
var formatData = function (data) {
return JSON.stringify(data.content.msg.content);
};
var createElement = mailbox.createElement = function (data) {
2019-05-17 14:19:41 +00:00
var notif;
2020-02-05 11:16:04 +00:00
var avatar;
2020-02-05 12:33:32 +00:00
var userData = Util.find(data, ['content', 'msg', 'content', 'user']);
2020-02-05 11:16:04 +00:00
if (userData && typeof(userData) === "object" && userData.profile) {
avatar = h('span.cp-avatar');
Common.displayAvatar($(avatar), userData.avatar, userData.displayName || userData.name);
$(avatar).click(function (e) {
e.stopPropagation();
Common.openURL(Hash.hashToHref(userData.profile, 'profile'));
});
}
2019-05-17 14:19:41 +00:00
notif = h('div.cp-notification', {
'data-hash': data.content.hash
2020-02-05 11:16:04 +00:00
}, [
avatar,
h('div.cp-notification-content',
h('p', formatData(data)))
]);
2019-08-27 12:39:15 +00:00
if (typeof(data.content.getFormatText) === "function") {
$(notif).find('.cp-notification-content p').html(data.content.getFormatText());
}
2019-08-27 13:56:27 +00:00
if (data.content.isClickable) {
$(notif).find('.cp-notification-content').addClass("cp-clickable")
.click(data.content.handler);
}
2019-08-27 13:56:27 +00:00
if (data.content.isDismissible) {
var dismissIcon = h('span.fa.fa-times');
var dismiss = h('div.cp-notification-dismiss', {
title: Messages.notifications_dismiss
}, dismissIcon);
$(dismiss).addClass("cp-clickable")
.click(data.content.dismissHandler);
$(notif).append(dismiss);
}
2019-05-17 14:19:41 +00:00
return notif;
};
2019-05-15 16:22:39 +00:00
var onViewedHandlers = [];
var onMessageHandlers = [];
onViewedHandlers.push(function (data) {
var hash = data.hash.replace(/"/g, '\\\"');
var $notif = $('.cp-notification[data-hash="'+hash+'"]:not(.cp-app-notification-archived)');
if ($notif.length) {
$notif.remove();
}
});
2019-05-15 16:22:39 +00:00
// Call the onMessage handlers
var isNotification = function (type) {
2020-06-10 10:14:12 +00:00
return type === "notifications" || /^team-/.test(type);
};
2019-05-17 14:19:41 +00:00
var pushMessage = function (data, handler) {
var todo = function (f) {
2019-05-15 16:22:39 +00:00
try {
2019-06-24 10:15:34 +00:00
var el;
if (isNotification(data.type)) {
Notifications.add(Common, data);
2019-06-24 10:15:34 +00:00
el = createElement(data);
}
2019-05-17 14:19:41 +00:00
f(data, el);
2019-05-15 16:22:39 +00:00
} catch (e) {
console.error(e);
}
2019-05-17 14:19:41 +00:00
};
if (typeof (handler) === "function") {
return void todo(handler);
2019-05-15 16:22:39 +00:00
}
2019-05-17 14:19:41 +00:00
onMessageHandlers.forEach(todo);
2019-05-15 16:22:39 +00:00
};
var onViewed = function (data) {
// data = { type: 'type', hash: 'hash' }
onViewedHandlers.forEach(function (f) {
try {
f(data);
if (isNotification(data.type)) {
2019-06-24 10:15:34 +00:00
Notifications.remove(Common, data);
}
2019-05-15 16:22:39 +00:00
} catch (e) {
console.error(e);
}
});
removeFromHistory(data.type, data.hash);
};
var onMessage = function (data, cb) {
2019-05-15 16:22:39 +00:00
// data = { type: 'type', content: {msg: 'msg', hash: 'hash'} }
pushMessage(data);
2019-09-09 16:10:20 +00:00
if (data.content && typeof (data.content.getFormatText) === "function") {
var text = $('<div>').html(data.content.getFormatText()).text();
cb({
msg: text
});
}
2019-05-15 16:22:39 +00:00
if (!history[data.type]) { history[data.type] = []; }
history[data.type].push(data.content);
};
2019-05-17 14:19:41 +00:00
mailbox.dismiss = function (data, cb) {
var dataObj = {
hash: data.content.hash,
type: data.type
};
execCommand('DISMISS', dataObj, function (obj) {
if (obj && obj.error) { return void cb(obj.error); }
onViewed(dataObj);
cb();
});
};
var subscribed = false;
2019-05-17 14:19:41 +00:00
// Get all existing notifications + the new ones when they come
2019-06-24 10:15:34 +00:00
mailbox.subscribe = function (types, cfg) {
if (!subscribed) {
execCommand('SUBSCRIBE', null, function () {});
subscribed = true;
}
var teams = types.indexOf('team') !== -1;
2019-05-17 14:19:41 +00:00
if (typeof(cfg.onViewed) === "function") {
2019-06-24 10:15:34 +00:00
onViewedHandlers.push(function (data) {
var type = data.type;
if (types.indexOf(type) === -1 && !(teams && /^team-/.test(type))) { return; }
2019-06-24 10:15:34 +00:00
cfg.onViewed(data);
});
2019-05-17 14:19:41 +00:00
}
if (typeof(cfg.onMessage) === "function") {
2019-06-24 10:15:34 +00:00
onMessageHandlers.push(function (data, el) {
var type = data.type;
if (types.indexOf(type) === -1 && !(teams && /^team-/.test(type))) { return; }
2019-06-24 10:15:34 +00:00
cfg.onMessage(data, el);
});
2019-05-17 14:19:41 +00:00
}
Object.keys(history).forEach(function (type) {
if (types.indexOf(type) === -1 && !(teams && /^team-/.test(type))) { return; }
2019-05-17 14:19:41 +00:00
history[type].forEach(function (data) {
pushMessage({
type: type,
content: data
}, cfg.onMessage);
});
});
};
2019-06-27 16:21:54 +00:00
var historyState = false;
var onHistory = function () {};
mailbox.getMoreHistory = function (type, count, lastKnownHash, cb) {
if (historyState) { return void cb("ALREADY_CALLED"); }
historyState = true;
var txid = Util.uid();
execCommand('LOAD_HISTORY', {
type: type,
count: lastKnownHash ? count + 1 : count,
2019-06-27 16:21:54 +00:00
txid: txid,
lastKnownHash: lastKnownHash
}, function (err, obj) {
if (obj && obj.error) { console.error(obj.error); }
});
var messages = [];
onHistory = function (data) {
if (data.txid !== txid) { return; }
if (data.complete) {
onHistory = function () {};
var end = messages.length < count;
cb(null, messages, end);
2019-06-27 16:21:54 +00:00
historyState = false;
return;
}
if (data.hash !== lastKnownHash) {
messages.push({
type: type,
content: {
msg: data.message,
time: data.time,
hash: data.hash
}
});
}
2019-06-27 16:21:54 +00:00
};
};
2019-06-28 14:06:44 +00:00
mailbox.getNotificationsHistory = function (type, count, lastKnownHash, cb) {
mailbox.getMoreHistory(type, count, lastKnownHash, function (err, messages, end) {
if (!Array.isArray(messages)) { return void cb(err); }
2019-06-28 14:06:44 +00:00
messages.forEach(function (data) {
data.content.archived = true;
Notifications.add(Common, data);
});
cb(err, messages, end);
2019-06-28 14:06:44 +00:00
});
};
2019-05-15 16:22:39 +00:00
// CHANNEL WITH WORKER
sframeChan.on('EV_MAILBOX_EVENT', function (obj, cb) {
2019-05-15 16:22:39 +00:00
// obj = { ev: 'type', data: obj }
var ev = obj.ev;
var data = obj.data;
2019-06-27 16:21:54 +00:00
if (ev === 'HISTORY') {
return void onHistory(data);
}
2019-05-15 16:22:39 +00:00
if (ev === 'MESSAGE') {
return void onMessage(data, cb);
2019-05-15 16:22:39 +00:00
}
if (ev === 'VIEWED') {
return void onViewed(data);
}
});
return mailbox;
};
return Mailbox;
});