Advertisement
Guest User

Untitled

a guest
Aug 1st, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. Parse.Cloud.define("addFriendToFriendsRelation", function(request, response) {
  2.  
  3. Parse.Cloud.useMasterKey();
  4.  
  5. var friendRequestId = request.params.friendRequest;
  6. var query = new Parse.Query("FriendRequest");
  7.  
  8. //get the friend request object
  9. query.get(friendRequestId, {
  10.  
  11. success: function(friendRequest) {
  12. //get the user the request was from
  13. var fromUser = friendRequest.get("from");
  14. //get the user the request is to
  15. var toUser = friendRequest.get("to");
  16.  
  17. var relation = fromUser.relation("friends");
  18. //add the user the request was to (the accepting user) to the fromUsers friends
  19. relation.add(toUser);
  20.  
  21. //save the fromUser
  22. fromUser.save(null, {
  23.  
  24. success: function() {
  25. //saved the user, now edit the request status and save it
  26. friendRequest.set("status", "accepted");
  27. friendRequest.save(null, {
  28.  
  29. success: function() {
  30.  
  31. response.success("saved relation and updated friendRequest");
  32. },
  33.  
  34. error: function(error) {
  35.  
  36. response.error("Error 1");
  37. }
  38.  
  39. });
  40.  
  41. },
  42.  
  43. error: function(error) {
  44.  
  45. response.error("Error 2");
  46.  
  47. }
  48.  
  49. });
  50.  
  51. },
  52.  
  53. error: function(error) {
  54.  
  55. response.error("Error 3");
  56.  
  57. }
  58.  
  59. });
  60.  
  61. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement