Communities

This page will guide you through the steps you need to take to integrate community chat groups into your applications for Web

Create Community

When creating a new community, first instantiates the CommunnityRepository, a class that contain all community related methods. Then call createCommunity() to obtain the LiveObject and observe it in order to obtain the final community model.

const liveCommunity
 = CommunityRepository.createCommunity({
  displayName: 'community1',
  description: 'hello and welcome!',
  avatarFileId: 'fileId123',
  categoryIds: ['new'],
  tags: ['foo'],
  metadata: {},
  isPublic: true,
  userIds: ['user1', 'user2'],
})

liveCommunity.once('dataUpdated', model => {
  console.log(`Community created: ${model.displayName}`);
});

Note that the event listener was registered using once(). Unlike on(), once() will automatically unregister the event listener once the first event was emitted. This is useful if you just need to use the model once but do not need to listen to further events.

Get Community

In the case where you only want to fetch communities data without joining, you can use the communityForId() method:

const liveCommunity = Community.communityForId('abc');

liveCommunity.once('dataUpdated', data => {
  ...
});

Join Community

const communityHasBeenJoined = await CommunityRepository.joinCommunity('abc');
console.log(communityHasBeenJoined) // true

Leave Community

const communityHasBeenLeft = await CommunityRepository.leaveCommunity('abc');
console.log(communityHasBeenLeft) // true

Community Query

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

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

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

  • the tags parameters let you filter communities based on the tags set (or not set) in each community

  • the categories 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

const communities = CommunityRepository.allCommunitiesWithFilters({
    search: 'community1',
    categoryId: 'newsCatId',
    isDeleted: false,
    tags: ['new'],
    filter: enum('all'|'member|'notMember'),
    sortBy: enum('firstCreated'|'lastCreated'|'displayName'),
});

communities.on('dataUpdated', models => {
  // reload data
});

Update Community

If you want to update a community, you can call the following:

const liveCommunity = CommunityRepository.updateCommunity({
    displayName: 'community1',
    description: 'hi this is a community',
    avatarFileId: 'fileId123',
    categoryIds: ['news'],
    tags: ['new'],
    metadata: {},
    isPublic: true,
});

liveCommunity.once('dataUpdated', data => {
  ...
});

Note. By default, only the communities original creator or adminstrators can update the community.

Community Membership

You can get a list of community members by calling the following method:

const members = CommunityCategory.getCommunityMembers({
    communityId: 'communnity123',
    filter: enum('all'|'member'|'notMember'),
    sortBy: enum('firstCreated'|'lastCreated'),

});
members.on('dataUpdated', models => {
  // reload member table
});

// unobserve data changes once you are finished
members.removeAllListeners('dataUpdated');

Community Categories

The CommunityRepository will also be able to manage community categories. When community are put into a category, you will be able to sort and filter each of the community in that category.

Note. Right now categories will only be creatable and updatable from the admin panel.

Get Category

You can fetch a particular category by calling the categoryForId() method:

const liveCategory = CommunityCategory.categoryForId('abc');

liveCategory.once('dataUpdated', data => {
  ...
});

You can also fetch a list of categories:

const liveCategory = CommunityCategory.categoriesForIds({
  categoryIds: ['news']
});

liveCategory.once('dataUpdated', data => {
  ...
});

Category Query

This method provides the ability to obtain all the categories.

const liveCategory = CommunityCategory.queryCategories({
    isDeleted: false,
    sortBy: enum('firstCreated'|'lastCreated'|'displayName'),
});

liveCategory.on('dataUpdated', models => {
  // reload data
});

Last updated