Display the saved links on a page

This commit is contained in:
Lewis Dale committed 2024-03-01 10:44:51 +00:00
1 parent 051b32a983
commit d491653fff
6 files changed
+133 -65

No files matched your search

+94
View File
@@ -0,0 +1,94 @@
const fs = require('node:fs');
const apiKey = 'bf7b63c4-b6f2-4568-b554-4a3440460070';
const cacheFile = '.cache/links.json';
const query = `query Search($after: String, $first: Int, $query: String, $format: String, $includeContent: Boolean) {
search(after: $after, first: $first, query: $query, format: $format, includeContent: $includeContent) {
... on SearchSuccess {
edges {
cursor
node {
title
slug
url
createdAt
isArchived
author
image
description
publishedAt
originalArticleUrl
labels {
id
name
color
}
siteName
savedAt
}
}
pageInfo {
hasNextPage
hasPreviousPage
startCursor
endCursor
totalCount
}
}
... on SearchError {
errorCodes
}
}
}
`;
const cachedContent = () => {
if (fs.existsSync(cacheFile)) {
return JSON.parse(fs.readFileSync(cacheFile));
}
return [];
}
const storeCache = (content = []) => {
fs.writeFileSync(cacheFile, JSON.stringify(content));
}
const fetchLatest = async (cache = []) => {
const after = cache[0]?.cursor;
const variables = {
"query": "label:Tech",
"first": 100,
after,
}
const results = await fetch('https://api-prod.omnivore.app/api/graphql', {
method: 'POST',
headers: {
'content-type': 'application/json',
'authorization': apiKey,
},
body: JSON.stringify({
query,
variables
})
});
if (results.ok) {
const {data} = await results.json();
if (data.search.edges.length) {
const links = [...data.search.edges, ...cache];
storeCache(links);
return links;
}
} else {
console.error(await results.text())
}
return cache;
}
module.exports = async function() {
return await fetchLatest(cachedContent());
}
-46
View File
@@ -1,46 +0,0 @@
const postCache = require('./_postData');
const dateSort = (a, b) => new Date(b.date) - new Date(a.date);
const mapComment = comment => {
return ({
author: {
name: comment.author_name,
avatars: comment.author_avatar_urls,
url: comment.author_url,
},
content: comment.content.rendered,
canonical: comment.meta.url,
date: comment.date,
});
}
module.exports = async () => {
// const cache = await postCache();
// const posts = Object.values(cache.posts)
// .sort(dateSort)
// .map(post => ({
// ...post,
// comments: Object.values(post.comments)
// .sort(dateSort)
// .reduce((comments, comment) => {
// if (!comments[comment.type]) {
// comments[comment.type] = [];
// }
// comments[comment.type].push(mapComment(comment));
// return comments;
// }, { like: [], reply: [], repost: [] }),
// tags: post.tags.map(tag => ({
// name: cache.tags[tag].name,
// slug: cache.tags[tag].slug,
// link: cache.tags[tag].link
// }))
// }));
// return posts;
return [];
};
-17
View File
@@ -1,17 +0,0 @@
const postCache = require('./_postData');
module.exports = async () => {
// const cache = await postCache();
// return Object.values(cache.tags).map(tag => {
// const posts = Object.values(cache.posts).filter(post => post.tags.includes(tag.id)).sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
// return {
// ...tag,
// posts,
// }
// });
return [];
};