2

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.

Has anyone successfully accomplished this?

Any tips / advice / tutorial on how to approach this?

Thank you!

EDIT:

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.

Luke Irvin
  • 1,179
  • 1
  • 20
  • 39

1 Answers1

10

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.

Timothy Walters
  • 16,866
  • 2
  • 41
  • 49
  • Couls you show some code on how to make a class? I'm a little confused. Would it add that to parse? – i_duhh_bomb Jan 02 '14 at 15:22
  • In the Data Browser on parse.com, an object is called a "Class", in your code it is just another Parse Object. Some (most) languages allow you to create a class to represent the object type, but that is optional. – Timothy Walters Jan 05 '14 at 22:23
  • @TimothyWalters thats a useful answer, just going a little further with it. How are the users actually connected once a friend request is accepted? is this in another object where the shared info is stored? – Dano007 May 15 '14 at 14:13
  • 3
    @Dano007 I guess you would just look up the Friendship Class and figure out if it contains both users with a status of "approved" – chrs Jul 10 '14 at 21:55
  • @TimothyWalters I have been looking at your code recently and using it to create a friend system in Parse. I have also seen many more recent posts that talk about adding and keeping track of friends in cloud code, and also setting up ACL's there. I'm just trying to process all of this and figure out the best way to proceed... any tips? Also, it would be amazing, since so many people need this feature, if there were some objective-C sample app set up. Especially with the cloud code/ACL stuff if that is now the way to go. I would even purchase it if given the opportunity! – SAHM May 04 '15 at 01:59
  • Reference for above comment: http://blog.parse.com/learn/engineering/parse-security-v-how-to-make-friends/ – SAHM May 04 '15 at 02:01
  • @SAHM here's a couple of articles I wrote about it a while back, using JavaScript and Parse, but easy enough to convert JavaScript to anything: http://timothywalters-devthoughts.blogspot.com.au I might do a version with Swift code, unlikely I would do Objective-C though, Swift is much more fun for iOS. – Timothy Walters May 04 '15 at 09:09