nuke everything, start over

This commit is contained in:
mStar 2024-05-27 18:10:35 +02:00
parent e261de7060
commit a2a937791d
44 changed files with 7 additions and 1106 deletions

View file

@ -1,31 +0,0 @@
// Copyright (c) 2024 mStar
//
// Licensed under the EUPL, Version 1.2
//
// You may not use this work except in compliance with the Licence.
// You should have received a copy of the Licence along with this work. If not, see:
// <https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12>.
// See the Licence for the specific language governing permissions and limitations under the Licence.
//
package storage
type Follow struct {
Follower Person
FollowerID uint
Follows Person
FollowsID uint
}
func (s *Storage) GetFollowersFor(url string) ([]Follow, error) {
var pHit int64
s.Db.Where("url = ?", url).Count(&pHit)
if pHit <= 0 {
return nil, ErrNotFound
}
var followed *Person = &Person{}
s.Db.Table("people").First(&followed, "url = ?", url)
var f []Follow = make([]Follow, 0)
s.Db.Table("follows").Find(&f, "follows_id = ?", followed.ID)
return f, nil
}

View file

@ -1,37 +0,0 @@
// Copyright (c) 2024 mStar
//
// Licensed under the EUPL, Version 1.2
//
// You may not use this work except in compliance with the Licence.
// You should have received a copy of the Licence along with this work. If not, see:
// <https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12>.
// See the Licence for the specific language governing permissions and limitations under the Licence.
//
package storage
import (
"github.com/lib/pq"
"gorm.io/gorm"
)
type Note struct {
gorm.Model // Includes primary key (uid) as well as various time info
// Public data
Uid string
Content string
CreatorID uint
Creator Person
Instance string // url
Local bool
Tags pq.StringArray `gorm:"type:text[]"`
RepliesTo *Note
RepliesToID *uint
DeliverTo pq.StringArray `gorm:"type:text[]"` // url
// Private data
// None yet
}

View file

@ -1,71 +0,0 @@
// Copyright (c) 2024 mStar
//
// Licensed under the EUPL, Version 1.2
//
// You may not use this work except in compliance with the Licence.
// You should have received a copy of the Licence along with this work. If not, see:
// <https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12>.
// See the Licence for the specific language governing permissions and limitations under the Licence.
//
package storage
import (
"errors"
"github.com/MatejLach/astreams"
"gorm.io/gorm"
)
type Person struct {
gorm.Model // Includes primary key (uid) as well as various time info
// Public data
Uid string
ApID string
Name string
Instance string // url
Local bool
PublicKey string
Url string
ManuallyApprovesFollowers bool
// Private data, set if local == true
Mail *string // mail
PwHash *string
Inbox string // url
Outbox string // url
PrivateKey *string // private key
}
func (pdb *Person) IntoAPPerson() (*astreams.Person, error) {
var actor astreams.Actor
actor.PublicKey = &astreams.PublicKey{
ID: pdb.ApID,
Owner: pdb.Url,
PublicKeyPem: pdb.PublicKey,
}
actor.Inbox = &astreams.StringWithOrderedCollection{
URL: pdb.Inbox,
}
actor.Outbox = &astreams.StringWithOrderedCollection{
URL: pdb.Outbox,
}
actor.ManuallyApprovesFollowers = pdb.ManuallyApprovesFollowers
actor.PreferredUsername = pdb.Name
return nil, errors.New("unimplemented")
}
// Gets a person from the db by name
func (s *Storage) GetPersonByName(name string) (*Person, error) {
var nrFound int64
s.Db.Table("people").Where("name = ?", name).Count(&nrFound)
if nrFound <= 0 {
return nil, ErrNotFound
}
var p Person
s.Db.Table("people").Where("name = ?", name).First(&p)
return &p, nil
}

View file

@ -1,55 +0,0 @@
// Copyright (c) 2024 mStar
//
// Licensed under the EUPL, Version 1.2
//
// You may not use this work except in compliance with the Licence.
// You should have received a copy of the Licence along with this work. If not, see:
// <https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12>.
// See the Licence for the specific language governing permissions and limitations under the Licence.
//
package storage
import (
"errors"
"fmt"
"gitlab.com/mstarongitlab/linstrom/config"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
var ErrNotFound = errors.New("entry not found")
type Storage struct {
Db *gorm.DB
}
func NewDb(cfg *config.Config) (*gorm.DB, error) {
db, err := gorm.Open(postgres.Open(cfg.General.DbPath))
if err != nil {
return nil, fmt.Errorf("failed to connect to db %s: %w", cfg.General.DbPath, err)
}
err = db.AutoMigrate(
&Person{},
&Note{
Tags: make([]string, 0),
DeliverTo: make([]string, 0),
},
&Follow{},
)
if err != nil {
return nil, fmt.Errorf("failed to apply migrations: %w", err)
}
return db, nil
}
func NewStorage(cfg *config.Config) (*Storage, error) {
db, err := NewDb(cfg)
if err != nil {
return nil, fmt.Errorf("failed to create database connection while creating storage: %w", err)
}
return &Storage{
Db: db,
}, nil
}