stream-chat-react-native-core
Version:
The official React Native and Expo components for Stream Chat, a service for building chat applications
56 lines (50 loc) • 1.93 kB
text/typescript
import type { ChannelAPIResponse } from 'stream-chat';
import { getChannelActiveLocations } from './getChannelActiveLocations';
import { getChannelMessages } from './getChannelMessages';
import { getDraftForChannels } from './getDraftsForChannels';
import { getMembers } from './getMembers';
import { getReads } from './getReads';
import { selectChannels } from './queries/selectChannels';
import { mapStorableToChannel } from '../mappers/mapStorableToChannel';
import { SqliteClient } from '../SqliteClient';
/**
* Returns the list of channels with state enriched for given channel ids.
*
* @param {Object} param
* @param {Array} param.channelIds List of channel ids to fetch.
* @param {Array} param.currentUserId Id of the current logged in user.
*
* @returns {Array} Channels with enriched state.
*/
export const getChannels = async ({
channelIds,
currentUserId,
}: {
channelIds: string[];
currentUserId: string;
}): Promise<Omit<ChannelAPIResponse, 'duration'>[]> => {
SqliteClient.logger?.('info', 'getChannels', { channelIds, currentUserId });
const [channels, cidVsDraft, cidVsMembers, cidVsReads, cidVsMessages, cidVsActiveLocations] =
await Promise.all([
selectChannels({ channelIds }),
getDraftForChannels({ channelIds, currentUserId }),
getMembers({ channelIds }),
getReads({ channelIds }),
getChannelMessages({
channelIds,
currentUserId,
}),
getChannelActiveLocations({ channelIds }),
]);
// Enrich the channels with state
return channels.map((c) => ({
...mapStorableToChannel(c),
active_live_locations: cidVsActiveLocations[c.cid] || [],
draft: cidVsDraft[c.cid],
members: cidVsMembers[c.cid] || [],
membership: (cidVsMembers[c.cid] || []).find((member) => member.user_id === currentUserId),
messages: cidVsMessages[c.cid] || [],
pinned_messages: [],
read: cidVsReads[c.cid] || [],
}));
};