#!/bin/env tclsh
cd [file dirname [dict get [info frame [info frame]] file]]
package require logger
package require json
package require json::write
package require sqlite3
package require inifile
package require sha1
source src/config.tcl
source src/at.tcl
source src/ws.tcl
source src/jetstream.tcl
source src/httpd.tcl
source src/feed.tcl
source src/algos.tcl
set log [logger::init tclfeed]
set logd [logger::init tclfeed::database]
${log}::info "tclfeed v0.0.1"
### DB STUFF ###
sqlite3 db [::ini::value $config database path]
foreach algo $::algos::list {
db eval {SELECT count(*) as n FROM sqlite_master WHERE name=$algo;} values {
if ![set values(n)] {
${log}::info "Creating table \"$algo\"!"
if [info exists ::algos::${algo}::cols] {
db eval "CREATE TABLE $algo (uri TEXT NOT NULL PRIMARY KEY, ord TEXT NOT NULL, [set ::algos::${algo}::cols]);"
} else {
db eval "CREATE TABLE $algo (uri TEXT NOT NULL PRIMARY KEY, ord TEXT NOT NULL);"
}
}
}
}
proc db_cleanup {interval max_posts} {
global ::algos::list logd
${logd}::info "Performing database cleanup!"
foreach algo $::algos::list {
# slightly cursed
db eval "DELETE FROM $algo WHERE uri IN (SELECT uri FROM $algo ORDER BY ord DESC LIMIT -1 OFFSET :max_posts); REINDEX $algo;"
}
db eval "VACUUM";
${logd}::info "DB cleanup completed without error!"
${logd}::debug "Scheduling next cleanup for [clock format [expr {[clock seconds] + $interval}]]."
after [expr {$interval * 1000}] [list db_cleanup $interval $max_posts]
}
db_cleanup [::ini::value $config database cleanup_interval] [::ini::value $config database max_posts]
### JETSTREAM ###
jetstream::listen [::ini::value $config atproto jetstream_host] $::algos::list
### HTTPD ###
httpd::router main [list \
/xrpc/app.bsky.feed.getFeedSkeleton [list ::feed::serve_skeleton \
$::algos::list \
[::ini::value $config atproto publisher_did]] \
/xrpc/app.bsky.feed.describeFeedGenerator [list ::feed::serve_generator_description \
$::algos::list \
[::ini::value $config atproto publisher_did] \
[::ini::value $config atproto self_host]]
]
httpd::listen main [::ini::value $config httpd port]
vwait nil