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)")
})

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)")
})

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)

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)")
})

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)")
})

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 follow 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 follow 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)")
})

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)")
})

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)")
})

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)")
})

Activities with labels

You can also filter all activities with labels.

Give us your feedback! Was this article helpful?

😀 🙁