// peertube-instance-index-filter // Copyright (C) 2025 Arns Udovič // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . package main import ( "database/sql" "strconv" "time" _ "github.com/mattn/go-sqlite3" ) var migrations = []string{ "SELECT 1;", "ALTER TABLE instances ADD COLUMN rejected INTEGER NOT NULL DEFAULT 0;", "ALTER TABLE instances ADD COLUMN reject_reason TEXT;", "ALTER TABLE index_host ADD COLUMN instance_url TEXT;", } type IndexHost struct { Url string InstanceUrl string LastFetchedAt string } func connectDB() *sql.DB { db, err := sql.Open("sqlite3", "./instances.db") if err != nil { panic(err) } checkPreinstall(db) return db } func checkPreinstall(db *sql.DB) { sqlStmt := ` CREATE TABLE IF NOT EXISTS instances ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, url TEXT NOT NULL, data TEXT NOT NULL, created_at TEXT NOT NULL ); CREATE INDEX IF NOT EXISTS idx_instances_url ON instances (created_at); CREATE TABLE IF NOT EXISTS index_host ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, url TEXT NOT NULL, last_fetched_at TEXT NOT NULL ); CREATE TABLE IF NOT EXISTS counter ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, key TEXT NOT NULL, counter INTEGER NOT NULL ); CREATE UNIQUE INDEX IF NOT EXISTS idx_counter_key ON counter (key); INSERT OR IGNORE INTO counter (key, counter) VALUES ('last_migration', 0); ` _, err := db.Exec(sqlStmt) if err != nil { panic(err) } runMigrations(db) } func runMigrations(db *sql.DB) { lastMigratuion, err := getCounter(db, "last_migration") if err != nil { panic(err) } for i := lastMigratuion; i < len(migrations); i++ { _, err := db.Exec(migrations[i]) if err != nil { panic(err) } db.Exec("UPDATE counter SET counter = counter+1 WHERE key = 'last_migration';") } } func getCounter(db *sql.DB, key string) (int, error) { var counter int err := db.QueryRow("SELECT counter FROM counter WHERE key = ?", key).Scan(&counter) return counter, err } func indexExists(db *sql.DB, url string) (bool, error) { var count int err := db.QueryRow("SELECT COUNT(*) FROM index_host WHERE url = ?", url).Scan(&count) return count > 0, err } func addIndex(db *sql.DB, indexHost IndexHost) { db.Exec("INSERT INTO index_host (url, instance_url, last_fetched_at) VALUES (?, ?, '2000-01-01');", indexHost.Url, indexHost.InstanceUrl) } func rejectHost(db *sql.DB, host string, reason string) { db.Exec("UPDATE instances SET rejected = 1, reject_reason = ? WHERE url = ?", reason, host) } func getIndexHosts(db *sql.DB) []IndexHost { indexHosts := []IndexHost{} rows, err := db.Query("SELECT url, instance_url, last_fetched_at FROM index_host") if err != nil { panic(err) } for rows.Next() { var indexHost IndexHost rows.Scan(&indexHost.Url, &indexHost.InstanceUrl, &indexHost.LastFetchedAt) indexHosts = append(indexHosts, indexHost) } return indexHosts } func updateLastFetched(db *sql.DB, indexHost IndexHost) { db.Exec("UPDATE index_host SET last_fetched_at = ? WHERE url = ?", time.Now().Add(-24*time.Hour).Format("2006-01-02"), indexHost.Url) } func addInstance(db *sql.DB, instance Instance) { var count int db.QueryRow("SELECT COUNT(*) FROM instance WHERE url = ?", instance.Url).Scan(&count) if count > 0 { return } db.Exec("INSERT INTO instances (url, data, created_at) VALUES (?, ?, ?);", instance.Url, instance.Data, instance.CreatedAt) } func getHosts(db *sql.DB, start int, count int, since string, search string, property string) ([]string, error) { hosts := []string{} query := "SELECT " + property + " FROM instances WHERE rejected = 0" if since != "" { query += " AND created_at > \"" + since + "\"" } if search != "" { query += " AND url LIKE \"%" + search + "%\"" } rows, err := db.Query(query + " ORDER BY created_at DESC LIMIT ? OFFSET ?", count, start) if err != nil { return hosts, err } for rows.Next() { var host string rows.Scan(&host) hosts = append(hosts, host) } return hosts, nil } func getHostsTotal(db *sql.DB, since string, search string) (int, error) { total := "0" query := "SELECT count(url) FROM instances WHERE rejected = 0" if since != "" { query += " AND created_at > \"" + since + "\"" } if search != "" { query += " AND url LIKE \"%" + search + "%\"" } row := db.QueryRow(query) row.Scan(&total) totali, err := strconv.Atoi(total) if err != nil { return 0, err } return totali, nil } func getRejectedHostsTotal(db *sql.DB) (int, error) { total := "0" query := "SELECT count(url) FROM instances WHERE rejected = 1" row := db.QueryRow(query) row.Scan(&total) totali, err := strconv.Atoi(total) if err != nil { return 0, err } return totali, nil } func getDuplicateIds(db *sql.DB) ([]string, error) { ids := []string{} query := "SELECT id FROM instances GROUP BY url HAVING COUNT(*) > 1" rows, err := db.Query(query) if err != nil { return ids, err } for rows.Next() { var id string rows.Scan(&id) ids = append(ids, id) } return ids, nil } func removeInstance(db *sql.DB, id string) { db.Exec("DELETE FROM instances WHERE id = ?", id) }