update logic

This commit is contained in:
Danil Kovtonyuk 2020-02-29 19:17:53 +10:00
parent 49b87d51b3
commit ef1693cfec
2 changed files with 44 additions and 24 deletions

View File

@ -7,7 +7,8 @@ const {
TWITTER_ACCESS_TOKEN_KEY,
TWITTER_ACCESS_TOKEN_SECRET,
NUXT_ENV_TWITTER_HASHTAG,
TWITTER_INTERVAL_ATTESTATION
TWITTER_INTERVAL_ATTESTATION,
NODE_ENV
} = process.env
const client = new Twitter({
@ -19,24 +20,15 @@ const client = new Twitter({
const { Contribution } = require('./models')
async function attestationWatcher() {
function attestationWatcher() {
// get the last saved tweet
let initTweet
try {
initTweet = require('/tmp/lastTweet.json').lastTweet
} catch (e) {
initTweet = process.env.LAST_TWEET
initTweet = 0
}
// get all contributions without attestation
const contributions = await Contribution.findAll({
where: {
socialType: 'twitter',
attestation: null
},
attributes: ['id', 'handle', 'socialType', 'attestation']
})
const params = {
since_id: initTweet,
q: `#${NUXT_ENV_TWITTER_HASHTAG} -filter:retweets`,
@ -45,20 +37,48 @@ async function attestationWatcher() {
}
// search tweets with params
client.get('search/tweets', params, function(error, tweets, response) {
client.get('search/tweets', params, async function(error, tweets, response) {
if (!error) {
tweets.statuses.forEach((tweet) => {
contributions.forEach((contribution) => {
// compare account compliance
if (contribution.handle === tweet.user.screen_name) {
// update the database record by id
Contribution.update({ attestation: tweet.id_str }, { where: { id: contribution.id } })
for (const tweet of tweets.statuses) {
if (NODE_ENV === 'development') {
console.log(
`Succesful attestation https://${contribution.socialType}.com/${contribution.handle}/status/${tweet.id_str}`
'\x1B[36m%s\x1B[0m',
`${tweet.text} https://twitter.com/${tweet.user.screen_name}/status/${tweet.id_str}`
)
}
})
})
// find the contribution id in a tweet
let matchTweetContributionId = null
let tweetContributionId = null
if ((matchTweetContributionId = tweet.text.match(/#([0-9]+)/))) {
tweetContributionId = Number(matchTweetContributionId[1])
}
// if found the contribution id then search a contribution
if (tweetContributionId) {
// try update the database record by id
try {
const result = await Contribution.update(
{ attestation: tweet.id_str },
{
where: {
id: tweetContributionId,
handle: tweet.user.screen_name,
attestation: null
}
}
)
if (result[0]) {
console.log(
`Succesful attestation #${tweetContributionId} https://twitter.com/${tweet.user.screen_name}/status/${tweet.id_str}`
)
}
} catch (error) {
console.error(error)
}
}
}
// save the last tweet received
fs.writeFileSync(

View File

@ -53,7 +53,7 @@ const actions = {
window.location.replace(`/api/connect/${provider}`)
},
makeTweet({ state }) {
const tweetText = `Just made the contribution %23${state.contributionIndex} to Tornado.cash Trusted Setup Ceremony! 🚀 %23${process.env.NUXT_ENV_TWITTER_HASHTAG}`
const tweetText = `Just made the contribution %23${state.contributionIndex} to @TornadoCash Trusted Setup Ceremony! 🚀 %23${process.env.NUXT_ENV_TWITTER_HASHTAG}`
const popUpWindowWidth = 600
const popUpWindowHeight = 250
const dualScreenLeft = window.screenLeft !== undefined ? window.screenLeft : window.screenX