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
Communities.isFriend(UserId.create("user_id"), { isFriend: Boolean ->
    Log.d("Communities", "User is your friend: $isFriend")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to check if friends: $error")
})
1
2
3
4
5
6
let userId = UserId.create(withId: "user1")
Communities.isFriend(userId, success: { isFriend in
    print("Current user and \(userId) are \(isFriend == true ? "" : "not") friends")
}, failure: { error in
    print("Error while checking friend status: \(error)")
})
1
2
3
4
5
6
7
8
var userId = UserId.Create("user1");
Communities.IsFriend(userId,
    (result) => {
        Debug.Log("`user` is friend? " + result);
    },
    (error) => {
        Debug.Log("Failed to get friend status, error: " + error);
    });

Get Number 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
Communities.getFriendsCount(FriendsQuery.ofUser(UserId.currentUser()), { friendsCount: Int ->
    Log.d("Communities", "Friends count: $friendsCount")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to check friends count: $error")
})
1
2
3
4
5
6
let query = FriendsQuery.ofUser(UserId.currentUser())
Communities.friendsCount(query, success: { friendsCount in
    print("Number of friends of current user: \(friendsCount)")
}, failure: { error in
    print("Error while receiving friends count: \(error)")
})
1
2
3
4
5
6
7
8
var query = FriendsQuery.OfUser(UserId.CurrentUser());
Communities.GetFriendsCount(query,
    (result) => {
        Debug.Log("Number of friends: " + result);
    },
    (error) => {
        Debug.Log("Failed to get number of friends, error: " + error);
    });

Get List of Friends

You can get the list of all friends of the current user page by page.

1
2
3
4
5
6
7
8
val query = FriendsQuery.ofUser(UserId.currentUser())
val pagingQuery = PagingQuery(query)
Communities.getFriends(pagingQuery, { result: PagingResult<User> ->
    val friends = result.entries
    Log.d("Communities", "Friends: $friends")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to get friends: $error")
})
1
2
3
4
5
6
7
8
9
let query = FriendsQuery.ofUser(UserId.currentUser())
let pagingQuery = FriendsPagingQuery(query)
Communities.friends(pagingQuery, success: { response in
    response.friends.forEach {
        print("[\($0.displayName)] is friend")
    }
}, failure: { error in
    print("Error while receiving friends: \(error)")
})
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
var query = FriendsQuery.OfUser(UserId.CurrentUser());
var pagingQuery = new PagingQuery<FriendsQuery>(query);
Communities.GetFriends(pagingQuery,
    (result) => {
        result.Entries.ForEach(entry => {
            Debug.Log(entry.DisplayName + " is a friend");
        });
    },
    (error) => {
        Debug.Log("Failed to get list of friends, error: " + error);
    });

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
val pagingQuery = SimplePagingQuery.simple(20)
Communities.getSuggestedFriends(pagingQuery, { result: PagingResult<SuggestedFriend> ->
    val friends = result.entries
    Log.d("Communities", "Suggested friends: $friends")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to get suggested friends: $error")
})
1
2
3
4
5
6
7
8
let pagingQuery = PagingQuery()
Communities.suggestedFriends(pagingQuery, success: { response in
    response.suggestedFriends.forEach {
        print("[\($0.displayName)] is a suggested friend")
    }
}, failure: { error in
    print("Error while receiving suggested friends: \(error)")
})
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
var pagingQuery = new SimplePagingQuery();
Communities.GetSuggestedFriends(pagingQuery,
    (result) => {
        result.Entries.ForEach(entry => {
            Debug.Log(entry.DisplayName + " is a suggested friend");
        });
    },
    (error) => {
        Debug.Log("Failed to get suggested friends, error: " + error);
    });

Next steps

  • Learn how to create a chat with your friends here.
  • Learn how to send notifications to friends here.

Give us your feedback! Was this article helpful?

😀 🙁