Skip to content

Commit

Permalink
Add Year button to /best
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDaub committed Sep 20, 2024
1 parent 48fee12 commit 520719c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
24 changes: 15 additions & 9 deletions src/http.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ export async function launch(trie, libp2p) {
page = 0;
}

const periodValues = ["all", "month", "week", "day"];
const periodValues = ["all", "year", "month", "week", "day"];
let { period } = request.query;
if (!period || !periodValues.includes(period)) {
period = "week";
Expand Down Expand Up @@ -645,19 +645,25 @@ export async function launch(trie, libp2p) {
page = 0;
}

const periodValues = ["all", "month", "week", "day"];
const periodValues = ["all", "year", "month", "week", "day"];
let { period } = request.query;
if (!period || !periodValues.includes(period)) {
period = "week";
}

const content = await best(
trie,
reply.locals.theme,
page,
period,
DOMPurify.sanitize(request.query.domain),
);
let content;
try {
content = await best(
trie,
reply.locals.theme,
page,
period,
DOMPurify.sanitize(request.query.domain),
);
} catch (err) {
log(`Error rendering /best ${err.stack}`);
return reply.status(500).send("Internal Server Error");
}

reply.header(
"Cache-Control",
Expand Down
34 changes: 11 additions & 23 deletions src/views/best.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { URL } from "url";
import htm from "htm";
import vhtml from "vhtml";
import normalizeUrl from "normalize-url";
import { sub, differenceInMinutes, differenceInSeconds } from "date-fns";
import {
startOfYear,
sub,
differenceInMinutes,
differenceInSeconds,
} from "date-fns";
import DOMPurify from "isomorphic-dompurify";

import * as ens from "../ens.mjs";
Expand All @@ -29,7 +34,9 @@ async function getStories(trie, page, period, domain) {
let startDatetime = 0;
const unix = (date) => Math.floor(date.getTime() / 1000);
const now = new Date();
if (period === "month") {
if (period === "year") {
startDatetime = unix(startOfYear(now));
} else if (period === "month") {
startDatetime = unix(sub(now, { months: 1 }));
} else if (period === "week") {
startDatetime = unix(sub(now, { weeks: 1 }));
Expand All @@ -42,34 +49,15 @@ async function getStories(trie, page, period, domain) {
const orderBy = null;
const result = getBest(totalStories, from, orderBy, domain, startDatetime);

let writers = [];
try {
writers = await moderation.getWriters();
} catch (err) {
// noop
}

let stories = [];
for await (let story of result) {
const ensData = await ens.resolve(story.identity);

let avatars = [];
for await (let upvoter of story.upvoters) {
const profile = await ens.resolve(upvoter);
if (profile.safeAvatar) {
avatars.push(profile.safeAvatar);
}
}
const isOriginal = Object.keys(writers).some(
(domain) =>
normalizeUrl(story.href).startsWith(domain) &&
writers[domain] === story.identity,
);
stories.push({
...story,
displayName: ensData.displayName,
avatars: avatars,
isOriginal,
avatars: [],
isOriginal: false,
});
}
return stories;
Expand Down
5 changes: 5 additions & 0 deletions src/views/components/secondheader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ const secondheader = (theme, site, period, domain) => {
<span>All</span>
</button>
</a>
<a href="/best?period=year${domain ? `&domain=${domain}` : ""}">
<button style="${periodIconStyle(theme, period, "year")}">
<span>Year</span>
</button>
</a>
<a href="/best?period=month${domain ? `&domain=${domain}` : ""}">
<button style="${periodIconStyle(theme, period, "month")}">
<span>Month</span>
Expand Down

0 comments on commit 520719c

Please sign in to comment.