cryptpad/www/common/notifications.js

175 lines
5.7 KiB
JavaScript
Raw Normal View History

2019-05-22 16:03:52 +00:00
define([
'jquery',
'/common/hyperscript.js',
'/common/common-hash.js',
2019-07-11 12:16:04 +00:00
'/common/common-interface.js',
2019-05-23 13:12:59 +00:00
'/common/common-ui-elements.js',
'/customize/messages.js',
2019-07-11 12:16:04 +00:00
], function ($, h, Hash, UI, UIElements, Messages) {
2019-05-22 16:03:52 +00:00
var handlers = {};
var defaultDismiss = function (common, data) {
return function (e) {
if (e) {
e.preventDefault();
e.stopPropagation();
}
common.mailbox.dismiss(data, function (err) {
if (err) { return void console.error(err); }
});
};
};
// Friend request
handlers['FRIEND_REQUEST'] = function (common, data) {
2019-05-22 16:03:52 +00:00
var content = data.content;
var msg = content.msg;
// Display the notification
content.getFormatText = function () {
return Messages._getKey('friendRequest_notification', [msg.content.displayName || Messages.anonymous]);
};
// Check authenticity
if (msg.author !== msg.content.curvePublic) { return; }
// if not archived, add handlers
if (!content.archived) {
content.handler = function () {
UIElements.displayFriendRequestModal(common, data);
};
common.addFriendRequest(data);
}
2019-05-22 16:03:52 +00:00
};
handlers['FRIEND_REQUEST_ACCEPTED'] = function (common, data) {
2019-05-22 16:03:52 +00:00
var content = data.content;
var msg = content.msg;
content.getFormatText = function () {
return Messages._getKey('friendRequest_accepted', [msg.content.name || Messages.anonymous]);
};
if (!content.archived) {
content.dismissHandler = defaultDismiss(common, data);
}
2019-05-23 13:25:05 +00:00
};
handlers['FRIEND_REQUEST_DECLINED'] = function (common, data) {
2019-05-23 13:25:05 +00:00
var content = data.content;
var msg = content.msg;
content.getFormatText = function () {
return Messages._getKey('friendRequest_declined', [msg.content.name || Messages.anonymous]);
};
if (!content.archived) {
content.dismissHandler = defaultDismiss(common, data);
}
2019-05-22 16:03:52 +00:00
};
// Share pad
handlers['SHARE_PAD'] = function (common, data) {
var content = data.content;
var msg = content.msg;
var type = Hash.parsePadUrl(msg.content.href).type;
var key = type === 'drive' ? 'notification_folderShared' :
(type === 'file' ? 'notification_fileShared' :
'notification_padShared');
content.getFormatText = function () {
return Messages._getKey(key, [msg.content.name || Messages.anonymous, msg.content.title]);
};
content.handler = function () {
2019-07-13 09:47:58 +00:00
var todo = function () {
common.openURL(msg.content.href);
defaultDismiss(common, data)();
};
if (!msg.content.password) { return void todo(); }
common.getSframeChannel().query('Q_SESSIONSTORAGE_PUT', {
key: 'newPadPassword',
value: msg.content.password
}, todo);
};
if (!content.archived) {
content.dismissHandler = defaultDismiss(common, data);
}
};
// New support message from the admins
handlers['SUPPORT_MESSAGE'] = function (common, data) {
var content = data.content;
content.getFormatText = function () {
return Messages.support_notification;
};
content.handler = function () {
common.openURL('/support/');
defaultDismiss(common, data)();
};
2019-07-16 12:37:03 +00:00
};
2019-07-11 12:16:04 +00:00
handlers['REQUEST_PAD_ACCESS'] = function (common, data) {
var content = data.content;
var msg = content.msg;
// Check authenticity
if (msg.author !== msg.content.user.curvePublic) { return; }
// Display the notification
content.getFormatText = function () {
2019-07-13 09:47:58 +00:00
return 'Edit access request: ' + msg.content.title + ' - ' + msg.content.user.displayName;
}; // XXX
2019-07-11 12:16:04 +00:00
// if not archived, add handlers
content.handler = function () {
UI.confirm("Give edit rights?", function (yes) {
if (!yes) { return; }
2019-07-13 09:47:58 +00:00
common.getSframeChannel().event('EV_GIVE_ACCESS', {
channel: msg.content.channel,
user: msg.content.user
});
defaultDismiss(common, data)();
2019-07-11 12:16:04 +00:00
});
};
2019-07-13 09:47:58 +00:00
if (!content.archived) {
content.dismissHandler = defaultDismiss(common, data);
}
};
2019-07-13 09:47:58 +00:00
handlers['GIVE_PAD_ACCESS'] = function (common, data) {
var content = data.content;
var msg = content.msg;
// Check authenticity
if (msg.author !== msg.content.user.curvePublic) { return; }
if (!msg.content.href) { return; }
// Display the notification
content.getFormatText = function () {
return 'Edit access received: ' + msg.content.title + ' from ' + msg.content.user.displayName;
}; // XXX
// if not archived, add handlers
content.handler = function () {
common.openURL(msg.content.href);
defaultDismiss(common, data)();
};
2019-07-11 12:16:04 +00:00
};
2019-05-22 16:03:52 +00:00
return {
add: function (common, data) {
2019-05-22 16:03:52 +00:00
var type = data.content.msg.type;
if (handlers[type]) {
handlers[type](common, data);
// add getters to access simply some informations
data.content.isClickable = typeof data.content.handler === "function";
data.content.isDismissible = typeof data.content.dismissHandler === "function";
2019-05-22 16:03:52 +00:00
}
},
remove: function (common, data) {
common.removeFriendRequest(data.hash);
},
};
});