2022-08-19 12:26:42 -06:00
|
|
|
package salty
|
2022-08-14 10:04:15 -06:00
|
|
|
|
|
|
|
import (
|
2022-08-14 10:56:00 -06:00
|
|
|
"bytes"
|
2022-08-14 10:04:15 -06:00
|
|
|
"context"
|
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2022-09-07 12:26:14 -06:00
|
|
|
"net/url"
|
2022-08-14 10:04:15 -06:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/keys-pub/keys"
|
|
|
|
"github.com/oklog/ulid/v2"
|
|
|
|
"github.com/sour-is/ev/pkg/es/event"
|
2022-08-19 12:26:42 -06:00
|
|
|
"github.com/sour-is/ev/pkg/gql"
|
2022-08-14 10:04:15 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type SaltyUser struct {
|
2022-08-19 12:26:42 -06:00
|
|
|
pubkey *keys.EdX25519PublicKey
|
|
|
|
inbox ulid.ULID
|
2022-08-14 10:04:15 -06:00
|
|
|
|
|
|
|
event.AggregateRoot
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ event.Aggregate = (*SaltyUser)(nil)
|
|
|
|
|
|
|
|
// ApplyEvent applies the event to the aggrigate state
|
|
|
|
func (a *SaltyUser) ApplyEvent(lis ...event.Event) {
|
|
|
|
for _, e := range lis {
|
|
|
|
switch e := e.(type) {
|
|
|
|
case *UserRegistered:
|
2022-10-25 16:07:46 -06:00
|
|
|
// a.name = e.Name
|
2022-08-19 12:26:42 -06:00
|
|
|
a.pubkey = e.Pubkey
|
|
|
|
a.inbox = e.EventMeta().EventID
|
2022-10-25 16:07:46 -06:00
|
|
|
// a.SetStreamID(a.streamID())
|
2022-08-14 10:04:15 -06:00
|
|
|
default:
|
|
|
|
log.Printf("unknown event %T", e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-25 16:07:46 -06:00
|
|
|
func (a *SaltyUser) OnUserRegister(pubkey *keys.EdX25519PublicKey) error {
|
2022-10-13 15:32:25 -06:00
|
|
|
if err := event.NotExists(a); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-25 16:07:46 -06:00
|
|
|
event.Raise(a, &UserRegistered{Pubkey: pubkey})
|
2022-08-14 10:04:15 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-19 12:26:42 -06:00
|
|
|
func (a *SaltyUser) Inbox() string { return a.inbox.String() }
|
|
|
|
func (a *SaltyUser) Pubkey() string { return a.pubkey.String() }
|
2022-09-07 12:26:14 -06:00
|
|
|
func (s *SaltyUser) Endpoint(ctx context.Context) (string, error) {
|
2022-08-19 12:26:42 -06:00
|
|
|
svc := gql.FromContext[contextKey, *service](ctx, saltyKey)
|
2022-09-07 12:26:14 -06:00
|
|
|
return url.JoinPath(svc.BaseURL(), s.inbox.String())
|
2022-08-19 12:26:42 -06:00
|
|
|
}
|
|
|
|
|
2022-08-14 10:04:15 -06:00
|
|
|
type UserRegistered struct {
|
2022-08-14 10:56:00 -06:00
|
|
|
Name string
|
|
|
|
Pubkey *keys.EdX25519PublicKey
|
2022-08-14 10:04:15 -06:00
|
|
|
|
|
|
|
eventMeta event.Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ event.Event = (*UserRegistered)(nil)
|
|
|
|
|
|
|
|
func (e *UserRegistered) EventMeta() event.Meta {
|
|
|
|
if e == nil {
|
|
|
|
return event.Meta{}
|
|
|
|
}
|
|
|
|
return e.eventMeta
|
|
|
|
}
|
|
|
|
func (e *UserRegistered) SetEventMeta(m event.Meta) {
|
|
|
|
if e != nil {
|
|
|
|
e.eventMeta = m
|
|
|
|
}
|
|
|
|
}
|
2022-08-15 08:05:04 -06:00
|
|
|
func (e *UserRegistered) MarshalBinary() (text []byte, err error) {
|
2022-08-14 10:56:00 -06:00
|
|
|
var b bytes.Buffer
|
|
|
|
b.WriteString(e.Name)
|
|
|
|
b.WriteRune('\t')
|
|
|
|
b.WriteString(e.Pubkey.String())
|
|
|
|
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
2022-08-15 08:05:04 -06:00
|
|
|
func (e *UserRegistered) UnmarshalBinary(b []byte) error {
|
2022-08-14 10:56:00 -06:00
|
|
|
name, pub, ok := bytes.Cut(b, []byte{'\t'})
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("parse error")
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
e.Name = string(name)
|
|
|
|
e.Pubkey, err = keys.NewEdX25519PublicKeyFromID(keys.ID(pub))
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2022-10-13 15:32:25 -06:00
|
|
|
|
|
|
|
func NickToStreamID(nick string) string {
|
|
|
|
return fmt.Sprintf("saltyuser-%x", sha256.Sum256([]byte(strings.ToLower(nick))))
|
|
|
|
}
|
|
|
|
func HashToStreamID(hash string) string {
|
|
|
|
return fmt.Sprint("saltyuser-", hash)
|
|
|
|
}
|