diff --git a/src/helpers/runNotificationListener.ts b/src/helpers/runNotificationListener.ts index 92d622e..f552f9b 100644 --- a/src/helpers/runNotificationListener.ts +++ b/src/helpers/runNotificationListener.ts @@ -1,15 +1,12 @@ import { BigNumber } from 'ethers' import { PostStructOutput } from '@big-whale-labs/obss-storage-contract/dist/typechain/contracts/Feeds' -import { generateRandomName } from '@big-whale-labs/backend-utils' import { getTokens } from '@/models/Token' import { minimumNumberOfComments } from '@/data/hotPost' import checkAndSendHotPost from '@/helpers/sendHotPost' -import feedsData from '@/helpers/feedsData' import getFeedsContract from '@/helpers/getFeedsContract' -import getIPFSContent from '@/helpers/getIPFSContent' import ketlAttestationContract from '@/helpers/getKetlAttestation' import sendFirebaseNotification from '@/helpers/sendFirebaseNotification' -import structToCid from '@/helpers/structToCid' +import sendPost from '@/helpers/sendPost' ketlAttestationContract.on('EntanglementRegistered', async () => { try { @@ -43,36 +40,12 @@ getFeedsContract.on( getFeedsContract.on( 'PostAdded', - async ( - feedId: BigNumber, - postId: BigNumber, - [author, metadata]: PostStructOutput - ) => { + async (feedId: BigNumber, postId: BigNumber, post: PostStructOutput) => { try { const numberFeedId = feedId.toNumber() const numberPostId = postId.toNumber() - const feedName = feedsData[numberFeedId] - if (!feedName) { - console.error('Feed not fount') - return - } - const content = await getIPFSContent(structToCid(metadata)) - if (!content || !content.text) { - console.error('Post data is empty') - return - } - - const authorUsername = generateRandomName(author) - const tokens = await getTokens({ allPostsEnabled: true }) - - await sendFirebaseNotification({ - body: content.text, - feedId: numberFeedId, - postId: numberPostId, - title: `@${authorUsername} posted at ${feedName}`, - tokens, - }) + await sendPost(numberFeedId, numberPostId, post) } catch (e) { console.error(e) } diff --git a/src/helpers/sendHotPost.ts b/src/helpers/sendHotPost.ts index 7ead03b..8d2d5ee 100644 --- a/src/helpers/sendHotPost.ts +++ b/src/helpers/sendHotPost.ts @@ -6,7 +6,7 @@ import isHotPost from '@/helpers/isHotPost' import sendFirebaseNotification from '@/helpers/sendFirebaseNotification' import structToCid from '@/helpers/structToCid' -async function isSendedHotPost(feedId: number, postId: number) { +async function didSendHotPost(feedId: number, postId: number) { const sendedDate = await getItem(`hot-post-${feedId}-${postId}`) return sendedDate !== undefined } @@ -15,7 +15,7 @@ export default async function checkAndSendHotPost( feedId: number, postId: number ) { - if (await isSendedHotPost(feedId, postId)) return + if (await didSendHotPost(feedId, postId)) return const isHot = await isHotPost(feedId, postId) if (!isHot) return diff --git a/src/helpers/sendPost.ts b/src/helpers/sendPost.ts new file mode 100644 index 0000000..2a8c3af --- /dev/null +++ b/src/helpers/sendPost.ts @@ -0,0 +1,45 @@ +import { PostStructOutput } from '@big-whale-labs/obss-storage-contract/dist/typechain/contracts/Feeds' +import { generateRandomName } from '@big-whale-labs/backend-utils' +import { getItem, setItem } from 'node-persist' +import { getTokens } from '@/models/Token' +import feedsData from '@/helpers/feedsData' +import getIPFSContent from '@/helpers/getIPFSContent' +import sendFirebaseNotification from '@/helpers/sendFirebaseNotification' +import structToCid from '@/helpers/structToCid' + +async function didSendPost(feedId: number, postId: number) { + const sendedDate = await getItem(`post-${feedId}-${postId}`) + return sendedDate !== undefined +} + +export default async function sendPost( + feedId: number, + postId: number, + [author, metadata]: PostStructOutput +) { + if (await didSendPost(feedId, postId)) return + + const feedName = feedsData[feedId] + if (!feedName) { + console.error('Feed not fount') + return + } + const content = await getIPFSContent(structToCid(metadata)) + if (!content || !content.text) { + console.error('Post data is empty') + return + } + + const authorUsername = generateRandomName(author) + const tokens = await getTokens({ allPostsEnabled: true }) + + await sendFirebaseNotification({ + body: content.text, + feedId, + postId, + title: `@${authorUsername} posted at ${feedName}`, + tokens, + }) + + await setItem(`post-${feedId}-${postId}`, Date.now()) +}