Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

persist posts #47

Merged
merged 4 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 3 additions & 30 deletions src/helpers/runNotificationListener.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/sendHotPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 didSendHotPostHotPost(feedId: number, postId: number) {
const sendedDate = await getItem(`hot-post-${feedId}-${postId}`)
return sendedDate !== undefined
}
Expand All @@ -15,7 +15,7 @@ export default async function checkAndSendHotPost(
feedId: number,
postId: number
) {
if (await isSendedHotPost(feedId, postId)) return
if (await didSendHotPostHotPost(feedId, postId)) return

const isHot = await isHotPost(feedId, postId)
if (!isHot) return
Expand Down
45 changes: 45 additions & 0 deletions src/helpers/sendPost.ts
Original file line number Diff line number Diff line change
@@ -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 didSendHotPost(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 didSendHotPost(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())
}
Loading