mirror of https://github.com/sgoudham/carbon.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.0 KiB
JavaScript
71 lines
2.0 KiB
JavaScript
7 years ago
|
const Twitter = require('twit')
|
||
7 years ago
|
const morph = require('morphmorph')
|
||
7 years ago
|
|
||
7 years ago
|
const RATE_LIMIT_CODE = 420
|
||
7 years ago
|
const MAX_ALT_TEXT_LENGTH = 420
|
||
7 years ago
|
|
||
7 years ago
|
let client
|
||
|
try {
|
||
|
client = new Twitter({
|
||
|
consumer_key: process.env.TWITTER_CONSUMER_KEY,
|
||
|
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
|
||
|
access_token: process.env.TWITTER_ACCESS_TOKEN_KEY,
|
||
|
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
|
||
|
})
|
||
|
} catch (e) {
|
||
|
// eslint-disable-next-line
|
||
7 years ago
|
console.warn(e.message)
|
||
7 years ago
|
}
|
||
7 years ago
|
const extractMediaId = morph.get('data.media_id_string')
|
||
|
const extractImageUrl = morph.get('data.entities.media.0.display_url')
|
||
|
const extractErrorCode = morph.get('0.code')
|
||
|
|
||
7 years ago
|
const uploadImage = data => client.post('media/upload', { media_data: data })
|
||
7 years ago
|
const uploadMetadata = (altText, twitterRes = {}) => {
|
||
7 years ago
|
if (!altText) return twitterRes
|
||
7 years ago
|
|
||
7 years ago
|
const formattedAltText =
|
||
|
altText.length > MAX_ALT_TEXT_LENGTH
|
||
|
? `${altText.slice(0, MAX_ALT_TEXT_LENGTH - 3)}...`
|
||
|
: altText
|
||
7 years ago
|
|
||
|
return client
|
||
|
.post('media/metadata/create', {
|
||
|
media_id: extractMediaId(twitterRes),
|
||
|
alt_text: { text: formattedAltText }
|
||
|
})
|
||
|
.then(() => twitterRes)
|
||
|
}
|
||
7 years ago
|
const uploadTweet = (twitterRes = {}) =>
|
||
|
client.post('statuses/update', {
|
||
7 years ago
|
status: `Carbon Copy #${extractMediaId(twitterRes).slice(0, 8)}`,
|
||
|
media_ids: extractMediaId(twitterRes)
|
||
7 years ago
|
})
|
||
7 years ago
|
|
||
7 years ago
|
const respondSuccess = (res, url) => res.json({ url })
|
||
7 years ago
|
const respondFail = (res, err) => {
|
||
7 years ago
|
const errorCode = extractErrorCode(err)
|
||
7 years ago
|
|
||
|
// check for rate limit
|
||
|
if (errorCode === RATE_LIMIT_CODE) {
|
||
7 years ago
|
return res.status(420).send()
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
// eslint-disable-next-line
|
||
7 years ago
|
console.error(`Error: ${err.message || JSON.stringify(err, null, 2)}`)
|
||
7 years ago
|
return res.status(500).send()
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
|
module.exports = (req, res) => {
|
||
7 years ago
|
if (!req.body.imageData) {
|
||
7 years ago
|
return res.status(400).send()
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
return uploadImage(req.body.imageData)
|
||
|
.then(uploadMetadata.bind(null, req.body.altText))
|
||
7 years ago
|
.then(uploadTweet)
|
||
|
.then(extractImageUrl)
|
||
|
.then(respondSuccess.bind(null, res))
|
||
7 years ago
|
.catch(respondFail.bind(null, res))
|
||
|
}
|