plc-mirror/pds/pds.go

56 lines
1.2 KiB
Go
Raw Normal View History

2024-02-15 17:10:39 +01:00
package pds
import (
2024-02-21 10:35:25 +01:00
"context"
"fmt"
"path/filepath"
2024-02-15 17:10:39 +01:00
"time"
"gorm.io/gorm"
2024-02-15 21:29:08 +01:00
"github.com/uabluerail/indexer/models"
2024-02-15 17:10:39 +01:00
)
2024-02-21 10:35:25 +01:00
const Unknown models.ID = 0
var whitelist []string = []string{
"https://bsky.social",
"https://*.bsky.network",
}
2024-02-15 17:10:39 +01:00
type PDS struct {
2024-02-15 21:29:08 +01:00
ID models.ID `gorm:"primarykey"`
CreatedAt time.Time
UpdatedAt time.Time
2024-02-15 17:10:39 +01:00
Host string `gorm:"uniqueIndex"`
Cursor int64
FirstCursorSinceReset int64
LastList time.Time
CrawlLimit int
Disabled bool
2024-02-15 17:10:39 +01:00
}
func AutoMigrate(db *gorm.DB) error {
return db.AutoMigrate(&PDS{})
}
2024-02-21 10:35:25 +01:00
func EnsureExists(ctx context.Context, db *gorm.DB, host string) (*PDS, error) {
if !IsWhitelisted(host) {
return nil, fmt.Errorf("host %q is not whitelisted", host)
}
2024-02-21 10:35:25 +01:00
remote := PDS{Host: host}
if err := db.Model(&remote).Where(&PDS{Host: host}).FirstOrCreate(&remote).Error; err != nil {
return nil, fmt.Errorf("failed to get PDS record from DB for %q: %w", remote.Host, err)
}
return &remote, nil
}
func IsWhitelisted(host string) bool {
for _, p := range whitelist {
if match, _ := filepath.Match(p, host); match {
return true
}
}
return false
}