111 lines
2.8 KiB
Go
111 lines
2.8 KiB
Go
// peertube-instance-index-filter
|
|
// Copyright (C) 2025 Arns Udovič <zordsdavini@arns.lt>
|
|
//
|
|
// 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 <http://www.gnu.org/licenses/>.
|
|
|
|
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
var migrations = []string{
|
|
"SELECT 1;",
|
|
"ALTER TABLE instances ADD COLUMN rejected INTEGER NOT NULL DEFAULT 0;",
|
|
}
|
|
|
|
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, url string) {
|
|
db.Exec("INSERT INTO index_host (url, last_fetched_at) VALUES (?, '2000-01-01');", url)
|
|
}
|
|
|
|
func rejectHost(db *sql.DB, host string) {
|
|
db.Exec("UPDATE instances SET rejected = 1 WHERE url = ?", host)
|
|
}
|