Skip to content

Commit

Permalink
feat: add p2p URL support across all content in the reader
Browse files Browse the repository at this point in the history
  • Loading branch information
akhileshthite committed May 25, 2024
1 parent 21e6e8d commit 02385bb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
3 changes: 2 additions & 1 deletion actor-mini-profile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { db } from './dbInstance.js'
import { resolveP2PUrl } from './db.js'

class ActorMiniProfile extends HTMLElement {
static get observedAttributes () {
Expand Down Expand Up @@ -50,7 +51,7 @@ class ActorMiniProfile extends HTMLElement {
// Actor icon
const img = document.createElement('img')
img.className = 'profile-mini-icon'
img.src = iconUrl
img.src = resolveP2PUrl(iconUrl)
img.alt = actorInfo.name ? actorInfo.name : 'Actor icon'
clickableContainer.appendChild(img)

Expand Down
3 changes: 2 additions & 1 deletion actor-profile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { db } from './dbInstance.js'
import { resolveP2PUrl } from './db.js'

class ActorProfile extends HTMLElement {
static get observedAttributes () {
Expand Down Expand Up @@ -52,7 +53,7 @@ class ActorProfile extends HTMLElement {

const img = document.createElement('img')
img.classList.add('profile-icon')
img.src = iconUrl
img.src = resolveP2PUrl(iconUrl)
img.alt = actorInfo.name ? actorInfo.name : 'Actor icon'
actorContainer.appendChild(img) // Append to the actor container

Expand Down
14 changes: 14 additions & 0 deletions db.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ export function isP2P (url) {
return url.startsWith(HYPER_PREFIX) || url.startsWith(IPNS_PREFIX)
}

export function resolveP2PUrl (url) {
if (!url) return url

if (url.startsWith(HYPER_PREFIX)) {
return url.replace(HYPER_PREFIX, 'https://hyper.hypha.coop/hyper/')
} else if (url.startsWith(IPNS_PREFIX)) {
return url.replace(IPNS_PREFIX, 'https://ipfs.hypha.coop/ipns/')
}

return url
}

export class ActivityPubDB extends EventTarget {
constructor (db, fetch = globalThis.fetch) {
super()
Expand Down Expand Up @@ -103,6 +115,8 @@ export class ActivityPubDB extends EventTarget {
if (url && typeof url === 'object') {
return url
}
url = resolveP2PUrl(url)

let response
// Try fetching directly for all URLs (including P2P URLs)
// TODO: Signed fetch
Expand Down
17 changes: 16 additions & 1 deletion post.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* global customElements, HTMLElement */
import DOMPurify from './dependencies/dompurify/purify.js'
import { db } from './dbInstance.js'
import { resolveP2PUrl } from './db.js'

function formatDate (dateString) {
const options = { year: 'numeric', month: 'short', day: 'numeric' }
Expand Down Expand Up @@ -54,6 +55,7 @@ class DistributedPost extends HTMLElement {
this.renderErrorContent('No post URL provided')
return
}
postUrl = resolveP2PUrl(postUrl)

try {
const content = await db.getNote(postUrl)
Expand Down Expand Up @@ -120,6 +122,19 @@ class DistributedPost extends HTMLElement {
const parser = new DOMParser()
const contentDOM = parser.parseFromString(sanitizedContent, 'text/html')

const images = contentDOM.querySelectorAll('img')
images.forEach(img => {
const src = img.getAttribute('src')
console.log('Original src:', src)
img.setAttribute('src', resolveP2PUrl(src))
})

const videos = contentDOM.querySelectorAll('video source')
videos.forEach(video => {
const src = video.getAttribute('src')
video.setAttribute('src', resolveP2PUrl(src))
})

// Process all anchor elements to handle actor and posts mentions
const anchors = contentDOM.querySelectorAll('a')
anchors.forEach(async (anchor) => {
Expand Down Expand Up @@ -346,7 +361,7 @@ class ActorInfo extends HTMLElement {

const img = document.createElement('img')
img.classList.add('actor-icon')
img.src = iconUrl
img.src = resolveP2PUrl(iconUrl)
img.alt = actorInfo.name ? actorInfo.name : 'Actor icon'
img.addEventListener('click', this.navigateToActorProfile.bind(this))
author.appendChild(img)
Expand Down

0 comments on commit 02385bb

Please sign in to comment.