22
33import $ from 'jquery' ;
44import { Host } from '../../core/host' ;
5- import { ajax } from '../../environment' ;
5+ import { ajax , i18n } from '../../environment' ;
66
77export default new Host ( 'bluesky' , {
88 name : 'bluesky' ,
99 logo : 'https://bsky.app/static/favicon.png' ,
1010 permissions : [ 'https://embed.bsky.app/oembed' ] ,
1111 domains : [ 'bsky.app' ] ,
12- detect : ( { href } ) => ( / ^ ^ h t t p s ? : \/ \/ ( b s k y ) \. a p p \/ p r o f i l e \/ [ \w . - ] + \/ p o s t + / i) . exec ( href ) ,
12+ detect : ( { href } ) => ( / ^ ^ h t t p s ? : \/ \/ ( b s k y ) \. a p p \/ p r o f i l e \/ [ ^ \\ ] + \/ p o s t + / i) . exec ( href ) ,
13+ options :{
14+ forceReplaceNativeExpando : {
15+ title : 'showImagesForceReplaceNativeExpandoTitle' ,
16+ description : 'showImagesForceReplaceNativeExpandoDesc' ,
17+ value : true ,
18+ type : 'boolean' ,
19+ noconfig : true ,
20+ } ,
21+ } ,
1322 async handleLink ( href ) {
14- const post = await ajax ( {
15- url : 'https://embed.bsky.app/oembed' ,
16- query : { url : href } ,
17- type :'json' ,
18- } ) ;
19-
23+ let posthtml ;
24+ try {
25+ const post = await ajax ( {
26+ url : 'https://embed.bsky.app/oembed' ,
27+ query : { url : href . replace ( / \/ + $ / , '' ) } , // Remove trailing slashes, as the bluesky embed app does accept it.
28+ type :'json' ,
29+ } ) ;
30+ // This removes the embedded script that we cannot use for security reasons.
31+ posthtml = $ . parseHTML ( post . html ) [ 0 ]
32+ } catch ( error ) {
33+ // If we get here, the embed API likely threw a 403, which happens when a user requests that the post is only viewed
34+ // by logged in users on bsky.app. There is no way to embed this post.
35+ posthtml = `<blockquote class="bluesky-embed">${ i18n ( 'blueskyExpandoUserRequestedLoginToView' ) } </blockquote>`
36+ }
2037 // Script requires element to be attached to document when starting
2138 const $dummy = $ ( '<div>' ) ;
22-
2339 return {
2440 type : 'GENERIC_EXPANDO' ,
2541 muted : true ,
2642 expandoClass : 'selftext' ,
2743 generate : ( ) => $dummy [ 0 ] ,
28- onAttach : ( ) => { $dummy . html ( post . html ) ; } ,
44+ onAttach : ( ) => {
45+ $dummy . html ( posthtml ) ;
46+ } ,
2947 } ;
3048 } ,
31- } ) ;
49+ } ,
50+ ) ;
0 commit comments