2
0
Fork 0
webfinger/db.go

59 lines
1.4 KiB
Go

package main
import (
"fmt"
"log"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
type AccountMigration struct {
AccountID int64 `gorm:"account_id"`
Acct string `gorm:"acct"`
FollowersCount string `gorm:"followers_count"`
TargetAccountID int64 `gorm:"target_account_id"`
}
type Account struct {
ID int64 `gorm:"id"`
Username string `gorm:"username"`
Domain string `gorm:"domain"`
MovedToAccountID int64 `gorm:"moved_to_account_id"`
}
func findMigration(db *gorm.DB, acct int64) *Account {
acc := &Account{ID: acct}
db.Model(&Account{}).Find(&acc)
if acc.MovedToAccountID != 0 {
return findMigration(db, acc.MovedToAccountID)
}
return acc
}
func getAccount(db *gorm.DB, acct int64) *Account {
acc := &Account{ID: acct}
db.Model(&Account{}).Find(&acc)
return acc
}
func generateOutput() {
dsn := "host= user= password= dbname= port= sslmode="
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
log.Fatal("Unable to connect to database")
}
var accts = []AccountMigration{}
db.Model(&AccountMigration{}).Where("target_account_id IS NOT NULL").Find(&accts)
for _, acct := range accts {
a := findMigration(db, acct.TargetAccountID)
ghacct := getAccount(db, acct.AccountID)
if a.Domain == "" {
a.Domain = "gearheads.social"
}
fmt.Printf("redirects[\"%s@gearheads.social\"] = []string{\"%s\", \"%s\"}\n", ghacct.Username, a.Username, a.Domain)
}
}