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

339 lines
12 KiB
JavaScript
Raw Normal View History

2023-10-20 14:35:26 +00:00
// SPDX-FileCopyrightText: 2023 XWiki CryptPad Team <contact@cryptpad.org> and contributors
//
// SPDX-License-Identifier: AGPL-3.0-or-later
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',
], 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 priv = Common.getMetadataMgr().getPrivateData();
2019-05-15 16:22:39 +00:00
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 (obj) {
cb(obj && obj.error, obj);
if (obj && obj.error) {
return void console.error(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']);
if (Util.find(data, ['content', 'msg', 'type']) === 'BROADCAST_DELETE') {
return;
}
if (data.type === 'broadcast') {
avatar = h('i.fa.fa-bullhorn.cp-broadcast');
if (/^LOCAL\|/.test(data.content.hash)) {
$(avatar).addClass('preview');
}
2021-04-09 16:10:13 +00:00
} else if (data.type === 'reminders') {
avatar = h('i.fa.fa-calendar.cp-broadcast.preview');
if (priv.app !== 'calendar') { avatar.classList.add('cp-reminder'); }
$(avatar).click(function (e) {
e.stopPropagation();
if (data.content && data.content.handler) {
return void data.content.handler();
}
Common.openURL(Hash.hashToHref('', 'calendar'));
});
} else if (userData && typeof(userData) === "object" && userData.profile) {
2020-02-05 11:16:04 +00:00
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'));
});
}
var order = -Math.floor((Util.find(data, ['content', 'msg', 'ctime']) || 0) / 1000);
2023-12-05 14:39:58 +00:00
const tabIndexValue = undefined;//data.content.isDismissible ? undefined : '0';
2023-11-12 18:33:37 +00:00
notif = h('li.cp-notification', {
2023-11-27 15:12:51 +00:00
role: 'menuitem',
2023-12-05 14:39:58 +00:00
tabindex: '0',
style: 'order:'+order+';',
2019-05-17 14:19:41 +00:00
'data-hash': data.content.hash
2020-02-05 11:16:04 +00:00
}, [
avatar,
h('div.cp-notification-content', {
tabindex: tabIndexValue
}, [
h('p', formatData(data))
])
2020-02-05 11:16:04 +00:00
]);
2019-08-27 12:39:15 +00:00
if (typeof(data.content.getFormatText) === "function") {
$(notif).find('.cp-notification-content p').html(data.content.getFormatText());
2021-04-12 15:55:56 +00:00
if (data.content.autorefresh) {
var it = setInterval(function () {
if (!data.content.autorefresh) {
clearInterval(it);
return;
}
$(notif).find('.cp-notification-content p').html(data.content.getFormatText());
}, 60000);
}
}
2023-12-05 14:39:58 +00:00
$(notif).mouseenter((e) => {
e.stopPropagation();
$(notif).focus();
});
2019-08-27 13:56:27 +00:00
if (data.content.isClickable) {
$(notif).find('.cp-notification-content').addClass("cp-clickable").on('click keypress', function (event) {
if (event.type === 'click' || (event.type === 'keypress' && event.which === 13)) {
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")
.on('click keypress', function (event) {
if (event.type === 'click' || (event.type === 'keypress' && event.which === 13)) {
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, '\\\"');
2022-09-12 17:06:25 +00:00
if (/^REMINDER\|/.test(hash)) { hash = hash.split('-')[0]; }
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) {
2021-04-09 16:10:13 +00:00
return type === "notifications" || /^team-/.test(type) || type === "broadcast" || type === "reminders";
};
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 = mailbox.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
};
if (/^LOCAL\|/.test(dataObj.hash)) {
onViewed(dataObj);
cb();
return;
}
2019-05-17 14:19:41 +00:00
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" && !cfg.history) {
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 onHistory = function () {};
2021-04-08 14:37:26 +00:00
var onHistoryEnd;
mailbox.getMoreHistory = function (type, count, lastKnownHash, cb) {
if (type === "broadcast" && onHistoryEnd) {
onHistoryEnd.reg(cb);
return;
}
if (onHistoryEnd) { return void cb("ALREADY_CALLED"); }
onHistoryEnd = Util.mkEvent();
onHistoryEnd.reg(cb);
2019-06-27 16:21:54 +00:00
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;
2021-04-08 14:37:26 +00:00
onHistoryEnd.fire(null, messages, end);
onHistoryEnd = undefined;
2019-06-27 16:21:54 +00:00
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;
});