mirror of
https://github.com/CompeyDev/fxtwitter-docker.git
synced 2025-04-05 10:30:55 +01:00
Differentiate status response types
This commit is contained in:
parent
6fd43aa9f5
commit
0e44e2c721
4 changed files with 27 additions and 18 deletions
|
@ -2,11 +2,10 @@ import { Router } from 'itty-router';
|
|||
import { Constants } from './constants';
|
||||
import { handleStatus } from './status';
|
||||
import { Strings } from './strings';
|
||||
import { Flags } from './types';
|
||||
|
||||
const router = Router();
|
||||
|
||||
const statusRequest = async (request: any, event: FetchEvent, flags: Flags = {}) => {
|
||||
const statusRequest = async (request: any, event: FetchEvent, flags: InputFlags = {}) => {
|
||||
const { handle, id, mediaNumber } = request.params;
|
||||
const url = new URL(request.url);
|
||||
const userAgent = request.headers.get('User-Agent');
|
||||
|
@ -26,27 +25,32 @@ const statusRequest = async (request: any, event: FetchEvent, flags: Flags = {})
|
|||
|
||||
let response: Response;
|
||||
|
||||
let status = await handleStatus(
|
||||
let statusResponse = await handleStatus(
|
||||
id.match(/\d{2,20}/)?.[0],
|
||||
parseInt(mediaNumber || 1),
|
||||
userAgent,
|
||||
flags
|
||||
);
|
||||
|
||||
if (status instanceof Response) {
|
||||
if (statusResponse.response) {
|
||||
console.log('handleStatus sent response');
|
||||
response = status;
|
||||
} else {
|
||||
response = statusResponse.response;
|
||||
} else if (statusResponse.text) {
|
||||
/* Fallback if a person browses to a direct media link with a Tweet without media */
|
||||
if (!isBotUA) {
|
||||
return Response.redirect(`${Constants.TWITTER_ROOT}/${handle}/status/${id}`, 302);
|
||||
}
|
||||
console.log('handleStatus sent embed');
|
||||
|
||||
response = new Response(status, {
|
||||
response = new Response(statusResponse.text, {
|
||||
headers: Constants.RESPONSE_HEADERS,
|
||||
status: 200
|
||||
});
|
||||
} else {
|
||||
response = new Response(Strings.ERROR_UNKNOWN, {
|
||||
headers: Constants.RESPONSE_HEADERS,
|
||||
status: 500
|
||||
});
|
||||
}
|
||||
|
||||
return response;
|
||||
|
|
|
@ -6,24 +6,23 @@ import { renderCard } from './card';
|
|||
import { handleQuote } from './quote';
|
||||
import { sanitizeText } from './utils';
|
||||
import { Strings } from './strings';
|
||||
import { Flags } from './types';
|
||||
|
||||
export const returnError = (error: string) => {
|
||||
return Strings.BASE_HTML.format({
|
||||
export const returnError = (error: string): StatusResponse => {
|
||||
return {text: Strings.BASE_HTML.format({
|
||||
lang: '',
|
||||
headers: [
|
||||
`<meta content="${Constants.BRANDING_NAME}" property="og:title"/>`,
|
||||
`<meta content="${error}" property="og:description"/>`
|
||||
].join('')
|
||||
});
|
||||
})};
|
||||
};
|
||||
|
||||
export const handleStatus = async (
|
||||
status: string,
|
||||
mediaNumber?: number,
|
||||
userAgent?: string,
|
||||
flags?: Flags
|
||||
): Promise<string | Response> => {
|
||||
flags?: InputFlags
|
||||
): Promise<StatusResponse> => {
|
||||
console.log('Direct?', flags?.direct);
|
||||
const conversation = await fetchUsingGuest(status);
|
||||
|
||||
|
@ -278,7 +277,7 @@ export const handleStatus = async (
|
|||
if (flags?.direct && redirectMedia) {
|
||||
let response = Response.redirect(redirectMedia, 302);
|
||||
console.log(response);
|
||||
return response;
|
||||
return { response: response };
|
||||
}
|
||||
|
||||
if (mediaList.length > 1) {
|
||||
|
@ -329,8 +328,8 @@ export const handleStatus = async (
|
|||
/* When dealing with a Tweet of unknown lang, fall back to en */
|
||||
let lang = tweet.lang === 'unk' ? 'en' : tweet.lang || 'en';
|
||||
|
||||
return Strings.BASE_HTML.format({
|
||||
return { text: Strings.BASE_HTML.format({
|
||||
lang: `lang="${lang}"`,
|
||||
headers: headers.join('')
|
||||
});
|
||||
}) };
|
||||
};
|
||||
|
|
|
@ -39,5 +39,6 @@ export const Strings = {
|
|||
PHOTO_COUNT: `Photo {number} of {total}`,
|
||||
ERROR_API_FAIL: 'Tweet failed to load due to an API error :(',
|
||||
ERROR_PRIVATE: `I can't embed Tweets from private accounts, sorry about that :(`,
|
||||
ERROR_TWEET_NOT_FOUND: `Sorry, that Tweet doesn't exist :(`
|
||||
ERROR_TWEET_NOT_FOUND: `Sorry, that Tweet doesn't exist :(`,
|
||||
ERROR_UNKNOWN: `Unknown error occurred, sorry about that :(`
|
||||
};
|
||||
|
|
7
src/types.ts → src/types.d.ts
vendored
7
src/types.ts → src/types.d.ts
vendored
|
@ -1,6 +1,11 @@
|
|||
/* tweetTypes has all the Twitter API-related types */
|
||||
|
||||
export type Flags = {
|
||||
type InputFlags = {
|
||||
standard?: boolean;
|
||||
direct?: boolean;
|
||||
};
|
||||
|
||||
interface StatusResponse {
|
||||
text?: string;
|
||||
response?: Response;
|
||||
}
|
Loading…
Add table
Reference in a new issue