Skip to content
Snippets Groups Projects
post-gweet.ts 916 B
Newer Older
import { postHashtags, retrieveHashtagsFromGweet } from "@/components/trends";
import { postMedia } from "./post-media";

export const postGweet = async ({
  content,
  userId,
  replyToGweetId,
}: {
  content: string;
  userId: string;
  replyToGweetId?: string | null;
  quoteGweetId?: string | null;
}) => {
  const gweet = {
    content,
    userId,
    ...(replyToGweetId && { replyToGweetId }),
    ...(quoteGweetId && { quoteGweetId }),
  };

  try {
    const data = await fetch('/api/gweets', {
      method: 'POST',
      body: JSON.stringify(gweet)
    }).then((result) => result.json())

    if (files.length > 0) {
      await postMedia({ files, gweet_id: data.id });
    }

    const hashtags = retrieveHashtagsFromGweet(content);
    if (hashtags) await postHashtags(hashtags);

    return data;
  } catch (error: any) {
    return error.response.data;
  }
};