Follow Topics

Follow

To follow topics use:

1
2
3
4
5
Communities.follow(FollowQuery.topics("dogs", "cats", "daily-challenges"), { topicsFollowing ->
    Log.d("Communities", "Successfully followed the topics. Now you follow $topicsFollowing topics!")
}, { error ->
    Log.d("Communities", "Failed to follow topics: $error")
})
1
2
3
4
5
Communities.follow(FollowQuery.topics(["dogs", "cats", "daily-challenges"]), success: { topicsFollowing in
    print("Successfully followed the topics. Now you follow \(topicsFollowing) topics!")
}, failure: { error in
    print("Failed to follow topics: \(error)")
})
1
2
3
4
5
6
7
Communities.Follow(FollowQuery.Topics("dogs", "cats", "daily-challenges"),
    (topicsFollowing) => {
        Debug.Log("Successfully followed the topics. Number of followed topics: " + topicsFollowing);
    },
    (error) => {
        Debug.Log("Failed to follow topics, error " + error);
    });

Unfollow

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

1
2
3
4
5
Communities.unfollow(FollowQuery.topics("dogs", "cats", "daily-challenges"), { topicsFollowing ->
    Log.d("Communities", "Successully unfollowed the topics. Now you follow $topicsFollowing topics!")
}, { error ->
    Log.d("Communities", "Failed to follow topics: $error")
})
1
2
3
4
5
Communities.unfollow(FollowQuery.topics(["dogs", "cats", "daily-challenges"]), success: { topicsFollowing in
    print("Successully unfollowed the topics. Now you follow \(topicsFollowing) topics!")
}, failure: { error in
    print("Failed to follow topics: \(error)")
})
1
2
3
4
5
6
7
Communities.Unfollow(FollowQuery.Topics("dogs", "cats", "daily-challenges"),
    (topicsFollowing) => {
        Debug.Log("Successfully unfollowed the topics. Number of followed topics: " + topicsFollowing);
    },
    (error) => {
        Debug.Log("Failed to unfollow topics, error " + error);
    });

Is Following

To check if a certain user is following topics, use:

1
2
3
4
5
6
Communities.isFollowing(UserId.currentUser(), FollowQuery.topics("cats", "dogs"), { isFollowing ->
    Log.d("Communities", "Current user is following topic 'cats': ${isFollowing["cats"]}")
    Log.d("Communities", "Current user is following topic 'dogs': ${isFollowing["dogs"]}")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to get followers of topic: $error")
})
1
2
3
4
5
6
Communities.isFollowing(UserId.currentUser(), query: FollowQuery.topics(["cats", "dogs"]), success: { isFollowing in
    print("Current user is following topic 'cats': \(isFollowing["cats"])")
    print("Current user is following topic 'dogs': \(isFollowing["dogs"])")
}, failure: { error in
    print("Failed to check followers of topics: \(error)")
})
1
2
3
4
5
6
7
8
9
Communities.IsFollowing(UserId.CurrentUser(), FollowQuery.Topics("cats", "dogs"),
    (result) => {
        Debug.Log("Current user is following topic 'cats': " + result["cats"]);
        Debug.Log("Current user is following topic 'dogs': " + result["dogs"]);
    },
    (error) => {
        Debug.Log("Failed to check followers of topics, error: " + error);
    }
);

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

Unexisting topics won’t be included in the result map.

Get Followers

To get followers of a topic:

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

Communities.followers(pagingQuery, success: { result in
    print("Followers of topic 'cats': \(result.followers)")
}, failure: { error in
    print("Failed to get followers of topic: \(error)")
})
1
2
3
4
5
6
7
8
9
var followersQuery = FollowersQuery.OfTopic("cats");
var pagingQuery = new PagingQuery<FollowersQuery>(followersQuery);
Communities.GetFollowers(pagingQuery,
    (result) => {
        Debug.Log("Followers of topic 'cats': " + result.Entries);
    },
    (error) => {
        Debug.Log("Failed to get followers of topic: " + 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 topic:

1
2
3
4
5
6
val topicsFollowers = FollowersQuery.ofTopic("level1")
Communities.getFollowersCount(topicsFollowers, { followersCount: Int ->
    Log.d("Communities", "Followers count of topic 'level1': $followersCount")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to get followers of topic: $error")
})
1
2
3
4
5
6
let topicFollowers = FollowersQuery.ofTopic("level1")
Communities.followersCount(topicFollowers, success: { followersCount in
    print("Followers count of topic 'level1': \(followersCount)")
}, failure: { error in
    print("Failed to get followers count of topic: \(error)")
})
1
2
3
4
5
6
7
8
var topicFollowers = FollowersQuery.OfTopic("cats");
Communities.GetFollowersCount(topicFollowers,
    (result) => {
        Debug.Log("Followers count of topic 'cats': " + result);
    },
    (error) => {
        Debug.Log("Failed to get followers count of topic: " + error);
    });

Get Following Topics

You can also get all the topics that are followed by a certain user:

1
2
3
4
5
6
7
8
val query = TopicsQuery.followedBy(UserId.currentUser())
val pagingQuery = PagingQuery(query)
Communities.getTopics(pagingQuery, { result: PagingResult<Topic> ->
    val topics = result.entries
    Log.d("Communities", "Topics followed by current user: $topics")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to get list of topics: $error")
})
1
2
3
4
5
6
7
let query = TopicsQuery.followedBy(UserId.currentUser())
let pagingQuery = TopicsPagingQuery(query)
Communities.topics(pagingQuery, success: { result in
    print("Topics followed by current user: \(result.topics)")
}, failure: { error in
    print("Failed to get list of topics: \(error)")
})
1
2
3
4
5
6
7
8
9
var query = TopicsQuery.FollowedBy(UserId.CurrentUser());
var pagingQuery = new PagingQuery<TopicsQuery(query);
Communities.GetTopics(pagingQuery,
    (result) => {
        Debug.Log("Topics followed by current user: " + result.Entries);
    },
    (error) => {
        Debug.Log("Failed to get list of topics: " + error);
    });

Give us your feedback! Was this article helpful?

😀 🙁