Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Self-Destructing Message Android App Relating Users in Parse.com Storing Friend Relationships

Prevent the user from adding or deleting him/herself as a friend

EDIT: Sign-up isn't working properly, see below

I noticed in the app there are no checks in place to prevent the user from adding or deleting him/herself as a friend. I read through the Parse documentation and made a fix for this:

In EditFriendsActivity.java, add the following line right before query.findInBackground();

// Prevent you from adding or removing yourself as a friend
query.whereNotEqualTo(ParseConstants.KEY_USERNAME, mCurrentUser.getString(ParseConstants.KEY_USERNAME));

But what if you want to send pictures to yourself? I'm assuming in the future we're going to add functionality where you can only send pictures to people on your friends list, so in order for you to be friends with yourself by default, we have to establish the relationship when the user first signs up for an account:

In SignUpActivity.java, refactor all instances of newUser to mNewUser and then add the member variable mNewUser to the top of SignUpActivity.java:

protected ParseUser newUser;

Afterwards, add the following code:

ParseRelation<ParseUser> relation = mNewUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION);
relation.add(mNewUser);

in your done(ParseException e) method inside your SignUpCallback(), right after you've checked that your exception == null and right before Intent intent = new Intent(SignUpActivity.this, MainActivity.class);

This relation will be permanent because you will always be able to send pictures to yourself.

EDIT: For some reason the new relationship doesn't show up on the Parse backend? here's the full code right after the user clicks "Sign Up" and the form fields pass validation:

setProgressBarIndeterminateVisibility(true);

mNewUser = new ParseUser();
mNewUser.setUsername(username);
mNewUser.setPassword(password);
mNewUser.setEmail(email);
mNewUser.signUpInBackground(new SignUpCallback() {

    @Override
    public void done(ParseException e) {
        setProgressBarIndeterminateVisibility(false);
        if (e == null) {
            // Add yourself as your own friend so you can send pictures to yourself
            ParseRelation<ParseUser> relation = mNewUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION);
            relation.add(mNewUser);
            Intent intent = new Intent(SignUpActivity.this,
                    MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    SignUpActivity.this);
            builder.setMessage(e.getMessage())
                    .setTitle(R.string.signup_error_title)
                    .setPositiveButton(android.R.string.ok,
                            null);
            AlertDialog dialog = builder.create();
            dialog.show();
        }
    }
});