Channel Participation

Members Query

You can get a list of all members, or add memberships, roles , search parameters to get certain members of the channel.

import { ChannelRepository, MemberFilter } from '@amityco/js-sdk';

let members;

const liveCollection = ChannelRepository.queryMembers({ 
  channelId: 'channel1',
  memberships: [MemberFilter.Member],
  roles: ['role1'],
  search: 'asd',
});

liveCollection.on('dataUpdated', newModels => {
  members = newModels;
});

liveCollection.on('dataError', error => {
  console.error(error);
});

members= liveCollection.models;

Manage Members

You can also add and remove members, as well as remove yourself as a member of the channel (leaving the channel).

// add 'user1' and 'user2' to this channel
const isAdded = await ChannelRepository.addMembers({
  channelId: 'channel1',
  userIds: [ 'user1', 'user2' ],
});

// remove 'user3' from this channel
const isRemoved = await ChannelRepository.removeMembers({
  channelId: 'channelId',
  userIds: [ 'user3' ],
});

// leave this channel
const isLeaved = await ChannelRepository.leave('channelId');

Reading Status And Unread Count

You can get the number of unread messages in a channel through the unreadCount field.

const liveObject = ChannelRepository.getChannel('channelId');

liveObject.on('dataUpdated', channel => {
  // the number of unread messages
  console.log(channel.unreadCount);
});

The startReading() and stopReading() methods let the server know that the current user is reading a channel. After the startReading()and stopReading() methods are called, the unreadCount is reset to 0.

You can call both methods as many times as you require. The SDK takes care of multiple device management: thus, a user can read multiple channels from one or more devices simultaneously. In case of an abrupt disconnection (be it the app has been terminated or the Internet is down, etc.), the SDK backend automatically calls the stopReading command on behalf of the user.

// start reading a channel
await ChannelRepository.startReading({ channelId: 'channel1' });

// stop reading a channel
await ChannelRepository.stopReading({ channelId: 'channel1' });

Moderation

ChannelModeration class provides various methods to moderate the users present in channel. You can ban/unban users, assign roles or remove it from user.

Note:

  1. The channel creator is automatically assigned as the channel moderator.

  2. The previous/last moderator is not allowed to leave a community and an error is displayed.

  3. The channel moderator can promote a user/member to moderator.

  4. The channel moderator can demote a moderator to a user/member.

This applies only to Live and Community channels’. This does not apply to Conversation Channel.

const isBanned = await ChannelRepository.banMembers({ 
  channelId: 'channel1', 
  userIds: ['user1']
);

const isUnbanned = await ChannelRepository.banMembers({ 
  channelId: 'channel1', 
  userIds: ['user1']
);

await ChannelRepository.addRole({ 
  channelId: 'channel1', 
  userIds: ['user1'],
  role: 'role1',
);

await ChannelRepository.removeRole({ 
  channelId: 'channel1', 
  userIds: ['user1'],
  role: 'role1',
);

Last updated