Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
2.5 kB · 82 lines
Go
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283package model
import ( "context" "errors" "fmt" "time"
glex "github.com/streamplace/glex/runtime" "gorm.io/gorm" "stream.place/streamplace/pkg/appbsky" "stream.place/streamplace/pkg/placestream")
type Block struct { RKey string `gorm:"primaryKey;column:rkey"` CID string `gorm:"column:cid"` RepoDID string `json:"repoDID" gorm:"column:repo_did;index:idx_repo_did_subject_did,priority:1"` Repo *Repo `json:"repo,omitempty" gorm:"foreignKey:DID;references:RepoDID"` SubjectDID string `gorm:"column:subject_did;index:idx_repo_did_subject_did,priority:2"` Record []byte CreatedAt time.Time}
func (b *Block) ToStreamplaceBlock() (placestream.Defs_BlockView, error) { if b == nil { return placestream.Defs_BlockView{}, fmt.Errorf("block is nil") } if b.Repo == nil { return placestream.Defs_BlockView{}, fmt.Errorf("block repo is nil") } if b.Record == nil { return placestream.Defs_BlockView{}, fmt.Errorf("block record is nil") } var block appbsky.GraphBlock if err := glex.DecodeCBOR(b.Record, &block); err != nil { return placestream.Defs_BlockView{}, fmt.Errorf("error decoding block record: %w", err) } return placestream.Defs_BlockView{ LexiconTypeID: "place.stream.defs#blockView", Blocker: appbsky.ActorDefs_ProfileViewBasic{ Did: b.RepoDID, Handle: b.Repo.Handle, }, Cid: b.CID, IndexedAt: b.CreatedAt.Format(time.RFC3339), Record: block, Uri: fmt.Sprintf(`at://%s/app.bsky.graph.block/%s`, b.RepoDID, b.RKey), }, nil}
func (m *DBModel) CreateBlock(ctx context.Context, block *Block) error { return createOrVerify(ctx, m, block, map[string]any{"rkey": block.RKey})}
func (m *DBModel) DeleteBlock(ctx context.Context, rkey string) error { return m.DB.Where("rkey = ?", rkey).Delete(&Block{}).Error}
func (m *DBModel) GetBlock(ctx context.Context, rkey string) (*Block, error) { var block Block err := m.DB.Preload("Repo").Where("rkey = ?", rkey).First(&block).Error if errors.Is(err, gorm.ErrRecordNotFound) { return nil, nil } if err != nil { return nil, err } return &block, nil}
func (m *DBModel) GetUserBlock(ctx context.Context, userDID, subjectDID string) (*Block, error) { var block Block err := m.DB.Where("repo_did = ? AND subject_did = ?", userDID, subjectDID).First(&block).Error if errors.Is(err, gorm.ErrRecordNotFound) { return nil, nil } if err != nil { return nil, err } return &block, nil}