Skip to content

Commit

Permalink
Add caching for nouns votes and onchain ad
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDaub committed Oct 24, 2024
1 parent e6f348c commit 45146eb
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function handleMessage(

try {
const index = null;
submission = await getSubmission(index, message.href);
submission = getSubmission(index, message.href);
} catch (err) {
// NOTE: We can ignore the error here if it's being thrown
}
Expand Down
6 changes: 3 additions & 3 deletions src/cache.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ export function getLastComment(submissionId) {
};
}

export async function getSubmission(index, href, identityFilter, hrefs) {
export function getSubmission(index, href, identityFilter, hrefs) {
let submission;
if (index) {
submission = db
Expand Down Expand Up @@ -596,9 +596,9 @@ export async function getSubmission(index, href, identityFilter, hrefs) {
// NOTE: When I tried using a map and Promise allSettled to parallelize,
// the ethers component of identityFilter was constantly failing with call
// exceptions. So when I serialized this call it started working.
for await (const upvoter of upvoters) {
for (const upvoter of upvoters) {
try {
const validated = await identityFilter(upvoter, submission.identity);
const validated = identityFilter(upvoter, submission.identity);
validatedUpvoters.push(validated);
} catch (err) {}
}
Expand Down
2 changes: 1 addition & 1 deletion src/http.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ export async function launch(trie, libp2p) {

const index = request.query.index;
try {
submission = await getSubmission(index);
submission = getSubmission(index);
} catch (e) {
const code = 404;
const httpMessage = "Not Found";
Expand Down
2 changes: 1 addition & 1 deletion src/subscriptions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export async function triggerNotification(message) {
if (message.type !== "comment") return;

const [_, index] = message.href.split("kiwi:");
const submission = await getSubmission(index);
const submission = getSubmission(index);

const ensData = await resolve(message.identity);
if (!ensData.displayName) return;
Expand Down
49 changes: 27 additions & 22 deletions src/views/feed.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,20 @@ const html = htm.bind(vhtml);
// NOTE: Only set this date in synchronicity with the src/launch.mjs date!!
const cutoffDate = new Date("2024-10-12");
const thresholdKarma = 3;
export async function identityClassifier(upvoter) {
export function identityClassifier(upvoter) {
let votes = 0;
try {
votes = await getNounsVotes(upvoter.identity);
} catch (err) {
// noop

const cacheKey = `nouns-votes-${upvoter.identity}`;
if (cache.has(cacheKey)) {
votes = cache.get(cacheKey);
} else {
try {
getNounsVotes(upvoter.identity)
.then((votesNumber) => cache.set(cacheKey, votesNumber))
.catch((err) => log(`Error in getNounsVotes: ${err.stack}`));
} catch (err) {
// noop
}
}
const karmaScore = karma.resolve(upvoter.identity, cutoffDate);
return {
Expand All @@ -56,11 +64,11 @@ export async function identityClassifier(upvoter) {
isKiwi: karmaScore > thresholdKarma,
};
}
export async function identityFilter(upvoter, submitter) {
export function identityFilter(upvoter, submitter) {
if (upvoter === submitter) {
return upvoter;
}
upvoter = await identityClassifier(upvoter);
upvoter = identityClassifier(upvoter);
if (upvoter.isNoun || upvoter.isKiwi) {
return upvoter;
}
Expand All @@ -69,11 +77,6 @@ export async function identityFilter(upvoter, submitter) {

const provider = new ethers.providers.JsonRpcProvider(env.RPC_HTTP_HOST);
export async function getNounsVotes(address) {
const cacheKey = `nouns-votes-${address}`;
if (cache.has(cacheKey)) {
return cache.get(cacheKey);
}

const contractAddress = "0x9c8ff314c9bc7f6e59a9d9225fb22946427edc03";
const abi = [
{
Expand All @@ -89,7 +92,6 @@ export async function getNounsVotes(address) {
const votes = await contract.getCurrentVotes(address);

const votesNumber = votes.toNumber();
cache.set(cacheKey, votesNumber);
return votesNumber;
}

Expand All @@ -104,14 +106,9 @@ export async function getContestStories() {
return [];
}

const promises = result.links.map((href) =>
getSubmission(null, href, identityFilter),
);
let submissions = await Promise.allSettled(promises);
submissions = submissions
.filter((elem) => elem.status === "fulfilled")
.map((elem) => {
const submission = elem.value;
const submissions = result.links
.map((href) => getSubmission(null, href, identityFilter))
.map((submission) => {
submission.upvoters = submission.upvoters.map(({ identity }) => identity);
return submission;
})
Expand Down Expand Up @@ -369,7 +366,15 @@ export async function index(trie, page, domain) {
.map(({ value }) => value)
.slice(0, 2);

const ad = await getAd();
let ad;
const adCacheKey = "ad-cache-key";
if (cache.get(adCacheKey)) {
ad = cache.get(adCacheKey);
} else {
getAd()
.then((result) => cache.set(adCacheKey, result))
.catch((err) => log(`Err in getAd: ${err.stack}`));
}

const contestStories = await getContestStories();
const resolvedContestStories = await resolveIds(contestStories);
Expand Down
7 changes: 1 addition & 6 deletions src/views/story.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,7 @@ export async function generateStory(index) {

let submission;
try {
submission = await getSubmission(
index,
null,
identityClassifier,
result.links,
);
submission = getSubmission(index, null, identityClassifier, result.links);
} catch (err) {
log(
`Requested index "${index}" but didn't find because of error "${err.toString()}"`,
Expand Down

0 comments on commit 45146eb

Please sign in to comment.