Uuuh, lots of stuff

More config, moved files, some endpoints (wip), some storage
This commit is contained in:
mStar aka a person 2024-01-19 10:21:31 +00:00
parent f3514b5a42
commit 935fc33094
18 changed files with 394 additions and 70 deletions

View file

@ -1,59 +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 (
"crypto/rsa"
"net/mail"
"net/url"
"gorm.io/gorm"
)
type personDB struct {
gorm.Model // Includes primary key (uid) as well as various time info
// Public data
uid string
name string
instance url.URL
local bool
publicKey string
// Private data, set if local == true
mail *mail.Address
pwHash *string
inbox url.URL
privateKey *rsa.PrivateKey
following []url.URL
followers []url.URL
}
type noteDB struct {
gorm.Model // Includes primary key (uid) as well as various time info
// Public data
uid string
content string
creator url.URL
instance url.URL
local bool
tags []string
repliesTo *url.URL
deliverTo []url.URL
// Private data
// None yet
}

26
storage/follows.go Normal file
View file

@ -0,0 +1,26 @@
// 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 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
}

37
storage/note.go Normal file
View file

@ -0,0 +1,37 @@
// 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
}

59
storage/person.go Normal file
View file

@ -0,0 +1,59 @@
// 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")
}

View file

@ -13,16 +13,40 @@ package storage
import (
"fmt"
"gitlab.com/beckersam/linstrom/config"
"gitlab.com/mstarongitlab/linstrom/config"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
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)
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)
}
db.AutoMigrate(&personDB{}, &noteDB{})
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
}