Compare commits
2 Commits
84b19fb14e
...
2ecd833726
Author | SHA1 | Date |
---|---|---|
Jeffrey Paul | 2ecd833726 | 4 years ago |
Jeffrey Paul | b3f672b84a | 4 years ago |
@ -1,41 +0,0 @@ |
||||
package database |
||||
|
||||
import ( |
||||
"time" |
||||
|
||||
"git.eeqj.de/sneak/feta/instance" |
||||
"github.com/google/uuid" |
||||
"github.com/jinzhu/gorm" |
||||
|
||||
_ "github.com/jinzhu/gorm/dialects/sqlite" |
||||
) |
||||
|
||||
// NB that when you add a model below you must add it to this list!
|
||||
|
||||
func (m *Manager) doMigrations() { |
||||
m.db.AutoMigrate(&apinstance{}) |
||||
} |
||||
|
||||
type apinstance struct { |
||||
gorm.Model |
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;"` |
||||
ErrorCount uint |
||||
SuccessCount uint |
||||
HighestID int |
||||
Hostname string |
||||
Identified bool |
||||
Fetching bool |
||||
Disabled bool |
||||
Implementation string |
||||
NextFetch time.Time |
||||
NodeInfoURL string |
||||
ServerVersionString string |
||||
ServerImplementationString string |
||||
} |
||||
|
||||
func (m *Manager) ListInstances() ([]*instance.Instance, error) { |
||||
output := make([]*instance.Instance, 0) |
||||
// FIXME have this produce a list of Instance
|
||||
|
||||
return output, nil |
||||
} |
@ -0,0 +1,92 @@ |
||||
package database |
||||
|
||||
import ( |
||||
"git.eeqj.de/sneak/feta/instance" |
||||
"github.com/rs/zerolog/log" |
||||
|
||||
_ "github.com/jinzhu/gorm/dialects/sqlite" |
||||
) |
||||
|
||||
func (m *Manager) SaveInstance(i *instance.Instance) error { |
||||
i.Lock() |
||||
defer i.Unlock() |
||||
var x APInstance |
||||
if m.db.Where("UUID = ?", i.UUID).First(&x).RecordNotFound() { |
||||
log.Info(). |
||||
Str("hostname", i.Hostname). |
||||
Msg("instance not in db, inserting") |
||||
// item does not exist in db yet, must insert
|
||||
ni := APInstance{ |
||||
UUID: i.UUID, |
||||
Disabled: i.Disabled, |
||||
ErrorCount: i.ErrorCount, |
||||
FSMState: i.Status(), |
||||
Fetching: i.Fetching, |
||||
HighestID: i.HighestID, |
||||
Hostname: i.Hostname, |
||||
Identified: i.Identified, |
||||
Implementation: i.Implementation, |
||||
NextFetch: i.NextFetch, |
||||
NodeInfoURL: i.NodeInfoURL, |
||||
ServerImplementationString: i.ServerImplementationString, |
||||
ServerVersionString: i.ServerVersionString, |
||||
SuccessCount: i.SuccessCount, |
||||
} |
||||
r := m.db.Create(&ni) |
||||
return r.Error |
||||
} else { |
||||
log.Info(). |
||||
Str("hostname", i.Hostname). |
||||
Str("id", i.UUID.String()). |
||||
Msg("instance found in db, updating") |
||||
// exists in db, update db
|
||||
var ei APInstance |
||||
// EI EI uh-oh
|
||||
m.db.Where("UUID = ?", i.UUID).First(&ei) |
||||
ei.Disabled = i.Disabled |
||||
ei.ErrorCount = i.ErrorCount |
||||
ei.FSMState = i.Status() |
||||
ei.Fetching = i.Fetching |
||||
ei.HighestID = i.HighestID |
||||
ei.Hostname = i.Hostname |
||||
ei.Identified = i.Identified |
||||
ei.Implementation = string(i.Implementation) |
||||
ei.NextFetch = i.NextFetch |
||||
ei.NodeInfoURL = i.NodeInfoURL |
||||
ei.ServerImplementationString = i.ServerImplementationString |
||||
ei.ServerVersionString = i.ServerVersionString |
||||
ei.SuccessCount = i.SuccessCount |
||||
|
||||
r := m.db.Save(&ei) |
||||
return r.Error |
||||
} |
||||
} |
||||
|
||||
func (m *Manager) ListInstances() ([]*instance.Instance, error) { |
||||
output := make([]*instance.Instance, 0) |
||||
|
||||
var results []APInstance |
||||
m.db.Find(&results) |
||||
|
||||
for _, i := range results { |
||||
newinst := instance.New(func(x *instance.Instance) { |
||||
x.UUID = i.UUID |
||||
x.Disabled = i.Disabled |
||||
x.ErrorCount = i.ErrorCount |
||||
x.InitialFSMState = i.FSMState |
||||
x.Fetching = i.Fetching |
||||
x.HighestID = i.HighestID |
||||
x.Hostname = i.Hostname |
||||
x.Identified = i.Identified |
||||
x.Implementation = i.Implementation |
||||
x.NextFetch = i.NextFetch |
||||
x.NodeInfoURL = i.NodeInfoURL |
||||
x.ServerImplementationString = i.ServerImplementationString |
||||
x.ServerVersionString = i.ServerVersionString |
||||
x.SuccessCount = i.SuccessCount |
||||
}) |
||||
output = append(output, newinst) |
||||
} |
||||
|
||||
return output, nil |
||||
} |
@ -0,0 +1,49 @@ |
||||
package database |
||||
|
||||
import ( |
||||
"time" |
||||
|
||||
"github.com/google/uuid" |
||||
"github.com/jinzhu/gorm" |
||||
"github.com/rs/zerolog/log" |
||||
|
||||
_ "github.com/jinzhu/gorm/dialects/sqlite" |
||||
) |
||||
|
||||
type StoredToot struct { |
||||
gorm.Model |
||||
UUID uuid.UUID `gorm:"type:uuid;primary_key;"` |
||||
//Original string `sql:"type:text"`
|
||||
Original []byte |
||||
Hash string `gorm:"unique_index"` |
||||
ServerCreated time.Time |
||||
Acct string |
||||
Content []byte |
||||
URL string |
||||
Hostname string |
||||
} |
||||
|
||||
type APInstance struct { |
||||
gorm.Model |
||||
UUID uuid.UUID `gorm:"type:uuid;primary_key;"` |
||||
ErrorCount uint |
||||
SuccessCount uint |
||||
HighestID uint |
||||
Hostname string `gorm:"type:varchar(100);unique_index"` |
||||
Identified bool |
||||
Fetching bool |
||||
Disabled bool |
||||
Implementation string |
||||
NextFetch time.Time |
||||
NodeInfoURL string |
||||
ServerVersionString string |
||||
ServerImplementationString string |
||||
FSMState string |
||||
} |
||||
|
||||
// NB that when you add a model below you must add it to this list!
|
||||
func (m *Manager) doMigrations() { |
||||
log.Info().Msg("doing database migrations if required") |
||||
m.db.AutoMigrate(&APInstance{}) |
||||
m.db.AutoMigrate(&StoredToot{}) |
||||
} |
@ -0,0 +1,47 @@ |
||||
package database |
||||
|
||||
import ( |
||||
"fmt" |
||||
"strings" |
||||
|
||||
"git.eeqj.de/sneak/feta/toot" |
||||
|
||||
"github.com/google/uuid" |
||||
_ "github.com/jinzhu/gorm/dialects/sqlite" |
||||
) |
||||
|
||||
func (m *Manager) TootExists(t *toot.Toot) bool { |
||||
var try StoredToot |
||||
if m.db.Where("Hash = ?", t.GetHash()).First(&try).RecordNotFound() { |
||||
return false |
||||
} else { |
||||
return true |
||||
} |
||||
} |
||||
|
||||
func (m *Manager) StoreToot(t *toot.Toot) error { |
||||
|
||||
nt := new(StoredToot) |
||||
nt.UUID = uuid.New() |
||||
nt.ServerCreated = t.Parsed.CreatedAt |
||||
nt.Original = t.Original |
||||
// FIXME normalize this, check for @ and append hostname if none
|
||||
nt.Acct = fmt.Sprintf("%s@%s", t.Parsed.Account.Acct, strings.ToLower(t.FromHost)) |
||||
nt.URL = t.Parsed.URL |
||||
nt.Content = t.Parsed.Content |
||||
nt.Hostname = strings.ToLower(t.FromHost) |
||||
nt.Hash = t.GetHash() |
||||
r := m.db.Create(&nt) |
||||
//panic(fmt.Sprintf("%+v", t))
|
||||
return r.Error |
||||
} |
||||
|
||||
func (m *Manager) StoreToots(tc []*toot.Toot) error { |
||||
for _, item := range tc { |
||||
err := m.StoreToot(item) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
} |
||||
return nil |
||||
} |
@ -0,0 +1,13 @@ |
||||
package storage |
||||
|
||||
import ( |
||||
"git.eeqj.de/sneak/feta/toot" |
||||
) |
||||
|
||||
// TootStorageBackend is the interface to which storage backends must
|
||||
// conform for storing toots
|
||||
type TootStorageBackend interface { |
||||
TootExists(t *toot.Toot) bool |
||||
StoreToot(t *toot.Toot) error |
||||
StoreToots(tc []*toot.Toot) error |
||||
} |
Loading…
Reference in new issue