2023-02-01 07:45:18 +00:00
|
|
|
import {
|
|
|
|
|
WALLABAG_CLIENT_ID,
|
|
|
|
|
WALLABAG_CLIENT_SECRET,
|
|
|
|
|
WALLABAG_USERNAME,
|
|
|
|
|
WALLABAG_PASSWORD,
|
2023-02-10 00:25:21 +00:00
|
|
|
WALLABAG_URL,
|
2023-02-13 06:30:22 +00:00
|
|
|
PAGE_SIZE,
|
|
|
|
|
USE_REDIS_CACHE
|
2023-02-01 07:45:18 +00:00
|
|
|
} from '$env/static/private';
|
2023-02-04 07:23:00 +00:00
|
|
|
import intersect from 'just-intersect';
|
2023-12-15 23:13:59 +00:00
|
|
|
import type { Article, ArticlePageLoad, WallabagArticle } from '$lib/types/article';
|
2023-11-12 22:57:07 +00:00
|
|
|
import { ArticleTag } from '$lib/types/articleTag';
|
|
|
|
|
import type { PageQuery } from '$lib/types/pageQuery';
|
2023-02-01 07:45:18 +00:00
|
|
|
import { URLSearchParams } from 'url';
|
2023-11-12 22:57:07 +00:00
|
|
|
import { redis } from '$lib/server/redis';
|
2023-02-01 07:45:18 +00:00
|
|
|
|
|
|
|
|
const base: string = WALLABAG_URL;
|
|
|
|
|
|
|
|
|
|
export async function fetchArticlesApi(
|
|
|
|
|
method: string,
|
|
|
|
|
resource: string,
|
2024-05-30 19:34:50 +00:00
|
|
|
queryParams: Record<string, string>
|
2023-02-01 07:45:18 +00:00
|
|
|
) {
|
2024-05-30 19:03:03 +00:00
|
|
|
let perPage = Number(queryParams?.limit);
|
|
|
|
|
if (perPage > 30) {
|
|
|
|
|
perPage = Number(PAGE_SIZE);
|
|
|
|
|
} else {
|
|
|
|
|
perPage = Number(queryParams?.limit);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 06:30:22 +00:00
|
|
|
const pageQuery: PageQuery = {
|
|
|
|
|
sort: 'updated',
|
2024-05-30 19:03:03 +00:00
|
|
|
perPage,
|
2023-02-13 06:30:22 +00:00
|
|
|
since: 0,
|
2024-05-30 19:03:03 +00:00
|
|
|
page: Number(queryParams?.page) || 1,
|
2023-02-13 06:30:22 +00:00
|
|
|
tags: 'programming',
|
|
|
|
|
content: 'metadata'
|
|
|
|
|
};
|
2023-02-16 01:04:54 +00:00
|
|
|
const entriesQueryParams = new URLSearchParams({
|
|
|
|
|
...pageQuery,
|
|
|
|
|
perPage: `${pageQuery.perPage}`,
|
|
|
|
|
since: `${pageQuery.since}`,
|
|
|
|
|
page: `${pageQuery.page}`
|
|
|
|
|
});
|
|
|
|
|
|
2023-02-13 06:30:22 +00:00
|
|
|
if (USE_REDIS_CACHE) {
|
|
|
|
|
const cached = await redis.get(entriesQueryParams.toString());
|
|
|
|
|
|
|
|
|
|
if (cached) {
|
|
|
|
|
const response = JSON.parse(cached);
|
|
|
|
|
const ttl = await redis.ttl(entriesQueryParams.toString());
|
|
|
|
|
|
|
|
|
|
return { ...response, cacheControl: `max-age=${ttl}` };
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-01 07:45:18 +00:00
|
|
|
|
|
|
|
|
const authBody = {
|
|
|
|
|
grant_type: 'password',
|
|
|
|
|
client_id: WALLABAG_CLIENT_ID,
|
|
|
|
|
client_secret: WALLABAG_CLIENT_SECRET,
|
|
|
|
|
username: WALLABAG_USERNAME,
|
|
|
|
|
password: WALLABAG_PASSWORD
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const authResponse = await fetch(`${base}/oauth/v2/token`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
|
|
|
body: new URLSearchParams(authBody)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const auth = await authResponse.json();
|
|
|
|
|
|
2023-02-03 06:40:02 +00:00
|
|
|
const pageResponse = await fetch(`${WALLABAG_URL}/api/entries.json?${entriesQueryParams}`, {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${auth.access_token}`
|
2023-02-01 07:45:18 +00:00
|
|
|
}
|
2023-02-03 06:40:02 +00:00
|
|
|
});
|
2023-02-01 07:45:18 +00:00
|
|
|
|
|
|
|
|
if (!pageResponse.ok) {
|
|
|
|
|
throw new Error(pageResponse.statusText);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-15 23:13:59 +00:00
|
|
|
const cacheControl = pageResponse.headers.get('cache-control') || 'no-cache';
|
2023-02-12 22:07:30 +00:00
|
|
|
|
2024-05-30 19:03:03 +00:00
|
|
|
const { _embedded: favoriteArticles, page, pages, total, limit } = await pageResponse.json();
|
2023-02-03 06:40:02 +00:00
|
|
|
const articles: Article[] = [];
|
2023-02-01 07:45:18 +00:00
|
|
|
|
2024-05-30 19:03:03 +00:00
|
|
|
favoriteArticles.items.forEach((article: WallabagArticle) => {
|
2023-02-04 07:23:00 +00:00
|
|
|
const rawTags = article?.tags?.map((tag) => tag.slug);
|
|
|
|
|
if (intersect(rawTags, Object.values(ArticleTag))?.length > 0) {
|
|
|
|
|
const tags = rawTags.map((rawTag) => rawTag as unknown as ArticleTag);
|
|
|
|
|
articles.push({
|
|
|
|
|
tags,
|
|
|
|
|
title: article.title,
|
2023-02-14 06:54:54 +00:00
|
|
|
url: new URL(article.url),
|
2024-05-30 19:03:03 +00:00
|
|
|
domain_name: article.domain_name?.replace('www.', '') ?? '',
|
2023-02-04 07:23:00 +00:00
|
|
|
hashed_url: article.hashed_url,
|
|
|
|
|
reading_time: article.reading_time,
|
|
|
|
|
preview_picture: article.preview_picture,
|
|
|
|
|
created_at: new Date(article.created_at),
|
|
|
|
|
updated_at: new Date(article.updated_at),
|
|
|
|
|
archived_at: article.archived_at ? new Date(article.archived_at) : null
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-02-03 06:40:02 +00:00
|
|
|
});
|
2023-02-01 07:45:18 +00:00
|
|
|
|
2023-12-15 23:13:59 +00:00
|
|
|
const responseData: ArticlePageLoad = {
|
2023-02-12 22:07:30 +00:00
|
|
|
articles,
|
|
|
|
|
currentPage: page,
|
2024-05-30 19:03:03 +00:00
|
|
|
totalPages: pages,
|
2023-02-12 22:07:30 +00:00
|
|
|
limit,
|
2024-05-30 19:03:03 +00:00
|
|
|
totalArticles: total,
|
2023-02-12 22:07:30 +00:00
|
|
|
cacheControl
|
|
|
|
|
};
|
2023-02-13 06:30:22 +00:00
|
|
|
|
|
|
|
|
if (USE_REDIS_CACHE) {
|
|
|
|
|
redis.set(entriesQueryParams.toString(), JSON.stringify(responseData), 'EX', 43200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return responseData;
|
2023-02-01 07:45:18 +00:00
|
|
|
}
|