use nested fields to avoid tons of extra SS api calls
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 32s Details

This commit is contained in:
Ladd Hoffman 2024-04-20 22:27:38 -05:00
parent 93136716b8
commit deb2c3c4de
1 changed files with 5 additions and 6 deletions

View File

@ -34,7 +34,8 @@ const getContract = (name) => new ethers.Contract(
);
const fetchPaperInfo = async (paperId, retryDelay = 5000) => {
const url = `https://api.semanticscholar.org/graph/v1/paper/${paperId}?fields=title,url,authors,references`;
const url = `https://api.semanticscholar.org/graph/v1/paper/${paperId}?`
+ 'fields=title,url,authors,references.title,references.url,references.authors';
let retry = false;
let paper;
const response = await axios.get(url, {
@ -146,19 +147,15 @@ module.exports = async (req, res) => {
// Read the paper info from SS
const paper = await fetchPaperInfo(paperId);
// Send HTTP code 202 - Accepted
res.status(202).end();
console.log('references count:', paper.references.length);
const eachCitationWeightPercent = Math.floor(30 / paper.references.length);
const citations = await Promise.mapSeries(
paper.references.filter((x) => !!x.paperId),
async ({ paperId: citedPaperId }) => {
async (citedPaper) => {
// We need to fetch this paper so we can generate the post we WOULD add to the forum.
// That way, if we later add the cited paper to the blockchain it will have the correct hash.
// The forum allows dangling citations to support this use case.
const citedPaper = await fetchPaperInfo(citedPaperId);
const citedPost = await generatePost(citedPaper);
return {
weightPercent: eachCitationWeightPercent,
@ -187,10 +184,12 @@ module.exports = async (req, res) => {
} catch (e) {
if (e.reason === 'A post with this contentId already exists') {
console.log(`Post already added for paper ${paperId}`);
res.status(204).end();
return;
}
throw e;
}
console.log(`Added post to blockchain for paper ${paperId}`);
res.status(201).end();
};