UNPKG

@amityco/ts-sdk-react-native

Version:

Amity Social Cloud Typescript SDK

63 lines (54 loc) 1.83 kB
import { getActiveClient } from '~/client/api/activeClient'; import { ingestInCache } from '~/cache/api/ingestInCache'; import { fireEvent } from '~/core/events'; import { prepareMembershipPayload } from '~/group/utils'; import { LinkedObject } from '~/utils/linkedObject'; /* begin_public_function id: post.create.clip_post */ /** * ```js * import { PostRepository } from '@amityco/ts-sdk' * const created = await PostRepository.createClipPost({ * targetType: 'user', * targetId: 'foobar', * dataType: 'clip', * data: { text: 'hello world' }, * attachments: [{ type: 'clip', fileId: 'fileId123', displayMode: 'fill', isMuted: false }] * })) * ``` * * Creates an {@link Amity.Post} * * @param bundle The data necessary to create a new {@link Amity.Post} * @returns The newly created {@link Amity.Post} * * @category Post API * @async */ export const createClipPost = async <T extends Amity.PostContentType | string>( bundle: Pick<Amity.Post<T>, 'targetType' | 'targetId'> & Partial<Pick<Amity.Post<T>, 'metadata' | 'mentionees' | 'tags'>> & { data: { [k: string]: any }; attachments: { type: T; fileId: Amity.File['fileId']; displayMode?: Amity.ClipDisplayMode; isMuted?: boolean; }[]; }, ): Promise<Amity.Cached<Amity.Post>> => { const client = getActiveClient(); client.log('post/createPost', bundle); const { data: payload } = await client.http.post<Amity.PostPayload>('/api/v4/posts', bundle); fireEvent('post.created', payload); const data = prepareMembershipPayload(payload, 'communityUsers'); const cachedAt = client.cache && Date.now(); if (client.cache) ingestInCache(data, { cachedAt }); const { posts } = data; return { data: LinkedObject.post(posts[0]), cachedAt, }; }; /* end_public_function */