Skip to content

Commit

Permalink
feat: query block
Browse files Browse the repository at this point in the history
- render styles: card and list
- search by site
- sort by terms: UpdateTime, CreateTime and Title (Path soon)
- reverse sorting
- render both on app and web
  • Loading branch information
horacioh committed Nov 28, 2024
1 parent 72db58f commit 91a08d3
Show file tree
Hide file tree
Showing 53 changed files with 3,174 additions and 1,076 deletions.
27 changes: 21 additions & 6 deletions backend/api/documents/v3alpha/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,18 @@ func (srv *Server) ListComments(ctx context.Context, in *documents.ListCommentsR
}

func commentToProto(c cid.Cid, cmt *blob.Comment) (*documents.Comment, error) {
content, err := commentContentToProto(cmt.Body)
if err != nil {
return nil, err
}

pb := &documents.Comment{
Id: c.String(),
TargetAccount: cmt.GetSpace().String(),
TargetPath: cmt.Path,
TargetVersion: docmodel.NewVersion(cmt.Version...).String(),
Author: cmt.Signer.String(),
Content: commentContentToProto(cmt.Body),
Content: content,
CreateTime: timestamppb.New(cmt.Ts),
}

Expand All @@ -175,20 +180,30 @@ func commentToProto(c cid.Cid, cmt *blob.Comment) (*documents.Comment, error) {
return pb, nil
}

func commentContentToProto(in []blob.CommentBlock) []*documents.BlockNode {
func commentContentToProto(in []blob.CommentBlock) ([]*documents.BlockNode, error) {
if in == nil {
return nil
return nil, nil
}

out := make([]*documents.BlockNode, len(in))
for i, b := range in {
blockpb, err := docmodel.BlockToProto(b.Block, cid.Undef)
if err != nil {
return nil, err
}

children, err := commentContentToProto(b.Children)
if err != nil {
return nil, err
}

out[i] = &documents.BlockNode{
Block: docmodel.BlockToProto(b.Block, cid.Undef),
Children: commentContentToProto(b.Children),
Block: blockpb,
Children: children,
}
}

return out
return out, nil
}

func commentContentFromProto(in []*documents.BlockNode) []blob.CommentBlock {
Expand Down
37 changes: 18 additions & 19 deletions backend/api/documents/v3alpha/docmodel/docmodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/ipfs/go-cid"
"github.com/multiformats/go-multibase"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/timestamppb"
)

Expand Down Expand Up @@ -553,7 +554,10 @@ func (dm *Document) Hydrate(ctx context.Context) (*documents.Document, error) {
panic(fmt.Errorf("BUG: failed to find CID for block op ID: %d:%d", opid.Actor, opid.Ts))
}

blkpb := BlockToProto(blk, c)
blkpb, err := BlockToProto(blk, c)
if err != nil {
return nil, err
}

child := &documents.BlockNode{Block: blkpb}
appendChild(pair.Parent, child)
Expand All @@ -570,13 +574,7 @@ func BlockFromProto(b *documents.Block) (blob.Block, error) {
return blob.Block{}, errors.New("block ID is required")
}

var remaining map[string]any
if len(b.Attributes) > 0 {
remaining = make(map[string]any, len(b.Attributes))
for k, v := range b.Attributes {
remaining[k] = v
}
}
remaining := b.Attributes.AsMap()

return blob.Block{
ID: b.Id,
Expand Down Expand Up @@ -617,24 +615,25 @@ func annotationsFromProto(in []*documents.Annotation) []blob.Annotation {

// BlockToProto converts our internal block representation into a protobuf block.
// It's largely the same, but we use CBOR in our permanent data, and we use protobuf in our API.
func BlockToProto(b blob.Block, revision cid.Cid) *documents.Block {
var attrs map[string]string
if len(b.Attributes) > 0 {
attrs = make(map[string]string, len(b.Attributes))
for k, v := range b.Attributes {
attrs[k], _ = v.(string)
}
}

return &documents.Block{
func BlockToProto(b blob.Block, revision cid.Cid) (*documents.Block, error) {
bpb := &documents.Block{
Id: b.ID,
Type: b.Type,
Text: b.Text,
Link: b.Link,
Attributes: attrs,
Attributes: nil,
Annotations: annotationsToProto(b.Annotations),
Revision: revision.String(),
}
if len(b.Attributes) > 0 {
attrs, err := structpb.NewStruct(b.Attributes)
if err != nil {
return nil, err
}
bpb.Attributes = attrs
}

return bpb, nil
}

func annotationsToProto(in []blob.Annotation) []*documents.Annotation {
Expand Down
7 changes: 6 additions & 1 deletion backend/api/networking/v1alpha/networking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package networking

import (
"context"
"errors"
"seed/backend/blob"
"seed/backend/config"
"seed/backend/core"
Expand All @@ -15,6 +16,7 @@ import (

"github.com/stretchr/testify/require"
"go.uber.org/zap"
"google.golang.org/grpc"
)

func TestNetworkingGetPeerInfo(t *testing.T) {
Expand Down Expand Up @@ -55,7 +57,10 @@ func makeTestServer(t *testing.T, u coretest.Tester) *Server {
}()

t.Cleanup(func() {
require.NoError(t, <-errc)
err := <-errc
if err != nil && !errors.Is(err, grpc.ErrServerStopped) {
t.Fatal(err)
}
})

select {
Expand Down
38 changes: 2 additions & 36 deletions backend/blob/blob_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"seed/backend/core"
"seed/backend/ipfs"
"slices"
"strconv"
"time"

"github.com/go-viper/mapstructure/v2"
Expand Down Expand Up @@ -84,8 +83,6 @@ func (c *Change) Ops() iter.Seq2[Op, error] {
}
}

var numericAttributes = []string{"size", "width", "height", "start"}

func init() {
cbornode.RegisterCborType(Change{})
cbornode.RegisterCborType(ChangeBody{})
Expand All @@ -107,44 +104,13 @@ func init() {
return nil, err
}

if len(in.Attributes) > 0 {
// TODO(burdiyan): we do this trick because our API currently only accepts string values,
// but we want to be able to store other types as well.
// These are the attributes that we currently have that we know should be numbers.
for _, attr := range numericAttributes {
vv, ok := v[attr].(string)
if !ok {
continue
}

n, err := strconv.Atoi(vv)
_ = err
v[attr] = n
}
}

return v, nil
})).
TransformUnmarshal(atlas.MakeUnmarshalTransformFunc(func(in map[string]any) (Block, error) {
var v Block
if err := mapstructure.Decode(in, &v); err != nil {
return Block{}, err
return v, err
}

if len(v.Attributes) > 0 {
// TODO(burdiyan): we do this trick because our API currently only accepts string values,
// but we want to be able to store other types as well.
// These are the attributes that we currently have that we know should be numbers.
for _, attr := range numericAttributes {
vv, ok := v.Attributes[attr].(int)
if !ok {
continue
}

v.Attributes[attr] = strconv.Itoa(vv)
}
}

return v, nil
})).
Complete(),
Expand All @@ -161,7 +127,7 @@ func init() {
TransformUnmarshal(atlas.MakeUnmarshalTransformFunc(func(in map[string]any) (Annotation, error) {
var v Annotation
if err := mapstructure.Decode(in, &v); err != nil {
return Annotation{}, err
return v, err
}
return v, nil
})).
Expand Down
Loading

0 comments on commit 91a08d3

Please sign in to comment.