diff --git a/libraries/nestjs-libraries/src/integrations/social/farcaster.provider.ts b/libraries/nestjs-libraries/src/integrations/social/farcaster.provider.ts index 32e68bfa..ac9fd256 100644 --- a/libraries/nestjs-libraries/src/integrations/social/farcaster.provider.ts +++ b/libraries/nestjs-libraries/src/integrations/social/farcaster.provider.ts @@ -9,8 +9,6 @@ import dayjs from 'dayjs'; import { SocialAbstract } from '@gitroom/nestjs-libraries/integrations/social.abstract'; import { NeynarAPIClient } from '@neynar/nodejs-sdk'; import { Integration } from '@prisma/client'; -import { AuthService } from '@gitroom/helpers/auth/auth.service'; -import { groupBy } from 'lodash'; import { FarcasterDto } from '@gitroom/nestjs-libraries/dtos/posts/providers-settings/farcaster.dto'; import { Tool } from '@gitroom/nestjs-libraries/integrations/tool.decorator'; import { Rules } from '@gitroom/nestjs-libraries/chat/rules.description.decorator'; @@ -81,48 +79,83 @@ export class FarcasterProvider accessToken: string, postDetails: PostDetails[] ): Promise { - const ids = []; - const subreddit = - !postDetails?.[0]?.settings?.subreddit || - postDetails?.[0]?.settings?.subreddit.length === 0 + const [firstPost] = postDetails; + const ids: { releaseURL: string; postId: string }[] = []; + + const channels = + !firstPost?.settings?.subreddit || + firstPost?.settings?.subreddit.length === 0 ? [undefined] - : postDetails?.[0]?.settings?.subreddit; + : firstPost?.settings?.subreddit; - for (const channel of subreddit) { - let idHash = ''; - for (const post of postDetails) { - const data = await client.publishCast({ - embeds: - post?.media?.map((media) => ({ - url: media.path, - })) || [], - signerUuid: accessToken, - text: post.message, - ...(idHash ? { parent: idHash } : {}), - ...(channel?.value?.id ? { channelId: channel?.value?.id } : {}), - }); - idHash = data.cast.hash; + for (const channel of channels) { + const data = await client.publishCast({ + embeds: + firstPost?.media?.map((media) => ({ + url: media.path, + })) || [], + signerUuid: accessToken, + text: firstPost.message, + ...(channel?.value?.id ? { channelId: channel?.value?.id } : {}), + }); - ids.push({ - // @ts-ignore - releaseURL: `https://warpcast.com/${data.cast.author.username}/${idHash}`, - status: 'success', - id: post.id, - postId: data.cast.hash, - // @ts-ignore - author: data.cast.author.username, - }); - } + ids.push({ + // @ts-ignore + releaseURL: `https://warpcast.com/${data.cast.author.username}/${data.cast.hash}`, + postId: data.cast.hash, + }); } - const list = Object.values(groupBy(ids, (p) => p.id)).map((p) => ({ - id: p[0].id, - postId: p.map((p) => String(p.postId)).join(','), - releaseURL: p.map((p) => p.releaseURL).join(','), - status: 'published', - })); + return [ + { + id: firstPost.id, + postId: ids.map((p) => p.postId).join(','), + releaseURL: ids.map((p) => p.releaseURL).join(','), + status: 'published', + }, + ]; + } - return list; + async comment( + id: string, + postId: string, + lastCommentId: string | undefined, + accessToken: string, + postDetails: PostDetails[], + integration: Integration + ): Promise { + const [commentPost] = postDetails; + const ids: { releaseURL: string; postId: string }[] = []; + + // postId can be comma-separated if posted to multiple channels + const parentIds = (lastCommentId || postId).split(','); + + for (const parentHash of parentIds) { + const data = await client.publishCast({ + embeds: + commentPost?.media?.map((media) => ({ + url: media.path, + })) || [], + signerUuid: accessToken, + text: commentPost.message, + parent: parentHash, + }); + + ids.push({ + // @ts-ignore + releaseURL: `https://warpcast.com/${data.cast.author.username}/${data.cast.hash}`, + postId: data.cast.hash, + }); + } + + return [ + { + id: commentPost.id, + postId: ids.map((p) => p.postId).join(','), + releaseURL: ids.map((p) => p.releaseURL).join(','), + status: 'published', + }, + ]; } @Tool({