From b1442666618d3a0ccf48a8c31c8dbb8232272147 Mon Sep 17 00:00:00 2001 From: Steven Bell Date: Sun, 7 Aug 2022 02:10:32 -0500 Subject: [PATCH 1/6] add alby integration to podcast index --- package.json | 4 +- ui/src/components/Player/index.tsx | 264 +++++++++++++++++---- ui/src/components/Player/styles.scss | 52 ++++ ui/src/pages/Podcast/PodcastInfo/index.tsx | 109 +++++---- 4 files changed, 331 insertions(+), 98 deletions(-) diff --git a/package.json b/package.json index 64ab2d1..b3d3672 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "@types/redux-actions": "^2.6.1", "@types/redux-thunk": "^2.1.0", "apexcharts": "^3.20.0", + "canvas-confetti": "^1.5.1", "connected-react-router": "^6.7.0", "crypto-js": "^4.0.0", "dotenv": "^8.2.0", @@ -39,7 +40,8 @@ "reduxsauce": "^1.1.2", "shortid": "^2.2.15", "typesafe-actions": "^5.1.0", - "uuid": "^8.3.2" + "uuid": "^8.3.2", + "webln": "^0.3.0" }, "devDependencies": { "@babel/core": "^7.11.6", diff --git a/ui/src/components/Player/index.tsx b/ui/src/components/Player/index.tsx index 324afcc..9869849 100644 --- a/ui/src/components/Player/index.tsx +++ b/ui/src/components/Player/index.tsx @@ -1,16 +1,19 @@ import * as React from 'react' import AudioPlayer from 'react-h5-audio-player' -import sha256 from 'crypto-js/sha256'; -import { AUDIO_PRELOAD_ATTRIBUTE } from "react-h5-audio-player/src/constants"; -import { v4 as uuidv4 } from 'uuid'; -import {Link} from "react-router-dom"; -import { getISODate, getPrettyDate } from "../../utils"; +import sha256 from 'crypto-js/sha256' +import { AUDIO_PRELOAD_ATTRIBUTE } from 'react-h5-audio-player/src/constants' +import { v4 as uuidv4 } from 'uuid' +import { Link } from 'react-router-dom' +import { getISODate, getPrettyDate } from '../../utils' +import { requestProvider } from 'webln' +import confetti from 'canvas-confetti' import 'react-h5-audio-player/src/styles.scss' import './styles.scss' interface IProps { episode?: any + podcast?: any onPlay?: any onPause?: any onCanPlay?: any @@ -19,10 +22,12 @@ interface IProps { export default class Player extends React.Component { static defaultProps = { - preload: "none" + preload: 'none', } state = { playing: false, + satAmount: 100, + boostagram: '', } player = React.createRef() @@ -36,21 +41,21 @@ export default class Player extends React.Component { } onCanPlay() { - if (this.props.onCanPlay){ + if (this.props.onCanPlay) { this.props.onCanPlay() } } - play(){ + play() { this.player.current.audio.current.play() } - pause(){ + pause() { this.player.current.audio.current.pause() } onPlay() { - if (this.props.onPlay){ + if (this.props.onPlay) { this.props.onPlay() } this.setState({ @@ -59,7 +64,7 @@ export default class Player extends React.Component { } onPause() { - if (this.props.onPause){ + if (this.props.onPause) { this.props.onPause() } this.setState({ @@ -68,75 +73,217 @@ export default class Player extends React.Component { } componentDidMount() { - this.setMediaSessionActionHandlers(); + this.setMediaSessionActionHandlers() } componentDidUpdate() { - this.setMediaSessionMetadata(); + this.setMediaSessionMetadata() } setMediaSessionActionHandlers() { - let navigator: any = window.navigator; - if('mediaSession' in window.navigator) { - navigator.mediaSession.setActionHandler('seekbackward', () => this.seekBackward()); - navigator.mediaSession.setActionHandler('seekforward', () => this.seekForward()); + let navigator: any = window.navigator + if ('mediaSession' in window.navigator) { + navigator.mediaSession.setActionHandler('seekbackward', () => + this.seekBackward() + ) + navigator.mediaSession.setActionHandler('seekforward', () => + this.seekForward() + ) } } seekBackward() { - let audio = this.player.current.audio.current; - audio.currentTime = Math.max(0, audio.currentTime - 5); + let audio = this.player.current.audio.current + audio.currentTime = Math.max(0, audio.currentTime - 5) } seekForward() { - let audio = this.player.current.audio.current; - audio.currentTime = Math.min(audio.duration, audio.currentTime + 5); + let audio = this.player.current.audio.current + audio.currentTime = Math.min(audio.duration, audio.currentTime + 5) } setMediaSessionMetadata() { - let navigator: any = window.navigator; - let episode = this.props.episode; - let image: string = episode.image || episode.feedImage; - if(episode && 'mediaSession' in window.navigator) { + let navigator: any = window.navigator + let episode = this.props.episode + let image: string = episode.image || episode.feedImage + if (episode && 'mediaSession' in window.navigator) { // @ts-ignore navigator.mediaSession.metadata = new MediaMetadata({ title: episode.title, artist: episode.feedTitle, - ...(image && - { - artwork: [{ src: image, sizes: '512x512'}] - } - ) - }); + ...(image && { + artwork: [{ src: image, sizes: '512x512' }], + }), + }) } } + handleSatChange = (e: any) => { + this.setState({ + satAmount: e.target.validity.valid + ? e.target.value + : this.state.satAmount, + }) + } + + handleTextAreaChange = (e: any) => { + this.setState({ + boostagram: e.target.value, + }) + } + + boost = async () => { + const { episode, podcast } = this.props + let webln + let destinations = + episode?.value?.destinations || podcast?.value?.destinations + + const getBaseRecord = () => { + return { + podcast: podcast?.title, + feedID: podcast?.id, + itemID: episode?.id, + episode: episode?.title, + ts: Math.trunc(this.player.current.audio.current.currentTime), + action: 'boost', + app_name: 'Podcast Index', + value_msat: 0, + value_msat_total: this.state.satAmount * 1000, + name: undefined, + message: this.state.boostagram, + } + } + + let feesDestinations = destinations.filter((v) => v.fee) + let splitsDestinations = destinations.filter((v) => !v.fee) + let runningTotal = this.state.satAmount + + try { + webln = await requestProvider() + } catch (err) { + // Tell the user what went wrong + alert( + `${err.message} \r\n Try using Alby ( https://getalby.com/ ) on the Desktop \r\n or installing Blue Wallet ( https://bluewallet.io/ ) \r\n or Blixt Wallet ( https://blixtwallet.github.io/ ) \r\n on your mobile device.` + ) + } + + if (webln) { + this.throwConfetti() + for (const dest of feesDestinations) { + let feeRecord = getBaseRecord() + + let amount = Math.round( + (dest.split / 100) * this.state.satAmount + ) + if (amount) { + runningTotal -= amount + feeRecord.name = dest.name + feeRecord.value_msat = amount * 1000 + + let customRecords = { '7629169': JSON.stringify(feeRecord) } + + if (dest.customKey) { + customRecords[dest.customKey] = dest.customValue + } + + try { + await webln.keysend({ + destination: dest.address, + amount: amount, + customRecords: customRecords, + }) + } catch (err) { + alert(`error with ${dest.name}: ${err.message}`) + } + } + } + + for (const dest of splitsDestinations) { + let record = getBaseRecord() + let amount = Math.round((dest.split / 100) * runningTotal) + record.name = dest.name + record.value_msat = amount * 1000 + if (amount >= 1) { + let customRecords = { '7629169': JSON.stringify(record) } + if (dest.customKey) { + customRecords[dest.customKey] = dest.customValue + } + + try { + await webln.keysend({ + destination: dest.address, + amount: amount, + customRecords: customRecords, + }) + } catch (err) { + alert(`error with ${dest.name}: ${err.message}`) + } + } + } + } + } + + throwConfetti() { + let end = Date.now() + 0.1 * 1000 + + let colors = [ + '#fa6060', + '#faa560', + '#faf760', + '#b2fa60', + '#60c1fa', + '#7260fa', + '#fa60f2', + ] + + ;(function frame() { + confetti({ + particleCount: 12, + angle: 60, + spread: 75, + origin: { x: 0, y: 0.9 }, + colors: colors, + }) + confetti({ + particleCount: 12, + angle: 120, + spread: 75, + origin: { x: 1, y: 0.9 }, + colors: colors, + }) + + if (Date.now() < end) { + requestAnimationFrame(frame) + } + })() + } + render() { - const {episode, preload} = this.props + const { episode, preload } = this.props //See if a pciguid exists in local storage. They are stored using a hash of the enclosure url as the key to avoid //character encoding issues with what browsers accept as a valid key. If the value exists, get it. If not, create //a new on and store it for potential use later if this enclosure is played again by this user let enclosureHash = sha256(episode.enclosureUrl) - var pciStatsGuid = localStorage.getItem(enclosureHash) - if(pciStatsGuid === null) { - pciStatsGuid = uuidv4(); + let pciStatsGuid = localStorage.getItem(enclosureHash) + if (pciStatsGuid === null) { + pciStatsGuid = uuidv4() localStorage.setItem(enclosureHash, pciStatsGuid) } //Attach the pciguid value to the end of the enclosure url as a query parameter to pass back to the host/cdn for //anonymous, yet reliable tracking stats - var pciGuid = "" - if(episode.enclosureUrl.indexOf('?') > -1) { + let pciGuid = '' + if (episode.enclosureUrl.indexOf('?') > -1) { pciGuid = '&_ulid=' + pciStatsGuid } else { pciGuid = '?_ulid=' + pciStatsGuid } //Tag a _from on the end to give a stats hint - var fromTag = "&_from=podcastindex.org"; - if(episode.enclosureUrl.indexOf('_from=') > -1) { - fromTag = ""; + let fromTag = '&_from=podcastindex.org' + if (episode.enclosureUrl.indexOf('_from=') > -1) { + fromTag = '' } //Assemble the new url @@ -152,15 +299,21 @@ export default class Player extends React.Component {

{episode.title}

- {episode.feedTitle !== undefined ? - + {episode.feedTitle !== undefined ? ( + {`from: ${episode.feedTitle}`} - : "" - } + ) : ( + '' + )}

-

@@ -178,13 +331,30 @@ export default class Player extends React.Component { {/* */} , ]} - /> +
+