@amityco/ts-sdk-react-native
Version:
Amity Social Cloud Typescript SDK
93 lines (78 loc) • 2.5 kB
text/typescript
import { addPostSetting } from '~/communityRepository/utils';
import { updateMembershipStatus } from '~/communityRepository/utils/communityWithMembership';
import { isAmityLivestreamPost } from '~/utils/postTypePredicate';
const updateStreamReferences = (
streams: Amity.RawStream[],
streamId: string | undefined,
postId: string,
) => {
if (!streamId) return streams;
return streams.map(stream =>
stream.streamId === streamId
? {
...stream,
referenceType: 'post',
referenceId: postId,
postId,
}
: stream,
);
};
export const preparePostPayload = (payload: Amity.PostPayload): Amity.ProcessedPostPayload => {
const { posts: postsData, postChildren, videoStreamings, ...postPayload } = payload;
// Unpack community payload by mapping payload field to postSetting value.
const communitiesWithPostSetting = addPostSetting({ communities: postPayload.communities });
// map users with community
const mappedCommunityUsers: Array<Amity.Membership<'community'>> = postPayload.communityUsers.map(
communityUser => {
const user = postPayload.users.find(user => user.userId === communityUser.userId)!;
return {
...communityUser,
user,
};
},
);
const communityWithMembershipStatus = updateMembershipStatus(
communitiesWithPostSetting,
mappedCommunityUsers,
);
let mappedNewStream: Amity.RawStream[] = [];
// feed type
const posts = postsData.map(post => {
const feedType = postPayload.feeds.find(feed => feed.feedId === post.feedId)?.feedType;
const childPosts = payload.postChildren.filter(
children => children.parentPostId === post.postId,
);
if (childPosts.length > 0 && isAmityLivestreamPost(childPosts[0])) {
mappedNewStream = updateStreamReferences(
videoStreamings,
childPosts[0].data?.streamId,
post.postId,
);
}
return {
...post,
childPosts,
feedType,
};
});
return {
...postPayload,
postChildren,
videoStreamings: mappedNewStream,
posts,
communities: communityWithMembershipStatus,
communityUsers: mappedCommunityUsers,
};
};
export const prepareSemanticSearchPostPayload = ({
searchResult,
polls,
...postPayload
}: Amity.SemanticSearchPostPayload): Amity.ProcessedSemanticSearchPostPayload => {
const processedPostPayload = preparePostPayload(postPayload);
return {
...processedPostPayload,
polls,
};
};