Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
fix(backend): refactor db schema and fix site reference materials
Browse files Browse the repository at this point in the history
  • Loading branch information
burdiyan committed Nov 27, 2023
1 parent 43fa4f2 commit 25cf762
Show file tree
Hide file tree
Showing 38 changed files with 1,904 additions and 1,551 deletions.
4 changes: 2 additions & 2 deletions backend/cmd/mintter-site/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"os"

"mintter/backend/cmd/mintter-site/sites"
"mintter/backend/daemon"
"mintter/backend/daemon/storage"

"github.com/burdiyan/go/mainutil"
"github.com/peterbourgon/ff/v3"
Expand Down Expand Up @@ -59,7 +59,7 @@ Flags:
return err
}

dir, err := daemon.InitRepo(cfg.Base.DataDir, nil)
dir, err := storage.InitRepo(cfg.Base.DataDir, nil)
if err != nil {
return err
}
Expand Down
43 changes: 12 additions & 31 deletions backend/cmd/mintter-site/sites/sites.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,43 +288,24 @@ func (ws *Website) PublishBlobs(ctx context.Context, in *groups.PublishBlobsRequ
return nil, fmt.Errorf("error getting groupID on the site, is the site initialized?: %w", err)
}

groupOwner, err := storage.GetKV(conn, keySiteOwner)
if err != nil || groupOwner == "" {
return nil, fmt.Errorf("error getting group owner on the site, is the site initialized?: %w", err)
}

var role groups.Role
{
edb, err := hypersql.LookupEnsure(conn, storage.LookupResource, groupID)
if groupOwner == callerAccount.String() {
role = groups.Role_OWNER
} else {
r, err := hypersql.GetGroupRole(conn, groupID, "hm://a/"+callerAccount.String())
if err != nil {
return nil, fmt.Errorf("couldn't get group (%s) resource: %w", groupID, err)
}

owner, err := storage.GetKV(conn, keySiteOwner)
if err != nil || owner == "" {
return nil, fmt.Errorf("error getting owner on the site, is the site initialized?: %w", err)
}

if owner == callerAccount.String() {
role = groups.Role_OWNER
} else {
groupOwner, err := hypersql.ResourceGetOwner(conn, edb)
if err != nil {
return nil, fmt.Errorf("couldn't get the owner of the group %s: %w", groupID, err)
}

pkdb, err := hypersql.LookupEnsure(conn, storage.LookupPublicKey, callerAccount)
if err != nil {
return nil, fmt.Errorf("couldn't get member entity for account [%s]: %w", callerAccount.String(), err)
}

// See if the caller is in the owner's group
r, err := hypersql.GroupGetRole(conn, edb, groupOwner, pkdb)
if err != nil {
return nil, fmt.Errorf("couldn't get role of member %s in group %s: %w", callerAccount.String(), groupID, err)
}

role = groups.Role(r)
return nil, err
}
role = groups.Role(r)
}

if role != groups.Role_OWNER && role != groups.Role_EDITOR {
return nil, status.Errorf(codes.PermissionDenied, "Caller [%s] does not have enough permissions to publish to this site.", callerAccount.String())
return nil, status.Errorf(codes.PermissionDenied, "Caller %q does not have enough permissions to publish to this site.", callerAccount.String())
}

blobs, err := ws.blobs.Await(ctx)
Expand Down
3 changes: 2 additions & 1 deletion backend/cmd/mintter-site/sites/sites_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"mintter/backend/config"
"mintter/backend/core/coretest"
"mintter/backend/daemon"
"mintter/backend/daemon/storage"
accounts "mintter/backend/genproto/accounts/v1alpha"
groups "mintter/backend/genproto/groups/v1alpha"
"mintter/backend/ipfs"
Expand Down Expand Up @@ -121,7 +122,7 @@ func makeTestSite(t *testing.T, name string) *App {
user := coretest.NewTester(name)

cfg := testConfig(t)
dir, err := daemon.InitRepo(cfg.Base.DataDir, user.Device.Wrapped())
dir, err := storage.InitRepo(cfg.Base.DataDir, user.Device.Wrapped())
require.NoError(t, err)

app, err := Load(ctx, "http://127.0.0.1:"+strconv.Itoa(cfg.HTTP.Port), cfg, dir)
Expand Down
3 changes: 2 additions & 1 deletion backend/cmd/mintterd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"mintter/backend/config"
"mintter/backend/daemon"
"mintter/backend/daemon/storage"
"mintter/backend/logging"

"github.com/burdiyan/go/mainutil"
Expand Down Expand Up @@ -67,7 +68,7 @@ func main() {
defer sentry.Flush(2 * time.Second)
}

dir, err := daemon.InitRepo(cfg.Base.DataDir, nil)
dir, err := storage.InitRepo(cfg.Base.DataDir, nil)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion backend/cmd/mkdb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"mintter/backend/config"
"mintter/backend/core/coretest"
"mintter/backend/daemon"
"mintter/backend/daemon/storage"
"os"

"github.com/burdiyan/go/mainutil"
Expand Down Expand Up @@ -38,7 +39,7 @@ func run() error {
return err
}

dir, err := daemon.InitRepo(cfg.Base.DataDir, alice.Device.Wrapped())
dir, err := storage.InitRepo(cfg.Base.DataDir, alice.Device.Wrapped())
if err != nil {
return err
}
Expand Down
7 changes: 7 additions & 0 deletions backend/core/principal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"encoding/binary"
"fmt"
"unsafe"

"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
Expand Down Expand Up @@ -65,6 +66,12 @@ func (p Principal) String() string {
return s
}

// UnsafeString casts raw bytes to a without copying.
// Useful for using as map key.
func (p Principal) UnsafeString() string {
return unsafe.String(&p[0], len(p))
}

// Verify implements Verifier.
func (p Principal) Verify(data []byte, sig Signature) error {
code, key := p.Explode()
Expand Down
14 changes: 14 additions & 0 deletions backend/core/principal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package core

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestPrincipalUnsafeString(t *testing.T) {
// Hardcoded key generated offline.
me, err := DecodePrincipal("z6Mkv1LjkRosErBhmqrkmb5sDxXNs6EzBDSD8ktywpYLLGuC")
require.NoError(t, err)
require.Equal(t, string(me), me.UnsafeString())
}
15 changes: 9 additions & 6 deletions backend/daemon/api/documents/v1alpha/content_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ func (srv *Server) ListCitations(ctx context.Context, in *documents.ListCitation

targetEntity := in.DocumentId

var backlinks []hypersql.BacklinksForEntityResult
var backlinks []hypersql.BacklinksForDocumentResult
if err := srv.blobs.Query(ctx, func(conn *sqlite.Conn) error {
edb, err := hypersql.EntitiesLookupID(conn, targetEntity)
if err != nil {
return err
}
if edb.ResourcesID == 0 {
return status.Error(codes.NotFound, "document not found")
}

list, err := hypersql.BacklinksForEntity(conn, edb.EntitiesID)
list, err := hypersql.BacklinksForDocument(conn, edb.ResourcesID)
backlinks = list
return err
}); err != nil {
Expand All @@ -41,17 +44,17 @@ func (srv *Server) ListCitations(ctx context.Context, in *documents.ListCitation
}

for i, link := range backlinks {
var ld hyper.LinkData
if err := json.Unmarshal(link.BlobAttrsExtra, &ld); err != nil {
var ld hyper.DocLinkMeta
if err := json.Unmarshal(link.ResourceLinksMeta, &ld); err != nil {
return nil, fmt.Errorf("failed to decode link data: %w", err)
}

src := cid.NewCidV1(uint64(link.BlobsCodec), link.BlobsMultihash)

resp.Links[i] = &documents.Link{
Source: &documents.LinkNode{
DocumentId: link.EntitiesEID,
BlockId: link.BlobAttrsAnchor,
DocumentId: link.ResourcesIRI,
BlockId: ld.Anchor,
Version: src.String(),
},
Target: &documents.LinkNode{
Expand Down
9 changes: 3 additions & 6 deletions backend/daemon/api/documents/v1alpha/content_graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ func TestBacklinks(t *testing.T) {
Type: "link",
Starts: []int32{0},
Ends: []int32{5},
Attributes: map[string]string{
"url": pub.Document.Id + "?v=" + pub.Version + "#b1",
},
Ref: pub.Document.Id + "?v=" + pub.Version + "#b1",
},
},
}}},
Expand All @@ -63,9 +61,7 @@ func TestBacklinks(t *testing.T) {
Type: "link",
Starts: []int32{0},
Ends: []int32{5},
Attributes: map[string]string{
"url": pub.Document.Id + "?v=" + pub.Version,
},
Ref: pub.Document.Id + "?v=" + pub.Version,
},
},
}}},
Expand Down Expand Up @@ -122,6 +118,7 @@ func TestBacklinks(t *testing.T) {
},
}

