Skip to content

Commit

Permalink
Skip old news
Browse files Browse the repository at this point in the history
  • Loading branch information
arildm committed Apr 24, 2024
1 parent e2b2855 commit bf1b290
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/home/news.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Yaml = import("js-yaml").then((m) => m.default);
import once from "lodash/once";
import { retry } from "@/util";
import { addDays, retry } from "@/util";
import type { ByLang } from "@/util.types";

const NEWS_URL: string = import.meta.env.VITE_NEWS_URL;
Expand Down Expand Up @@ -40,10 +40,11 @@ export const fetchAllNews = once(async (): Promise<NewsItem[]> => {
* @throws Fetch or parse errors
*/
export async function fetchNews(filterFeatured?: boolean): Promise<NewsItem[]> {
// Get news and skip items that have expired
// TODO Also skip items older than 6 months
const now = new Date();
let items = (await fetchAllNews()).filter(
(item) => !item.expires || item.expires > new Date(),
(item) =>
(!item.expires || item.expires > now) && // Skip expired items
item.created > addDays(now, -365 / 2), // Skip items older than 6 months
);

// Filter by the "featured" tag if specified
Expand Down
14 changes: 14 additions & 0 deletions src/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, test } from "vitest";
import {
addDays,
enarray,
ensureExtension,
formatDate,
Expand All @@ -15,6 +16,19 @@ import {
unarray,
} from "@/util";

describe("addDays", () => {
const d = new Date("24 April 2024 15:50 (CEST)");
test("across a month", () => {
expect(addDays(d, 10)).toEqual(new Date("4 May 2024 15:50 (CEST)"));
expect(addDays(d, -24)).toEqual(new Date("31 March 2024 15:50 (CEST)"));
});
test("across DST", () => {
// Notice the hour is different
expect(addDays(d, 200)).toEqual(new Date("10 November 2024 14:50 (CET)"));
expect(addDays(d, -25)).toEqual(new Date("30 March 2024 14:50 (CET)"));
});
});

describe("enarray", () => {
test("leaves array unchanged", () => {
expect(enarray([1, 2, 3])).toStrictEqual([1, 2, 3]);
Expand Down
12 changes: 12 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import clone from "lodash/clone";
import round from "lodash/round";

/** The number of milliseconds in a full day. */
const DAY_MS = 24 * 60 * 60 * 1000;

/**
* Add or subtract a number of days to a date
*
* Works with 24-hour cycles, so the hour (and possibly date) will change across DST changes.
**/
export function addDays(date: Date, days: number) {
return new Date(date.getTime() + days * DAY_MS);
}

export const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));

/** Trigger a file download in the browser by adding a temporary link and click it */
Expand Down

0 comments on commit bf1b290

Please sign in to comment.