Fix imports, remove unnecessary imports, and replace all single apostrophe to double apostrophes
330 lines
12 KiB
TypeScript
330 lines
12 KiB
TypeScript
import { createDirectusConnection } from "@/lib/directus";
|
|
import { print } from "graphql";
|
|
import getBlogs from "@/graphql/blogs/getBlogs.graphql";
|
|
import getBlogPost from "@/graphql/blogs/getBlog.graphql";
|
|
import getLastBlogPosts from "@/graphql/blogs/getLastBlogPosts.graphql";
|
|
import getPaginatedBlogs from "@/graphql/blogs/getPaginatedBlogs.graphql";
|
|
import { formatDate } from "@/lib/dates";
|
|
import { getImageSize, getImageUrl } from "@/lib/images";
|
|
import { getImage } from "astro:assets";
|
|
|
|
export async function getAllBlogs(settings: GlobalSettings): Promise<BlogPost[]> {
|
|
const client = await createDirectusConnection();
|
|
const result = await client.query(print(getBlogs), {
|
|
date: formatDate(new Date(), "%Y-%M-%D")
|
|
});
|
|
|
|
let blogs: BlogPost[] = [];
|
|
|
|
result["Blogs"].forEach((blogRecord: any) => {
|
|
let dates: string[] = [
|
|
settings.blog.lastModified.toISOString(),
|
|
settings.website.lastModified.toISOString(),
|
|
blogRecord["date_created"],
|
|
blogRecord["date_updated"],
|
|
blogRecord["search_engine"][0]["date_created"],
|
|
blogRecord["search_engine"][0]["date_updated"],
|
|
blogRecord["search_engine"][0]["thumbnail"]["created_on"]
|
|
];
|
|
|
|
const blogThumbnailImage =
|
|
getImageSize(blogRecord["search_engine"][0]["thumbnail"]["width"], blogRecord["search_engine"][0]["thumbnail"]["height"], 0.756);
|
|
|
|
const blog: BlogPost = {
|
|
exists: true,
|
|
type: "BlogPost",
|
|
id: blogRecord["id"],
|
|
lastModified: new Date(),
|
|
title: blogRecord["title"],
|
|
content: blogRecord["content"],
|
|
date: blogRecord["date"],
|
|
url: blogRecord["url"],
|
|
thumbnail: {
|
|
url: blogRecord["search_engine"][0]["thumbnail"]["filename_disk"],
|
|
width: blogRecord["search_engine"][0]["thumbnail"]["width"],
|
|
height: blogRecord["search_engine"][0]["thumbnail"]["height"]
|
|
},
|
|
searchEngine: {
|
|
title: blogRecord["search_engine"][0]["title"],
|
|
description: blogRecord["search_engine"][0]["description"],
|
|
allowCrawlers: blogRecord["search_engine"][0]["allow_crawler"],
|
|
canonical: blogRecord["search_engine"][0]["canonical"],
|
|
priority: blogRecord["search_engine"][0]["priority"],
|
|
thumbnail: {
|
|
url: blogRecord["search_engine"][0]["thumbnail"]["filename_disk"],
|
|
width: blogThumbnailImage.width,
|
|
height: blogThumbnailImage.height
|
|
}
|
|
},
|
|
tags: []
|
|
};
|
|
|
|
blogRecord["tags"].forEach((tagRecord: any) => {
|
|
blog["tags"].push({
|
|
text: tagRecord["Tags_id"]["text"],
|
|
code: tagRecord["Tags_id"]["code"],
|
|
color: tagRecord["Tags_id"]["color"]
|
|
});
|
|
|
|
dates.push(tagRecord["Tags_id"]["date_created"]);
|
|
dates.push(tagRecord["Tags_id"]["date_updated"]);
|
|
});
|
|
|
|
if (dates.filter(e => e !== null).length === 0) {
|
|
blog.lastModified = new Date();
|
|
}
|
|
else {
|
|
const sortedDates: string[] = dates.sort((a: string, b: string) => {
|
|
return new Date(b).getTime() - new Date(a).getTime();
|
|
});
|
|
|
|
blog.lastModified = new Date(sortedDates[0]);
|
|
}
|
|
|
|
blogs.push(blog);
|
|
});
|
|
|
|
return blogs;
|
|
}
|
|
|
|
export async function getBlog(settings: GlobalSettings, route: string): Promise<BlogPost> {
|
|
const client = await createDirectusConnection();
|
|
const result = await client.query(print(getBlogPost), {
|
|
route: route
|
|
});
|
|
|
|
const blogRecord = result["Blogs"][0];
|
|
|
|
let dates: string[] = [
|
|
settings.blog.lastModified.toISOString(),
|
|
settings.website.lastModified.toISOString(),
|
|
blogRecord["date_created"],
|
|
blogRecord["date_updated"],
|
|
blogRecord["search_engine"][0]["date_created"],
|
|
blogRecord["search_engine"][0]["date_updated"],
|
|
blogRecord["search_engine"][0]["thumbnail"]["created_on"]
|
|
];
|
|
|
|
const blogThumbnailImage = getImageSize(blogRecord["search_engine"][0]["thumbnail"]["width"],
|
|
blogRecord["search_engine"][0]["thumbnail"]["height"], 0.756);
|
|
|
|
const thumbnail = await getImage({
|
|
src: getImageUrl(blogRecord["search_engine"][0]["thumbnail"]["filename_disk"]),
|
|
width: blogThumbnailImage.width,
|
|
height: blogThumbnailImage.height,
|
|
format: "jpeg"
|
|
});
|
|
|
|
const blog: BlogPost = {
|
|
exists: true,
|
|
type: "BlogPost",
|
|
id: blogRecord["id"],
|
|
lastModified: new Date(),
|
|
title: blogRecord["title"],
|
|
content: blogRecord["content"],
|
|
date: blogRecord["date"],
|
|
url: blogRecord["url"],
|
|
thumbnail: {
|
|
url: blogRecord["search_engine"][0]["thumbnail"]["filename_disk"],
|
|
width: blogRecord["search_engine"][0]["thumbnail"]["width"],
|
|
height: blogRecord["search_engine"][0]["thumbnail"]["height"]
|
|
},
|
|
searchEngine: {
|
|
title: blogRecord["search_engine"][0]["title"],
|
|
description: blogRecord["search_engine"][0]["description"],
|
|
allowCrawlers: blogRecord["search_engine"][0]["allow_crawler"],
|
|
canonical: blogRecord["search_engine"][0]["canonical"],
|
|
priority: blogRecord["search_engine"][0]["priority"],
|
|
thumbnail: {
|
|
url: `${settings.website.domainName}${thumbnail.src}`,
|
|
width: blogThumbnailImage.width,
|
|
height: blogThumbnailImage.height
|
|
}
|
|
},
|
|
tags: []
|
|
};
|
|
|
|
blogRecord["tags"].forEach((tagRecord: any) => {
|
|
blog["tags"].push({
|
|
text: tagRecord["Tags_id"]["text"],
|
|
code: tagRecord["Tags_id"]["code"],
|
|
color: tagRecord["Tags_id"]["color"]
|
|
});
|
|
|
|
dates.push(tagRecord["Tags_id"]["date_created"]);
|
|
dates.push(tagRecord["Tags_id"]["date_updated"]);
|
|
});
|
|
|
|
if (dates.filter(e => e !== null).length === 0) {
|
|
blog.lastModified = new Date();
|
|
}
|
|
else {
|
|
const sortedDates: string[] = dates.sort((a: string, b: string) => {
|
|
return new Date(b).getTime() - new Date(a).getTime();
|
|
});
|
|
|
|
blog.lastModified = new Date(sortedDates[0]);
|
|
}
|
|
|
|
return blog;
|
|
}
|
|
|
|
export async function getLastBlogs(amount: number): Promise<BlogPost[]> {
|
|
const client = await createDirectusConnection();
|
|
const result = await client.query(print(getLastBlogPosts), {
|
|
date: formatDate(new Date(), "%Y-%M-%D"),
|
|
amount: amount
|
|
});
|
|
|
|
let blogs: BlogPost[] = [];
|
|
|
|
result["Blogs"].forEach((blogRecord: any) => {
|
|
let dates: string[] = [
|
|
blogRecord["date_created"],
|
|
blogRecord["date_updated"],
|
|
blogRecord["search_engine"][0]["date_created"],
|
|
blogRecord["search_engine"][0]["date_updated"],
|
|
blogRecord["search_engine"][0]["thumbnail"]["created_on"]
|
|
];
|
|
|
|
const blogThumbnailImage =
|
|
getImageSize(blogRecord["search_engine"][0]["thumbnail"]["width"], blogRecord["search_engine"][0]["thumbnail"]["height"], 0.756);
|
|
|
|
const blog: BlogPost = {
|
|
exists: true,
|
|
type: "BlogPost",
|
|
id: blogRecord["id"],
|
|
lastModified: new Date(),
|
|
title: blogRecord["title"],
|
|
content: blogRecord["content"],
|
|
date: blogRecord["date"],
|
|
url: blogRecord["url"],
|
|
thumbnail: {
|
|
url: blogRecord["search_engine"][0]["thumbnail"]["filename_disk"],
|
|
width: blogRecord["search_engine"][0]["thumbnail"]["width"],
|
|
height: blogRecord["search_engine"][0]["thumbnail"]["height"]
|
|
},
|
|
searchEngine: {
|
|
title: blogRecord["search_engine"][0]["title"],
|
|
description: blogRecord["search_engine"][0]["description"],
|
|
allowCrawlers: blogRecord["search_engine"][0]["allow_crawler"],
|
|
canonical: blogRecord["search_engine"][0]["canonical"],
|
|
priority: blogRecord["search_engine"][0]["priority"],
|
|
thumbnail: {
|
|
url: blogRecord["search_engine"][0]["thumbnail"]["filename_disk"],
|
|
width: blogThumbnailImage.width,
|
|
height: blogThumbnailImage.height
|
|
}
|
|
},
|
|
tags: []
|
|
};
|
|
|
|
blogRecord["tags"].forEach((tagRecord: any) => {
|
|
blog["tags"].push({
|
|
text: tagRecord["Tags_id"]["text"],
|
|
code: tagRecord["Tags_id"]["code"],
|
|
color: tagRecord["Tags_id"]["color"]
|
|
});
|
|
|
|
dates.push(tagRecord["Tags_id"]["date_created"]);
|
|
dates.push(tagRecord["Tags_id"]["date_updated"]);
|
|
});
|
|
|
|
if (dates.filter(e => e !== null).length === 0) {
|
|
blog.lastModified = new Date();
|
|
}
|
|
else {
|
|
const sortedDates: string[] = dates.sort((a: string, b: string) => {
|
|
return new Date(b).getTime() - new Date(a).getTime();
|
|
});
|
|
|
|
blog.lastModified = new Date(sortedDates[0]);
|
|
}
|
|
|
|
blogs.push(blog);
|
|
});
|
|
|
|
return blogs;
|
|
}
|
|
|
|
export async function getAllPaginatedBlogs(settings: GlobalSettings, page: number): Promise<BlogPost[]> {
|
|
const client = await createDirectusConnection();
|
|
const result = await client.query(print(getPaginatedBlogs), {
|
|
date: formatDate(new Date(), "%Y-%M-%D"),
|
|
limit: 8,
|
|
pageNumber: page
|
|
});
|
|
|
|
let blogs: BlogPost[] = [];
|
|
|
|
result["Blogs"].forEach((blogRecord: any) => {
|
|
let dates: string[] = [
|
|
settings.blog.lastModified.toISOString(),
|
|
settings.website.lastModified.toISOString(),
|
|
blogRecord["date_created"],
|
|
blogRecord["date_updated"],
|
|
blogRecord["search_engine"][0]["date_created"],
|
|
blogRecord["search_engine"][0]["date_updated"],
|
|
blogRecord["search_engine"][0]["thumbnail"]["created_on"]
|
|
];
|
|
|
|
const blogThumbnailImage =
|
|
getImageSize(blogRecord["search_engine"][0]["thumbnail"]["width"], blogRecord["search_engine"][0]["thumbnail"]["height"], 0.756);
|
|
|
|
const blog: BlogPost = {
|
|
exists: true,
|
|
type: "BlogPost",
|
|
id: blogRecord["id"],
|
|
lastModified: new Date(),
|
|
title: blogRecord["title"],
|
|
content: blogRecord["content"],
|
|
date: blogRecord["date"],
|
|
url: blogRecord["url"],
|
|
thumbnail: {
|
|
url: blogRecord["search_engine"][0]["thumbnail"]["filename_disk"],
|
|
width: blogRecord["search_engine"][0]["thumbnail"]["width"],
|
|
height: blogRecord["search_engine"][0]["thumbnail"]["height"]
|
|
},
|
|
searchEngine: {
|
|
title: blogRecord["search_engine"][0]["title"],
|
|
description: blogRecord["search_engine"][0]["description"],
|
|
allowCrawlers: blogRecord["search_engine"][0]["allow_crawler"],
|
|
canonical: blogRecord["search_engine"][0]["canonical"],
|
|
priority: blogRecord["search_engine"][0]["priority"],
|
|
thumbnail: {
|
|
url: blogRecord["search_engine"][0]["thumbnail"]["filename_disk"],
|
|
width: blogThumbnailImage.width,
|
|
height: blogThumbnailImage.height
|
|
}
|
|
},
|
|
tags: []
|
|
};
|
|
|
|
blogRecord["tags"].forEach((tagRecord: any) => {
|
|
blog["tags"].push({
|
|
text: tagRecord["Tags_id"]["text"],
|
|
code: tagRecord["Tags_id"]["code"],
|
|
color: tagRecord["Tags_id"]["color"]
|
|
});
|
|
|
|
dates.push(tagRecord["Tags_id"]["date_created"]);
|
|
dates.push(tagRecord["Tags_id"]["date_updated"]);
|
|
});
|
|
|
|
if (dates.filter(e => e !== null).length === 0) {
|
|
blog.lastModified = new Date();
|
|
}
|
|
else {
|
|
const sortedDates: string[] = dates.sort((a: string, b: string) => {
|
|
return new Date(b).getTime() - new Date(a).getTime();
|
|
});
|
|
|
|
blog.lastModified = new Date(sortedDates[0]);
|
|
}
|
|
|
|
blogs.push(blog);
|
|
});
|
|
|
|
return blogs;
|
|
}
|