Querying the Social Graph

Prerequisite

Check if User is a Friend

That is how you check if some user is a friend of the current user.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
final String anotherUserId = anotherUser.getId();
GetSocial.User.isFriend(anotherUserId, new Callback<Boolean>() {
    @Override
    public void onSuccess(Boolean isFriend) {
        Log.i("GetSocial", "Are " + GetSocial.User.getId() + " and " + anotherUserId + " friends? " + isFriend);
    }

    @Override
    public void onFailure(GetSocialException exception) {
        Log.e("GetSocial", "Exception while checking friendship: " + exception.getMessage());
    }
});

Get Amount of Friends

You can get the number of friends of the current user with the help of the following method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
GetSocial.User.getFriendsCount(new Callback<Integer>() {
    @Override
    public void onSuccess(Integer friendsCount) {
        // Usually developers update some UI badge, which shows the counter
        Log.i("GetSocial", "Amount of friends for current user: " + friendsCount);
    }

    @Override
    public void onFailure(GetSocialException exception) {
        Log.e("GetSocial", "Exception while receiving friends count: " + exception.getMessage());
    }
});

Get List of Friends

You can get the list of all friends of the current user page by page.
Use limit and offset to traverse multiple pages of friends.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
int limit = 100;
int offset = 0;
GetSocial.User.getFriends(offset, limit, new Callback<List<PublicUser>>() {
    @Override
    public void onSuccess(List<PublicUser> friends) {
        for (PublicUser friend : friends) {
            Log.i("GetSocial", "Received a friend: " + friend.getId() + " " + friend.getDisplayName());
        }
    }

    @Override
    public void onFailure(GetSocialException exception) {
        Log.e("GetSocial", "Exception while receiving friends: " + exception.getMessage());
    }
});

Suggested Friends

When the user is building his social graph and creating connections with other users, GetSocial backend is finding the best possible options to make new friendship for your user. As a result, we make a list of users, which are not directly connected to the user but have mutual friends with him. That means that these users could be friends with your user in real life, but they have not met in your game yet. With Suggested Friends functionality you can make building user’s social graph process much faster. Use it as described below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
int limit = 10;
int offset = 0;
GetSocial.User.getSuggestedFriends(offset, limit, new Callback<List<SuggestedFriend>>() {
    @Override
    public void onSuccess(List<SuggestedFriend> friends) {
        for (SuggestedFriend friend : friends) {
            Log.i("GetSocial", "Possible friend: " + friend.getDisplayName() + " (" + friend.getMutualFriendsCount() + " mutual friends)");
        }
    }

    @Override
    public void onFailure(GetSocialException exception) {
        Log.e("GetSocial", "Exception while receiving suggested friends: " + exception.getMessage());
    }
});

Give us your feedback! Was this article helpful?

😀 🙁