chore: changes to salty service
This commit is contained in:
@@ -16,7 +16,6 @@ import (
|
||||
)
|
||||
|
||||
type SaltyUser struct {
|
||||
name string
|
||||
pubkey *keys.EdX25519PublicKey
|
||||
inbox ulid.ULID
|
||||
|
||||
@@ -30,30 +29,25 @@ func (a *SaltyUser) ApplyEvent(lis ...event.Event) {
|
||||
for _, e := range lis {
|
||||
switch e := e.(type) {
|
||||
case *UserRegistered:
|
||||
a.name = e.Name
|
||||
// a.name = e.Name
|
||||
a.pubkey = e.Pubkey
|
||||
a.inbox = e.EventMeta().EventID
|
||||
a.SetStreamID(a.streamID())
|
||||
// a.SetStreamID(a.streamID())
|
||||
default:
|
||||
log.Printf("unknown event %T", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (a *SaltyUser) streamID() string {
|
||||
return fmt.Sprintf("saltyuser-%x", sha256.Sum256([]byte(strings.ToLower(a.name))))
|
||||
}
|
||||
|
||||
func (a *SaltyUser) OnUserRegister(name string, pubkey *keys.EdX25519PublicKey) error {
|
||||
func (a *SaltyUser) OnUserRegister(pubkey *keys.EdX25519PublicKey) error {
|
||||
if err := event.NotExists(a); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
event.Raise(a, &UserRegistered{Name: name, Pubkey: pubkey})
|
||||
event.Raise(a, &UserRegistered{Pubkey: pubkey})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *SaltyUser) Nick() string { return a.name }
|
||||
func (a *SaltyUser) Inbox() string { return a.inbox.String() }
|
||||
func (a *SaltyUser) Pubkey() string { return a.pubkey.String() }
|
||||
func (s *SaltyUser) Endpoint(ctx context.Context) (string, error) {
|
||||
|
||||
@@ -7,7 +7,6 @@ extend type Mutation {
|
||||
}
|
||||
|
||||
type SaltyUser @goModel(model: "github.com/sour-is/ev/app/salty.SaltyUser"){
|
||||
nick: String!
|
||||
pubkey: String!
|
||||
inbox: String!
|
||||
endpoint: String!
|
||||
|
||||
@@ -177,9 +177,9 @@ func (s *service) CreateSaltyUser(ctx context.Context, nick string, pub string)
|
||||
streamID := NickToStreamID(nick)
|
||||
span.AddEvent(streamID)
|
||||
|
||||
return s.createSaltyUser(ctx, nick, streamID, pub)
|
||||
return s.createSaltyUser(ctx, streamID, pub)
|
||||
}
|
||||
func (s *service) createSaltyUser(ctx context.Context, nick, streamID, pub string) (*SaltyUser, error) {
|
||||
func (s *service) createSaltyUser(ctx context.Context, streamID, pub string) (*SaltyUser, error) {
|
||||
ctx, span := lg.Span(ctx)
|
||||
defer span.End()
|
||||
|
||||
@@ -190,16 +190,16 @@ func (s *service) createSaltyUser(ctx context.Context, nick, streamID, pub strin
|
||||
}
|
||||
|
||||
a, err := es.Create(ctx, s.es, streamID, func(ctx context.Context, agg *SaltyUser) error {
|
||||
return agg.OnUserRegister(nick, key)
|
||||
return agg.OnUserRegister(key)
|
||||
})
|
||||
switch {
|
||||
case errors.Is(err, es.ErrShouldNotExist):
|
||||
span.RecordError(err)
|
||||
return nil, fmt.Errorf("user exists")
|
||||
return nil, fmt.Errorf("user exists: %w", err)
|
||||
|
||||
case err != nil:
|
||||
span.RecordError(err)
|
||||
return nil, fmt.Errorf("internal error")
|
||||
return nil, fmt.Errorf("internal error: %w", err)
|
||||
}
|
||||
|
||||
return a, nil
|
||||
@@ -296,14 +296,15 @@ func (s *service) apiv1(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = s.createSaltyUser(ctx, "", HashToStreamID(req.Hash), req.Key)
|
||||
_, err = s.createSaltyUser(ctx, HashToStreamID(req.Hash), req.Key)
|
||||
if errors.Is(err, event.ErrShouldNotExist) {
|
||||
http.Error(w, "Already Exists", http.StatusConflict)
|
||||
return
|
||||
} else if err != nil {
|
||||
http.Error(w, "Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
http.Error(w, "Endpoint Created", http.StatusCreated)
|
||||
return
|
||||
|
||||
|
||||
Reference in New Issue
Block a user