Follow Users

Follow

To follow users use:

1
2
3
4
5
Communities.follow(FollowQuery.users(UserIdList.create("424242424242")), { usersFollowing ->
    Log.d("Communities", "Successfully followed the users. Now you follow $usersFollowing users!")
}, { error ->
    Log.d("Communities", "Failed to follow users: $error")
})
1
2
3
4
5
Communities.follow(FollowQuery.users(UserIdList.create(["32274837428"])), success: { usersFollowing in
    print("Successfully followed the users. Now you follow \(usersFollowing) users!")
}, failure: { error in
    print("Failed to follow users: \(error)")
})
1
2
3
4
5
6
7
8
9
var userIds = UserIdList.Create("user1", "user2");
var followQuery = FollowQuery.Users(userIds);
Communities.Follow(followQuery,
    (followedUsers) => {
        Debug.Log("Successfully followed the users. Number of followed users: " + followedUsers);
    },
    (error) => {
        Debug.Log("Failed to follow users, error: " + error);
    });
1
2
3
4
5
var userIds = UserIdList.create(['user1', 'user2']);
var followQuery = FollowQuery.users(userIds);
Communities.follow(followQuery)
    .then((followedUsers) => print('Successfully followed the users. Number of followed users: $followedUsers'))
    .catchError((error) => print('Failed to follow users, error: $error'));

Unfollow

Unfollow works in the same way, as Follow, just use method unfollow:

1
2
3
4
5
Communities.unfollow(FollowQuery.users(UserIdList.create("424242424242")), { usersFollowing ->
    Log.d("Communities", "Successfully unfollowed the users. Now you follow $usersFollowing users!")
}, { error ->
    Log.d("Communities", "Failed to follow users: $error")
})
1
2
3
4
5
Communities.unfollow(FollowQuery.users(UserIdList.create(["32274837428"])), success: { usersFollowing in
    print("Successfully unfollowed the users. Now you follow \(usersFollowing) users!")
}, failure: { error in
    print("Failed to unfollow users: \(error)")
})
1
2
3
4
5
6
7
8
9
var userIds = UserIdList.Create("user1", "user2");
var followQuery = FollowQuery.Users(userIds);
Communities.Unfollow(followQuery,
    (followedUsers) => {
        Debug.Log("Successfully unfollowed the users. Number of followed users: " + followedUsers);
    },
    (error) => {
        Debug.Log("Failed to unfollow users, error: " + error);
    });
1
2
3
4
5
var userIds = UserIdList.create(['user1', 'user2']);
var followQuery = FollowQuery.users(userIds);
Communities.unfollow(followQuery)
    .then((followedUsers) => print('Successfully unfollowed the users. Number of followed users: $followedUsers'))
    .catchError((error) => print('Failed to unfollow users, error: $error'));

Is Following

To check if a certain user is following other users, use:

1
2
3
4
5
6
Communities.isFollowing(UserId.currentUser(), FollowQuery.users(UserIdList.create("user1", "user2")), { isFollowing ->
    Log.d("Communities", "Current user is following user 'user1': ${isFollowing["user1"]}")
    Log.d("Communities", "Current user is following user 'user2': ${isFollowing["user2"]}")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to get followers of the user: $error")
})
1
2
3
4
5
6
Communities.isFollowing(UserId.currentUser(), query: FollowQuery.users(UserIdList.create(["user1", "user2"])), success: { isFollowing in
    print("Current user is following user 'user1': \(isFollowing["user1"])")
    print("Current user is following user 'user2': \(isFollowing["user2"])")
}, failure: { error in
    print("Failed to check followers of users: \(error)")
})
1
2
3
4
5
6
7
8
9
var userIds = UserIdList.Create("user1", "user2");
var followQuery = FollowQuery.Users(userIds);
Communities.IsFollowing(UserId.CurrentUser(), followQuery,
    (isFollowing) => {
        Debug.Log("Current user is following user `user2`: " + isFollowing["user2"]);
    },
    (error) => {
        Debug.Log("Failed to check followers of users, error: " + error);
    });
1
2
3
4
5
6
7
8
var userIds = UserIdList.create(['user1', 'user2']);
var followQuery = FollowQuery.users(userIds);
Communities.isFollowing(UserId.currentUser(), followQuery)
    .then((result) {
        var user2Followed = result['user2'];
        print('Current user is following user "user2": $user2Followed');
    })
    .catchError((error) => print('Failed to check followers of users, error: $error'));

First parameter is a user ID. We’re checking if current user is following some users. The result is a map of String -> Boolean, where key is the id of user passed to FollowQuery and value is true, is user is following this user or false otherwise.

