1import { updateLastPosts } from '$lib/bluesky';
2import { getLastTrack, updateNowPlayingTrack } from '$lib/lastfm';
3import { steamReadLastGame, steamUpdateNowPlaying } from '$lib/steam';
4import { updateCommits } from '$lib/activity';
5import { ToadScheduler, SimpleIntervalJob, Task, AsyncTask } from 'toad-scheduler';
6import {
7 incrementFakeVisitCount,
8 incrementLegitVisitCount,
9 pushMetric,
10 sendAllMetrics
11} from '$lib/metrics';
12import {
13 addLastVisitor,
14 decrementVisitCount,
15 incrementVisitCount,
16 notifyDarkVisitors,
17 removeLastVisitor
18} from '$lib/visits';
19import { testUa } from '$lib/robots';
20import { error } from '@sveltejs/kit';
21import { _fetchEntries } from './routes/(site)/guestbook/+page.server';
22
23const update = async () => {
24 try {
25 await Promise.all([
26 steamUpdateNowPlaying(),
27 updateNowPlayingTrack(),
28 updateLastPosts(),
29 _fetchEntries(),
30 updateCommits(),
31 sendAllMetrics()
32 ]);
33 } catch (err) {
34 console.log(`error while updating: ${err}`);
35 }
36};
37
38await update();
39
40const scheduler = new ToadScheduler();
41const task = new AsyncTask('update task', update, (err) =>
42 console.log(`error while updating: ${err}`)
43);
44const job = new SimpleIntervalJob({ seconds: 5 }, task);
45scheduler.addSimpleIntervalJob(job);
46
47export const handle = async ({ event, resolve }) => {
48 notifyDarkVisitors(event.url, event.request); // no await so it doesnt block
49
50 const isPrefetch = () => {
51 return (
52 event.request.headers.get('Sec-Purpose')?.includes('prefetch') ||
53 event.request.headers.get('Purpose')?.includes('prefetch') ||
54 event.request.headers.get('x-purpose')?.includes('preview') ||
55 event.request.headers.get('x-moz')?.includes('prefetch')
56 );
57 };
58 const isApi = () => {
59 return event.url.pathname.startsWith('/_api');
60 };
61 const isRss = () => {
62 return event.url.pathname.endsWith('/_rss');
63 };
64
65 // block any requests if the user agent is disallowed by our robots txt
66 const isFakeVisit =
67 (await testUa(event.url.toString(), event.request.headers.get('user-agent') ?? '')) === false;
68 if (isFakeVisit) {
69 pushMetric({ gazesys_visit_fake_total: await incrementFakeVisitCount() });
70 throw error(403, 'get a better user agent silly');
71 }
72
73 // only push metric if legit page visit (still want rss to count here though)
74 const isPageVisit = !isApi() && !isPrefetch();
75 if (isPageVisit) pushMetric({ gazesys_visit_real_total: await incrementLegitVisitCount() });
76
77 // only add visitors if its a "legit" page visit
78 let id = null;
79 let valid = false;
80 if (isPageVisit && !isRss()) {
81 id = addLastVisitor(event.request, event.cookies);
82 valid = await incrementVisitCount(event.request, event.cookies);
83 }
84
85 // actually resolve event
86 const resp = await resolve(event);
87 // remove visitors if it was a 404
88 if (resp.status === 404) {
89 if (id !== null) removeLastVisitor(id);
90 if (valid) decrementVisitCount();
91 }
92
93 return resp;
94};