You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cryptpad/www/common/common-messaging.js

144 lines
4.8 KiB
JavaScript

8 years ago
define([
'/bower_components/chainpad-crypto/crypto.js',
7 years ago
'/common/common-hash.js',
'/common/common-util.js',
'/common/common-constants.js',
'/customize/messages.js',
7 years ago
'/common/common-realtime.js',
7 years ago
], function (Crypto, Hash, Util, Constants, Messages, Realtime) {
var Msg = {};
8 years ago
7 years ago
var createData = Msg.createData = function (proxy, hash) {
5 years ago
var data = {
7 years ago
channel: hash || Hash.createChannelId(),
displayName: proxy['cryptpad.username'],
profile: proxy.profile && proxy.profile.view,
8 years ago
edPublic: proxy.edPublic,
curvePublic: proxy.curvePublic,
notifications: Util.find(proxy, ['mailboxes', 'notifications', 'channel']),
avatar: proxy.profile && proxy.profile.avatar,
uid: proxy.uid,
8 years ago
};
5 years ago
if (hash === false) { delete data.channel; }
return data;
8 years ago
};
var getFriend = Msg.getFriend = function (proxy, pubkey) {
if (!pubkey) { return; }
if (pubkey === proxy.curvePublic) {
7 years ago
var data = createData(proxy);
8 years ago
delete data.channel;
return data;
}
return proxy.friends ? proxy.friends[pubkey] : undefined;
};
7 years ago
var getFriendList = Msg.getFriendList = function (proxy) {
if (!proxy.friends) { proxy.friends = {}; }
return proxy.friends;
};
7 years ago
var eachFriend = function (friends, cb) {
Object.keys(friends).forEach(function (id) {
if (id === 'me') { return; }
cb(friends[id], id, friends);
});
};
7 years ago
Msg.getFriendChannelsList = function (proxy) {
var list = [];
eachFriend(proxy.friends, function (friend) {
7 years ago
list.push(friend.channel);
});
return list;
};
5 years ago
Msg.declineFriendRequest = function (store, data, cb) {
store.mailbox.sendTo('DECLINE_FRIEND_REQUEST', {}, {
channel: data.notifications,
curvePublic: data.curvePublic
}, function (obj) {
cb(obj);
});
};
Msg.acceptFriendRequest = function (store, data, cb) {
var friend = getFriend(store.proxy, data.curvePublic) || {};
var myData = createData(store.proxy, friend.channel || data.channel);
5 years ago
store.mailbox.sendTo('ACCEPT_FRIEND_REQUEST', { user: myData }, {
channel: data.notifications,
curvePublic: data.curvePublic
}, function (obj) {
cb(obj);
});
};
Msg.addToFriendList = function (cfg, data, cb) {
7 years ago
var proxy = cfg.proxy;
7 years ago
var friends = getFriendList(proxy);
var pubKey = data.curvePublic; // todo validata data
8 years ago
if (pubKey === proxy.curvePublic) { return void cb("E_MYKEY"); }
8 years ago
friends[pubKey] = data;
7 years ago
Realtime.whenRealtimeSyncs(cfg.realtime, function () {
cb();
7 years ago
cfg.pinPads([data.channel], function (res) {
if (res.error) { console.error(res.error); }
});
});
8 years ago
};
Msg.updateMyData = function (store, curve) {
var myData = createData(store.proxy, false);
if (store.proxy.friends) {
store.proxy.friends.me = Util.clone(myData);
delete store.proxy.friends.me.channel;
}
if (store.modules['team']) {
store.modules['team'].updateMyData(myData);
}
var todo = function (friend) {
if (!friend || !friend.notifications) { return; }
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);
};
5 years ago
Msg.removeFriend = function (store, curvePublic, cb) {
var proxy = store.proxy;
var friend = proxy.friends[curvePublic];
if (!friend) { return void cb({error: 'ENOENT'}); }
5 years ago
if (!friend.notifications) { return void cb({error: 'EINVAL'}); }
5 years ago
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);
});
});
};
8 years ago
return Msg;
});