-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
1,100 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package gql | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/ipfs/go-cid" | ||
"github.com/ipni/go-libipni/metadata" | ||
"github.com/multiformats/go-multicodec" | ||
) | ||
|
||
type ipniResolver struct { | ||
PeerID string | ||
Config string | ||
} | ||
|
||
func (r *resolver) IpniProviderInfo() (*ipniResolver, error) { | ||
cfg, err := json.Marshal(r.cfg.IndexProvider) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &ipniResolver{PeerID: r.h.ID().String(), Config: string(cfg)}, nil | ||
} | ||
|
||
type adMetadata struct { | ||
Protocol string | ||
Metadata string | ||
} | ||
|
||
type adResolver struct { | ||
ContextID string | ||
Entries string | ||
PreviousEntry string | ||
Provider string | ||
Addresses []string | ||
IsRemove bool | ||
Metadata []*adMetadata | ||
} | ||
|
||
func (r *resolver) IpniAdvertisement(ctx context.Context, args struct{ AdCid string }) (*adResolver, error) { | ||
adCid, err := cid.Parse(args.AdCid) | ||
if err != nil { | ||
return nil, fmt.Errorf("parsing ad cid %s: %w", args.AdCid, err) | ||
} | ||
|
||
ad, err := r.idxProv.GetAdv(ctx, adCid) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var contextId string | ||
c, err := cid.Parse(ad.ContextID) | ||
if err == nil { | ||
contextId = c.String() | ||
} else { | ||
contextId = base64.StdEncoding.EncodeToString(ad.ContextID) | ||
} | ||
|
||
var mds []*adMetadata | ||
if len(ad.Metadata) > 0 { | ||
// Metadata may contain more than one protocol, sorted by ascending order of their protocol ID. | ||
// Therefore, decode the metadata as metadata.Metadata, then read each protocol's data. | ||
// See: https://github.com/ipni/specs/blob/main/IPNI.md#metadata | ||
dtm := metadata.Default.New() | ||
if err := dtm.UnmarshalBinary(ad.Metadata); err != nil { | ||
for _, protoId := range dtm.Protocols() { | ||
adMd := &adMetadata{ | ||
Protocol: protoId.String(), | ||
} | ||
mds = append(mds, adMd) | ||
if protoId == multicodec.TransportGraphsyncFilecoinv1 { | ||
proto := dtm.Get(protoId) | ||
gsmd, ok := proto.(*metadata.GraphsyncFilecoinV1) | ||
if ok { | ||
bz, err := json.Marshal(gsmd) | ||
if err == nil { | ||
adMd.Metadata = string(bz) | ||
} else { | ||
adMd.Metadata = err.Error() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return &adResolver{ | ||
ContextID: contextId, | ||
Metadata: mds, | ||
PreviousEntry: ad.PreviousID.String(), | ||
Provider: ad.Provider, | ||
Addresses: ad.Addresses, | ||
IsRemove: ad.IsRm, | ||
}, nil | ||
} | ||
|
||
func (r *resolver) IpniAdvertisementEntries(ctx context.Context, args struct{ AdCid string }) ([]*string, error) { | ||
adCid, err := cid.Parse(args.AdCid) | ||
if err != nil { | ||
return nil, fmt.Errorf("parsing ad cid %s: %w", args.AdCid, err) | ||
} | ||
|
||
ad, err := r.idxProv.GetAdv(ctx, adCid) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(ad.ContextID) == 0 { | ||
return nil, nil | ||
} | ||
|
||
var entries []*string | ||
it, err := r.idxProvWrapper.MultihashLister(ctx, "", ad.ContextID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for entry, err := it.Next(); err == nil; entry, err = it.Next() { | ||
str := entry.B58String() | ||
entries = append(entries, &str) | ||
} | ||
|
||
return entries, nil | ||
} | ||
|
||
func (r *resolver) IpniAdvertisementEntriesCount(ctx context.Context, args struct{ AdCid string }) (int32, error) { | ||
adCid, err := cid.Parse(args.AdCid) | ||
if err != nil { | ||
return 0, fmt.Errorf("parsing ad cid %s: %w", args.AdCid, err) | ||
} | ||
|
||
ad, err := r.idxProv.GetAdv(ctx, adCid) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
if len(ad.ContextID) == 0 { | ||
return 0, nil | ||
} | ||
|
||
it, err := r.idxProvWrapper.MultihashLister(ctx, "", ad.ContextID) | ||
if err != nil { | ||
return 0, err | ||
} | ||
var count int32 | ||
for _, err := it.Next(); err == nil; _, err = it.Next() { | ||
count++ | ||
} | ||
|
||
return count, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
.js-obj { | ||
position: relative; | ||
color: #777777; | ||
line-height: 1.5em; | ||
} | ||
|
||
.js-obj.expandable > .js-obj-name { | ||
cursor: pointer; | ||
} | ||
|
||
.js-obj .js-obj { | ||
padding-left: 1em; | ||
display: none; | ||
} | ||
.js-obj.expanded .js-obj { | ||
display: block; | ||
} | ||
|
||
.js-obj .expand-collapse { | ||
margin-left: 0.5em; | ||
opacity: 0.5; | ||
width: 0.8em; | ||
height: 0.8em; | ||
display: inline-block; | ||
background: url("./bootstrap-icons/icons/caret-right-fill.svg"); | ||
background-size: cover; | ||
} | ||
|
||
.js-obj.expanded .expand-collapse { | ||
background: url("./bootstrap-icons/icons/caret-down-fill.svg") | ||
} |
Oops, something went wrong.