@amityco/ts-sdk-react-native
Version:
Amity Social Cloud Typescript SDK
52 lines (43 loc) • 1.5 kB
text/typescript
import { getActiveClient } from '~/client/api/activeClient';
import { ingestInCache } from '~/cache/api/ingestInCache';
import { prepareCommunityPayload } from '~/communityRepository/utils';
import { fireEvent } from '~/core/events';
/**
* @deprecated This function will to be deprecated and use the new community.join().
*/
/* begin_public_function
id: community.join
*/
/**
* ```js
* import { joinCommunity } from '@amityco/ts-sdk-react-native'
* const isJoined = await joinCommunity('foobar')
* ```
*
* Joins a {@link Amity.Community} object
*
* @param communityId the {@link Amity.Community} to join
* @returns A success boolean if the {@link Amity.Community} was joined
*
* @category Community API
* @async
*/
export const joinCommunity = async (
communityId: Amity.Community['communityId'],
): Promise<boolean> => {
const client = getActiveClient();
client.log('community/joinCommunity', communityId);
const { data: payload } = await client.http.post<Amity.CommunityMembershipPayload>(
`/api/v3/communities/${communityId}/join`,
);
fireEvent('local.community.joined', payload);
const data = prepareCommunityPayload(payload);
const cachedAt = client.cache && Date.now();
if (client.cache) ingestInCache(data, { cachedAt });
const { communityUsers } = data;
return !!communityUsers.find(
communityUser =>
communityUser.communityId === communityId && communityUser.communityMembership === 'member',
);
};
/* end_public_function */