cache sharing for posts. Begin infill implementation.
This commit is contained in:
@@ -2,55 +2,20 @@ import { createTRPCRouter, publicProcedure } from "../utils";
|
||||
import { ConnectionFactory } from "~/server/utils";
|
||||
import { withCacheAndStale } from "~/server/cache";
|
||||
import { incrementPostReadSchema } from "../schemas/blog";
|
||||
import type { Post, PostWithCommentsAndLikes } from "~/db/types";
|
||||
import type { PostWithCommentsAndLikes } from "~/db/types";
|
||||
|
||||
const BLOG_CACHE_TTL = 24 * 60 * 60 * 1000; // 24 hours
|
||||
|
||||
export const blogRouter = createTRPCRouter({
|
||||
getRecentPosts: publicProcedure.query(async () => {
|
||||
return withCacheAndStale("blog-recent-posts", BLOG_CACHE_TTL, async () => {
|
||||
// Get database connection
|
||||
// Shared cache function for all blog posts
|
||||
const getAllPostsData = async (privilegeLevel: string) => {
|
||||
return withCacheAndStale(
|
||||
`blog-posts-${privilegeLevel}`,
|
||||
BLOG_CACHE_TTL,
|
||||
async () => {
|
||||
const conn = ConnectionFactory();
|
||||
|
||||
// Query for the 3 most recent published posts
|
||||
const query = `
|
||||
SELECT
|
||||
p.id,
|
||||
p.title,
|
||||
p.subtitle,
|
||||
p.date,
|
||||
p.published,
|
||||
p.category,
|
||||
p.author_id,
|
||||
p.banner_photo,
|
||||
p.reads,
|
||||
COUNT(DISTINCT pl.user_id) as total_likes,
|
||||
COUNT(DISTINCT c.id) as total_comments
|
||||
FROM Post p
|
||||
LEFT JOIN PostLike pl ON p.id = pl.post_id
|
||||
LEFT JOIN Comment c ON p.id = c.post_id
|
||||
WHERE p.published = TRUE
|
||||
GROUP BY p.id, p.title, p.subtitle, p.date, p.published, p.category, p.author_id, p.reads
|
||||
ORDER BY p.date DESC
|
||||
LIMIT 3;
|
||||
`;
|
||||
|
||||
const results = await conn.execute(query);
|
||||
return results.rows as unknown as PostWithCommentsAndLikes[];
|
||||
});
|
||||
}),
|
||||
|
||||
getPosts: publicProcedure.query(async ({ ctx }) => {
|
||||
const privilegeLevel = ctx.privilegeLevel;
|
||||
|
||||
return withCacheAndStale(
|
||||
`blog-posts-${privilegeLevel}`,
|
||||
BLOG_CACHE_TTL,
|
||||
async () => {
|
||||
const conn = ConnectionFactory();
|
||||
|
||||
// Fetch all posts with aggregated data
|
||||
let postsQuery = `
|
||||
// Fetch all posts with aggregated data
|
||||
let postsQuery = `
|
||||
SELECT
|
||||
p.id,
|
||||
p.title,
|
||||
@@ -70,17 +35,17 @@ export const blogRouter = createTRPCRouter({
|
||||
LEFT JOIN Comment c ON p.id = c.post_id
|
||||
`;
|
||||
|
||||
if (privilegeLevel !== "admin") {
|
||||
postsQuery += ` WHERE p.published = TRUE`;
|
||||
}
|
||||
if (privilegeLevel !== "admin") {
|
||||
postsQuery += ` WHERE p.published = TRUE`;
|
||||
}
|
||||
|
||||
postsQuery += ` GROUP BY p.id, p.title, p.subtitle, p.body, p.banner_photo, p.date, p.published, p.category, p.author_id, p.reads, p.attachments`;
|
||||
postsQuery += ` ORDER BY p.date ASC;`;
|
||||
postsQuery += ` GROUP BY p.id, p.title, p.subtitle, p.body, p.banner_photo, p.date, p.published, p.category, p.author_id, p.reads, p.attachments`;
|
||||
postsQuery += ` ORDER BY p.date DESC;`;
|
||||
|
||||
const postsResult = await conn.execute(postsQuery);
|
||||
const posts = postsResult.rows as unknown as PostWithCommentsAndLikes[];
|
||||
const postsResult = await conn.execute(postsQuery);
|
||||
const posts = postsResult.rows as unknown as PostWithCommentsAndLikes[];
|
||||
|
||||
const tagsQuery = `
|
||||
const tagsQuery = `
|
||||
SELECT t.value, t.post_id
|
||||
FROM Tag t
|
||||
JOIN Post p ON t.post_id = p.id
|
||||
@@ -88,21 +53,35 @@ export const blogRouter = createTRPCRouter({
|
||||
ORDER BY t.value ASC
|
||||
`;
|
||||
|
||||
const tagsResult = await conn.execute(tagsQuery);
|
||||
const tags = tagsResult.rows as unknown as {
|
||||
value: string;
|
||||
post_id: number;
|
||||
}[];
|
||||
const tagsResult = await conn.execute(tagsQuery);
|
||||
const tags = tagsResult.rows as unknown as {
|
||||
value: string;
|
||||
post_id: number;
|
||||
}[];
|
||||
|
||||
const tagMap: Record<string, number> = {};
|
||||
tags.forEach((tag) => {
|
||||
const key = `${tag.value}`;
|
||||
tagMap[key] = (tagMap[key] || 0) + 1;
|
||||
});
|
||||
const tagMap: Record<string, number> = {};
|
||||
tags.forEach((tag) => {
|
||||
const key = `${tag.value}`;
|
||||
tagMap[key] = (tagMap[key] || 0) + 1;
|
||||
});
|
||||
|
||||
return { posts, tags, tagMap, privilegeLevel };
|
||||
}
|
||||
);
|
||||
return { posts, tags, tagMap, privilegeLevel };
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
export const blogRouter = createTRPCRouter({
|
||||
getRecentPosts: publicProcedure.query(async ({ ctx }) => {
|
||||
// Always use public privilege level for recent posts (only show published)
|
||||
const allPostsData = await getAllPostsData("public");
|
||||
|
||||
// Return only the 3 most recent posts (already sorted DESC by date)
|
||||
return allPostsData.posts.slice(0, 3);
|
||||
}),
|
||||
|
||||
getPosts: publicProcedure.query(async ({ ctx }) => {
|
||||
const privilegeLevel = ctx.privilegeLevel;
|
||||
return getAllPostsData(privilegeLevel);
|
||||
}),
|
||||
|
||||
incrementPostRead: publicProcedure
|
||||
|
||||
33
src/server/api/routers/infill.ts
Normal file
33
src/server/api/routers/infill.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { publicProcedure, createTRPCRouter } from "~/server/api/utils";
|
||||
import { env } from "~/env/server";
|
||||
|
||||
// Helper to detect mobile devices from User-Agent
|
||||
const isMobileDevice = (userAgent: string | undefined): boolean => {
|
||||
if (!userAgent) return false;
|
||||
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
|
||||
userAgent
|
||||
);
|
||||
};
|
||||
|
||||
export const infillRouter = createTRPCRouter({
|
||||
getConfig: publicProcedure.query(({ ctx }) => {
|
||||
// Only admins get the config
|
||||
if (ctx.privilegeLevel !== "admin") {
|
||||
return { endpoint: null, token: null };
|
||||
}
|
||||
|
||||
// Get User-Agent from request headers
|
||||
const userAgent = ctx.event.nativeEvent.node.req.headers["user-agent"];
|
||||
|
||||
// Block mobile devices - infill is desktop only
|
||||
if (isMobileDevice(userAgent)) {
|
||||
return { endpoint: null, token: null };
|
||||
}
|
||||
|
||||
// Return endpoint and token (or null if not configured)
|
||||
return {
|
||||
endpoint: env.VITE_INFILL_ENDPOINT || null,
|
||||
token: env.INFILL_BEARER_TOKEN || null
|
||||
};
|
||||
})
|
||||
});
|
||||
Reference in New Issue
Block a user