Re-add polls

This commit is contained in:
dangered wolf 2022-07-25 19:41:50 -04:00
parent a4d646ae26
commit 046383f56b
No known key found for this signature in database
GPG key ID: 41E4D37680ED8B58
4 changed files with 76 additions and 54 deletions

View file

@ -1,47 +1,4 @@
import { Strings } from './strings';
let barLength = 36;
export const calculateTimeLeft = (date: Date) => {
const now = new Date();
const diff = date.getTime() - now.getTime();
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((diff % (1000 * 60)) / 1000);
return { days, hours, minutes, seconds };
};
export const calculateTimeLeftString = (date: Date) => {
const { days, hours, minutes, seconds } = calculateTimeLeft(date);
const daysString =
days > 0
? `${days} ${days === 1 ? Strings.SINGULAR_DAY_LEFT : Strings.PLURAL_DAYS_LEFT}`
: '';
const hoursString =
hours > 0
? `${hours} ${hours === 1 ? Strings.SINGULAR_HOUR_LEFT : Strings.PLURAL_HOURS_LEFT}`
: '';
const minutesString =
minutes > 0
? `${minutes} ${
minutes === 1 ? Strings.SINGULAR_MINUTE_LEFT : Strings.PLURAL_MINUTES_LEFT
}`
: '';
const secondsString =
seconds > 0
? `${seconds} ${
seconds === 1 ? Strings.SINGULAR_SECOND_LEFT : Strings.PLURAL_SECONDS_LEFT
}`
: '';
return (
daysString ||
hoursString ||
minutesString ||
secondsString ||
Strings.FINAL_POLL_RESULTS
);
};
import { calculateTimeLeftString } from './pollHelper';
export const renderCard = async (
card: TweetCard
@ -54,7 +11,6 @@ export const renderCard = async (
// Telegram's bars need to be a lot smaller to fit its bubbles
let choices: { [label: string]: number } = {};
let totalVotes = 0;
let timeLeft = '';
if (typeof values !== 'undefined') {
/* TODO: make poll code cleaner */
@ -63,11 +19,12 @@ export const renderCard = async (
typeof values.choice2_count !== 'undefined'
) {
let poll = {} as APIPoll;
poll.ends_at = values.end_datetime_utc?.string_value || '';
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);
timeLeft = calculateTimeLeftString(date);
poll.time_left_en = calculateTimeLeftString(date);
}
choices[values.choice1_label?.string_value || ''] = parseInt(
values.choice1_count.string_value

42
src/pollHelper.ts Normal file
View file

@ -0,0 +1,42 @@
import { Strings } from './strings';
export const calculateTimeLeft = (date: Date) => {
const now = new Date();
const diff = date.getTime() - now.getTime();
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((diff % (1000 * 60)) / 1000);
return { days, hours, minutes, seconds };
};
export const calculateTimeLeftString = (date: Date) => {
const { days, hours, minutes, seconds } = calculateTimeLeft(date);
const daysString =
days > 0
? `${days} ${days === 1 ? Strings.SINGULAR_DAY_LEFT : Strings.PLURAL_DAYS_LEFT}`
: '';
const hoursString =
hours > 0
? `${hours} ${hours === 1 ? Strings.SINGULAR_HOUR_LEFT : Strings.PLURAL_HOURS_LEFT}`
: '';
const minutesString =
minutes > 0
? `${minutes} ${
minutes === 1 ? Strings.SINGULAR_MINUTE_LEFT : Strings.PLURAL_MINUTES_LEFT
}`
: '';
const secondsString =
seconds > 0
? `${seconds} ${
seconds === 1 ? Strings.SINGULAR_SECOND_LEFT : Strings.PLURAL_SECONDS_LEFT
}`
: '';
return (
daysString ||
hoursString ||
minutesString ||
secondsString ||
Strings.FINAL_POLL_RESULTS
);
};

View file

@ -1,13 +1,10 @@
import { Constants } from './constants';
import { linkFixer } from './linkFixer';
import { colorFromPalette } from './palette';
import { renderCard } from './card';
import { handleQuote } from './quote';
import { sanitizeText } from './utils';
import { Strings } from './strings';
import { handleMosaic } from './mosaic';
import { getAuthorText } from './author';
import { statusAPI } from './api';
import { calculateTimeLeftString } from './pollHelper';
export const returnError = (error: string): StatusResponse => {
return {
@ -104,7 +101,11 @@ export const handleStatus = async (
const { photos } = tweet.media;
let photo = photos[mediaNumber || 0];
if (typeof mediaNumber !== 'number' && tweet.media.mosaic) {
if (
typeof mediaNumber !== 'number' &&
tweet.media.mosaic &&
userAgent?.indexOf('Telegram') === -1
) {
photo = {
url:
userAgent?.indexOf('Telegram') !== -1
@ -159,6 +160,29 @@ export const handleStatus = async (
}
let siteName = Constants.BRANDING_NAME;
let newText = tweet.text;
if (tweet.poll) {
const { poll } = tweet;
let barLength = 34;
let str = '';
if (userAgent?.indexOf('Telegram') !== -1) {
barLength = 24;
}
tweet.poll.choices.forEach(choice => {
// render bar
const bar = '█'.repeat((choice.percentage / 100) * barLength);
str += `${bar}
${choice.label}  (${choice.percentage}%)
`;
});
str += `\n${poll.total_votes} votes · ${poll.time_left_en}`;
newText += `\n\n${str}`;
}
if (!tweet.media?.video && !tweet.media?.photos) {
headers.push(
@ -171,8 +195,6 @@ export const handleStatus = async (
);
}
let newText = tweet.text;
if (api.tweet?.translation) {
const { translation } = api.tweet;

1
src/types.d.ts vendored
View file

@ -54,6 +54,7 @@ interface APIPoll {
choices: APIPollChoice[];
total_votes: number;
ends_at: string;
time_left_en: string;
}
interface APIPhoto {