Rich Luick
Rich Luick

Reputation: 2364

Adding a Two Way Friend Relation on Parse for Android

I am currently working on an app with a friendship feature similar to Facebook(a request is sent and if accepted they both become friends). The sending user can select multiple users from a list and send them all invites at once. When this happens, the receiving users are added to a relation called "pendingRelation" for the sending user. However, I would also like the sending user to be added as a "pendingRelation" for all the receiving users as soon as the request is sent. I have messed around and haven't been able to find a good solution for this. The code to add the selected users as "pendingRelation" is simple.

private boolean sendFriendRequest() {

    //Cycles through list of selected friends and adds as "Pending"
    for (int i = 0; i < mPendingFriends.size(); i++) { //Cycles through list
        mPendingRelation.add(mPendingFriends.get(i));
    }

    mCurrentUser.saveInBackground(new SaveCallback() {
        @Override
        public void done(ParseException e) {
            if (e != null) {
                Log.e(TAG, e.getMessage());
            }
        }
    });

If anyone can help me add the sender as a "pendingRelation" to the reciever as well to create this two-way relationship that would be great. Thanks!

Upvotes: 0

Views: 1326

Answers (2)

Chris L
Chris L

Reputation: 1061

I am trying to figure out the same thing for my app (although I am using javascript so I can't give you working code.. sorry.).

To expand on Torsten's answer. One solution would be to create a table(class) in Parse with the fields "pendingFriendRequest" "associatedUserID_requestingUser" "associatedUserID_receivingUser" and "accepted (boolean)".

SIDE NOTE: You could also add a matching function this way by querying this table(class) and determining whether there are two "pendingFriendRequests" from each individual user for the other user.

Then, you can present the user the results of querying this table and an option to "accept" (you can also present an option to ignore/delete and just drop the row).

Once the user clicks "accept" link that to a function which then creates the user relation.

In javascript it looks something like this:

        likeAUser: function (userId) {
        var currentUser = $rootScope.loggedInUser;
        var User = Parse.Object.extend("User");
        var user = new User();
        user.id = userId;

        var likes = currentUser.relation("Likes");
        //console.log(likes);
        likes.add(user);
        return currentUser.save();
    },

and within the function you are creating the new relation you would then drop(delete) the row from the "pendingRequests" table.

I hope this helps, I have been racking my brain on how to do this and this is the best way I can figure out pending Parse making it easier to interact with relations elements inside their user class. Sorry I don't know the android version to help more.

Upvotes: 0

Torsten Ojaperv
Torsten Ojaperv

Reputation: 1104

I've actually thought about making a follow system with Parse which is quite similiar to your problem. Have you thought about making a new ParseObject instead of relation? Then you could add something like ParseUser pendingUser, ParseUser requestUser, boolean isAccepted.

Anywho if you can't find help from here you can try post it to parse.com questions.

Upvotes: 1

Related Questions