mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {Dimensions} from 'react-native' 2 3import {isSafari} from 'lib/browser' 4import {isWeb} from 'platform/detection' 5 6const {height: SCREEN_HEIGHT} = Dimensions.get('window') 7 8const IFRAME_HOST = isWeb 9 ? // @ts-ignore only for web 10 window.location.host === 'localhost:8100' 11 ? 'http://localhost:8100' 12 : 'https://bsky.app' 13 : __DEV__ && !process.env.JEST_WORKER_ID 14 ? 'http://localhost:8100' 15 : 'https://bsky.app' 16 17export const embedPlayerSources = [ 18 'youtube', 19 'youtubeShorts', 20 'twitch', 21 'spotify', 22 'soundcloud', 23 'appleMusic', 24 'vimeo', 25 'giphy', 26 'tenor', 27 'flickr', 28] as const 29 30export type EmbedPlayerSource = (typeof embedPlayerSources)[number] 31 32export type EmbedPlayerType = 33 | 'youtube_video' 34 | 'youtube_short' 35 | 'twitch_video' 36 | 'spotify_album' 37 | 'spotify_playlist' 38 | 'spotify_song' 39 | 'soundcloud_track' 40 | 'soundcloud_set' 41 | 'apple_music_playlist' 42 | 'apple_music_album' 43 | 'apple_music_song' 44 | 'vimeo_video' 45 | 'giphy_gif' 46 | 'tenor_gif' 47 | 'flickr_album' 48 49export const externalEmbedLabels: Record<EmbedPlayerSource, string> = { 50 youtube: 'YouTube', 51 youtubeShorts: 'YouTube Shorts', 52 vimeo: 'Vimeo', 53 twitch: 'Twitch', 54 giphy: 'GIPHY', 55 tenor: 'Tenor', 56 spotify: 'Spotify', 57 appleMusic: 'Apple Music', 58 soundcloud: 'SoundCloud', 59 flickr: 'Flickr', 60} 61 62export interface EmbedPlayerParams { 63 type: EmbedPlayerType 64 playerUri: string 65 isGif?: boolean 66 source: EmbedPlayerSource 67 metaUri?: string 68 hideDetails?: boolean 69 dimensions?: { 70 height: number 71 width: number 72 } 73} 74 75const giphyRegex = /media(?:[0-4]\.giphy\.com|\.giphy\.com)/i 76const gifFilenameRegex = /^(\S+)\.(webp|gif|mp4)$/i 77 78export function parseEmbedPlayerFromUrl( 79 url: string, 80): EmbedPlayerParams | undefined { 81 let urlp 82 try { 83 urlp = new URL(url) 84 } catch (e) { 85 return undefined 86 } 87 88 // youtube 89 if (urlp.hostname === 'youtu.be') { 90 const videoId = urlp.pathname.split('/')[1] 91 const seek = encodeURIComponent(urlp.searchParams.get('t') ?? 0) 92 if (videoId) { 93 return { 94 type: 'youtube_video', 95 source: 'youtube', 96 playerUri: `${IFRAME_HOST}/iframe/youtube.html?videoId=${videoId}&start=${seek}`, 97 } 98 } 99 } 100 if ( 101 urlp.hostname === 'www.youtube.com' || 102 urlp.hostname === 'youtube.com' || 103 urlp.hostname === 'm.youtube.com' || 104 urlp.hostname === 'music.youtube.com' 105 ) { 106 const [_, page, shortOrLiveVideoId] = urlp.pathname.split('/') 107 108 const isShorts = page === 'shorts' 109 const isLive = page === 'live' 110 const videoId = 111 isShorts || isLive 112 ? shortOrLiveVideoId 113 : (urlp.searchParams.get('v') as string) 114 const seek = encodeURIComponent(urlp.searchParams.get('t') ?? 0) 115 116 if (videoId) { 117 return { 118 type: isShorts ? 'youtube_short' : 'youtube_video', 119 source: isShorts ? 'youtubeShorts' : 'youtube', 120 hideDetails: isShorts ? true : undefined, 121 playerUri: `${IFRAME_HOST}/iframe/youtube.html?videoId=${videoId}&start=${seek}`, 122 } 123 } 124 } 125 126 // twitch 127 if ( 128 urlp.hostname === 'twitch.tv' || 129 urlp.hostname === 'www.twitch.tv' || 130 urlp.hostname === 'm.twitch.tv' 131 ) { 132 const parent = isWeb 133 ? // @ts-ignore only for web 134 window.location.hostname 135 : 'localhost' 136 137 const [_, channelOrVideo, clipOrId, id] = urlp.pathname.split('/') 138 139 if (channelOrVideo === 'videos') { 140 return { 141 type: 'twitch_video', 142 source: 'twitch', 143 playerUri: `https://player.twitch.tv/?volume=0.5&!muted&autoplay&video=${clipOrId}&parent=${parent}`, 144 } 145 } else if (clipOrId === 'clip') { 146 return { 147 type: 'twitch_video', 148 source: 'twitch', 149 playerUri: `https://clips.twitch.tv/embed?volume=0.5&autoplay=true&clip=${id}&parent=${parent}`, 150 } 151 } else if (channelOrVideo) { 152 return { 153 type: 'twitch_video', 154 source: 'twitch', 155 playerUri: `https://player.twitch.tv/?volume=0.5&!muted&autoplay&channel=${channelOrVideo}&parent=${parent}`, 156 } 157 } 158 } 159 160 // spotify 161 if (urlp.hostname === 'open.spotify.com') { 162 const [_, typeOrLocale, idOrType, id] = urlp.pathname.split('/') 163 164 if (idOrType) { 165 if (typeOrLocale === 'playlist' || idOrType === 'playlist') { 166 return { 167 type: 'spotify_playlist', 168 source: 'spotify', 169 playerUri: `https://open.spotify.com/embed/playlist/${ 170 id ?? idOrType 171 }`, 172 } 173 } 174 if (typeOrLocale === 'album' || idOrType === 'album') { 175 return { 176 type: 'spotify_album', 177 source: 'spotify', 178 playerUri: `https://open.spotify.com/embed/album/${id ?? idOrType}`, 179 } 180 } 181 if (typeOrLocale === 'track' || idOrType === 'track') { 182 return { 183 type: 'spotify_song', 184 source: 'spotify', 185 playerUri: `https://open.spotify.com/embed/track/${id ?? idOrType}`, 186 } 187 } 188 } 189 } 190 191 // soundcloud 192 if ( 193 urlp.hostname === 'soundcloud.com' || 194 urlp.hostname === 'www.soundcloud.com' 195 ) { 196 const [_, user, trackOrSets, set] = urlp.pathname.split('/') 197 198 if (user && trackOrSets) { 199 if (trackOrSets === 'sets' && set) { 200 return { 201 type: 'soundcloud_set', 202 source: 'soundcloud', 203 playerUri: `https://w.soundcloud.com/player/?url=${url}&auto_play=true&visual=false&hide_related=true`, 204 } 205 } 206 207 return { 208 type: 'soundcloud_track', 209 source: 'soundcloud', 210 playerUri: `https://w.soundcloud.com/player/?url=${url}&auto_play=true&visual=false&hide_related=true`, 211 } 212 } 213 } 214 215 if ( 216 urlp.hostname === 'music.apple.com' || 217 urlp.hostname === 'music.apple.com' 218 ) { 219 // This should always have: locale, type (playlist or album), name, and id. We won't use spread since we want 220 // to check if the length is correct 221 const pathParams = urlp.pathname.split('/') 222 const type = pathParams[2] 223 const songId = urlp.searchParams.get('i') 224 225 if (pathParams.length === 5 && (type === 'playlist' || type === 'album')) { 226 // We want to append the songId to the end of the url if it exists 227 const embedUri = `https://embed.music.apple.com${urlp.pathname}${ 228 urlp.search ? '?i=' + songId : '' 229 }` 230 231 if (type === 'playlist') { 232 return { 233 type: 'apple_music_playlist', 234 source: 'appleMusic', 235 playerUri: embedUri, 236 } 237 } else if (type === 'album') { 238 if (songId) { 239 return { 240 type: 'apple_music_song', 241 source: 'appleMusic', 242 playerUri: embedUri, 243 } 244 } else { 245 return { 246 type: 'apple_music_album', 247 source: 'appleMusic', 248 playerUri: embedUri, 249 } 250 } 251 } 252 } 253 } 254 255 if (urlp.hostname === 'vimeo.com' || urlp.hostname === 'www.vimeo.com') { 256 const [_, videoId] = urlp.pathname.split('/') 257 if (videoId) { 258 return { 259 type: 'vimeo_video', 260 source: 'vimeo', 261 playerUri: `https://player.vimeo.com/video/${videoId}?autoplay=1`, 262 } 263 } 264 } 265 266 if (urlp.hostname === 'giphy.com' || urlp.hostname === 'www.giphy.com') { 267 const [_, gifs, nameAndId] = urlp.pathname.split('/') 268 269 /* 270 * nameAndId is a string that consists of the name (dash separated) and the id of the gif (the last part of the name) 271 * We want to get the id of the gif, then direct to media.giphy.com/media/{id}/giphy.webp so we can 272 * use it in an <Image> component 273 */ 274 275 if (gifs === 'gifs' && nameAndId) { 276 const gifId = nameAndId.split('-').pop() 277 278 if (gifId) { 279 return { 280 type: 'giphy_gif', 281 source: 'giphy', 282 isGif: true, 283 hideDetails: true, 284 metaUri: `https://giphy.com/gifs/${gifId}`, 285 playerUri: `https://i.giphy.com/media/${gifId}/200.webp`, 286 } 287 } 288 } 289 } 290 291 // There are five possible hostnames that also can be giphy urls: media.giphy.com and media0-4.giphy.com 292 // These can include (presumably) a tracking id in the path name, so we have to check for that as well 293 if (giphyRegex.test(urlp.hostname)) { 294 // We can link directly to the gif, if its a proper link 295 const [_, media, trackingOrId, idOrFilename, filename] = 296 urlp.pathname.split('/') 297 298 if (media === 'media') { 299 if (idOrFilename && gifFilenameRegex.test(idOrFilename)) { 300 return { 301 type: 'giphy_gif', 302 source: 'giphy', 303 isGif: true, 304 hideDetails: true, 305 metaUri: `https://giphy.com/gifs/${trackingOrId}`, 306 playerUri: `https://i.giphy.com/media/${trackingOrId}/200.webp`, 307 } 308 } else if (filename && gifFilenameRegex.test(filename)) { 309 return { 310 type: 'giphy_gif', 311 source: 'giphy', 312 isGif: true, 313 hideDetails: true, 314 metaUri: `https://giphy.com/gifs/${idOrFilename}`, 315 playerUri: `https://i.giphy.com/media/${idOrFilename}/200.webp`, 316 } 317 } 318 } 319 } 320 321 // Finally, we should see if it is a link to i.giphy.com. These links don't necessarily end in .gif but can also 322 // be .webp 323 if (urlp.hostname === 'i.giphy.com' || urlp.hostname === 'www.i.giphy.com') { 324 const [_, mediaOrFilename, filename] = urlp.pathname.split('/') 325 326 if (mediaOrFilename === 'media' && filename) { 327 const gifId = filename.split('.')[0] 328 return { 329 type: 'giphy_gif', 330 source: 'giphy', 331 isGif: true, 332 hideDetails: true, 333 metaUri: `https://giphy.com/gifs/${gifId}`, 334 playerUri: `https://i.giphy.com/media/${gifId}/200.webp`, 335 } 336 } else if (mediaOrFilename) { 337 const gifId = mediaOrFilename.split('.')[0] 338 return { 339 type: 'giphy_gif', 340 source: 'giphy', 341 isGif: true, 342 hideDetails: true, 343 metaUri: `https://giphy.com/gifs/${gifId}`, 344 playerUri: `https://i.giphy.com/media/${ 345 mediaOrFilename.split('.')[0] 346 }/200.webp`, 347 } 348 } 349 } 350 351 const tenorGif = parseTenorGif(urlp) 352 if (tenorGif.success) { 353 const {playerUri, dimensions} = tenorGif 354 355 return { 356 type: 'tenor_gif', 357 source: 'tenor', 358 isGif: true, 359 hideDetails: true, 360 playerUri, 361 dimensions, 362 } 363 } 364 365 // this is a standard flickr path! we can use the embedder for albums and groups, so validate the path 366 if (urlp.hostname === 'www.flickr.com' || urlp.hostname === 'flickr.com') { 367 let i = urlp.pathname.length - 1 368 while (i > 0 && urlp.pathname.charAt(i) === '/') { 369 --i 370 } 371 372 const path_components = urlp.pathname.slice(1, i + 1).split('/') 373 if (path_components.length === 4) { 374 // discard username - it's not relevant 375 const [photos, _, albums, id] = path_components 376 if (photos === 'photos' && albums === 'albums') { 377 // this at least has the shape of a valid photo-album URL! 378 return { 379 type: 'flickr_album', 380 source: 'flickr', 381 playerUri: `https://embedr.flickr.com/photosets/${id}`, 382 } 383 } 384 } 385 386 if (path_components.length === 3) { 387 const [groups, id, pool] = path_components 388 if (groups === 'groups' && pool === 'pool') { 389 return { 390 type: 'flickr_album', 391 source: 'flickr', 392 playerUri: `https://embedr.flickr.com/groups/${id}`, 393 } 394 } 395 } 396 // not an album or a group pool, don't know what to do with this! 397 return undefined 398 } 399 400 // link shortened flickr path 401 if (urlp.hostname === 'flic.kr') { 402 const b58alph = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ' 403 let [_, type, idBase58Enc] = urlp.pathname.split('/') 404 let id = 0n 405 for (const char of idBase58Enc) { 406 const nextIdx = b58alph.indexOf(char) 407 if (nextIdx >= 0) { 408 id = id * 58n + BigInt(nextIdx) 409 } else { 410 // not b58 encoded, ergo not a valid link to embed 411 return undefined 412 } 413 } 414 415 switch (type) { 416 case 'go': 417 const formattedGroupId = `${id}` 418 return { 419 type: 'flickr_album', 420 source: 'flickr', 421 playerUri: `https://embedr.flickr.com/groups/${formattedGroupId.slice( 422 0, 423 -2, 424 )}@N${formattedGroupId.slice(-2)}`, 425 } 426 case 's': 427 return { 428 type: 'flickr_album', 429 source: 'flickr', 430 playerUri: `https://embedr.flickr.com/photosets/${id}`, 431 } 432 default: 433 // we don't know what this is so we can't embed it 434 return undefined 435 } 436 } 437} 438 439export function getPlayerAspect({ 440 type, 441 hasThumb, 442 width, 443}: { 444 type: EmbedPlayerParams['type'] 445 hasThumb: boolean 446 width: number 447}): {aspectRatio?: number; height?: number} { 448 if (!hasThumb) return {aspectRatio: 16 / 9} 449 450 switch (type) { 451 case 'youtube_video': 452 case 'twitch_video': 453 case 'vimeo_video': 454 return {aspectRatio: 16 / 9} 455 case 'youtube_short': 456 if (SCREEN_HEIGHT < 600) { 457 return {aspectRatio: (9 / 16) * 1.75} 458 } else { 459 return {aspectRatio: (9 / 16) * 1.5} 460 } 461 case 'spotify_album': 462 case 'apple_music_album': 463 case 'apple_music_playlist': 464 case 'spotify_playlist': 465 case 'soundcloud_set': 466 return {height: 380} 467 case 'spotify_song': 468 if (width <= 300) { 469 return {height: 155} 470 } 471 return {height: 232} 472 case 'soundcloud_track': 473 return {height: 165} 474 case 'apple_music_song': 475 return {height: 150} 476 default: 477 return {aspectRatio: 16 / 9} 478 } 479} 480 481export function getGifDims( 482 originalHeight: number, 483 originalWidth: number, 484 viewWidth: number, 485) { 486 const scaledHeight = (originalHeight / originalWidth) * viewWidth 487 488 return { 489 height: scaledHeight > 250 ? 250 : scaledHeight, 490 width: (250 / scaledHeight) * viewWidth, 491 } 492} 493 494export function getGiphyMetaUri(url: URL) { 495 if (giphyRegex.test(url.hostname) || url.hostname === 'i.giphy.com') { 496 const params = parseEmbedPlayerFromUrl(url.toString()) 497 if (params && params.type === 'giphy_gif') { 498 return params.metaUri 499 } 500 } 501} 502 503export function parseTenorGif(urlp: URL): 504 | {success: false} 505 | { 506 success: true 507 playerUri: string 508 dimensions: {height: number; width: number} 509 } { 510 if (urlp.hostname !== 'media.tenor.com') { 511 return {success: false} 512 } 513 514 let [_, id, filename] = urlp.pathname.split('/') 515 516 if (!id || !filename) { 517 return {success: false} 518 } 519 520 if (!id.includes('AAAAC')) { 521 return {success: false} 522 } 523 524 const h = urlp.searchParams.get('hh') 525 const w = urlp.searchParams.get('ww') 526 527 if (!h || !w) { 528 return {success: false} 529 } 530 531 const dimensions = { 532 height: Number(h), 533 width: Number(w), 534 } 535 536 if (isWeb) { 537 if (isSafari) { 538 id = id.replace('AAAAC', 'AAAP1') 539 filename = filename.replace('.gif', '.mp4') 540 } else { 541 id = id.replace('AAAAC', 'AAAP3') 542 filename = filename.replace('.gif', '.webm') 543 } 544 } else { 545 id = id.replace('AAAAC', 'AAAAM') 546 } 547 548 return { 549 success: true, 550 playerUri: `https://t.gifs.bsky.app/${id}/${filename}`, 551 dimensions, 552 } 553}