Skip to content

Commit

Permalink
update association
Browse files Browse the repository at this point in the history
  • Loading branch information
totegamma committed Mar 23, 2024
1 parent 4a1205e commit 564ec7a
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 95 deletions.
36 changes: 0 additions & 36 deletions x/association/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ var tracer = otel.Tracer("association")
// Handler is the interface for handling HTTP requests
type Handler interface {
Get(c echo.Context) error
Post(c echo.Context) error
Delete(c echo.Context) error
GetFiltered(c echo.Context) error
GetCounts(c echo.Context) error
GetOwnByTarget(c echo.Context) error
Expand Down Expand Up @@ -112,37 +110,3 @@ func (h handler) GetFiltered(c echo.Context) error {
return c.JSON(http.StatusOK, echo.Map{"status": "ok", "content": associations})
}
}

// Post creates a new association
// returns the created association
func (h handler) Post(c echo.Context) error {
ctx, span := tracer.Start(c.Request().Context(), "HandlerPost")
defer span.End()

var request postRequest
err := c.Bind(&request)
if err != nil {
return err
}
created, err := h.service.PostAssociation(ctx, request.SignedObject, request.Signature, request.Streams, request.TargetType)
if err != nil {
return err
}
return c.JSON(http.StatusCreated, echo.Map{"status": "ok", "content": created})
}

// Delete deletes an association by ID
// returns the deleted association
func (h handler) Delete(c echo.Context) error {
ctx, span := tracer.Start(c.Request().Context(), "HandlerDelete")
defer span.End()

associationID := c.Param("id")
requester, _ := c.Get(core.RequesterIdCtxKey).(string)

deleted, err := h.service.Delete(ctx, associationID, requester)
if err != nil {
return err
}
return c.JSON(http.StatusOK, echo.Map{"status": "ok", "content": deleted})
}
28 changes: 0 additions & 28 deletions x/association/model.go

This file was deleted.

