Querying the Social Graph¶
Prerequisite¶
- Finished Getting Started guide for Android, iOS, Unity or React Native
Check if User is a Friend¶
That is how you check if some user is a friend of the current user.
```java tab=”Android(Java)”
final String anotherUserId = anotherUser.getId();
GetSocial.User.isFriend(anotherUserId, new Callback
@Override
public void onSuccess(Boolean isFriend) {
Log.i(“GetSocial”, “Are ” + GetSocial.User.getId() + ” and ” + anotherUserId + ” friends? ” + isFriend);
}
1 2 3 4 |
|
});
kotlin tab=”Android(Kotlin)”
val anotherUserId = anotherUser.id
GetSocial.User.isFriend(anotherUserId, object : Callback
override fun onSuccess(isFriend : Boolean) {
println(“Are ${GetSocial.User.getId()} and ${anotherUserId} friends? ${isFriend}”)
}
override fun onFailure(exception: GetSocialException) {
println(“Exception while checking friendship, error: ${exception.message}”)
}
})
```
```objc tab=”iOS(Objective-C)”
NSString *anotherUserId = anotherUser.userId;
[GetSocialUser isFriend:anotherUserId success:^(BOOL isFriend) {
NSLog(@”Are %@ and %@ friends? %@”, GetSocialUser.userId, anotherUserId, isFriend ? @”Yes” : @”No”);
} failure:^(NSError *error) {
NSLog(@”Exception while checking friendship: %@”, error);
}];
```swift tab="iOS(Swift)"
let anotherUserId = anotherUser.userId
GetSocialUser.isFriend(anotherUserId, success: { isFriend in
print("Are \(GetSocialUser.userId) and \(anotherUserId) friends? \(isFriend ? "Yes" : "No")")
}, failure: { error in
print("Exception while checking friendship: \(error)")
})
```csharp tab=”Unity”
var anotherUserId = user.Id;
GetSocial.User.IsFriend (anotherUserId,
isFriend => {
Debug.Log (“Are ” + GetSocial.User.getId() + ” and ” + anotherUserId + ” friends? ” + isFriend);
},
error => {
Debug.Log (“Exception while checking friendship: ” + error.Message);
});
```javascript tab="React Native"
const anotherUserId = user.userId;
GetSocialUser.isFriend(anotherUserId).then(
(isFriend) => {
console.log('Are you and ' + anotherUserId + " friends? " + isFriend);
},
(error) => {
console.log('Exception while checking friendship: ' + error);
});
Get Amount of Friends¶
You can get the number of friends of the current user with the help of the following method.
```java tab=”Android(Java)”
GetSocial.User.getFriendsCount(new Callback
@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);
}
1 2 3 4 |
|
});
kotlin tab=”Android(Kotlin)”
GetSocial.User.getFriendsCount(object : Callback
override fun onSuccess(friendsCount : Int) {
// Usually developers update some UI badge, which shows the counter
println(“Amount of friends for current user: ${friendsCount}”)
}
override fun onFailure(exception: GetSocialException) {
println(“Exception while receiving friends count, error: ${exception.message}”)
}
})
```
```objc tab=”iOS(Objective-C)”
[GetSocialUser friendsCountWithSuccess:^(int friendsCount) {
NSLog(@”Amount of friends for current user: %d”, friendsCount);
} failure:^(NSError *error) {
NSLog(@”Exception while receiving friends count: %@”, error);
}];
```swift tab="iOS(Swift)"
GetSocialUser.friendsCount(withSuccess: { friendsCount in
print("Amount of friends for current user: \(friendsCount)")
}, failure: { error in
print("Exception while receiving friends count: \(error)")
})
```csharp tab=”Unity”
GetSocial.User.GetFriendsCount (
friendsCount => {
Debug.Log (“Amount of friends for current user: ” + friendsCount);
},
error => {
Debug.Log (“Exception while receiving friends count: ” + error.Message);
});
```javascript tab="React Native"
GetSocialUser.getFriendsCount().then(
(friendsCount) => {
console.log('Amount of friends for current user: ' + friendsCount);
},
(error) => {
console.log('Exception while receiving friends count: ' + error);
});
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.
```java tab=”Android(Java)”
int limit = 100;
int offset = 0;
GetSocial.User.getFriends(offset, limit, new Callback>() {
@Override
public void onSuccess(List
for (PublicUser friend : friends) {
Log.i(“GetSocial”, “Received a friend: ” + friend.getId() + ” ” + friend.getDisplayName());
}
}
1 2 3 4 |
|
});
kotlin tab=”Android(Kotlin)”
val limit = 100
val offset = 0
GetSocial.User.getFriends(offset, limit, object : Callback> {
override fun onSuccess(friends: List
friends.forEach {friend ->
println(“Received a friend: ${friend.id} ${friend.displayName}”)
}
}
override fun onFailure(exception: GetSocialException) {
println(“Exception while receiving friends, error: ${exception.message}”)
}
})
```
```objc tab=”iOS(Objective-C)”
int offset = 0;
int limit = 100;
[GetSocialUser friendsWithOffset:offset limit:limit success:^(NSArray
for (GetSocialPublicUser *friend in friends) {
NSLog(@”Received a friend: %@ %@”, friend.userId, friend.displayName);
}
} failure:^(NSError *error) {
NSLog(@”Exception while receiving friends: %@”, error);
}];
```swift tab="iOS(Swift)"
let offset: Int32 = 0
let limit: Int32 = 100
GetSocialUser.friends(withOffset: offset, limit: limit, success: { friends in
for friend in friends {
print("Received a friend: \(friend.userId) \(friend.displayName)")
}
}, failure: { error in
print("Exception while receiving friends: \(error)")
})
```csharp tab=”Unity”
var offset = 0;
var limit = 100;
GetSocial.User.GetFriends(offset, limit,
friends => {
foreach (var friend in friends) {
Debug.Log (“Received a friend: ” + friend.Id + ” ” + friend.DisplayName);
}
},
error => {
Debug.Log (“Exception while receiving friends: ” + error.Message);
});
```javascript tab="React Native"
const offset = 0;
const limit = 100;
GetSocialUser.getFriends(offset, limit).then(
(friends) => {
friends.forEach(friend => {
console.log('Received a friend: ' + friend.userId + ' ' + friend.displayName);
});
},
(error) => {
console.log('Exception while receiving friends: ' + 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:
```java tab=”Android(Java)”
int limit = 10;
int offset = 0;
GetSocial.User.getSuggestedFriends(offset, limit, new Callback>() {
@Override
public void onSuccess(List
for (SuggestedFriend friend : friends) {
Log.i(“GetSocial”, “Possible friend: ” + friend.getDisplayName() + ” (” + friend.getMutualFriendsCount() + ” mutual friends)”);
}
}
1 2 3 4 |
|
});
kotlin tab=”Android(Kotlin)”
val limit = 10
val offset = 0
GetSocial.User.getSuggestedFriends(offset, limit, object : Callback> {
override fun onSuccess(friends: List
friends.forEach {friend ->
println(“Possible friend: ${friend.displayName} (${friend.mutualFriendsCount} mutual friends)”)
}
}
override fun onFailure(exception: GetSocialException) {
println(“Exception while receiving suggested friends, error: ${exception.message}”)
}
})
```
```objc tab=”iOS(Objective-C)”
int offset = 0;
int limit = 10;
[GetSocialUser suggestedFriendsWithOffset:offset limit:limit success:^(NSArray
for (GetSocialSuggestedFriend *friend in friends) {
NSLog(@”Possible friend: %@ (%d mutual friends)”, friend.displayName, friend.mutualFriendsCount);
}
} failure:^(NSError *error) {
NSLog(@”Exception while receiving suggested friends: %@”, error);
}];
```swift tab="iOS(Swift)"
let offset: Int32 = 0
let limit: Int32 = 10
GetSocialUser.suggestedFriends(withOffset: offset, limit: limit, success: { friends in
for friend in friends {
print("Possible friend: \(friend.displayName) (\(friend.mutualFriendsCount) mutual friends)")
}
}, failure: { error in
print("Exception while receiving suggested friends: \(error)")
})
```csharp tab=”Unity”
var offset = 0;
var limit = 10;
GetSocial.User.GetSuggestedFriends(offset, limit,
friends => {
foreach (var friend in friends) {
Debug.Log (“Possible friend: ” + friend.DisplayName + “(” + friend.MutualFriendsCount + ” mutual friends)”);
}
},
error => {
Debug.Log (“Exception while receiving suggested friends: ” + error.Message);
});
```javascript tab="React Native"
const offset = 0;
const limit = 0;
GetSocialUser.getSuggestedFriends(offset, limit).then(
(suggestedFriends) => {
suggestedFriends.forEach(suggestedFriend => {
console.log('Possible friend: ' + suggestedFriend.userId + ' ' + suggestedFriend.displayName);
});
},
(error) => {
console.log('Exception while receiving suggested friends: ' + error);
});