Skip to content

Labels

Create Labels

Labels are automatically created when activities are posted.
We stored them internally all lowercase, therefore any search will be be case-insensitive.

Get labels

You can get a paginated list of all labels in your application:

val query = LabelsQuery.all()
val pagingQuery = PagingQuery(query)
Communities.getLabels(pagingQuery, { result: PagingResult<Label> ->
    val labels = result.entries
    Log.d("Communities", "labels: $labels")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to get list of labels: $error")
})
let query = LabelsQuery.all()
let pagingQuery = LabelsPagingQuery(query)
Communities.labels(pagingQuery, success: { result in
    print("labels: \(result.labels)")
}, failure: { error in
    print("Failed to get list of labels: \(error)")
})
LabelsQuery query = LabelsQuery.all();
Communities.getLabels(PagingQuery(query)).then((result) {
    print('Labels: ' + result.entries);
}).catchError((error) {
    print('Failed to get list of labels, error: ' + error);
});
const query = LabelsQuery.all();
const pagingQuery = new PagingQuery(query);
Communities.getLabels(pagingQuery)
    .then((result) => console.log('Labels:', result.entries))
    .catch((error) => console.error('Failed to get list of labels:', error))

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

Find labels

You can also search labels by a substring, trending or labels followed by a specific user.

val searchString = "cat"
val query = LabelsQuery.find(searchString)
val pagingQuery = PagingQuery(query)
Communities.getLabels(pagingQuery, { result: PagingResult<Label> ->
    val labels = result.entries
    Log.d("Communities", "labels contains $searchString: $labels")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to get list of labels: $error")
})
let searchString = "cat"
let query = LabelsQuery.find(searchString)
let pagingQuery = LabelsPagingQuery(query)
Communities.labels(pagingQuery, success: { result in
    print("Labels containing \(searchString): \(result.labels)")
}, failure: { error in
    print("Failed to get list of labels: \(error)")
})
String searchString = 'cat';
LabelsQuery query = LabelsQuery.find(searchText)
Communities.getLabels(PagingQuery(query)).then((result) {
    print('Labels containing ' + searchString + ': ' + result.entries);
}).catchError((error) {
    print('Failed to get list of labels, error: ' + error);
});
const searchString = 'cat';
const query = LabelsQuery.find(searchString);
const pagingQuery = new PagingQuery(query);
Communities.getLabels(pagingQuery)
    .then((result) => {
        console.log(`Labels containing ${searchString}:`, result.entries);
    })
    .catch((error) => console.error('Failed to get list of labels:', error));

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

It is possible to get trending labels:

val query = LabelsQuery.find("cats").onlyTrending(true)
let query = LabelsQuery.find("cats").onlyTrending(true)
LabelsQuery query = LabelsQuery.find('cats').onlyTrending(true);
const query = LabelsQuery.find('cats').onlyTrending(true);

Get Labels Count

Use the same query as you’re using to Get or Find labels.

val searchString = "cat"
val query = LabelsQuery.find(searchString)
Communities.getLabelsCount(query, { result: Int ->
    Log.d("Communities", "labels count contains $searchString: $result")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to get number of labels: $error")
})
let searchString = "cat"
let query = LabelsQuery.find(searchString)
Communities.labelsCount(query, success: { labelsCount in
    print("labels count containing \(searchString): \(labelsCount)")
}, failure: { error in
    print("Failed to get number of labels: \(error)")
})
String searchString = 'cat';
LabelsQuery query = LabelsQuery.find(searchText)
Communities.getLabelsCount(query).then((result) {
    print('Number of labels including cats: ' + result.entries);
}).catchError((error) {
    print('Failed to get number of labels, error: ' + error);
});
const searchString = 'cat';
const query = LabelsQuery.find(searchString);
Communities.getLabelsCount(query)
    .then((count) => {
        console.log(`Labels count containing ${searchString}: ${count}`);
    })
    .catch((error) => console.error('Failed to get number of labels:', error));

