Skip to content

Commit

Permalink
add: logsnag event tracking for article creation/deletion/publication
Browse files Browse the repository at this point in the history
  • Loading branch information
matfire committed Jan 20, 2024
1 parent 8a5b719 commit a593514
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/routes/api/articles/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@ import { fail, json, redirect } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { db } from '$lib/server/db';
import { userArticles } from '$lib/server/drizzle';
import { env } from '$env/dynamic/private';
import { LogSnag } from '@logsnag/node';

export const POST: RequestHandler = async ({ locals, request }) => {
const session = await locals.auth.validate();
if (!session) throw redirect(302, '/login');
const { LOGSNAG_PROJECT, LOGSNAG_TOKEN } = env;
const { content, iv } = Object.fromEntries(await request.formData());
if (!content || !iv) throw fail(500, { message: 'invalid data' });
const data = await db
.insert(userArticles)
.values({ author: session.user.userId, content: content.toString(), iv: iv.toString() });
if (LOGSNAG_PROJECT && LOGSNAG_TOKEN) {
const logsnag = new LogSnag({ token: LOGSNAG_TOKEN, project: LOGSNAG_PROJECT });
await logsnag.track({
channel: 'articles',
event: 'New Article',
user_id: session.user.userId
});
}
return json({ id: Number(data.lastInsertRowid!) });
};
11 changes: 11 additions & 0 deletions src/routes/api/articles/[id]/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { db } from '$lib/server/db';
import { userArticles } from '$lib/server/drizzle';
import { fail, json, type RequestHandler } from '@sveltejs/kit';
import { eq } from 'drizzle-orm';
import { env } from '$env/dynamic/private';
import { LogSnag } from '@logsnag/node';

export const PUT: RequestHandler = async ({ locals, params, request }) => {
const session = await locals.auth.validate();
Expand Down Expand Up @@ -31,5 +33,14 @@ export const DELETE: RequestHandler = async ({ locals, params }) => {
if (article[0].author !== session.user.userId)
throw fail(500, { message: 'invalid permissions' });
await db.delete(userArticles).where(eq(userArticles.id, parseInt(params.id!)));
const { LOGSNAG_PROJECT, LOGSNAG_TOKEN } = env;
if (LOGSNAG_PROJECT && LOGSNAG_TOKEN) {
const logsnag = new LogSnag({ token: LOGSNAG_TOKEN, project: LOGSNAG_PROJECT });
await logsnag.track({
channel: 'articles',
event: 'Deleted Article',
user_id: session.user.userId
});
}
return json({ message: 'deleted' });
};
12 changes: 12 additions & 0 deletions src/routes/api/articles/publish/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import { userPublications } from '$lib/server/drizzle';
import { fail, json, type RequestHandler } from '@sveltejs/kit';
import { eq } from 'drizzle-orm';
import fm from 'front-matter';
import { env } from '$env/dynamic/private';
import { LogSnag } from '@logsnag/node';

//TODO parallelize posts
export const POST: RequestHandler = async ({ locals, request }) => {
const session = await locals.auth.validate();
const { LOGSNAG_PROJECT, LOGSNAG_TOKEN } = env;
if (!session) {
throw fail(500, { message: 'not authenticad' });
}
Expand Down Expand Up @@ -37,6 +40,15 @@ export const POST: RequestHandler = async ({ locals, request }) => {
res.set(publisher.name, 'ko');
}
}
if (LOGSNAG_PROJECT && LOGSNAG_TOKEN) {
const logsnag = new LogSnag({ token: LOGSNAG_TOKEN, project: LOGSNAG_PROJECT });
await logsnag.track({
channel: 'articles',
event: 'Published Article',
user_id: session.user.userId,
tags: { platforms: res.size }
});
}
return json({
message: 'finished',
status: Array.from(res).map(([key, val]) => `${key}: ${val}`)
Expand Down

0 comments on commit a593514

Please sign in to comment.