|
|
@ -8,21 +8,40 @@ define([
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var handlers = {};
|
|
|
|
var handlers = {};
|
|
|
|
|
|
|
|
var removeHandlers = {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Store the friend request displayed to avoid duplicates
|
|
|
|
|
|
|
|
var friendRequest = {};
|
|
|
|
handlers['FRIEND_REQUEST'] = function (ctx, data, cb) {
|
|
|
|
handlers['FRIEND_REQUEST'] = function (ctx, data, cb) {
|
|
|
|
// Check if the request is valid (send by the correct user)
|
|
|
|
// Check if the request is valid (send by the correct user)
|
|
|
|
if (data.msg.author !== data.msg.content.curvePublic) { return void cb(true); }
|
|
|
|
if (data.msg.author !== data.msg.content.curvePublic) { return void cb(true); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Don't show duplicate friend request: if we already have a friend request
|
|
|
|
|
|
|
|
// in memory from the same user, dismiss the new one
|
|
|
|
|
|
|
|
if (friendRequest[data.msg.author]) { return void cb(true); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
friendRequest[data.msg.author] = true;
|
|
|
|
|
|
|
|
|
|
|
|
// If the user is already in our friend list, automatically accept the request
|
|
|
|
// If the user is already in our friend list, automatically accept the request
|
|
|
|
if (Messaging.getFriend(ctx.store.proxy, data.msg.author)) {
|
|
|
|
if (Messaging.getFriend(ctx.store.proxy, data.msg.author)) {
|
|
|
|
Messaging.acceptFriendRequest(ctx.store, data.msg.content, function (obj) {
|
|
|
|
Messaging.acceptFriendRequest(ctx.store, data.msg.content, function (obj) {
|
|
|
|
if (obj && obj.error) { return void cb(); }
|
|
|
|
if (obj && obj.error) {
|
|
|
|
|
|
|
|
return void cb();
|
|
|
|
|
|
|
|
}
|
|
|
|
cb(true);
|
|
|
|
cb(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cb();
|
|
|
|
cb();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
removeHandlers['FRIEND_REQUEST'] = function (ctx, box, data) {
|
|
|
|
|
|
|
|
if (friendRequest[data.curvePublic]) {
|
|
|
|
|
|
|
|
delete friendRequest[data.curvePublic];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var friendRequestDeclined = {};
|
|
|
|
handlers['DECLINE_FRIEND_REQUEST'] = function (ctx, box, data, cb) {
|
|
|
|
handlers['DECLINE_FRIEND_REQUEST'] = function (ctx, box, data, cb) {
|
|
|
|
setTimeout(function () {
|
|
|
|
setTimeout(function () {
|
|
|
|
// Our friend request was declined.
|
|
|
|
// Our friend request was declined.
|
|
|
@ -31,16 +50,34 @@ define([
|
|
|
|
// Remove the pending message and display the "declined" state in the UI
|
|
|
|
// Remove the pending message and display the "declined" state in the UI
|
|
|
|
delete ctx.store.proxy.friends_pending[data.msg.author];
|
|
|
|
delete ctx.store.proxy.friends_pending[data.msg.author];
|
|
|
|
ctx.updateMetadata();
|
|
|
|
ctx.updateMetadata();
|
|
|
|
|
|
|
|
if (friendRequestDeclined[data.msg.author]) { return; }
|
|
|
|
box.sendMessage({
|
|
|
|
box.sendMessage({
|
|
|
|
type: 'FRIEND_REQUEST_DECLINED',
|
|
|
|
type: 'FRIEND_REQUEST_DECLINED',
|
|
|
|
content: {
|
|
|
|
content: {
|
|
|
|
user: data.msg.author,
|
|
|
|
user: data.msg.author,
|
|
|
|
name: data.msg.content.displayName
|
|
|
|
name: data.msg.content.displayName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}, function () {
|
|
|
|
|
|
|
|
if (friendRequestDeclined[data.msg.author]) {
|
|
|
|
|
|
|
|
// TODO remove our message because another one was sent first?
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
friendRequestDeclined[data.msg.author] = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, getRandomTimeout(ctx));
|
|
|
|
}, getRandomTimeout(ctx));
|
|
|
|
cb(true);
|
|
|
|
cb(true);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
handlers['FRIEND_REQUEST_DECLINED'] = function (ctx, box, data, cb) {
|
|
|
|
|
|
|
|
if (friendRequestDeclined[data.msg.content.user]) { return void cb(true); }
|
|
|
|
|
|
|
|
friendRequestDeclined[data.msg.content.user] = true;
|
|
|
|
|
|
|
|
cb();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
removeHandlers['FRIEND_REQUEST_DECLINED'] = function (ctx, box, data) {
|
|
|
|
|
|
|
|
if (friendRequestDeclined[data.content.user]) {
|
|
|
|
|
|
|
|
delete friendRequestDeclined[data.content.user];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var friendRequestAccepted = {};
|
|
|
|
handlers['ACCEPT_FRIEND_REQUEST'] = function (ctx, box, data, cb) {
|
|
|
|
handlers['ACCEPT_FRIEND_REQUEST'] = function (ctx, box, data, cb) {
|
|
|
|
// Our friend request was accepted.
|
|
|
|
// Our friend request was accepted.
|
|
|
|
setTimeout(function () {
|
|
|
|
setTimeout(function () {
|
|
|
@ -65,24 +102,57 @@ define([
|
|
|
|
curvePublic: data.msg.author,
|
|
|
|
curvePublic: data.msg.author,
|
|
|
|
displayName: data.msg.content.displayName
|
|
|
|
displayName: data.msg.content.displayName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}, function () {
|
|
|
|
|
|
|
|
if (friendRequestAccepted[data.msg.author]) {
|
|
|
|
|
|
|
|
// TODO remove our message because another one was sent first?
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
friendRequestAccepted[data.msg.author] = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, getRandomTimeout(ctx));
|
|
|
|
}, getRandomTimeout(ctx));
|
|
|
|
cb(true);
|
|
|
|
cb(true);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
handlers['FRIEND_REQUEST_ACCEPTED'] = function (ctx, box, data, cb) {
|
|
|
|
|
|
|
|
if (friendRequestAccepted[data.msg.content.user]) { return void cb(true); }
|
|
|
|
|
|
|
|
friendRequestAccepted[data.msg.content.user] = true;
|
|
|
|
|
|
|
|
cb();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
removeHandlers['FRIEND_REQUEST_ACCEPTED'] = function (ctx, box, data) {
|
|
|
|
|
|
|
|
if (friendRequestAccepted[data.content.user]) {
|
|
|
|
|
|
|
|
delete friendRequestAccepted[data.content.user];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return function (ctx, box, data, cb) {
|
|
|
|
|
|
|
|
var type = data.msg.type;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (handlers[type]) {
|
|
|
|
return {
|
|
|
|
try {
|
|
|
|
add: function (ctx, box, data, cb) {
|
|
|
|
handlers[type](ctx, box, data, cb);
|
|
|
|
var type = data.msg.type;
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
console.error(e);
|
|
|
|
if (handlers[type]) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
handlers[type](ctx, box, data, cb);
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
console.error(e);
|
|
|
|
|
|
|
|
cb();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
cb();
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
},
|
|
|
|
cb();
|
|
|
|
remove: function (ctx, box, data, h) {
|
|
|
|
|
|
|
|
// We sometimes try to delete non-existant data (with "delete box.content[h]")
|
|
|
|
|
|
|
|
// In this case, we don't have the data in memory so we don't need to call
|
|
|
|
|
|
|
|
// any "remove" handler
|
|
|
|
|
|
|
|
if (!data) { return; }
|
|
|
|
|
|
|
|
var type = data.type;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (removeHandlers[type]) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
removeHandlers[type](ctx, box, data, h);
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
console.error(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|