Code cleanup and API fetch poll Tweet test

This commit is contained in:
dangered wolf 2022-08-17 15:39:24 -04:00
parent 3e6bf109f4
commit bded3c1bd6
No known key found for this signature in database
GPG key ID: 41E4D37680ED8B58
3 changed files with 69 additions and 50 deletions

View file

@ -8,49 +8,29 @@ export const renderCard = async (
console.log('rendering card');
const choices: { [label: string]: number } = {};
let totalVotes = 0;
if (typeof values !== 'undefined') {
/* TODO: make poll code cleaner. It really sucks. */
if (
typeof values.choice1_count !== 'undefined' &&
typeof values.choice2_count !== 'undefined'
) {
if (typeof values.choice1_count !== 'undefined') {
const poll = {} as APIPoll;
if (typeof values.end_datetime_utc !== 'undefined') {
poll.ends_at = values.end_datetime_utc.string_value || '';
const date = new Date(values.end_datetime_utc.string_value);
poll.time_left_en = calculateTimeLeftString(date);
}
choices[values.choice1_label?.string_value || ''] = parseInt(
values.choice1_count.string_value
);
totalVotes += parseInt(values.choice1_count.string_value);
choices[values.choice2_label?.string_value || ''] = parseInt(
values.choice2_count.string_value
);
totalVotes += parseInt(values.choice2_count.string_value);
if (typeof values.choice3_count !== 'undefined') {
choices[values.choice3_label?.string_value || ''] = parseInt(
values.choice3_count.string_value
);
totalVotes += parseInt(values.choice3_count.string_value);
}
if (typeof values.choice4_count !== 'undefined') {
choices[values.choice4_label?.string_value || ''] =
parseInt(values.choice4_count.string_value) || 0;
totalVotes += parseInt(values.choice4_count.string_value);
poll.time_left_en = calculateTimeLeftString(new Date(values.end_datetime_utc.string_value));
}
poll.total_votes = totalVotes;
poll.choices = Object.keys(choices).map(label => {
const choices: { [label: string]: number } = {
[values.choice1_label?.string_value || '']: parseInt(values.choice1_count?.string_value || '0'),
[values.choice2_label?.string_value || '']: parseInt(values.choice2_count?.string_value || '0'),
[values.choice3_label?.string_value || '']: parseInt(values.choice3_count?.string_value || '0'),
[values.choice4_label?.string_value || '']: parseInt(values.choice4_count?.string_value || '0')
}
poll.total_votes = Object.values(choices).reduce((a, b) => a + b, 0);
poll.choices = Object.keys(choices).filter(label => label !== '').map(label => {
return {
label: label,
count: choices[label],
percentage: (Math.round((choices[label] / totalVotes) * 1000) || 0) / 10 || 0
percentage: (Math.round((choices[label] / poll.total_votes) * 1000) || 0) / 10 || 0
};
});

View file

@ -1,5 +1,6 @@
import { Constants } from '../constants';
/* Handler for mosaic (multi-image combiner) */
export const handleMosaic = async (
mediaList: APIPhoto[],
id: string
@ -21,17 +22,8 @@ export const handleMosaic = async (
const baseUrl = `https://${selectedDomain}/`;
let path = '';
if (mosaicMedia[0]) {
path += `/${mosaicMedia[0]}`;
}
if (mosaicMedia[1]) {
path += `/${mosaicMedia[1]}`;
}
if (mosaicMedia[2]) {
path += `/${mosaicMedia[2]}`;
}
if (mosaicMedia[3]) {
path += `/${mosaicMedia[3]}`;
for (let j = 0; j < 4; j++) {
path += `/${mosaicMedia[j]}`;
}
const size = calcSize(
@ -52,12 +44,10 @@ export const handleMosaic = async (
We use this to generate accurate mosaic sizes which helps Discord render it correctly */
const SPACING_SIZE = 10;
/*
* This number will be multiplied by the height and weight
* if all combined images are over 2000 pixels in height or width.
* In my tests setting this to 0.5 increased performance by 4x (at the cost of 50% resolution)
* NOTE: This only works for when there's 4 images supplied.
*/
/* This number will be multiplied by the height and weight
if all combined images are over 2000 pixels in height or width.
In my tests setting this to 0.5 increased performance by 4x (at the cost of 50% resolution)
NOTE: This only works for when there's 4 images supplied. */
const BIG_IMAGE_MULTIPLIER = 1;
const calcSize = (images: Size[]): Size => {

View file

@ -257,3 +257,52 @@ test('API fetch multi-video Tweet', async () => {
expect(videos[3].format).toEqual('video/mp4');
expect(videos[3].type).toEqual('video');
});
test('API fetch poll Tweet', async () => {
const result = await cacheWrapper(
new Request('https://api.fxtwitter.com/status/1547441637739184128', {
method: 'GET',
headers: botHeaders
})
);
expect(result.status).toEqual(200);
const response = (await result.json()) as APIResponse;
expect(response).toBeTruthy();
expect(response.code).toEqual(200);
expect(response.message).toEqual('OK');
const tweet = response.tweet as APITweet;
expect(tweet).toBeTruthy();
expect(tweet.url).toEqual('https://twitter.com/dangeredwolf/status/1547441637739184128');
expect(tweet.id).toEqual('1547441637739184128');
expect(tweet.text).toEqual('Poll with 4 values');
expect(tweet.author.screen_name?.toLowerCase()).toEqual('dangeredwolf');
expect(tweet.author.name).toBeTruthy();
expect(tweet.author.avatar_url).toBeTruthy();
expect(tweet.author.banner_url).toBeTruthy();
expect(tweet.author.avatar_color).toBeTruthy();
expect(tweet.twitter_card).toEqual('tweet');
expect(tweet.created_at).toEqual('Thu Jul 14 04:43:31 +0000 2022');
expect(tweet.created_timestamp).toEqual(1657773811);
expect(tweet.lang).toEqual('en');
expect(tweet.replying_to).toBeNull();
expect(tweet.poll).toBeTruthy();
const poll = tweet.poll as APIPoll;
expect(poll.ends_at).toEqual('2022-07-15T04:43:31Z');
expect(poll.time_left_en).toEqual('Final results');
expect(poll.total_votes).toEqual(194);
const choices = poll.choices as APIPollChoice[];
expect(choices[0].label).toEqual('1');
expect(choices[0].count).toEqual(14);
expect(choices[0].percentage).toEqual(7.2);
expect(choices[1].label).toEqual('2');
expect(choices[1].count).toEqual(36);
expect(choices[1].percentage).toEqual(18.6);
expect(choices[2].label).toEqual('3');
expect(choices[2].count).toEqual(80);
expect(choices[2].percentage).toEqual(41.2);
expect(choices[3].label).toEqual('4');
expect(choices[3].count).toEqual(64);
expect(choices[3].percentage).toEqual(33);
});