Skip to content

Commit

Permalink
Merge branch 'review-fb-error' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
iparamonau committed Nov 7, 2024
2 parents 54c45e5 + 942a487 commit 52385d2
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 44 deletions.
113 changes: 73 additions & 40 deletions plugins/custom/fb-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
// Though the errors we actually need are not in the doc...
const KNOWN_CODES = {
2: 408, // Downtime
24: 404, // Seems to be 404 for Instagrams
// 24: 404, // 404s and restricted for Instagrams, handled separately

100: 404, // https://developers.facebook.com/docs/graph-api/reference/oembed-video/
901: 417, // ./oembed-post/, ./oembed-page/
Expand All @@ -16,52 +16,85 @@ export default {
368: 451 // Temporary blocked
}

let result = {}; // When left empty - it allows a fallback to generic parsers and get the accurate http code.
const fbError = oembedError.body && oembedError.body.error || {};
const fbError = oembedError.body?.error || {};

var error = fbError.code
&& KNOWN_CODES[fbError.code] /* is defined */
const error = fbError.code && KNOWN_CODES[fbError.code] /* is a known error code */
? KNOWN_CODES[fbError.code]
: null;

if (!error &&
(!oembedError.body // Connection issue like ERR_HTTP2_STREAM_ERROR
|| fbError.is_transient) // Unexpected transient error
) {

error = 408;
}

if (fbError.code === 2 /* downtime */ && fbError.is_transient && options.cache_ttl === 0) {
// handle error elsewhere, e.g. fall back to generic parsers
error = null;
}

const isVideo = /(?:videos?|watch)/i.test(url);

if (error && !isVideo) {
result.responseError = error;
}
let result = {};

if (error === 415) {
result.message = CONFIG.FB_ERROR_MESSAGE ||
"HEADS UP: Facebook & Instagram now require your own access_token configured. "
+ " See https://github.com/itteco/iframely/issues/284";
} else if (isVideo && error === 404) {
result.message = "This video cannot be embedded.";
result.fallback = "generic";
result = {
responseError: 415,
message: CONFIG.FB_ERROR_MESSAGE ||
"HEADS UP: Facebook & Instagram now require your own access_token configured. "
+ " See https://github.com/itteco/iframely/issues/284"
};

// FB video with disabled embedding
} else if (error === 404 && /(?:videos?|watch)/i.test(url)) {
result = {
fallback: "generic",
message: "This video cannot be embedded."
};

// Instagram post with disabled embedding
} else if (fbError.error_subcode == 2207046) {
// Instagram's post with disabled embedding
result.message = "Owner has disabled embedding of this post";
result.fallback = "generic";
} else if (/endpoint must be reviewed and approved by Facebook/.test(oembedError?.body?.error?.message)
&& options.getProviderOptions('facebook.oembed_read_error_msg')
&& /^887156172/.test(options.getProviderOptions('facebook.access_token'))) {

result.message = options.getProviderOptions('facebook.oembed_read_error_msg');

} else if ((fbError.error_user_msg || fbError.message) && error !== 404) {
result.message = fbError.error_user_msg || fbError.message;
result = {
fallback: "generic",
message: "Owner has disabled embedding of this post"
};

// Instagram post either 404 or private or sensitive
} else if (/instagram/i.test(url) && (fbError.code === 24 || fbError.code === 2 || !fbError.code)) {
result = {
fallback: "generic",
message: fbError.error_user_msg || "Media can't be embedded from private, inactive, and age-restricted accounts"
};

// Issues with FB access token
} else if (/endpoint must be reviewed and approved by Facebook/i.test(fbError.message)
|| /Invalid OAuth access token/i.test(fbError.message)) {

result.message = /^(88715617|64356396)/.test(options.getProviderOptions('facebook.access_token'))
? options.getProviderOptions('facebook.oembed_read_error_msg', fbError.message)
: fbError.message;

// FB downtime
} else if (fbError.code === 2 && fbError.is_transient) {
if (options.cache_ttl === 0) {
result = {
fallback: 'generic',
message: "Facebook reports a downtime"
};
} else {
result = {
responseError: 408,
message: "Facebook reports a downtime"
}
}

// Unexpected unknown transient error.
} else if (!error && options.cache_ttl !== 0
&& (!oembedError.body // Connection issue like ERR_HTTP2_STREAM_ERROR
|| fbError.is_transient
)) {
result.responseError = 408;

// Other known error
} else if (error) {
result.responseError = error;
if (error !== 404 && (fbError.error_user_msg || fbError.message)) {
result.message = fbError.error_user_msg || fbError.message;
}

// Other unknown error
} else {
result = {
fallback: "generic",
message: fbError.error_user_msg || fbError.message
}
}

log('Facebook oembed api - error getting oembed for', url, JSON.stringify(fbError), JSON.stringify(result));
Expand Down
10 changes: 10 additions & 0 deletions plugins/domains/instagram.com/instagram-post-allow-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,20 @@ export default {
&& !meta.title && !meta.description
&& meta['html-title'] === 'Instagram') {
return cb({responseStatusCode: 404})

} else if (
// URL from instagram-profile
/^https?:\/\/(?:www\.)?instagram\.com\/([a-zA-Z0-9\._]+)\/?(?:\?.*)?$/i.test(meta.og?.url)
&& meta.og?.type === 'profile') {
return cb({responseStatusCode: 403})

} else {
return cb (null, {
ipOG: meta.og ? meta.og : {}
})
}
}

// private: https://www.instagram.com/tv/Cfv5RV6Ae8bMan9o-CS7vsZGxcmGHuTGvG0jNI0/?igsh=bmI4czZqb2l4NDQx
// deleted: https://www.instagram.com/p/B3A-0uuAhg0/
};
4 changes: 0 additions & 4 deletions plugins/domains/instagram.com/instagram.com.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,6 @@ export default {
result.__allowInstagramMeta = true;
}

if (urlMatch[1] && urlMatch[1].length > 30) {
result.message = 'This Instagram post is private.'; // IDs longer than 30 is for private posts as of March 11, 2020
}

if (!options.redirectsHistory && (/^https?:\/\/instagram\.com\//i.test(url) || /^http:\/\/www\.instagram\.com\//i.test(url))) {
result.redirect = url.replace(/^http:\/\//, 'https://').replace(/^https:\/\/instagram\.com\//i, 'https://www.instagram.com');
}
Expand Down

0 comments on commit 52385d2

Please sign in to comment.