Community

Community is a new way for users to join and interact with other users. Each channel contains a community. When creating a community, user can pass a displayName, which will be the name of the community.

You will not be able to create a community from an existing channel. New communities will need to be created with new channelId.

There are three ways of obtaining a community: via create, join, or get. They all return a LiveObject with the final community model.

Community management methods are all contained in a EkoCommunityRepository class. Before calling any community methods, you must ensure to first instantiate a repository instance.

Create Repository

let communityRepository = EkoCommunityRepository(client: client)

Create Community

First you need to create the community using EkoCommunityCreationDataBuilder instance. The mandatory property is displayName. You can set other properties as per your necessities.

let builder = EkoCommunityCreationDataBuilder()
builder.setDisplayName("Test")
builder.setTags(["test"])
builder.setCommunityDescription("For testing")
builder.setIsPublic(false)
builder.setMetadata(["test": "test-content"])
‚Äč
communityRepository.createCommunity(builder, completion: { (liveObject, error) in
// Do something
})

Join Community

communityRepository.joinCommunity(withCommunityId: "community-id") { (success, error) in
// Do something
}

Leave Community

communityRepository.leaveCommunity(withCommunityId: "community-id") { (success, error) in
// Do something
}

Get Community

In the case where you only want to fetch a community's data without joining, you can use the getCommunity(withId:) method:

collection = communityRepository.getCommunity(withId: testCommunityId)
token = collection.observe { [weak self] (community, error) in
// Do something
}

Community Query

There are methods to obtain communities that only match specific criteria:

  • the keyword parameter let you filter communities based on the community displayName

  • the filter parameter let you filter communities based on the logged in user membership status

  • the categoryId parameters let you filter communities based on community categories

  • the sortBy parameters let you filter communities based on the order that the communities were created or based on alphabetical order

  • the includeDeleted parameters let you specify if you want to include eleted communities in your query

If you want to fetch all communities, you can pass the keyword as nil.

let collection = communityRepository.getCommunitiesWithKeyword("search-keyword", filter: .all, sortBy: .lastCreated, categoryId: nil, includeDeleted: false)
token = collection.observe({ [weak self] (collection, _, _) in
// React to changes here
})

Community Repository also provides a way to fetch trending communities or recommended communities. getTrendingCommunities method fetches trending communities whereas getRecommendedCommunities method fetches recommended communities.

Update Community

SDK provides the builder class EkoCommunityUpdateDataBuilder which allows you to set properties that you want to update. Then you can use updateCommunity method in EkoCommunityRepository to update the community.

let builder = EkoCommunityUpdateDataBuilder()
builder.setDisplayName("updated-name")
builder.setCommunityDescription("updated-description")
builder.setIsPublic(false)
...
communityRepository.updateCommunity(withId: "community-id", builder: builder, completion: { (community, error) in
// React to changes here
})

You can also delete the community using deleteCommunity(_:) method.

Note. By default, only the community's original creator or adminstrators can update/delete the community.

Community Membership

You can get a list of community members through EkoCommunityParticipation class. It exposes getMemberships method which accepts filter and sortBy parameters.

let membershipCollection = communityParticipation.getMemberships(.all, roles: [], sortBy: .firstCreated)
token = membershipCollection.observe({ (collection, change, error) in
// React to changes here
})

Same class provides addUsers(_:) and removeUsers(_:) which can be used to add user or remove user from community.

Community Moderation

EkoCommunityModeration class provides a way to moderate the community by banning or unbanning users. banUsers(_:) method accepts array of user ids to ban and unbanUsers(_:) accepts array of user ids to unban. You can also add/remove roles using addRole(_:) and removeRole(_:) method.

addRole(_:) and removeRole(_:) doesnot create new roles but assign and remove existing roles from given user.

Community Categories

The EkoCommunityRepository will also be able to manage community categories. When communities are put into a category, you will be able to sort and filter each of the communities in that category. You can access the categories for any communities through categories property in each EkoCommunity object.

Note: Categories can only be created and updated from admin panel.

Get Categories

categoryColllection = communityRepository.getAllCategories(.displayName, includeDeleted: true|false)
categoryCollectionToken = categoryColllection?.observe({ (collection, _, _) in
// React to changes here
})