require.Equal(t, len(want.Links), len(cits.Links), "number of links must match")
diff := cmp.Diff(makeSet(want.Links), makeSet(cits.Links), testutil.ExportedFieldsFilter())
if diff != "" {
t.Fatal(diff)
Expand Down
12 changes: 6 additions & 6 deletions backend/daemon/api/entities/v1alpha/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ func getChange(conn *sqlite.Conn, c cid.Cid, id int64) (*entities.Change, error)
out = &entities.Change{
Id: c.String(),
Author: core.Principal(info.PublicKeysPrincipal).String(),
CreateTime: timestamppb.New(hlc.Unpack(info.ChangesHLCTime).Time()),
CreateTime: timestamppb.New(hlc.Unpack(info.StructuralBlobsTs).Time()),
IsTrusted: info.IsTrusted > 0,
}

deps, err := hypersql.ChangesGetDeps(conn, info.ChangesBlob)
deps, err := hypersql.ChangesGetDeps(conn, info.StructuralBlobsID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -116,11 +116,11 @@ func (api *Server) GetEntityTimeline(ctx context.Context, in *entities.GetEntity
if err != nil {
return err
}
if eid.EntitiesID == 0 {
if eid.ResourcesID == 0 {
return errutil.NotFound("no such entity %s", in.Id)
}

changes, err := hypersql.ChangesInfoForEntity(conn, eid.EntitiesID)
changes, err := hypersql.ChangesInfoForEntity(conn, eid.ResourcesID)
if err != nil {
return err
}
Expand All @@ -137,13 +137,13 @@ func (api *Server) GetEntityTimeline(ctx context.Context, in *entities.GetEntity
chpb := &entities.Change{
Id: cs,
Author: core.Principal(ch.PublicKeysPrincipal).String(),
CreateTime: timestamppb.New(hlc.Unpack(ch.ChangesHLCTime).Time()),
CreateTime: timestamppb.New(hlc.Unpack(ch.StructuralBlobsTs).Time()),
IsTrusted: ch.IsTrusted > 0,
}
heads[cs] = struct{}{}
out.ChangesByTime = append(out.ChangesByTime, cs)

deps, err := hypersql.ChangesGetDeps(conn, ch.ChangesBlob)
deps, err := hypersql.ChangesGetDeps(conn, ch.StructuralBlobsID)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 25cf762

Please sign in to comment.