Skip to content

Commit

Permalink
Top and New profile page
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDaub committed Sep 13, 2023
1 parent 3baa674 commit 0fc2bd3
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 194 deletions.
9 changes: 9 additions & 0 deletions src/http.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,19 @@ export async function launch(trie, libp2p) {
return reply.status(200).type("text/html").send(join(reply.locals.theme));
});
app.get("/upvotes", async (request, reply) => {
let mode = "top";
if (request.query.mode === "new") mode = "new";

let page = parseInt(request.query.page);
if (isNaN(page) || page < 1) {
page = 0;
}
const content = await upvotes(
trie,
reply.locals.theme,
request.query.address,
page,
mode,
);
return reply.status(200).type("text/html").send(content);
});
Expand Down
6 changes: 3 additions & 3 deletions src/views/components/secondheader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const html = htm.bind(vhtml);

const style = "width: 1rem; position: relative; top: 0.15rem;";

const broadcastSVG = html`
export const broadcastSVG = html`
<svg
style="${style}"
xmlns="http://www.w3.org/2000/svg"
Expand Down Expand Up @@ -58,7 +58,7 @@ const broadcastSVG = html`
</svg>
`;

const trophySVG = html`
export const trophySVG = html`
<svg
style="${style}"
xmlns="http://www.w3.org/2000/svg"
Expand Down Expand Up @@ -114,7 +114,7 @@ const trophySVG = html`
</svg>
`;

const fireSVG = html`
export const fireSVG = html`
<svg
style="${style}"
xmlns="http://www.w3.org/2000/svg"
Expand Down
17 changes: 8 additions & 9 deletions src/views/feed.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,15 @@ const calculateScore = (votes, itemHourAge, gravity = 1.8) => {
return (votes - 1) / Math.pow(itemHourAge + 2, gravity);
};

async function topstories(leaves, start, end) {
export async function topstories(leaves, minimalUpvotes = 2) {
return count(leaves)
.filter((story) => story.upvotes > 2)
.filter((story) => story.upvotes > minimalUpvotes)
.map((story) => {
const score = calculateScore(story.upvotes, itemAge(story.timestamp));
story.score = score;
return story;
})
.sort((a, b) => b.score - a.score)
.slice(start, end);
.sort((a, b) => b.score - a.score);
}

async function editors(leaves) {
Expand Down Expand Up @@ -153,13 +152,13 @@ async function editors(leaves) {
}

export default async function index(trie, theme, page) {
const aWeekAgo = sub(new Date(), {
weeks: 1,
const lookBack = sub(new Date(), {
weeks: 3,
});
const from = null;
const amount = null;
const parser = JSON.parse;
const aWeekAgoUnixTime = Math.floor(aWeekAgo.getTime() / 1000);
const lookBackUnixTime = Math.floor(lookBack.getTime() / 1000);
const allowlist = await registry.allowlist();
const delegations = await registry.delegations();

Expand All @@ -168,7 +167,7 @@ export default async function index(trie, theme, page) {
from,
amount,
parser,
aWeekAgoUnixTime,
lookBackUnixTime,
allowlist,
delegations,
);
Expand All @@ -183,7 +182,7 @@ export default async function index(trie, theme, page) {
const totalStories = parseInt(env.TOTAL_STORIES, 10);
const start = totalStories * page;
const end = totalStories * (page + 1);
const storyPromises = await topstories(leaves, start, end);
const storyPromises = (await topstories(leaves)).slice(start, end);

let stories = [];
for await (let story of storyPromises) {
Expand Down
Loading

0 comments on commit 0fc2bd3

Please sign in to comment.