build view count

This commit is contained in:
2026-03-28 22:09:30 -05:00
parent d3fb42e8ed
commit 98e5a5a740
5 changed files with 16 additions and 6 deletions

View File

@@ -70,6 +70,10 @@ if (!memesCols.find((c) => c.name === 'share_count')) {
db.exec('ALTER TABLE memes ADD COLUMN share_count INTEGER NOT NULL DEFAULT 0');
}
if (!memesCols.find((c) => c.name === 'view_count')) {
db.exec('ALTER TABLE memes ADD COLUMN view_count INTEGER NOT NULL DEFAULT 0');
}
// Indexes that depend on migrated columns — created after columns are guaranteed to exist
db.exec(`
CREATE INDEX IF NOT EXISTS idx_memes_collection_id ON memes(collection_id);

View File

@@ -31,6 +31,9 @@ export async function shareRoutes(app: FastifyInstance) {
return reply.status(404).send('Not found');
}
// Count every visit to the share page as a link click
db.prepare('UPDATE memes SET view_count = view_count + 1 WHERE id = ?').run(meme.id);
const base = getBaseUrl(req as any);
const pageUrl = `${base}/m/${meme.id}`;
const imageUrl = `${base}/images/${meme.file_path}`;

View File

@@ -12,6 +12,7 @@ export interface Meme {
collection_id: number | null;
ocr_text: string | null;
share_count: number;
view_count: number;
created_at: string;
tags: string[];
}