cryptpad/www/common/common-messaging.js

144 lines
4.8 KiB
JavaScript
Raw Normal View History

2017-07-06 08:24:28 +00:00
define([
'/bower_components/chainpad-crypto/crypto.js',
2017-08-11 08:59:54 +00:00
'/common/common-hash.js',
'/common/common-util.js',
2017-11-21 15:46:19 +00:00
'/common/common-constants.js',
'/customize/messages.js',
2017-08-11 08:59:54 +00:00
'/common/common-realtime.js',
2017-11-30 14:01:17 +00:00
], function (Crypto, Hash, Util, Constants, Messages, Realtime) {
2019-05-21 16:43:11 +00:00
var Msg = {};
2017-07-06 08:24:28 +00:00
2017-08-11 08:59:54 +00:00
var createData = Msg.createData = function (proxy, hash) {
2019-09-20 13:27:20 +00:00
var data = {
2017-08-11 08:59:54 +00:00
channel: hash || Hash.createChannelId(),
displayName: proxy['cryptpad.username'],
2017-07-13 15:32:57 +00:00
profile: proxy.profile && proxy.profile.view,
2017-07-07 16:53:21 +00:00
edPublic: proxy.edPublic,
curvePublic: proxy.curvePublic,
2019-05-21 16:43:11 +00:00
notifications: Util.find(proxy, ['mailboxes', 'notifications', 'channel']),
avatar: proxy.profile && proxy.profile.avatar,
2021-09-01 10:37:16 +00:00
uid: proxy.uid,
2017-07-07 16:53:21 +00:00
};
2019-09-20 13:27:20 +00:00
if (hash === false) { delete data.channel; }
return data;
2017-07-07 16:53:21 +00:00
};
2019-05-21 16:43:11 +00:00
var getFriend = Msg.getFriend = function (proxy, pubkey) {
if (!pubkey) { return; }
if (pubkey === proxy.curvePublic) {
2017-08-11 08:59:54 +00:00
var data = createData(proxy);
2017-07-07 16:53:21 +00:00
delete data.channel;
return data;
}
return proxy.friends ? proxy.friends[pubkey] : undefined;
};
2017-08-11 08:59:54 +00:00
var getFriendList = Msg.getFriendList = function (proxy) {
if (!proxy.friends) { proxy.friends = {}; }
return proxy.friends;
2017-07-06 16:00:03 +00:00
};
2017-08-11 08:59:54 +00:00
var eachFriend = function (friends, cb) {
Object.keys(friends).forEach(function (id) {
if (id === 'me') { return; }
cb(friends[id], id, friends);
});
};
2017-11-30 14:01:17 +00:00
Msg.getFriendChannelsList = function (proxy) {
2017-07-10 08:39:57 +00:00
var list = [];
2017-08-31 13:49:20 +00:00
eachFriend(proxy.friends, function (friend) {
2017-08-11 08:59:54 +00:00
list.push(friend.channel);
2017-07-10 08:39:57 +00:00
});
return list;
};
2020-02-05 12:33:32 +00:00
Msg.declineFriendRequest = function (store, data, cb) {
store.mailbox.sendTo('DECLINE_FRIEND_REQUEST', {}, {
channel: data.notifications,
curvePublic: data.curvePublic
}, function (obj) {
cb(obj);
});
};
2019-05-21 16:43:11 +00:00
Msg.acceptFriendRequest = function (store, data, cb) {
var friend = getFriend(store.proxy, data.curvePublic) || {};
var myData = createData(store.proxy, friend.channel || data.channel);
2020-02-05 12:33:32 +00:00
store.mailbox.sendTo('ACCEPT_FRIEND_REQUEST', { user: myData }, {
2019-05-21 16:43:11 +00:00
channel: data.notifications,
curvePublic: data.curvePublic
}, function (obj) {
cb(obj);
});
};
2019-05-21 16:43:11 +00:00
Msg.addToFriendList = function (cfg, data, cb) {
2017-11-30 14:01:17 +00:00
var proxy = cfg.proxy;
2017-08-11 08:59:54 +00:00
var friends = getFriendList(proxy);
var pubKey = data.curvePublic; // todo validata data
2017-07-06 08:24:28 +00:00
if (pubKey === proxy.curvePublic) { return void cb("E_MYKEY"); }
2017-07-06 08:24:28 +00:00
friends[pubKey] = data;
2017-11-30 14:01:17 +00:00
Realtime.whenRealtimeSyncs(cfg.realtime, function () {
cb();
2017-11-30 14:01:17 +00:00
cfg.pinPads([data.channel], function (res) {
if (res.error) { console.error(res.error); }
});
2017-07-10 08:39:57 +00:00
});
2017-07-06 08:24:28 +00:00
};
Msg.updateMyData = function (store, curve) {
2019-09-25 16:21:45 +00:00
var myData = createData(store.proxy, false);
if (store.proxy.friends) {
2020-11-25 14:14:33 +00:00
store.proxy.friends.me = Util.clone(myData);
delete store.proxy.friends.me.channel;
}
2019-09-25 16:21:45 +00:00
if (store.modules['team']) {
store.modules['team'].updateMyData(myData);
}
var todo = function (friend) {
if (!friend || !friend.notifications) { return; }
2020-11-25 14:14:33 +00:00
delete friend.user;
myData.channel = friend.channel;
store.mailbox.sendTo('UPDATE_DATA', myData, {
channel: friend.notifications,
curvePublic: friend.curvePublic
}, function (obj) {
if (obj && obj.error) { console.error(obj); }
});
};
if (curve) {
var friend = getFriend(store.proxy, curve);
return void todo(friend);
}
eachFriend(store.proxy.friends || {}, todo);
};
2019-09-16 13:56:50 +00:00
Msg.removeFriend = function (store, curvePublic, cb) {
var proxy = store.proxy;
var friend = proxy.friends[curvePublic];
if (!friend) { return void cb({error: 'ENOENT'}); }
2020-02-05 12:33:32 +00:00
if (!friend.notifications) { return void cb({error: 'EINVAL'}); }
2019-09-16 13:56:50 +00:00
store.mailbox.sendTo('UNFRIEND', {
curvePublic: proxy.curvePublic
}, {
channel: friend.notifications,
curvePublic: friend.curvePublic
}, function (obj) {
if (obj && obj.error) {
return void cb(obj);
}
store.messenger.onFriendRemoved(curvePublic, friend.channel);
delete proxy.friends[curvePublic];
Realtime.whenRealtimeSyncs(store.realtime, function () {
cb(obj);
});
});
};
2017-07-06 08:24:28 +00:00
return Msg;
});