generated from Borodutch/backend-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartServicePoster.ts
More file actions
126 lines (123 loc) · 3.79 KB
/
startServicePoster.ts
File metadata and controls
126 lines (123 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { Colors, EmbedBuilder } from 'discord.js'
import { DocumentType } from '@typegoose/typegoose'
import { Post, PostModel } from '@/models/Post'
import {
SCPostStorage,
SCPostStorage__factory,
} from '@big-whale-labs/seal-cred-posts-contract'
import filter = require('leo-profanity')
import { BigNumber } from 'ethers'
import Status from '@/models/Status'
import channels from '@/helpers/channels'
import data from '@/data'
import delay from '@/helpers/delay'
import getMessageFromError from '@/helpers/getMessageFromError'
import getSymbol from '@/helpers/getSymbol'
import isTwitterError from '@/helpers/isTwitterError'
import logError from '@/helpers/logError'
import provider from '@/helpers/provider'
async function postPost({
contractAddress,
blockchainId,
postingService,
}: DocumentType<Post>) {
let contract: SCPostStorage | undefined
let post: string | undefined
let derivativeAddress: string | undefined
let postContent: string | undefined
try {
contract = SCPostStorage__factory.connect(contractAddress, provider)
const fetchedPost = await contract.posts(blockchainId)
post = fetchedPost.post
derivativeAddress = fetchedPost.derivativeAddress
const symbol = (await getSymbol(derivativeAddress)).slice(0, -2) // cut extra "-d"
postContent =
postingService === 'farcaster'
? filter.clean(post)
: `${filter.clean(post)} @ ${symbol.split('.').join('\u2024')}` // replace dot with unicode character
const postFunction = data.find(
(datum) =>
datum.contract === contractAddress && datum.type === postingService
)?.post
if (!postFunction) {
throw new Error(
`No post function found for ${postingService} and ${contractAddress}`
)
}
const replyToId = BigNumber.from(fetchedPost.replyToId).gt(0)
? BigNumber.from(fetchedPost.replyToId)?.toHexString()
: undefined
console.log(
`Posting "${postContent}" (${postContent.length}, reply to ${
replyToId || '(not a reply)'
}) to ${postingService}...`
)
const id = await postFunction(postContent, replyToId)
await PostModel.updateOne(
{
contractAddress,
blockchainId,
postingService,
},
{
serviceId: id,
status: Status.published,
}
)
} catch (error) {
await PostModel.updateOne(
{
contractAddress,
blockchainId,
postingService,
},
{ status: Status.failedToPost }
)
const message = getMessageFromError(error)
const details = isTwitterError(error) ? error.data.detail : 'no details'
const description = `${message} [${details}] for the post (blockchain id: ${blockchainId})${
postContent ? `: \n\n${postContent}` : ''
}`
const embed = new EmbedBuilder()
.setColor(Colors.DarkRed)
.setTitle(`Error posting on ${postingService} for ${derivativeAddress}`)
.setDescription(description)
try {
await (
await channels.error()
).send({
embeds: [embed],
})
} catch (discordError) {
logError('Sending error message to Discord', error)
}
}
}
let checking = false
async function checkPostsToBePosted() {
if (checking) return
checking = true
console.log('Checking posts to be posted...')
try {
const postsToPost = await PostModel.find({
status: Status.approved,
})
console.log(
`Found ${postsToPost.length} posts to be posted that were approved...`
)
for (const post of postsToPost) {
await postPost(post)
await delay(2)
}
} catch (error) {
console.error(
'Error fetching pending posts',
error instanceof Error ? error.message : error
)
} finally {
checking = false
}
}
export default function () {
setInterval(() => checkPostsToBePosted(), 4 * 1000) // every 4 seconds
}