Skip to content

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.

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")
})
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)")
})
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);
    });
var userId = UserId.create('user-id');
Communities.isFriend(userId)
    .then((result) => print('User is friend? $result'))
    .catchError((error) => print('Failed to check friend status, error: $error'));
const userId = UserId.create('user-id');
Communities.isFriend(userId)
    .then((result) => {
        console.log('User is friend? ' + result);
    }, (error) => {
        console.log('Failed to check friend status, error: ' + error.message);
    });
const userId = GetSocialSDK.UserId.create('user-id');
GetSocialSDK.Communities.isFriend(userId)
    .then((result) => {
        console.log('User is friend? ' + result);
    }, (error) => {
        console.log('Failed to check 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.

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")
})
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)")
})
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);
    });
var query = FriendsQuery.ofUser(UserId.currentUser());
Communities.getFriendsCount(query).then((result) {
    print('Number of friends: $result');
}).catchError((error) => print('Failed to get number of friends, error: $error'));
const query = FriendsQuery.ofUser(UserId.currentUser());
Communities.getFriendsCount(query).then((result) => {
        console.log('Number of friends: ' + result);
    }, (error) => {
        console.log('Failed to get number of friends, error: ' + error);
    });
const query = GetSocialSDK.FriendsQuery.ofUser(GetSocialSDK.UserId.currentUser());
GetSocialSDK.Communities.getFriendsCount(query).then((result) => {
        console.log('Number of friends: ' + result);
    }, (error) => {
        console.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.

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")
})
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)")
})
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);
    });
var query = FriendsQuery.ofUser(UserId.currentUser());
Communities.getFriends(PagingQuery(query))
    .then((result) { result.entries.forEach((friend) { print(friend.displayName + ' is a friend'); }); })
    .catchError((error) => print('Failed to list of friends, error: $error'));
const query = FriendsQuery.ofUser(UserId.currentUser());
Communities.getFriends(new PagingQuery(query))
    .then((result) => {
        result.entries.forEach((friend) => {
            console.log(friend.displayName + ' is a friend');
        });
    }, (error) => {
        console.log('Failed to list of friends, error: ' + error);
    });
const query = GetSocialSDK.FriendsQuery.ofUser(GetSocialSDK.UserId.currentUser());
GetSocialSDK.Communities.getFriends(new GetSocialSDK.PagingQuery(query))
    .then((result) => {
        result.entries.forEach((friend) => {
            console.log(friend.displayName + ' is a friend');
        });
    }, (error) => {
        console.log('Failed to 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:

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")
})
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)")
})
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);
    });
var pagingQuery = PagingQuery();
Communities.getSuggestedFriends(pagingQuery)
    .then((result) { result.entries.forEach((friend) { print(friend.displayName + ' is a suggested friend'); }); })
    .catchError((error) => print('Failed to suggested friends, error: $error'));
const pagingQuery = new PagingQuery();
Communities.getSuggestedFriends(pagingQuery)
    .then((result) => {
        result.entries.forEach((user) => {
            console.log(`${user.displayName} is a suggested friend`);
        });
    })
    .catch((error) => {
        console.error('Failed to get suggested friends, error:', error)
    });
GetSocialSDK.Communities.getSuggestedFriends(new GetSocialSDK.PagingQuery())
    .then((result) => {
        result.entries.forEach((user) => {
            console.log(`${user.displayName} is a suggested friend`);
        });
    })
    .catch((error) => {
        console.error('Failed to get suggested friends, error:', error)
    });

Next steps

  • Learn how to send notifications to friends here.

Give us your feedback! Was this article helpful?

😀 🙁