@amityco/ts-sdk-react-native
Version:
Amity Social Cloud Typescript SDK
93 lines (78 loc) • 2.9 kB
text/typescript
import { getActiveClient } from '~/client/api';
import { pullFromCache } from '~/cache/api';
import { ingestInCache } from '~/cache/api/ingestInCache';
import { saveCommunityUsers } from '~/communityRepository/utils/saveCommunityUsers';
import { prepareCommunityPayload } from '../utils';
import { LinkedObject } from '~/utils/linkedObject';
/**
* ```js
* import { getCommunities } from '@amityco/ts-sdk-react-native'
* const communities = await getCommunities(['foo', 'bar'])
* ```
*
* Fetches a collection of {@link Amity.Community} objects
*
* @param communityIds the IDs of the {@link Amity.Community} to fetch
* @returns the associated collection of {@link Amity.Community} objects
*
* @category Community API
* @async
*/
export const getCommunities = async (
communityIds: Amity.Community['communityId'][],
includeDiscoverablePrivateCommunity?: boolean,
): Promise<Amity.Cached<Amity.Community[]>> => {
const client = getActiveClient();
client.log('community/getCommunities', communityIds);
const encodedCommunityIds = communityIds.map(communityId => encodeURIComponent(communityId));
// API-FIX: endpoint should not be /list, parameters should be querystring.
const { data: payload } = await client.http.get<Amity.CommunityPayload>(
`/api/v3/communities/list`,
{
params: {
communityIds: encodedCommunityIds,
includeDiscoverablePrivateCommunity: includeDiscoverablePrivateCommunity ?? true,
},
},
);
const data = prepareCommunityPayload(payload);
const cachedAt = client.cache && Date.now();
if (client.cache) {
ingestInCache(data, { cachedAt });
saveCommunityUsers(data.communities, data.communityUsers);
}
return {
data: data.communities.map(community => LinkedObject.community(community)),
cachedAt,
};
};
/**
* ```js
* import { getCommunities } from '@amityco/ts-sdk-react-native'
* const communities = getCommunities.locally(['foo', 'bar']) ?? []
* ```
*
* Fetches a collection of {@link Amity.Community} objects from cache
*
* @param communityIds the IDs of the {@link Amity.Community} to fetch
* @returns the associated collection of {@link Amity.Community} objects
*
* @category Community API
*/
getCommunities.locally = (
communityIds: Amity.Community['communityId'][],
): Amity.Cached<Amity.Community[]> | undefined => {
const client = getActiveClient();
client.log('community/getCommunities.locally', communityIds);
if (!client.cache) return;
const cached = communityIds
.map(communityId => pullFromCache<Amity.Community>(['community', 'get', communityId])!)
.filter(Boolean);
const communities = cached.map(({ data }) => data);
const oldest = cached.sort((a, b) => (a.cachedAt! < b.cachedAt! ? -1 : 1))?.[0];
if (cached?.length < communityIds.length) return;
return {
data: communities,
cachedAt: oldest.cachedAt,
};
};