cryptpad/www/common/outer/mailbox-handlers.js

60 lines
1.9 KiB
JavaScript
Raw Normal View History

2019-05-22 16:03:52 +00:00
define([
'/common/common-messaging.js',
], function (Messaging) {
var handlers = {};
handlers['FRIEND_REQUEST'] = function (ctx, data, cb) {
if (data.msg.author === data.msg.content.curvePublic &&
Messaging.getFriend(ctx.store.proxy, data.msg.author)) {
Messaging.acceptFriendRequest(ctx.store, data.msg.content, function (obj) {
if (obj && obj.error) { return void cb(); }
cb(true);
});
return;
}
cb();
};
2019-05-23 13:25:05 +00:00
handlers['DECLINE_FRIEND_REQUEST'] = function (ctx, box, data, cb) {
// Our friend request was declined.
if (!ctx.store.proxy.friends_pending[data.msg.author]) { return void cb(true); }
delete ctx.store.proxy.friends_pending[data.msg.author];
ctx.updateMetadata();
cb();
};
2019-05-22 16:03:52 +00:00
handlers['ACCEPT_FRIEND_REQUEST'] = function (ctx, box, data, cb) {
// Our friend request was accepted.
// Make sure we really sent it
if (!ctx.store.proxy.friends_pending[data.msg.author]) { return void cb(); }
// And add the friend
Messaging.addToFriendList({
proxy: ctx.store.proxy,
realtime: ctx.store.realtime,
pinPads: ctx.pinPads
}, data.msg.content, function (err) {
if (err) { console.error(err); }
delete ctx.store.proxy.friends_pending[data.msg.author];
if (ctx.store.messenger) {
ctx.store.messenger.onFriendAdded(data.msg.content);
}
ctx.updateMetadata();
});
cb();
};
return function (ctx, box, data, cb) {
var type = data.msg.type;
if (handlers[type]) {
try {
handlers[type](ctx, box, data, cb);
} catch (e) {
cb();
}
} else {
cb();
}
};
});