Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions databases/app/scripts/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ async function setup() {
console.info('\n--- Setup App database ---')
await create_visits_table()
await create_submissions_table()
await create_actions_table()
}

/**
Expand Down Expand Up @@ -59,3 +60,19 @@ async function create_submissions_table() {

console.info('Submissions table ready')
}

/**
* Creates the user actions table, used to record anonymously various user actions.
*/
async function create_actions_table() {
await db.execute(`
CREATE TABLE IF NOT EXISTS user_actions (
id INTEGER PRIMARY KEY,
action TEXT NOT NULL,
value TEXT,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
)
`)

console.info('User actions table ready')
}
37 changes: 37 additions & 0 deletions src/routes/api/user_action/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { query_app } from '$lib/server/db.app'
import { is_object } from '$lib/server/utils'
import sql from 'sql-template-tag'
import { is_allowed } from '../track/track.utils'
import { json } from '@sveltejs/kit'

type ValidBody = { action: string; value?: string }

const is_valid_body = (body: unknown): body is ValidBody =>
is_object(body) &&
'action' in body &&
typeof body.action === 'string' &&
(!('value' in body) || typeof body.value === 'string')

export const POST = async (event) => {
if (!is_allowed(event)) return json({ error: 'Forbidden' }, { status: 403 })

if (event.request.headers.get('Content-Type') !== 'application/json') {
return json({ error: 'Invalid Request' }, { status: 400 })
}

const body: unknown = await event.request.json()

if (!is_valid_body(body)) {
return json({ error: 'Invalid Request Body' }, { status: 400 })
}

const { action, value = null } = body

const { err } = await query_app(
sql`INSERT INTO user_actions (action, value) VALUES (${action}, ${value})`,
)

if (err) return json({ error: 'Database error' }, { status: 500 })

return json({ message: 'User action has been recorded' })
}
13 changes: 12 additions & 1 deletion src/routes/download/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<script lang="ts">
import MetaData from '$components/MetaData.svelte'

/**
* This is (temporarily) recorded to see if this feature is used at all.
*/
async function record_download() {
await fetch(`/api/user_action`, {
method: 'POST',
body: JSON.stringify({ action: 'download_database' }),
headers: { 'Content-Type': 'application/json' },
})
}
</script>

<MetaData title="Download" description="Download CatDat's SQLite database" />
Expand All @@ -18,7 +29,7 @@
beyond what is available through the web application.
</p>

<a href="/databases/catdat-snapshot.db" class="button" download>
<a href="/databases/catdat-snapshot.db" class="button" download onclick={record_download}>
Download CatDat database
</a>

Expand Down