Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Commit

Permalink
Fix bugs in GET moderators query
Browse files Browse the repository at this point in the history
  • Loading branch information
cpacia committed Mar 10, 2017
1 parent 5482c3a commit 7af2fb7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 43 deletions.
55 changes: 12 additions & 43 deletions api/jsonapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ import (
lockfile "github.com/ipfs/go-ipfs/repo/fsrepo/lock"
routing "github.com/ipfs/go-ipfs/routing/dht"
"golang.org/x/net/context"
"gx/ipfs/QmUAQaWbKxGCUTuoQVvvicbQNZ9APF5pDGWyAZSe93AtKH/go-multiaddr"
"gx/ipfs/QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J/go-multihash"
ps "gx/ipfs/QmeXj9VAjmYQZxpmVz7VzccbJrpmr8qkCDSjfVNsPTWTYU/go-libp2p-peerstore"
peer "gx/ipfs/QmfMmLGoKzCHDN7cGgk64PJr4iipzidDRME8HABSJqvmhC/go-libp2p-peer"
"sync"
)
Expand Down Expand Up @@ -1574,26 +1573,11 @@ func (i *jsonAPIHandler) GETModerators(w http.ResponseWriter, r *http.Request) {
}
var mods []string
for _, p := range peerInfoList {
if len(p.Addrs) == 0 {
continue
}
addr := p.Addrs[0]
if addr.Protocols()[0].Code != multiaddr.P_IPFS {
continue
}
val, err := addr.ValueForProtocol(multiaddr.P_IPFS)
id, err := core.ExtractIDFromPointer(p)
if err != nil {
continue
}
mh, err := multihash.FromB58String(val)
if err != nil {
continue
}
d, err := multihash.Decode(mh)
if err != nil {
continue
}
mods = append(mods, string(d.Digest))
mods = append(mods, id)
}
var resp string
removeDuplicates(mods)
Expand All @@ -1608,7 +1592,7 @@ func (i *jsonAPIHandler) GETModerators(w http.ResponseWriter, r *http.Request) {
wg.Done()
return
}
resp := &pb.PeerAndProfile{mod, &profile}
resp := &pb.PeerAndProfile{m, &profile}
mar := jsonpb.Marshaler{
EnumsAsInts: false,
EmitDefaults: true,
Expand Down Expand Up @@ -1671,34 +1655,19 @@ func (i *jsonAPIHandler) GETModerators(w http.ResponseWriter, r *http.Request) {

found := make(map[string]bool)
for p := range peerChan {
go func() {
if len(p.Addrs) == 0 {
return
}
addr := p.Addrs[0]
if addr.Protocols()[0].Code != multiaddr.P_IPFS {
return
}
val, err := addr.ValueForProtocol(multiaddr.P_IPFS)
if err != nil {
return
}
mh, err := multihash.FromB58String(val)
go func(pi ps.PeerInfo) {
pid, err := core.ExtractIDFromPointer(pi)
if err != nil {
return
}
d, err := multihash.Decode(mh)
if err != nil {
return
}
if !found[string(d.Digest)] {
found[string(d.Digest)] = true
if !found[pid] {
found[pid] = true
if strings.ToLower(include) == "profile" {
profile, err := i.node.FetchProfile(string(d.Digest))
profile, err := i.node.FetchProfile(pid)
if err != nil {
return
}
resp := pb.PeerAndProfileWithID{id, string(d.Digest), &profile}
resp := pb.PeerAndProfileWithID{id, pid, &profile}
m := jsonpb.Marshaler{
EnumsAsInts: false,
EmitDefaults: true,
Expand All @@ -1711,15 +1680,15 @@ func (i *jsonAPIHandler) GETModerators(w http.ResponseWriter, r *http.Request) {
}
i.node.Broadcast <- []byte(respJson)
} else {
resp := wsResp{id, string(d.Digest)}
resp := wsResp{id, pid}
respJson, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return
}
i.node.Broadcast <- []byte(respJson)
}
}
}()
}(p)
}
}()
}
Expand Down
28 changes: 28 additions & 0 deletions core/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package core

import (
"crypto/sha256"
"errors"
ma "gx/ipfs/QmUAQaWbKxGCUTuoQVvvicbQNZ9APF5pDGWyAZSe93AtKH/go-multiaddr"
mh "gx/ipfs/QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J/go-multihash"
ps "gx/ipfs/QmeXj9VAjmYQZxpmVz7VzccbJrpmr8qkCDSjfVNsPTWTYU/go-libp2p-peerstore"
)

// Hash with SHA-256 and encode as a multihash
Expand All @@ -18,3 +21,28 @@ func EncodeMultihash(b []byte) (*mh.Multihash, error) {
}
return &multihash, err
}

// Certain pointers, such as moderators, contain a peerID. This function
// will extract the ID from the underlying PeerInfo object.
func ExtractIDFromPointer(pi ps.PeerInfo) (string, error) {
if len(pi.Addrs) == 0 {
return "", errors.New("PeerInfo object has no addresses")
}
addr := pi.Addrs[0]
if addr.Protocols()[0].Code != ma.P_IPFS {
return "", errors.New("IPFS protocol not found in address")
}
val, err := addr.ValueForProtocol(ma.P_IPFS)
if err != nil {
return "", err
}
h, err := mh.FromB58String(val)
if err != nil {
return "", err
}
d, err := mh.Decode(h)
if err != nil {
return "", err
}
return string(d.Digest), nil
}

0 comments on commit 7af2fb7

Please sign in to comment.