Skip to content

Commit

Permalink
Refactor old feed page code
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDaub committed Oct 23, 2024
1 parent 83736e8 commit e6f348c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 63 deletions.
34 changes: 23 additions & 11 deletions src/http.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -425,18 +425,26 @@ export async function launch(trie, libp2p) {
if (isNaN(page) || page < 1) {
page = 0;
}
const results = await index(trie, page);

let results;
try {
results = await index(trie, page);
} catch (err) {
log(`Error in api/v1/feeds/hot: ${err.stack}`);
}
reply.header(
"Cache-Control",
"public, s-maxage=300, max-age=300, must-revalidate, stale-while-revalidate=30",
);
stories = results.stories;
} else if (request.params.name === "new") {
reply.header("Cache-Control", "no-cache");
stories = newAPI.getStories();
} else if (request.params.name === "images") {
reply.header("Cache-Control", "no-cache");
stories = imagesAPI.getStories();

try {
stories = newAPI.getStories();
} catch (err) {
log(`Error in api/v1/feeds/new: ${err.stack}`);
}
} else if (request.params.name === "best") {
reply.header(
"Cache-Control",
Expand All @@ -454,12 +462,16 @@ export async function launch(trie, libp2p) {
period = "week";
}

stories = await bestAPI.getStories(
trie,
page,
period,
request.query.domain,
);
try {
stories = await bestAPI.getStories(
trie,
page,
period,
request.query.domain,
);
} catch (err) {
log(`error in /api/v1/feeds/best: ${err.stack}`);
}
} else {
const code = 501;
const httpMessage = "Not Implemented";
Expand Down
57 changes: 6 additions & 51 deletions src/views/feed.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ function meanUpvoteRatio(leaves) {
return ratios.length > 0 ? sumRatios / ratios.length : 0;
}

export async function topstories(leaves, decayStrength) {
export async function topstories(leaves) {
const upvoteRatio = meanUpvoteRatio(leaves);
return leaves
.map((story) => {
Expand Down Expand Up @@ -277,39 +277,16 @@ export async function index(trie, page, domain) {
const lookbackUnixTime = Math.floor(lookback.getTime() / 1000);
const limit = -1;
let leaves = listNewest(limit, lookbackUnixTime);

const policy = await moderation.getLists();
const path = "/";
leaves = moderation.moderate(leaves, policy, path);

const totalStories = parseInt(env.TOTAL_STORIES, 10);
const parameters = await moderation.getFeedParameters();
let storyPromises = await topstories(leaves, parameters.decayStrength);

let threshold = 2;
let pill = true;
const now = new Date();
const old = sub(now, { hours: parameters.oldHours });
const oldInMinutes = differenceInMinutes(now, old);
const { fold } = parameters;
do {
const sample = storyPromises.filter(({ upvotes }) => upvotes > threshold);
const sum = sample.slice(0, fold).reduce((acc, { timestamp }) => {
const submissionTime = new Date(timestamp * 1000);
const diff = differenceInMinutes(now, submissionTime);
return acc + diff;
}, 0);
const averageAgeInMinutes = sum / fold;
if (averageAgeInMinutes > oldInMinutes) {
threshold--;
pill = false;
continue;
} else {
threshold++;
}
} while (pill);

log(`Feed threshold for upvotes ${threshold}`);
let storyPromises = await topstories(leaves);
const threshold = 1;
storyPromises = storyPromises.filter(({ upvotes }) => upvotes > threshold);

if (domain)
storyPromises = storyPromises.filter(
({ href }) => extractDomain(href) === domain,
Expand Down Expand Up @@ -429,34 +406,12 @@ const expandSVG = html`<svg
/>
</svg>`;

const pages = {};

export default async function (trie, theme, page, domain) {
const mints = await registry.mints();
const path = "/";
const totalStories = parseInt(env.TOTAL_STORIES, 10);

const key = `${page}-${domain}`;
let cacheRes = pages[key];
let content;

let maxAgeInSeconds = 60 * 60 * 24;
if (page === 0 && !domain) maxAgeInSeconds = 25;
if (page > 0 && page < 5 && !domain) maxAgeInSeconds = 60 * 5;

if (
!cacheRes ||
(cacheRes &&
differenceInSeconds(new Date(), cacheRes.age) > maxAgeInSeconds)
) {
content = await index(trie, page, domain);
pages[key] = {
content,
age: new Date(),
};
} else {
content = cacheRes.content;
}
const content = await index(trie, page, domain);
const { ad, originals, stories, start, contestStories } = content;

let query = `?page=${page + 1}`;
Expand Down
2 changes: 1 addition & 1 deletion src/views/moderation.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { fetchCache } from "../utils.mjs";

const cache = new FileSystemCache({
cacheDirectory: path.resolve(env.CACHE_DIR),
ttl: 60000 * 5, // 5min
ttl: 60000 * 60, // 60min
});
const fetch = fetchBuilder.withCache(cache);
const fetchStaleWhileRevalidate = fetchCache(fetch, cache);
Expand Down

0 comments on commit e6f348c

Please sign in to comment.