I've been trying to figure this out for quite a while, but I'm pretty stumped. I'm building an app with a Parse.com backend and I'm trying to implement this feature where when User A tries to send a game request to User B, both User A and User B acquire the game in a PFRelation. I've already done the part where I save the game to User A's (current user's) relation once he initiates the request:
NSString *challengePuzzleSize = [NSString stringWithFormat: @"%ld", (long)self.sizeSegmentedControl.selectedSegmentIndex + 5];
self.aNewMatch = [PFObject objectWithClassName:@"Messages"];
[self.aNewMatch setObject:file forKey:@"file"];
[self.aNewMatch setObject:fileType forKey:@"fileType"];
[self.aNewMatch setObject:self.opponent.objectId forKey:@"opponentIDs"];
[self.aNewMatch setObject:self.opponent.username forKey:@"opponentName"];
[self.aNewMatch setObject:[[PFUser currentUser] objectId] forKey:@"senderID"];
[self.aNewMatch setObject:[[PFUser currentUser] username] forKey:@"senderName"];
[self.aNewMatch setObject:self.opponent forKey:@"receiver"];
[self.aNewMatch setObject:challengePuzzleSize forKey:@"puzzleSize"];
[self.aNewMatch setObject:[PFUser currentUser] forKey:@"sender"];
[self.aNewMatch saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An error occurred." message:@"Please try sending your message again." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alertView show];
}
else {
PFRelation *currentGamesSender = [self.currentUser relationForKey:@"currentGames"];
NSLog(@"opponent: %@", self.opponent);
[currentGamesSender addObject:self.aNewMatch];
[self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
NSLog(@"Error %@ %@", error, [error userInfo]);
}
else {
NSLog(@"WORKS");
}
}];
I'm aware that I can't do the same thing with User B because he's not the current user and Parse restricts saving non-current users for security reasons. So, I'm trying to do this through Cloud Code which is what another person recommended when I was browsing for a solution. I have this as the call in Objective C:
[PFCloud callFunction:@"addCurrentGame" withParameters:@{@"userId": self.opponent.objectId, @"currentGameId": self.aNewMatch.objectId}];
But I'm unfamiliar with Javascript and I've been trying to figure out how to save a PFObject to User B's relation, and it isn't working for me. Here's that code:
Parse.Cloud.define("addCurrentGame", function(request, response) {
Parse.Cloud.useMasterKey();
var receiver = null;
var currentGame = null;
queryReceiverUser = new Parse.Query("User");
queryCurrentGame = new Parse.Query(“Messages”);
var receiverUserId = request.params.userId;
var currentGameId = request.params.currentGameId;
queryCurrentGame.get( currentGameId, {
success: function(_currentGame) {
currentGame = _currentGame;
error: function(error) {
console.log("Error retrieving user in query!");
}
});
queryReceiverUser.get( receiverUserId, {
success: function(_receiver) {
receiver = _receiver;
var currentGames = receiver.relation(“currentGames”);
currentGames.add(currentGame);
receiver.save(null, {
success: function() { console.log("Saved receiver”); },
error: function(err) { console.log("Error saving receiver, " + err.code); }
});
response.success("success");
},
error: function(error) {
console.log("Error retrieving user in query!");
}
});
});
I'm not at all sure how to create the PFObject game in Javascript and save it to the User B relation (my code isn't correct). If someone could please help me out with this, that would be awesome.