Saving song infos to DB, refactor logging
This commit is contained in:
@ -1,10 +1,47 @@
|
||||
import { env } from '$env/dynamic/private';
|
||||
import { IGNORE_USERS, MASTODON_INSTANCE } from '$env/static/private';
|
||||
import { enableVerboseLog, log } from '$lib/log';
|
||||
import type { Account, Post, Tag } from '$lib/mastodon/response';
|
||||
import { isTruthy } from '$lib/truthyString';
|
||||
import type { SongInfo } from '$lib/odesliResponse';
|
||||
import sqlite3 from 'sqlite3';
|
||||
|
||||
const { DEV } = import.meta.env;
|
||||
|
||||
type FilterParameter = {
|
||||
$limit: number | undefined | null;
|
||||
$since?: string | undefined | null;
|
||||
$before?: string | undefined | null;
|
||||
[x: string]: string | number | undefined | null;
|
||||
};
|
||||
|
||||
type PostRow = {
|
||||
id: string;
|
||||
content: string;
|
||||
created_at: string;
|
||||
url: string;
|
||||
account_id: string;
|
||||
acct: string;
|
||||
username: string;
|
||||
display_name: string;
|
||||
account_url: string;
|
||||
avatar: string;
|
||||
};
|
||||
|
||||
type PostTagRow = {
|
||||
post_id: string;
|
||||
tag: string;
|
||||
url: string;
|
||||
};
|
||||
|
||||
type SongRow = SongInfo & {
|
||||
post_url: string;
|
||||
};
|
||||
|
||||
type Migration = {
|
||||
id: number;
|
||||
name: string;
|
||||
statement: string;
|
||||
};
|
||||
|
||||
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
|
||||
@ -20,38 +57,32 @@ const ignoredUsers: string[] =
|
||||
);
|
||||
let databaseReady = false;
|
||||
|
||||
if (DEV && isTruthy(env.VERBOSE)) {
|
||||
if (enableVerboseLog) {
|
||||
sqlite3.verbose();
|
||||
db.on('change', (t, d, table, rowid) => {
|
||||
console.debug('DB change event', t, d, table, rowid);
|
||||
log.verbose('DB change event', t, d, table, rowid);
|
||||
});
|
||||
|
||||
db.on('trace', (sql) => {
|
||||
console.debug('Running', sql);
|
||||
log.verbose('Running', sql);
|
||||
});
|
||||
|
||||
db.on('profile', (sql) => {
|
||||
console.debug('Finished', sql);
|
||||
log.verbose('Finished', sql);
|
||||
});
|
||||
}
|
||||
|
||||
interface Migration {
|
||||
id: number;
|
||||
name: string;
|
||||
statement: string;
|
||||
}
|
||||
|
||||
db.on('open', () => {
|
||||
console.log('Opened database');
|
||||
log.info('Opened database');
|
||||
db.serialize();
|
||||
db.run('CREATE TABLE IF NOT EXISTS "migrations" ("id" integer,"name" TEXT, PRIMARY KEY (id))');
|
||||
db.all('SELECT id FROM migrations', (err, rows: Migration[]) => {
|
||||
if (err !== null) {
|
||||
console.error('Could not fetch existing migrations', err);
|
||||
log.error('Could not fetch existing migrations', err);
|
||||
databaseReady = true;
|
||||
return;
|
||||
}
|
||||
console.debug('Already applied migrations', rows);
|
||||
log.debug('Already applied migrations', rows);
|
||||
const appliedMigrations: Set<number> = new Set(rows.map((row) => row['id']));
|
||||
const toApply = getMigrations().filter((m) => !appliedMigrations.has(m.id));
|
||||
let remaining = toApply.length;
|
||||
@ -68,7 +99,7 @@ db.on('open', () => {
|
||||
databaseReady = true;
|
||||
}
|
||||
if (err !== null) {
|
||||
console.error(`Failed to apply migration ${migration.name}`, err);
|
||||
log.error(`Failed to apply migration ${migration.name}`, err);
|
||||
return;
|
||||
}
|
||||
db.run(
|
||||
@ -76,10 +107,10 @@ db.on('open', () => {
|
||||
[migration.id, migration.name],
|
||||
(e: Error) => {
|
||||
if (e !== null) {
|
||||
console.error(`Failed to mark migration ${migration.name} as applied`, e);
|
||||
log.error(`Failed to mark migration ${migration.name} as applied`, e);
|
||||
return;
|
||||
}
|
||||
console.info(`Applied migration ${migration.name}`);
|
||||
log.info(`Applied migration ${migration.name}`);
|
||||
}
|
||||
);
|
||||
});
|
||||
@ -87,7 +118,7 @@ db.on('open', () => {
|
||||
});
|
||||
});
|
||||
db.on('error', (err) => {
|
||||
console.error('Error opening database', err);
|
||||
log.error('Error opening database', err);
|
||||
});
|
||||
|
||||
function getMigrations(): Migration[] {
|
||||
@ -170,6 +201,23 @@ function getMigrations(): Migration[] {
|
||||
DROP TABLE poststags;
|
||||
ALTER TABLE poststags_new RENAME TO poststags;
|
||||
`
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
name: 'song info for posts',
|
||||
statement: `
|
||||
CREATE TABLE songs (
|
||||
id integer PRIMARY KEY,
|
||||
postedUrl TEXT NOT NULL,
|
||||
overviewUrl TEXT,
|
||||
type TEXT CHECK ( type in ('album', 'song') ),
|
||||
youtubeUrl TEXT,
|
||||
title TEXT,
|
||||
artistName TEXT,
|
||||
thumbnailUrl TEXT,
|
||||
post_url TEXT,
|
||||
FOREIGN KEY (post_url) REFERENCES posts(url)
|
||||
);`
|
||||
}
|
||||
];
|
||||
}
|
||||
@ -179,11 +227,11 @@ async function waitReady(): Promise<undefined> {
|
||||
return new Promise((resolve) => {
|
||||
const interval = setInterval(() => {
|
||||
if (DEV) {
|
||||
console.debug('Waiting for database to be ready');
|
||||
log.debug('Waiting for database to be ready');
|
||||
}
|
||||
if (databaseReady) {
|
||||
if (DEV) {
|
||||
console.debug('DB is ready');
|
||||
log.debug('DB is ready');
|
||||
}
|
||||
clearInterval(interval);
|
||||
resolve(undefined);
|
||||
@ -192,13 +240,8 @@ async function waitReady(): Promise<undefined> {
|
||||
});
|
||||
}
|
||||
|
||||
export async function savePost(post: Post): Promise<undefined> {
|
||||
if (!databaseReady) {
|
||||
await waitReady();
|
||||
}
|
||||
return await new Promise<undefined>((resolve, reject) => {
|
||||
console.debug(`Saving post ${post.url}`);
|
||||
const account = post.account;
|
||||
function saveAccountData(account: Account): Promise<undefined> {
|
||||
return new Promise<undefined>((resolve, reject) => {
|
||||
db.run(
|
||||
`
|
||||
INSERT INTO accounts (id, acct, username, display_name, url, avatar)
|
||||
@ -220,192 +263,281 @@ export async function savePost(post: Post): Promise<undefined> {
|
||||
],
|
||||
(err) => {
|
||||
if (err !== null) {
|
||||
console.error(`Could not insert/update account ${account.id}`, err);
|
||||
log.error(`Could not insert/update account ${account.id}`, err);
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
db.run(
|
||||
`
|
||||
INSERT INTO posts (id, content, created_at, url, account_id)
|
||||
VALUES (?, ?, ?, ?, ?) ON CONFLICT(url) DO UPDATE SET
|
||||
content=excluded.content,
|
||||
created_at=excluded.created_at,
|
||||
id=excluded.id,
|
||||
account_id=excluded.account_id;`,
|
||||
[post.id, post.content, post.created_at, post.url, post.account.url],
|
||||
(postErr) => {
|
||||
if (postErr !== null) {
|
||||
console.error(`Could not insert post ${post.url}`, postErr);
|
||||
reject(postErr);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!post.tags.length) {
|
||||
resolve(undefined);
|
||||
return;
|
||||
}
|
||||
|
||||
db.parallelize(() => {
|
||||
let remaining = post.tags.length;
|
||||
for (const tag of post.tags) {
|
||||
db.run(
|
||||
`
|
||||
INSERT INTO tags (url, tag) VALUES (?, ?)
|
||||
ON CONFLICT(url) DO UPDATE SET
|
||||
tag=excluded.tag;`,
|
||||
[tag.url, tag.name],
|
||||
(tagErr) => {
|
||||
if (tagErr !== null) {
|
||||
console.error(`Could not insert/update tag ${tag.url}`, tagErr);
|
||||
reject(tagErr);
|
||||
return;
|
||||
}
|
||||
db.run(
|
||||
'INSERT INTO poststags (post_id, tag_url) VALUES (?, ?)',
|
||||
[post.url, tag.url],
|
||||
(posttagserr) => {
|
||||
if (posttagserr !== null) {
|
||||
console.error(
|
||||
`Could not insert poststags ${tag.url}, ${post.url}`,
|
||||
posttagserr
|
||||
);
|
||||
reject(posttagserr);
|
||||
return;
|
||||
}
|
||||
// Don't decrease on fail
|
||||
remaining--;
|
||||
// Only resolve after all have been inserted
|
||||
if (remaining === 0) {
|
||||
resolve(undefined);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
);
|
||||
resolve(undefined);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
type FilterParameter = {
|
||||
$limit: number | undefined | null;
|
||||
$since?: string | undefined | null;
|
||||
$before?: string | undefined | null;
|
||||
[x: string]: string | number | undefined | null;
|
||||
};
|
||||
function savePostData(post: Post): Promise<undefined> {
|
||||
return new Promise<undefined>((resolve, reject) => {
|
||||
db.run(
|
||||
`
|
||||
INSERT INTO posts (id, content, created_at, url, account_id)
|
||||
VALUES (?, ?, ?, ?, ?) ON CONFLICT(url) DO UPDATE SET
|
||||
content=excluded.content,
|
||||
created_at=excluded.created_at,
|
||||
id=excluded.id,
|
||||
account_id=excluded.account_id;`,
|
||||
[post.id, post.content, post.created_at, post.url, post.account.url],
|
||||
(postErr) => {
|
||||
if (postErr !== null) {
|
||||
log.error(`Could not insert post ${post.url}`, postErr);
|
||||
reject(postErr);
|
||||
return;
|
||||
}
|
||||
resolve(undefined);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
export async function getPosts(since: string | null, before: string | null, limit: number) {
|
||||
function savePostTagData(post: Post): Promise<undefined> {
|
||||
return new Promise<undefined>((resolve, reject) => {
|
||||
if (!post.tags.length) {
|
||||
resolve(undefined);
|
||||
return;
|
||||
}
|
||||
|
||||
db.parallelize(() => {
|
||||
let remaining = post.tags.length;
|
||||
for (const tag of post.tags) {
|
||||
db.run(
|
||||
`
|
||||
INSERT INTO tags (url, tag) VALUES (?, ?)
|
||||
ON CONFLICT(url) DO UPDATE SET
|
||||
tag=excluded.tag;`,
|
||||
[tag.url, tag.name],
|
||||
(tagErr) => {
|
||||
if (tagErr !== null) {
|
||||
log.error(`Could not insert/update tag ${tag.url}`, tagErr);
|
||||
reject(tagErr);
|
||||
return;
|
||||
}
|
||||
db.run(
|
||||
'INSERT INTO poststags (post_id, tag_url) VALUES (?, ?)',
|
||||
[post.url, tag.url],
|
||||
(posttagserr) => {
|
||||
if (posttagserr !== null) {
|
||||
log.error(`Could not insert poststags ${tag.url}, ${post.url}`, posttagserr);
|
||||
reject(posttagserr);
|
||||
return;
|
||||
}
|
||||
// Don't decrease on fail
|
||||
remaining--;
|
||||
// Only resolve after all have been inserted
|
||||
if (remaining === 0) {
|
||||
resolve(undefined);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function saveSongInfoData(postUrl: string, songs: SongInfo[]): Promise<undefined> {
|
||||
return new Promise<undefined>((resolve, reject) => {
|
||||
if (songs.length === 0) {
|
||||
resolve(undefined);
|
||||
return;
|
||||
}
|
||||
db.parallelize(() => {
|
||||
let remaining = songs.length;
|
||||
for (const song of songs) {
|
||||
db.run(
|
||||
`
|
||||
INSERT INTO songs (postedUrl, overviewUrl, type, youtubeUrl, title, artistName, thumbnailUrl, post_url)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`,
|
||||
[
|
||||
song.postedUrl,
|
||||
song.pageUrl,
|
||||
song.type,
|
||||
song.youtubeUrl,
|
||||
song.title,
|
||||
song.artistName,
|
||||
song.thumbnailUrl,
|
||||
postUrl
|
||||
],
|
||||
(songErr) => {
|
||||
if (songErr !== null) {
|
||||
log.error(`Could not insert song ${song.postedUrl}`, songErr);
|
||||
reject(songErr);
|
||||
return;
|
||||
}
|
||||
// Don't decrease on fail
|
||||
remaining--;
|
||||
// Only resolve after all have been inserted
|
||||
if (remaining === 0) {
|
||||
resolve(undefined);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function savePost(post: Post, songs: SongInfo[]) {
|
||||
if (!databaseReady) {
|
||||
await waitReady();
|
||||
}
|
||||
const promise = await new Promise<Post[]>((resolve, reject) => {
|
||||
let filter_query = '';
|
||||
const params: FilterParameter = { $limit: limit };
|
||||
if (since === null && before === null) {
|
||||
filter_query = '';
|
||||
} else if (since !== null) {
|
||||
filter_query = 'WHERE posts.created_at > $since';
|
||||
params.$since = since;
|
||||
} else if (before !== null) {
|
||||
// Setting both, before and since doesn't make sense, so this case is not explicitly handled
|
||||
filter_query = 'WHERE posts.created_at < $before';
|
||||
params.$before = before;
|
||||
}
|
||||
|
||||
ignoredUsers.forEach((ignoredUser, index) => {
|
||||
const userParam = `$user_${index}`;
|
||||
const acctParam = userParam + 'a';
|
||||
const usernameParam = userParam + 'u';
|
||||
const prefix = filter_query === '' ? ' WHERE' : ' AND';
|
||||
filter_query += `${prefix} acct != ${acctParam} AND username != ${usernameParam} `;
|
||||
params[acctParam] = ignoredUser;
|
||||
params[usernameParam] = ignoredUser;
|
||||
});
|
||||
log.debug(`Saving post ${post.url}`);
|
||||
const account = post.account;
|
||||
await saveAccountData(account);
|
||||
log.debug(`Saved account data ${post.url}`);
|
||||
await savePostData(post);
|
||||
log.debug(`Saved post data ${post.url}`);
|
||||
await savePostTagData(post);
|
||||
log.debug(`Saved ${post.tags.length} tag data ${post.url}`);
|
||||
await saveSongInfoData(post.url, songs);
|
||||
log.debug(`Saved ${songs.length} song info data ${post.url}`);
|
||||
}
|
||||
|
||||
type PostResult = {
|
||||
id: string;
|
||||
content: string;
|
||||
created_at: string;
|
||||
url: string;
|
||||
account_id: string;
|
||||
acct: string;
|
||||
username: string;
|
||||
display_name: string;
|
||||
account_url: string;
|
||||
avatar: string;
|
||||
};
|
||||
function getPostData(filterQuery: string, params: FilterParameter): Promise<PostRow[]> {
|
||||
const sql = `SELECT posts.id, posts.content, posts.created_at, posts.url,
|
||||
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.url
|
||||
${filterQuery}
|
||||
ORDER BY created_at DESC
|
||||
LIMIT $limit`;
|
||||
|
||||
type PostTagResult = {
|
||||
post_id: string;
|
||||
tag: string;
|
||||
url: string;
|
||||
};
|
||||
|
||||
const sql = `SELECT posts.id, posts.content, posts.created_at, posts.url,
|
||||
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.url
|
||||
${filter_query}
|
||||
ORDER BY created_at DESC
|
||||
LIMIT $limit`;
|
||||
db.all(sql, params, (err, rows: PostResult[]) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
db.all(sql, params, (err, rows: PostRow[]) => {
|
||||
if (err != null) {
|
||||
console.error('Error loading posts', err);
|
||||
log.error('Error loading posts', err);
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
if (rows.length === 0) {
|
||||
// No need to check for tags
|
||||
resolve([]);
|
||||
return;
|
||||
}
|
||||
const postIdsParams = rows.map(() => '?').join(', ');
|
||||
db.all(
|
||||
`SELECT post_id, tags.url, tags.tag
|
||||
FROM poststags
|
||||
JOIN tags ON poststags.tag_url = tags.url
|
||||
WHERE post_id IN (${postIdsParams});`,
|
||||
rows.map((r: PostResult) => r.url),
|
||||
(tagErr, tagRows: PostTagResult[]) => {
|
||||
if (tagErr != null) {
|
||||
console.error('Error loading post tags', tagErr);
|
||||
reject(tagErr);
|
||||
return;
|
||||
}
|
||||
const tagMap: Map<string, Tag[]> = tagRows.reduce((result: Map<string, Tag[]>, item) => {
|
||||
const tag: Tag = {
|
||||
url: item.url,
|
||||
name: item.tag
|
||||
};
|
||||
result.set(item.post_id, [...(result.get(item.post_id) || []), tag]);
|
||||
return result;
|
||||
}, new Map());
|
||||
const posts = rows.map((row) => {
|
||||
return {
|
||||
id: row.id,
|
||||
content: row.content,
|
||||
created_at: row.created_at,
|
||||
url: row.url,
|
||||
tags: tagMap.get(row.id) || [],
|
||||
account: {
|
||||
id: row.account_id,
|
||||
acct: row.acct,
|
||||
username: row.username,
|
||||
display_name: row.display_name,
|
||||
url: row.account_url,
|
||||
avatar: row.avatar
|
||||
} as Account
|
||||
} as Post;
|
||||
});
|
||||
resolve(posts);
|
||||
}
|
||||
);
|
||||
resolve(rows);
|
||||
});
|
||||
});
|
||||
return promise;
|
||||
}
|
||||
|
||||
function getTagData(postIdsParams: String, postIds: string[]): Promise<Map<string, Tag[]>> {
|
||||
return new Promise((resolve, reject) => {
|
||||
db.all(
|
||||
`SELECT post_id, tags.url, tags.tag
|
||||
FROM poststags
|
||||
JOIN tags ON poststags.tag_url = tags.url
|
||||
WHERE post_id IN (${postIdsParams});`,
|
||||
postIds,
|
||||
(tagErr, tagRows: PostTagRow[]) => {
|
||||
if (tagErr != null) {
|
||||
log.error('Error loading post tags', tagErr);
|
||||
reject(tagErr);
|
||||
return;
|
||||
}
|
||||
const tagMap: Map<string, Tag[]> = tagRows.reduce((result: Map<string, Tag[]>, item) => {
|
||||
const tag: Tag = {
|
||||
url: item.url,
|
||||
name: item.tag
|
||||
};
|
||||
result.set(item.post_id, [...(result.get(item.post_id) || []), tag]);
|
||||
return result;
|
||||
}, new Map());
|
||||
resolve(tagMap);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
function getSongData(postIdsParams: String, postIds: string[]): Promise<Map<string, SongInfo[]>> {
|
||||
return new Promise((resolve, reject) => {
|
||||
db.all(
|
||||
`SELECT post_url, songs.postedUrl, songs.overviewUrl, songs.type, songs.youtubeUrl,
|
||||
songs.title, songs.artistName, songs.thumbnailUrl, songs.post_url
|
||||
FROM songs
|
||||
WHERE post_url IN (${postIdsParams});`,
|
||||
postIds,
|
||||
(tagErr, tagRows: SongRow[]) => {
|
||||
if (tagErr != null) {
|
||||
log.error('Error loading post tags', tagErr);
|
||||
reject(tagErr);
|
||||
return;
|
||||
}
|
||||
const songMap: Map<string, SongInfo[]> = tagRows.reduce(
|
||||
(result: Map<string, SongInfo[]>, item) => {
|
||||
result.set(item.post_url, [...(result.get(item.post_url) || []), item]);
|
||||
return result;
|
||||
},
|
||||
new Map()
|
||||
);
|
||||
resolve(songMap);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
export async function getPosts(
|
||||
since: string | null,
|
||||
before: string | null,
|
||||
limit: number
|
||||
): Promise<Post[]> {
|
||||
if (!databaseReady) {
|
||||
await waitReady();
|
||||
}
|
||||
|
||||
let filterQuery = '';
|
||||
const params: FilterParameter = { $limit: limit };
|
||||
if (since === null && before === null) {
|
||||
filterQuery = '';
|
||||
} else if (since !== null) {
|
||||
filterQuery = 'WHERE posts.created_at > $since';
|
||||
params.$since = since;
|
||||
} else if (before !== null) {
|
||||
// Setting both, before and since doesn't make sense, so this case is not explicitly handled
|
||||
filterQuery = 'WHERE posts.created_at < $before';
|
||||
params.$before = before;
|
||||
}
|
||||
|
||||
ignoredUsers.forEach((ignoredUser, index) => {
|
||||
const userParam = `$user_${index}`;
|
||||
const acctParam = userParam + 'a';
|
||||
const usernameParam = userParam + 'u';
|
||||
const prefix = filterQuery === '' ? ' WHERE' : ' AND';
|
||||
filterQuery += `${prefix} acct != ${acctParam} AND username != ${usernameParam} `;
|
||||
params[acctParam] = ignoredUser;
|
||||
params[usernameParam] = ignoredUser;
|
||||
});
|
||||
|
||||
const rows = await getPostData(filterQuery, params);
|
||||
if (rows.length === 0) {
|
||||
// No need to check for tags and songs
|
||||
return [];
|
||||
}
|
||||
|
||||
const postIdsParams = rows.map(() => '?').join(', ');
|
||||
const postIds = rows.map((r: PostRow) => r.url);
|
||||
const tagMap = await getTagData(postIdsParams, postIds);
|
||||
const songMap = await getSongData(postIdsParams, postIds);
|
||||
const posts = rows.map((row) => {
|
||||
return {
|
||||
id: row.id,
|
||||
content: row.content,
|
||||
created_at: row.created_at,
|
||||
url: row.url,
|
||||
tags: tagMap.get(row.url) || [],
|
||||
account: {
|
||||
id: row.account_id,
|
||||
acct: row.acct,
|
||||
username: row.username,
|
||||
display_name: row.display_name,
|
||||
url: row.account_url,
|
||||
avatar: row.avatar
|
||||
} as Account,
|
||||
songs: songMap.get(row.url) || []
|
||||
} as Post;
|
||||
});
|
||||
return posts;
|
||||
}
|
||||
|
Reference in New Issue
Block a user