trusted-setup-server/server/attestationWatcher.js

79 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-02-25 15:13:09 +01:00
const fs = require('fs')
const Twitter = require('twitter')
const {
TWITTER_CONSUMER_KEY,
TWITTER_CONSUMER_SECRET,
TWITTER_ACCESS_TOKEN_KEY,
TWITTER_ACCESS_TOKEN_SECRET,
2020-02-28 08:57:30 +01:00
NUXT_ENV_TWITTER_HASHTAG,
2020-02-25 15:13:09 +01:00
TWITTER_INTERVAL_ATTESTATION
} = process.env
const client = new Twitter({
consumer_key: TWITTER_CONSUMER_KEY,
consumer_secret: TWITTER_CONSUMER_SECRET,
access_token_key: TWITTER_ACCESS_TOKEN_KEY,
access_token_secret: TWITTER_ACCESS_TOKEN_SECRET
})
const { Contribution } = require('./models')
async function attestationWatcher() {
// get the last saved tweet
let initTweet
try {
initTweet = require('/tmp/lastTweet.json').lastTweet
} catch (e) {
initTweet = process.env.LAST_TWEET
}
// 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,
2020-02-28 08:57:30 +01:00
q: `#${NUXT_ENV_TWITTER_HASHTAG} -filter:retweets`,
2020-02-25 15:13:09 +01:00
result_type: 'recent',
count: 100
}
// search tweets with params
client.get('search/tweets', params, 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}`
)
}
})
})
// save the last tweet received
fs.writeFileSync(
'/tmp/lastTweet.json',
JSON.stringify({ lastTweet: tweets.search_metadata.max_id_str })
)
} else {
console.error('attestationWatcher error', error)
}
})
setTimeout(() => {
attestationWatcher()
}, TWITTER_INTERVAL_ATTESTATION)
}
module.exports = attestationWatcher