Add user-agent parameter to handleStatus

This commit is contained in:
dangered wolf 2022-07-14 18:22:18 -04:00
parent 901bda578a
commit 280a2d0376
No known key found for this signature in database
GPG key ID: 41E4D37680ED8B58
3 changed files with 13 additions and 5 deletions

View file

@ -1,4 +1,4 @@
const barLength = 36;
let barLength = 36;
export const calculateTimeLeft = (date: Date) => {
const now = new Date();
@ -22,12 +22,17 @@ export const calculateTimeLeftString = (date: Date) => {
return daysString || hoursString || minutesString || secondsString || 'Final results';
};
export const renderPoll = async (card: TweetCard): Promise<string> => {
export const renderPoll = async (card: TweetCard, userAgent: string = ''): Promise<string> => {
let str = '\n\n';
const values = card.binding_values;
console.log('rendering poll on ', card);
// Telegram's bars need to be a lot smaller to fit its bubbles
if (userAgent.indexOf('Telegram') > -1) {
barLength = 24;
}
let choices: { [label: string]: number } = {};
let totalVotes = 0;
let timeLeft = '';
@ -37,6 +42,7 @@ export const renderPoll = async (card: TweetCard): Promise<string> => {
timeLeft = calculateTimeLeftString(date);
}
/* TODO: make this cleaner */
if (
typeof values !== 'undefined' &&
typeof values.choice1_count !== 'undefined' &&

View file

@ -29,7 +29,7 @@ const statusRequest = async (request: any) => {
const userAgent = request.headers.get('User-Agent');
if (userAgent.match(/bot/gi) !== null) {
return new Response(await handleStatus(id, parseInt(mediaNumber || 1)), {
return new Response(await handleStatus(id, parseInt(mediaNumber || 1), userAgent), {
headers: Constants.RESPONSE_HEADERS,
status: 200
});
@ -49,6 +49,7 @@ router.get('/owoembed', async (request: any) => {
console.log('oembed hit!');
const { searchParams } = new URL(request.url);
/* Fallbacks */
let text = searchParams.get('text') || 'Twitter';
let author = searchParams.get('author') || 'dangeredwolf';
let status = searchParams.get('status') || '1547514042146865153';

View file

@ -8,7 +8,8 @@ import { handleQuote } from './quote';
export const handleStatus = async (
status: string,
mediaNumber?: number
mediaNumber?: number,
userAgent?: string
): Promise<string> => {
const conversation = await fetchUsingGuest(status);
@ -58,7 +59,7 @@ export const handleStatus = async (
let authorText = 'Twitter';
if (tweet.card) {
text += await renderPoll(tweet.card);
text += await renderPoll(tweet.card, userAgent);
}
text = linkFixer(tweet, text);