From ef1693cfeccda51839916ae1758a0184db9c39c6 Mon Sep 17 00:00:00 2001 From: Danil Kovtonyuk Date: Sat, 29 Feb 2020 19:17:53 +1000 Subject: [PATCH] update logic --- server/attestationWatcher.js | 66 +++++++++++++++++++++++------------- store/user.js | 2 +- 2 files changed, 44 insertions(+), 24 deletions(-) diff --git a/server/attestationWatcher.js b/server/attestationWatcher.js index eacfa1c..27f668f 100644 --- a/server/attestationWatcher.js +++ b/server/attestationWatcher.js @@ -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 } }) - console.log( - `Succesful attestation https://${contribution.socialType}.com/${contribution.handle}/status/${tweet.id_str}` + for (const tweet of tweets.statuses) { + if (NODE_ENV === 'development') { + console.log( + '\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( diff --git a/store/user.js b/store/user.js index f584d47..a0dfb88 100644 --- a/store/user.js +++ b/store/user.js @@ -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