Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to accept or deny a friend request with Parse?

Right now the "Friend" component in my app lists every user that has signed up for the app. User A can then add as many friends as they want and interact with them.

What I want to do is allow User A to send a request to those users so that User B, User C, etc. can either accept or deny the request to interact with User A.

Here is what I have tried.

I have a new class called Friendship that contains:

toUser - points to the User class

fromUser - points to the User class

status - string

PFQuery * query = [PFQuery queryWithClassName:@"Friendship"];
[query whereKey:@"toUser" equalTo:[PFUser currentUser]];
[query whereKey@"status" equalTo:@"pending"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (error)
    {
        //NSLog error
    }
    else {
         self.followingArray = objects;
         [self.myTableView reloadData];
    }
         

Objects is not returning anything.

like image 340
Luke Irvin Avatar asked Dec 04 '25 03:12

Luke Irvin


1 Answers

I would suggest a FriendRequest class as follows:

Class: FriendRequest
fromUser (pointer:User)
toUser (pointer:User)
status (string)

Your operations are broken down into the following:

  • Show pending requests sent by me
  • Show requests awaiting my approve/deny
  • Approve request
  • Deny request
  • Send a friend request
  • Un-friend a user

Showing pending requests would be a matter of doing a query against the FriendRequest class where fromUser is the current user where status is "pending".

Showing requests awaiting action would be a query against the FriendRequest class where toUser is the current user where status is "pending".

Approving a request would mean updating the FriendRequest record to have a status of "approved" and adding each users to the others Friends list (probably a relation field on the User class).

Denying a request would mean updating the FriendRequest record to have a status of "denied".

Sending a friend request would check to see if there is any existing record where fromUser is the current User and toUser is the target, or fromUser is the target and toUser is the current User, regardless of status. If there is an existing record, don't allow another request (unless you want to set some rules about this, e.g. can send a new request after 30 days, which would require storing the last request date). If no record was found, create a new FriendRequest record with a status of "pending".

Un-friending would mean removing the users from each others friends relations, and updating the FriendRequest.status to "unfriended".

Bonus points if you add an ability to see rejections/unfriended records.

like image 94
Timothy Walters Avatar answered Dec 06 '25 15:12

Timothy Walters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!