diff --git a/.env.EXAMPLE b/.env.EXAMPLE index 3c8bec1..f833e39 100644 --- a/.env.EXAMPLE +++ b/.env.EXAMPLE @@ -5,6 +5,7 @@ YOUTUBE_DISABLE = false MASTODON_INSTANCE = 'metalhead.club' BASE_URL = 'https://moshingmammut.phlaym.net' VERBOSE = false +IGNORE_USERS = @moshhead@metalhead.club PUBLIC_REFRESH_INTERVAL = 10000 PUBLIC_MASTODON_INSTANCE_DISPLAY_NAME = 'Metalhead.club' \ No newline at end of file diff --git a/src/lib/server/db.ts b/src/lib/server/db.ts index b6271c3..dc31867 100644 --- a/src/lib/server/db.ts +++ b/src/lib/server/db.ts @@ -1,10 +1,15 @@ import { env } from '$env/dynamic/private'; +import { IGNORE_USERS } 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'); +const ignoredUsers: string[] = + IGNORE_USERS === undefined + ? [] + : IGNORE_USERS.split(',').map((u) => (u.startsWith('@') ? u.substring(1) : u)); if (DEV && isTruthy(env.VERBOSE)) { sqlite3.verbose(); @@ -194,7 +199,7 @@ export async function savePost(post: Post): Promise { export async function getPosts(since: string | null, before: string | null, limit: number) { const promise = await new Promise((resolve, reject) => { - let filter_query; + let filter_query = ''; const params: any = { $limit: limit }; if (since === null && before === null) { filter_query = ''; @@ -206,14 +211,25 @@ export async function getPosts(since: string | null, before: string | null, limi 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; + }); + 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.id - ${filter_query} - ORDER BY created_at DESC - LIMIT $limit`; + 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 + ${filter_query} + ORDER BY created_at DESC + LIMIT $limit`; db.all(sql, params, (err, rows: any[]) => { if (err != null) { console.error('Error loading posts', err);