Skip to content

Commit

Permalink
Add metadata to ad entry
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDaub committed Oct 31, 2024
1 parent 649e2b9 commit a6085ae
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions src/views/feed.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ async function getAd() {
const [price, taxes] = await contract.price();
const submitter = await ens.resolve(identity);

return {
const post = {
upvotes: 0,
upvoters: [],
avatars: [],
Expand All @@ -217,6 +217,11 @@ async function getAd() {
displayName: submitter.displayName,
timestamp,
};
const augmentedPost = await addMetadata(post);
if (augmentedPost) {
post = augmentedPost;
}
return post;
}

const itemAge = (timestamp) => {
Expand Down Expand Up @@ -271,6 +276,21 @@ export async function topstories(leaves) {
.sort((a, b) => b.score - a.score);
}

async function addMetadata(post) {
let result;
try {
result = await metadata(post.href);
} catch (err) {
return null;
}
if (result && !result.image) return;

return {
...post,
metadata: result,
};
}

export async function index(trie, page, domain) {
const lookback = sub(new Date(), {
weeks: 3,
Expand Down Expand Up @@ -304,21 +324,6 @@ export async function index(trie, page, domain) {
// noop
}

async function addMetadata(post) {
let result;
try {
result = await metadata(post.href);
} catch (err) {
return null;
}
if (result && !result.image) return;

return {
...post,
metadata: result,
};
}

async function resolveIds(storyPromises) {
const stories = [];
for await (let story of storyPromises) {
Expand Down Expand Up @@ -375,8 +380,9 @@ export async function index(trie, page, domain) {
if (cache.get(adCacheKey)) {
ad = cache.get(adCacheKey);
} else {
const adTTLSeconds = 60 * 5;
getAd()
.then((result) => cache.set(adCacheKey, result))
.then((result) => cache.set(adCacheKey, result, [adTTLSeconds]))
.catch((err) => log(`Err in getAd: ${err.stack}`));
}

Expand Down

0 comments on commit a6085ae

Please sign in to comment.