Follow

To follow labels use:

Communities.follow(FollowQuery.labels("dogs", "cats", "daily-challenges"), { labelsFollowing ->
    Log.d("Communities", "Successfully followed the labels. Now you follow $labelsFollowing labels!")
}, { error ->
    Log.d("Communities", "Failed to follow labels: $error")
})
Communities.follow(FollowQuery.labels(["dogs", "cats", "daily-challenges"]), success: { labelsFollowing in
    print("Successfully followed the labels. Now you follow \(labelsFollowing) labels!")
}, failure: { error in
    print("Failed to follow labels: \(error)")
})
Communities.follow(FollowQuery.labels(['dogs', 'cats', 'daily-challenges']))
    .then((result) => print('Successfully followed the labels. Number of followed labels: $result'))
    .catchError((error) => print('Failed to follow labels, error: $error'));
Communities.follow(FollowQuery.labels(['dogs', 'cats', 'daily-challenges'])
    .then((labelsFollowing) => {
        console.log(`Successfully followed the Labels. Now you follow ${labelsFollowing} Labels!`);
    })
    .catch((error) => console.error('Failed to follow Labels:', error));

Unfollow

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

Communities.unfollow(FollowQuery.labels("dogs", "cats", "daily-challenges"), { labelsFollowing ->
    Log.d("Communities", "Successully unfollowed the labels. Now you follow $labelsFollowing labels!")
}, { error ->
    Log.d("Communities", "Failed to unfollow labels: $error")
})
Communities.unfollow(FollowQuery.labels(["dogs", "cats", "daily-challenges"]), success: { labelsFollowing in
    print("Successully unfollowed the labels. Now you follow \(labelsFollowing) labels!")
}, failure: { error in
    print("Failed to unfollow labels: \(error)")
})
Communities.unfollow(FollowQuery.labels(['dogs', 'cats', 'daily-challenges']))
    .then((result) => print('Successfully unfollowed the labels. Number of followed labels: $result'))
    .catchError((error) => print('Failed to unfollowed labels, error: $error'));
Communities.unfollow(FollowQuery.labels(['dogs', 'cats', 'daily-challenges'])
    .then((labelsFollowing) => {
        console.log(`Successfully unfollowed the Labels. Now you follow ${labelsFollowing} Labels!`);
    })
    .catch((error) => console.error('Failed to unfollow Labels:', error));

Is Following

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

Communities.isFollowing(UserId.currentUser(), FollowQuery.labels("cats", "dogs"), { isFollowing ->
    Log.d("Communities", "Current user is following labels 'cats': ${isFollowing["cats"]}")
    Log.d("Communities", "Current user is following labels 'dogs': ${isFollowing["dogs"]}")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to get followers of labels: $error")
})
Communities.isFollowing(UserId.currentUser(), query: FollowQuery.labels(["cats", "dogs"]), success: { isFollowing in
    print("Current user is following labels 'cats': \(isFollowing["cats"])")
    print("Current user is following labels 'dogs': \(isFollowing["dogs"])")
}, failure: { error in
    print("Failed to check followers of labels: \(error)")
})
Communities.isFollowing(UserId.currentUser(), FollowQuery.labels(['cats', 'dogs']))
    .then((result) {
        var catsFollowed = result['cats'];
        var dogsFollowed = result['dogs'];
        print('Current user is following labels "cats": $catsFollowed');
        print('Current user is following labels "dogs": $dogsFollowed');
    })
    .catchError((error) => { print('Failed to check followers of labels, error: $error') });
Communities.isFollowing(UserId.currentUser(), FollowQuery.labels(['cats', 'dogs'])
    .then((isFollowing) => {
        console.log(`You're ${isFollowing.cats ? '':'not '}following the label 'cats'`);
        console.log(`You're ${isFollowing.dogs ? '':'not '}following the label 'dogs'`);
    })
    .catch((error) => console.error('Failed to check followers of Labels:', error));

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

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

Get Followers

To get followers of a labels:

val followersQuery = FollowersQuery.ofLabel("cats")
val pagingQuery = PagingQuery(followersQuery).withLimit(25)
Communities.getFollowers(pagingQuery, { result: PagingResult<User> ->
    val followersOfCatsLabel = result.entries
    Log.d("Communities", "Followers of labels 'cats': $followersOfCatsLabel")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to get followers of the labels: $error")
})
let followersQuery = FollowersQuery.ofLabel("cats")
let pagingQuery = FollowersPagingQuery.init(followersQuery)
pagingQuery.limit = 25

Communities.followers(pagingQuery, success: { result in
    print("Followers of labels 'cats': \(result.followers)")
}, failure: { error in
    print("Failed to get followers of labels: \(error)")
})
var followersQuery = FollowersQuery.ofLabel('cats');
var pagingQuery = PagingQuery(followersQuery);
pagingQuery.limit = 25;
Communities.getFollowers(pagingQuery)
    .then((result) {
        var followers = result.entries;
        print('Followers of label "cats": $followers');
    })
    .catchError((error) => { print('Failed to check followers of label, error: $error') });
const followersQuery = FollowersQuery.ofLabel('cats');
const pagingQuery = new PagingQuery(followersQuery);
pagingQuery.limit = 25;

Communities.getFollowers(pagingQuery)
    .then((result) => {
        console.log('Followers of Label "cats":', result.entries);
    })
    .catch((error) => console.error('Failed to get followers of Labels:', 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 label:

val labelsFollowers = FollowersQuery.ofLabel("level1")
Communities.getFollowersCount(labelsFollowers, { followersCount: Int ->
    Log.d("Communities", "Followers count of labels 'level1': $followersCount")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to get followers of labels: $error")
})
let labelFollowers = FollowersQuery.ofLabel("level1")
Communities.followersCount(labelFollowers, success: { followersCount in
    print("Followers count of labels 'level1': \(followersCount)")
}, failure: { error in
    print("Failed to get followers count of labels: \(error)")
})
var labelFollowers = FollowersQuery.ofLabel('level1');
Communities.getFollowersCount(labelFollowers)
    .then((result) {
        print('Followers count of label "level1": $result');
    })
    .catchError((error) => { print('Failed to check followers count of label, error: $error') });
const LabelFollowers = FollowersQuery.ofLabel('level1');

Communities.getFollowersCount(LabelFollowers)
    .then((count) => {
        console.log(`Followers count of Labels 'level1': ${count}`);
    })
    .catch((error) => console.error('Failed to get followers count of Labels:', error));

Get Following labels

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

val query = LabelsQuery.followedBy(UserId.currentUser())
val pagingQuery = PagingQuery(query)
Communities.getLabels(pagingQuery, { result: PagingResult<Label> ->
    val labels = result.entries
    Log.d("Communities", "labels followed by current user: $labels")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to get list of labels: $error")
})
let query = LabelsQuery.followedBy(UserId.currentUser())
let pagingQuery = LabelsPagingQuery(query)
Communities.labels(pagingQuery, success: { result in
    print("labels followed by current user: \(result.labels)")
}, failure: { error in
    print("Failed to get list of labels: \(error)")
})
var query = LabelsQuery.all().followedBy(UserId.currentUser());
var pagingQuery = PagingQuery(query);
Communities.getLabels(pagingQuery)
    .then((result) {
        var labels = result.entries;
        print('Labels followed by current user: $labels');
    }).catchError((error) => { print('Failed to get list of labels, error: $error') });
const query = LabelsQuery.followedBy(UserId.currentUser());
const pagingQuery = new PagingQuery(query);
Communities.getLabels(pagingQuery)
    .then((result) => {
        console.log('Labels followed by current user:', result.entries);
    })
    .catch((error) => console.error('Failed to get list of Labels', error));

Activities with labels

You can also filter all activities with labels.

Give us your feedback! Was this article helpful?

😀 🙁