-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add p2p URL support across all content in the reader #20
Open
akhileshthite
wants to merge
5
commits into
main
Choose a base branch
from
resolve-p2p
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
02385bb
feat: add p2p URL support across all content in the reader
akhileshthite 80b9a00
Merge branch 'main' into resolve-p2p
akhileshthite 675329f
fix: resolve p2p URLs before rendering to handle null src attributes
akhileshthite 58366b8
feat: implement media loading with p2p-image and p2p-video components
akhileshthite b5f6370
feat: implement dynamic CSS loading for p2p-media custom elements to …
akhileshthite File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { supportsP2P, resolveP2PUrl } from './db.js' | ||
|
||
class P2PImage extends HTMLElement { | ||
constructor () { | ||
super() | ||
|
||
this.img = document.createElement('img') | ||
this.attachShadow({ mode: 'open' }) | ||
this.shadowRoot.appendChild(this.img) | ||
this.img.addEventListener('error', () => this.handleError()) | ||
} | ||
|
||
static get observedAttributes () { | ||
return ['src'] | ||
} | ||
|
||
connectedCallback () { | ||
if (this.hasAttribute('src')) { | ||
this.loadImage(this.getAttribute('src')) | ||
} | ||
} | ||
|
||
attributeChangedCallback (name, oldValue, newValue) { | ||
if (name === 'src' && newValue !== oldValue) { | ||
this.loadImage(newValue) | ||
} | ||
} | ||
|
||
async loadImage (src) { | ||
try { | ||
const p2pSupported = await supportsP2P(src) | ||
if (p2pSupported) { | ||
this.img.src = src // Attempt to load the original P2P URL | ||
} else { | ||
this.handleError() | ||
} | ||
} catch (error) { | ||
this.handleError() | ||
} | ||
} | ||
|
||
async handleError () { | ||
const fallbackSrc = resolveP2PUrl(this.getAttribute('src')) | ||
console.log(`Failed to load image. Resolving to gateway URL: ${fallbackSrc}`) | ||
this.img.src = fallbackSrc | ||
} | ||
} | ||
|
||
customElements.define('p2p-image', P2PImage) | ||
|
||
class P2PVideo extends HTMLElement { | ||
constructor () { | ||
super() | ||
this.video = document.createElement('video') | ||
this.video.controls = true | ||
this.attachShadow({ mode: 'open' }) | ||
this.shadowRoot.appendChild(this.video) | ||
} | ||
|
||
static get observedAttributes () { | ||
return ['src'] | ||
} | ||
|
||
connectedCallback () { | ||
if (this.hasAttribute('src')) { | ||
this.loadVideo(this.getAttribute('src')) | ||
} | ||
} | ||
|
||
attributeChangedCallback (name, oldValue, newValue) { | ||
if (name === 'src' && newValue !== oldValue) { | ||
this.loadVideo(newValue) | ||
} | ||
} | ||
|
||
async loadVideo (src) { | ||
this.video.innerHTML = '' // Clear any existing sources | ||
const source = document.createElement('source') | ||
source.src = src | ||
|
||
try { | ||
const p2pSupported = await supportsP2P(src) | ||
if (!p2pSupported) { | ||
throw new Error('P2P not supported for this URL') | ||
} | ||
} catch (error) { | ||
this.handleError(source) | ||
return // Skip setting the source if not supported | ||
} | ||
|
||
source.onerror = () => this.handleError(source) | ||
this.video.appendChild(source) | ||
} | ||
|
||
handleError (source) { | ||
const fallbackSrc = resolveP2PUrl(source.src) | ||
console.log(`Failed to load video source. Resolving to gateway URL: ${fallbackSrc}`) | ||
source.src = fallbackSrc | ||
} | ||
} | ||
|
||
customElements.define('p2p-video', P2PVideo) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ | |
<script type="module" src="./sidebar.js"></script> | ||
<script type="module" src="./actor-profile.js"></script> | ||
<script type="module" src="./post.js"></script> | ||
<script type="module" src="./p2p-media.js"></script> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should have post.js import any of it's dependencies directly instead of doing it top level. Similarly outbox should import post. |
||
<script type="module" src="./outbox.js"></script> | ||
<script type="module" src="./followed-accounts.js"></script> | ||
<script type="module" src="./theme-selector.js"></script> | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead, check if the current
img.src
is p2p