Unexisting users won’t be included in the resulting map.

Get Followers

To get followers of a user:

1
2
3
4
5
6
7
8
val followersQuery = FollowersQuery.ofUser(UserId.create("user-id"))
val pagingQuery = PagingQuery(followersQuery).withLimit(25)
Communities.getFollowers(pagingQuery, { result: PagingResult<User> ->
    val followers = result.entries
    Log.d("Communities", "Followers of the user: $followers")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to get followers of the user: $error")
})
1
2
3
4
5
6
7
8
9
let followersQuery = FollowersQuery.ofUser(UserId.create("62362836"))
let pagingQuery = FollowersPagingQuery.init(followersQuery)
pagingQuery.limit = 25

Communities.followers(pagingQuery, success: { result in
    print("Followers of user '62362836': \(result.followers)")
}, failure: { error in
    print("Failed to get followers of user: \(error)")
})
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
var userId = UserId.Create("user1");
var followersQuery = FollowersQuery.OfUser(userId);
var pagingQuery = new PagingQuery<FollowersQuery>(followersQuery);
Communities.GetFollowers(pagingQuery,
    (result) => {
        Debug.Log("Followers of user `user1`: " + result.Entries);
    },
    (error) => {
        Debug.Log("Failed to get followers, error: " + error);
    });
1
2
3
4
5
6
7
8
9
var userId = UserId.create('user1');
var followersQuery = FollowersQuery.ofUser(userId);
var pagingQuery = PagingQuery(followersQuery);
Communities.getFollowers(pagingQuery)
    .then((result) {
        var followers = result.entries;
        print('Followers of user "user1": $followers');
    })
    .catchError((error) => print('Failed to get followers, error: $error'));

This method uses the PagingQuery concept that is used across our SDK. Read more about this.

Followers Count

To get the followers count of a user:

1
2
3
4
5
6
val userFollowers = FollowersQuery.ofUser(UserId.create("user-id"))
Communities.getFollowersCount(userFollowers, { followersCount: Int ->
    Log.d("Communities", "Followers count of the user: $followersCount")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to get followers of the user: $error")
})
1
2
3
4
5
6
let userFollowers = FollowersQuery.ofUser(UserId.create("282738273"))
Communities.followersCount(userFollowers, success: { followersCount in
    print("Followers count of user '282738273': \(followersCount)")
}, failure: { error in
    print("Failed to get followers count of user: \(error)")
})
1
2
3
4
5
6
7
8
9
var userId = UserId.Create("user1");
var followersQuery = FollowersQuery.OfUser(userId);
Communities.GetFollowersCount(followersQuery,
    (result) => {
        Debug.Log("Number of followers of user `user1`: " + result);
    },
    (error) => {
        Debug.Log("Failed to get number of followers, error: " + error);
    });
1
2
3
4
5
6
7
var userId = UserId.create('user1');
var followersQuery = FollowersQuery.ofUser(userId);
Communities.getFollowers(followersQuery)
    .then((result) {
        print('Number of followers of user "user1": $result');
    })
    .catchError((error) => print('Failed to get number of followers, error: $error'));

Get Following Users

Get users followed by a specific user:

1
2
3
4
5
6
7
8
val query = UsersQuery.followedBy(UserId.currentUser())
val pagingQuery = PagingQuery(query)
Communities.getUsers(pagingQuery, { result: PagingResult<User> ->
    val users = result.entries
    Log.d("Communities", "Users followed by current user: $users")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to get users: $error")
})
1
2
3
4
5
6
7
8
let query = UsersQuery.followedBy(UserId.currentUser())
query.limit = 10
let pagingQuery = UsersPagingQuery(query)
Communities.users(pagingQuery, success: { response in
    // Show list of users
}, failure: { error in
    // Error occured
})
1
2
3
4
5
6
7
8
9
var query = UsersQuery.FollowedByUser(UserId.CurrentUser());
var pagingQuery = new PagingQuery<UsersQuery>(query);
Communities.GetUsers(pagingQuery,
    (result) => {
        Debug.Log("Users followed by current user: " + result.Entries);
    },
    (error) => {
        Debug.Log("Failed to get followings, error: " + error);
    });
1
2
3
4
5
6
7
8
var query = UsersQuery.all().followedBy(UserId.currentUser());
var pagingQuery = PagingQuery(query);
Communities.findUsers(pagingQuery)
    .then((result) {
        var users = result.entries;
        print('Users followed by current user: $users');
    })
    .catchError((error) => print('Failed to get followings, error: $error'));

Next steps

  • Learn how to query social graph here

Give us your feedback! Was this article helpful?

😀 🙁