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
import Arweave from "arweave"; | |
type Post = { | |
authorship: { | |
contributor: string; | |
}; | |
content: { | |
body: string; | |
timestamp: string; | |
title: string; | |
}; | |
digest: string; | |
originalDigest: string; | |
}; | |
const arweave = Arweave.init({ | |
host: "arweave.net", | |
port: 443, | |
protocol: "https", | |
}); | |
async function getPostsForContributor(address: string): Promise<Post[]> { | |
const arweaveTransactionIds = await arweave.transactions.search( | |
"Contributor", | |
address | |
); | |
const arweaveTransactions = await Promise.all( | |
arweaveTransactionIds.map((txId) => arweave.transactions.get(txId)) | |
); | |
const postsByOriginalDigest: Record<string, Post> = {}; | |
for (const transaction of arweaveTransactions) { | |
const tags: Record<string, string> = {}; | |
for (const tag of transaction.tags) { | |
const name = tag.get("name", { decode: true, string: true }); | |
const value = tag.get("value", { decode: true, string: true }); | |
tags[name] = value; | |
} | |
const appName = tags["App-Name"]; | |
if (appName !== "MirrorXYZ") { | |
continue; | |
} | |
const originalDigest = tags["Original-Content-Digest"]; | |
if (postsByOriginalDigest[originalDigest]) { | |
continue; | |
} | |
const rawData = transaction.get("data", { decode: true, string: true }); | |
postsByOriginalDigest[originalDigest] = { | |
...JSON.parse(rawData), | |
originalDigest, | |
}; | |
} | |
return Object.values(postsByOriginalDigest); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment