diff --git a/src/lib/server/db.ts b/src/lib/server/db.ts index 2fe4d42..66d801d 100644 --- a/src/lib/server/db.ts +++ b/src/lib/server/db.ts @@ -1,15 +1,23 @@ import { env } from '$env/dynamic/private'; -import { IGNORE_USERS } from '$env/static/private'; +import { IGNORE_USERS, MASTODON_INSTANCE } from '$env/static/private'; import type { Account, Post, Tag } from '$lib/mastodon/response'; import { isTruthy } from '$lib/truthyString'; import sqlite3 from 'sqlite3'; const { DEV } = import.meta.env; const db: sqlite3.Database = new sqlite3.Database('moshingmammut.db'); +// for the local masto instance, the instance name is *not* saved +// as part of the username or acct, so it needs to be stripped const ignoredUsers: string[] = IGNORE_USERS === undefined ? [] - : IGNORE_USERS.split(',').map((u) => (u.startsWith('@') ? u.substring(1) : u)); + : IGNORE_USERS.split(',') + .map((u) => (u.startsWith('@') ? u.substring(1) : u)) + .map((u) => + u.endsWith('@' + MASTODON_INSTANCE) + ? u.substring(0, u.length - ('@' + MASTODON_INSTANCE).length) + : u + ); let databaseReady = false; if (DEV && isTruthy(env.VERBOSE)) { @@ -113,6 +121,55 @@ function getMigrations(): Migration[] { FOREIGN KEY (post_id) REFERENCES posts(id), FOREIGN KEY (tag_url) REFERENCES tags(url) )` + }, + { + id: 2, + name: 'urls as keys', + statement: ` + CREATE TABLE accounts_new ( + id TEXT NOT NULL, + acct TEXT, + username TEXT, + display_name TEXT, + url TEXT NOT NULL PRIMARY KEY, + avatar TEXT + ); + INSERT INTO accounts_new (id, acct, username, display_name, url, avatar) + SELECT id, acct, username, display_name, url, avatar + FROM accounts; + DROP TABLE accounts; + ALTER TABLE accounts_new RENAME TO accounts; + + CREATE TABLE posts_new ( + id TEXT NOT NULL, + content TEXT, + created_at TEXT, + url TEXT NOT NULL PRIMARY KEY, + account_id TEXT NOT NULL, + FOREIGN KEY (account_id) REFERENCES accounts(url) + ); + INSERT INTO posts_new (id, content, created_at, url, account_id) + SELECT p.id, p.content, p.created_at, p.url, accounts.url + FROM posts as p + JOIN accounts ON accounts.id = p.account_id; + DROP TABLE posts; + ALTER TABLE posts_new RENAME TO posts; + + CREATE TABLE poststags_new ( + id integer PRIMARY KEY, + post_id TEXT NOT NULL, + tag_url TEXT NOT NULL, + FOREIGN KEY (post_id) REFERENCES posts(url), + FOREIGN KEY (tag_url) REFERENCES tags(url) + ); + + INSERT INTO poststags_new (id, post_id, tag_url) + SELECT pt.id, posts.url, pt.tag_url + FROM poststags as pt + JOIN posts ON posts.id = pt.post_id; + DROP TABLE poststags; + ALTER TABLE poststags_new RENAME TO poststags; + ` } ]; } @@ -263,7 +320,7 @@ export async function getPosts(since: string | null, before: string | null, limi accounts.id AS account_id, accounts.acct, accounts.username, accounts.display_name, accounts.url AS account_url, accounts.avatar FROM posts - JOIN accounts ON posts.account_id = accounts.id + JOIN accounts ON posts.account_id = accounts.url ${filter_query} ORDER BY created_at DESC LIMIT $limit`; @@ -284,7 +341,7 @@ export async function getPosts(since: string | null, before: string | null, limi FROM poststags JOIN tags ON poststags.tag_url = tags.url WHERE post_id IN (${postIdsParams});`, - rows.map((r: any) => r.id), + rows.map((r: any) => r.url), (tagErr, tagRows: any[]) => { if (tagErr != null) { console.error('Error loading post tags', tagErr);