49 changes: 26 additions & 23 deletions x/association/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (

// Service is the interface for association service
type Service interface {
PostAssociation(ctx context.Context, objectStr string, signature string, timelines []string, targetType string) (core.Association, error)
Create(ctx context.Context, documentStr string, signature string) (core.Association, error)
Delete(ctx context.Context, documentStr string) (core.Association, error)

Get(ctx context.Context, id string) (core.Association, error)
GetOwn(ctx context.Context, author string) ([]core.Association, error)
Delete(ctx context.Context, id, requester string) (core.Association, error)

GetByTarget(ctx context.Context, targetID string) ([]core.Association, error)
GetCountsBySchema(ctx context.Context, messageID string) (map[string]int64, error)
GetBySchema(ctx context.Context, messageID string, schema string) ([]core.Association, error)
Expand Down Expand Up @@ -51,31 +51,25 @@ func (s *service) Count(ctx context.Context) (int64, error) {
// PostAssociation creates a new association
// If targetType is messages, it also posts the association to the target message's timelines
// returns the created association
func (s *service) PostAssociation(ctx context.Context, objectStr string, signature string, timelines []string, targetType string) (core.Association, error) {
func (s *service) Create(ctx context.Context, documentStr string, signature string) (core.Association, error) {
ctx, span := tracer.Start(ctx, "ServicePostAssociation")
defer span.End()

err := s.key.ValidateSignedObject(ctx, objectStr, signature)
if err != nil {
span.RecordError(err)
return core.Association{}, err
}

var object SignedObject
err = json.Unmarshal([]byte(objectStr), &object)
var document core.CreateAssociation[any]
err := json.Unmarshal([]byte(documentStr), &document)
if err != nil {
span.RecordError(err)
return core.Association{}, err
}

association := core.Association{
Author: object.Signer,
Schema: object.Schema,
TargetTID: object.Target,
Payload: objectStr,
Author: document.Signer,
Schema: document.Schema,
TargetTID: document.Target,
Payload: documentStr,
Signature: signature,
Timelines: timelines,
Variant: object.Variant,
Timelines: document.Timelines,
Variant: document.Variant,
}

created, err := s.repo.Create(ctx, association)
Expand All @@ -84,11 +78,11 @@ func (s *service) PostAssociation(ctx context.Context, objectStr string, signatu
return created, err // TODO: if err is duplicate key error, server should return 409
}

if targetType != "messages" { // distribute is needed only when targetType is messages
if document.Target[0] != 'm' {
return created, nil
}

targetMessage, err := s.message.Get(ctx, created.TargetTID, object.Signer)
targetMessage, err := s.message.Get(ctx, created.TargetTID, document.Signer)
if err != nil {
span.RecordError(err)
return created, err
Expand Down Expand Up @@ -145,16 +139,25 @@ func (s *service) GetOwn(ctx context.Context, author string) ([]core.Association
}

// Delete deletes an association by ID
func (s *service) Delete(ctx context.Context, id, requester string) (core.Association, error) {
func (s *service) Delete(ctx context.Context, documentStr string) (core.Association, error) {
ctx, span := tracer.Start(ctx, "ServiceDelete")
defer span.End()

targetAssociation, err := s.repo.Get(ctx, id)
var document core.DeleteAssociation
err := json.Unmarshal([]byte(documentStr), &document)
if err != nil {
span.RecordError(err)
return core.Association{}, err
}

targetAssociation, err := s.repo.Get(ctx, document.Body.TargetID)
if err != nil {
span.RecordError(err)
return core.Association{}, err
}

requester := document.Signer

targetMessage, err := s.message.Get(ctx, targetAssociation.TargetTID, requester)
if err != nil {
span.RecordError(err)
Expand All @@ -165,7 +168,7 @@ func (s *service) Delete(ctx context.Context, id, requester string) (core.Associ
return core.Association{}, fmt.Errorf("you are not authorized to perform this action")
}

deleted, err := s.repo.Delete(ctx, id)
deleted, err := s.repo.Delete(ctx, document.Body.TargetID)
if err != nil {
span.RecordError(err)
return core.Association{}, err
Expand Down
6 changes: 3 additions & 3 deletions x/core/documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ type DeleteMessage struct { // type: d.message
// association
type CreateAssociation[T any] struct { // type: c.association
DocumentBase[T]
Streams []string `json:"streams"`
Variant string `json:"variant"`
Target string `json:"target"`
Timelines []string `json:"timelines"`
Variant string `json:"variant"`
Target string `json:"target"`
}

type DeleteAssociation struct { // type: d.association
Expand Down
25 changes: 20 additions & 5 deletions x/store/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package store
import (
"context"
"encoding/json"
"fmt"

"github.com/totegamma/concurrent/x/association"
"github.com/totegamma/concurrent/x/core"
"github.com/totegamma/concurrent/x/key"
"github.com/totegamma/concurrent/x/message"
Expand All @@ -14,12 +16,21 @@ type Service interface {
}

type service struct {
key key.Service
message message.Service
key key.Service
message message.Service
association association.Service
}

func NewService() Service {
return &service{}
func NewService(
key key.Service,
message message.Service,
association association.Service,
) Service {
return &service{
key: key,
message: message,
association: association,
}
}

func (s *service) Commit(ctx context.Context, document string, signature string) (any, error) {
Expand All @@ -43,7 +54,11 @@ func (s *service) Commit(ctx context.Context, document string, signature string)
return s.message.Create(ctx, document, signature)
case "message.delete":
return s.message.Delete(ctx, document)
case "association.create":
return s.association.Create(ctx, document, signature)
case "association.delete":
return s.association.Delete(ctx, document)
}

return nil, nil
return nil, fmt.Errorf("unknown document type: %s", base.Type)
}

0 comments on commit 564ec7a

Please sign in to comment.