Skip to content

Commit

Permalink
fix(backend): use full EIDs in the document APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
burdiyan committed Sep 11, 2023
1 parent a21faa9 commit 4dc6a36
Show file tree
Hide file tree
Showing 18 changed files with 180 additions and 203 deletions.
6 changes: 3 additions & 3 deletions backend/daemon/api/accounts/v1alpha/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,20 +284,20 @@ func (srv *Server) ListAccounts(ctx context.Context, in *accounts.ListAccountsRe
return nil, err
}

mine := me.Account().String()
mine := hyper.EntityID("hm://a/" + me.Account().String())

resp := &accounts.ListAccountsResponse{
Accounts: make([]*accounts.Account, 0, len(entities)-1), // all except our own account.
}

for _, e := range entities {
aid := e.TrimPrefix("hm://a/")
aid := e
if aid == mine {
continue
}

draft, err := srv.GetAccount(ctx, &accounts.GetAccountRequest{
Id: aid,
Id: aid.TrimPrefix("hm://a/"),
})
if err != nil {
continue
Expand Down
2 changes: 1 addition & 1 deletion backend/daemon/api/documents/v1alpha/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (api *Server) ListChanges(ctx context.Context, in *documents.ListChangesReq
return nil, status.Errorf(codes.InvalidArgument, "must provide document id")
}

eid := hyper.EntityID("hm://d/" + in.DocumentId)
eid := hyper.EntityID(in.DocumentId)

out := &documents.ListChangesResponse{}

Expand Down
4 changes: 2 additions & 2 deletions backend/daemon/api/documents/v1alpha/content_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (srv *Server) ListCitations(ctx context.Context, in *documents.ListCitation
return nil, status.Error(codes.InvalidArgument, "must specify document ID")
}

targetEntity := "hm://d/" + in.DocumentId
targetEntity := in.DocumentId

var backlinks []hypersql.BacklinksForEntityResult
if err := srv.blobs.Query(ctx, func(conn *sqlite.Conn) error {
Expand Down Expand Up @@ -50,7 +50,7 @@ func (srv *Server) ListCitations(ctx context.Context, in *documents.ListCitation

resp.Links[i] = &documents.Link{
Source: &documents.LinkNode{
DocumentId: hyper.EntityID(link.EntitiesEID).TrimPrefix("hm://d/"),
DocumentId: link.EntitiesEID,
BlockId: link.BlobAttrsAnchor,
Version: src.String(),
},
Expand Down
4 changes: 2 additions & 2 deletions backend/daemon/api/documents/v1alpha/content_graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestBacklinks(t *testing.T) {
Starts: []int32{0},
Ends: []int32{5},
Attributes: map[string]string{
"url": "hm://d/" + pub.Document.Id + "?v=" + pub.Version + "#b1",
"url": pub.Document.Id + "?v=" + pub.Version + "#b1",
},
},
},
Expand All @@ -64,7 +64,7 @@ func TestBacklinks(t *testing.T) {
Starts: []int32{0},
Ends: []int32{5},
Attributes: map[string]string{
"url": "hm://d/" + pub.Document.Id + "?v=" + pub.Version,
"url": pub.Document.Id + "?v=" + pub.Version,
},
},
},
Expand Down
3 changes: 1 addition & 2 deletions backend/daemon/api/documents/v1alpha/document_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,7 @@ func (dm *docModel) hydrate(ctx context.Context, blobs *hyper.Storage) (*documen
}

docpb := &documents.Document{
Id: e.ID().TrimPrefix("hm://d/"),
Eid: string(e.ID()),
Id: string(e.ID()),
CreateTime: timestamppb.New(time.Unix(int64(createTime), 0)),
Author: core.Principal(owner).String(),
}
Expand Down
4 changes: 2 additions & 2 deletions backend/daemon/api/documents/v1alpha/document_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,11 @@ func newTestDocModel(t *testing.T, blobs *hyper.Storage, account, device core.Ke
ts := clock.Now()
now := ts.Time().Unix()

id, nonce := hyper.NewUnforgeableID(account.Principal(), nil, now)
id, nonce := hyper.NewUnforgeableID("", account.Principal(), nil, now)
delegation, err := daemon.Register(context.Background(), blobs, account, device.PublicKey, time.Now())
require.NoError(t, err)

entity := hyper.NewEntity(hyper.EntityID("hm://d/" + id))
entity := hyper.NewEntity(hyper.EntityID(id))
dm, err := newDocModel(entity, device, delegation)
require.NoError(t, err)

Expand Down
22 changes: 11 additions & 11 deletions backend/daemon/api/documents/v1alpha/documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (api *Server) CreateDraft(ctx context.Context, in *documents.CreateDraftReq
}

if in.ExistingDocumentId != "" {
eid := hyper.EntityID("hm://d/" + in.ExistingDocumentId)
eid := hyper.EntityID(in.ExistingDocumentId)

_, err := api.blobs.FindDraft(ctx, eid)
if err == nil {
Expand Down Expand Up @@ -115,8 +115,8 @@ func (api *Server) CreateDraft(ctx context.Context, in *documents.CreateDraftReq
ts := clock.Now()
now := ts.Time().Unix()

docid, nonce := hyper.NewUnforgeableID(me.Account().Principal(), nil, now)
eid := hyper.EntityID("hm://d/" + docid)
docid, nonce := hyper.NewUnforgeableID("hm://d/", me.Account().Principal(), nil, now)
eid := hyper.EntityID(docid)

entity := hyper.NewEntityWithClock(eid, clock)

Expand Down Expand Up @@ -160,7 +160,7 @@ func (api *Server) UpdateDraft(ctx context.Context, in *documents.UpdateDraftReq
return nil, err
}

eid := hyper.EntityID("hm://d/" + in.DocumentId)
eid := hyper.EntityID(in.DocumentId)

draft, err := api.blobs.LoadDraft(ctx, eid)
if err != nil {
Expand Down Expand Up @@ -232,7 +232,7 @@ func (api *Server) GetDraft(ctx context.Context, in *documents.GetDraftRequest)
return nil, err
}

eid := hyper.EntityID("hm://d/" + in.DocumentId)
eid := hyper.EntityID(in.DocumentId)

entity, err := api.blobs.LoadDraftEntity(ctx, eid)
if err != nil {
Expand Down Expand Up @@ -267,7 +267,7 @@ func (api *Server) ListDrafts(ctx context.Context, in *documents.ListDraftsReque
}

for _, e := range entities {
docid := e.TrimPrefix("hm://d/")
docid := string(e)
draft, err := api.GetDraft(ctx, &documents.GetDraftRequest{
DocumentId: docid,
})
Expand All @@ -286,7 +286,7 @@ func (api *Server) PublishDraft(ctx context.Context, in *documents.PublishDraftR
return nil, status.Errorf(codes.InvalidArgument, "must specify document ID to get the draft")
}

eid := hyper.EntityID("hm://d/" + in.DocumentId)
eid := hyper.EntityID(in.DocumentId)

oid, err := eid.CID()
if err != nil {
Expand Down Expand Up @@ -317,7 +317,7 @@ func (api *Server) DeleteDraft(ctx context.Context, in *documents.DeleteDraftReq
return nil, status.Errorf(codes.InvalidArgument, "must specify draft ID to delete")
}

eid := hyper.EntityID("hm://d/" + in.DocumentId)
eid := hyper.EntityID(in.DocumentId)

if err := api.blobs.DeleteDraft(ctx, eid); err != nil {
return nil, err
Expand All @@ -332,7 +332,7 @@ func (api *Server) GetPublication(ctx context.Context, in *documents.GetPublicat
return nil, status.Errorf(codes.InvalidArgument, "must specify document ID to get the draft")
}

eid := hyper.EntityID("hm://d/" + in.DocumentId)
eid := hyper.EntityID(in.DocumentId)
version := hyper.Version(in.Version)

pub, err := api.loadPublication(ctx, eid, version, in.TrustedOnly)
Expand Down Expand Up @@ -421,7 +421,7 @@ func (api *Server) DeletePublication(ctx context.Context, in *documents.DeletePu
return nil, status.Errorf(codes.InvalidArgument, "must specify publication ID to delete")
}

eid := hyper.EntityID("hm://d/" + in.DocumentId)
eid := hyper.EntityID(in.DocumentId)

if err := api.blobs.DeleteEntity(ctx, eid); err != nil {
return nil, err
Expand All @@ -442,7 +442,7 @@ func (api *Server) ListPublications(ctx context.Context, in *documents.ListPubli
}

for _, e := range entities {
docid := e.TrimPrefix("hm://d/")
docid := string(e)
pub, err := api.GetPublication(ctx, &documents.GetPublicationRequest{
DocumentId: docid,
LocalOnly: true,
Expand Down
4 changes: 0 additions & 4 deletions backend/daemon/api/documents/v1alpha/documents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ func TestAPIUpdateDraft_Complex(t *testing.T) {

want := &documents.Document{
Id: draft.Id,
Eid: draft.Eid,
Author: draft.Author,
Editors: []string{draft.Author},
Title: "Hello Drafts V2",
Expand Down Expand Up @@ -373,7 +372,6 @@ func TestAPIUpdateDraft_Complex(t *testing.T) {

want := &documents.Document{
Id: draft.Id,
Eid: draft.Eid,
Author: draft.Author,
Editors: []string{draft.Author},
Title: "Hello Drafts V2",
Expand Down Expand Up @@ -424,7 +422,6 @@ func TestAPIUpdateDraft_Complex(t *testing.T) {

want := &documents.Document{
Id: draft.Id,
Eid: draft.Eid,
Author: draft.Author,
Editors: []string{draft.Author},
Title: "Hello Drafts V2",
Expand Down Expand Up @@ -573,7 +570,6 @@ func TestAPIUpdateDraft_WithList(t *testing.T) {

want := &documents.Document{
Id: draft.Id,
Eid: draft.Eid,
Title: "My new document title",
Author: draft.Author,
Editors: []string{draft.Author},
Expand Down
4 changes: 2 additions & 2 deletions backend/daemon/api/groups/v1alpha/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func (srv *Server) CreateGroup(ctx context.Context, in *groups.CreateGroupReques
ts := clock.Now()
createTime := ts.Time().Unix()

id, nonce := hyper.NewUnforgeableID(me.Account().Principal(), nil, createTime)
eid := hyper.EntityID("hm://g/" + id)
id, nonce := hyper.NewUnforgeableID("hm://g/", me.Account().Principal(), nil, createTime)
eid := hyper.EntityID(id)
e := hyper.NewEntityWithClock(eid, clock)

patch := map[string]any{
Expand Down
2 changes: 1 addition & 1 deletion backend/daemon/daemon_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ func TestBug_ListObjectsMustHaveCausalOrder(t *testing.T) {
var found *p2p.Object
seen := map[cid.Cid]struct{}{}
for _, obj := range list.Objects {
if obj.Id == "hm://d/"+pub.Document.Id {
if obj.Id == pub.Document.Id {
found = obj
}
for _, ch := range obj.ChangeIds {
Expand Down
Loading

0 comments on commit 4dc6a36

Please sign in to comment.