Skip to content

Commit

Permalink
Implement 'resolve' patching, fix repeating thumbnails
Browse files Browse the repository at this point in the history
  • Loading branch information
anbsky committed Aug 21, 2024
1 parent 2cf5fee commit 319d528
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 6 deletions.
35 changes: 32 additions & 3 deletions app/arweave/arweave.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func ReplaceAssetUrls(baseUrl string, structure any, collPath, itemPath string) (any, error) {
var origUrls []string
urlPaths := map[string]string{}
urlPaths := map[string][]string{}

jsonData, err := json.Marshal(structure)
if err != nil {
Expand All @@ -22,7 +22,11 @@ func ReplaceAssetUrls(baseUrl string, structure any, collPath, itemPath string)
urlPath := fmt.Sprintf("%s.%s.%s", collPath, key.String(), itemPath)
url := gjson.GetBytes(jsonData, urlPath).String()
origUrls = append(origUrls, url)
urlPaths[url] = urlPath
if slice, exists := urlPaths[url]; exists {
urlPaths[url] = append(slice, urlPath)
} else {
urlPaths[url] = []string{urlPath}
}
return true
})

Expand All @@ -33,7 +37,7 @@ func ReplaceAssetUrls(baseUrl string, structure any, collPath, itemPath string)
}

for oldURL, newURL := range subsUrls {
if path, exists := urlPaths[oldURL]; exists {
for _, path := range urlPaths[oldURL] {
jsonData, _ = sjson.SetBytes(jsonData, path, newURL)
}
}
Expand All @@ -42,6 +46,31 @@ func ReplaceAssetUrls(baseUrl string, structure any, collPath, itemPath string)
return d, json.Unmarshal(jsonData, &d)
}

func ReplaceAssetUrl(baseUrl string, structure any, path string) (any, error) {
jsonData, err := json.Marshal(structure)
if err != nil {
return nil, err
}

origUrl := gjson.GetBytes(jsonData, path).String()

resolver := NewAssetResolver(baseUrl)
subsUrls, err := resolver.ResolveUrls([]string{origUrl})

if err != nil {
return nil, err
}
if newUrl, ok := subsUrls[origUrl]; ok {
jsonData, err = sjson.SetBytes(jsonData, path, newUrl)
if err != nil {
return nil, err
}
}

var d any
return d, json.Unmarshal(jsonData, &d)
}

func GetClaimUrl(baseUrl, claim_id string) (string, error) {
resolver := NewAssetResolver(baseUrl)
r, err := resolver.ResolveClaims([]string{claim_id})
Expand Down
24 changes: 21 additions & 3 deletions app/arweave/arweave_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,44 @@ package arweave
import (
"encoding/json"
"os"
"regexp"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ybbus/jsonrpc"
)

func TestQueryParamsAsMap(t *testing.T) {
func TestReplaceAssetUrls(t *testing.T) {
require := require.New(t)
assert := assert.New(t)

f, err := os.ReadFile("../../claims.json")
require.NoError(err)
var resp jsonrpc.RPCResponse
require.NoError(json.Unmarshal(f, &resp))
// result := ReplaceAssetUrls("http://odycdn.com", resp.Result, "result.items", "signing_channel.value.thumbnail.url")
result, err := ReplaceAssetUrls("http://odycdn.com", resp.Result, "items", "value.thumbnail.url")
require.NoError(err)

out, err := json.MarshalIndent(result, "", " ")
require.NoError(err)
assert.Regexp(`http://odycdn.com/explore/\w{64}\?filename=\w{64}\.webp`, string(out))
re := regexp.MustCompile(`http://odycdn.com/explore/\w{64}\?filename=\w{64}\.webp`)
matches := re.FindAllString(string(out), -1)
assert.Equal(2, len(matches))
}

func TestReplaceAssetUrl(t *testing.T) {
require := require.New(t)
assert := assert.New(t)

f, err := os.ReadFile("../../resolve.json")
require.NoError(err)
var resp jsonrpc.RPCResponse
require.NoError(json.Unmarshal(f, &resp))
result, err := ReplaceAssetUrl("http://odycdn.com", resp.Result.(map[string]any)["lbry://@MySillyReactions#d1ae6a9097b44691d318a5bfc6dc1240311c75e2"], "value.thumbnail.url")
require.NoError(err)

out, err := json.MarshalIndent(result, "", " ")
require.NoError(err)
assert.Regexp(`http://odycdn.com/explore/\w{64}\?filename=\w{64}\.jpg`, string(out))
}
1 change: 1 addition & 0 deletions app/query/caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func (c *Caller) addDefaultHooks() {
c.AddPreflightHook(MethodClaimSearch, preflightHookClaimSearch, builtinHookName)

c.AddPostflightHook(MethodClaimSearch, postClaimSearchArfleetThumbs, builtinHookName)
c.AddPostflightHook(MethodResolve, postResolveArfleetThumbs, builtinHookName)
}

func (c *Caller) CloneWithoutHook(endpoint, method, name string) *Caller {
Expand Down
23 changes: 23 additions & 0 deletions app/query/processors.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,29 @@ func postClaimSearchArfleetThumbs(_ *Caller, ctx context.Context) (*jsonrpc.RPCR
return resp, nil
}

func postResolveArfleetThumbs(_ *Caller, ctx context.Context) (*jsonrpc.RPCResponse, error) {
logger := zapadapter.NewKV(nil).With("module", "query.preprocessors")
baseUrl := config.GetArfleetCDN()

resp := GetResponse(ctx)
claims, ok := resp.Result.(map[string]any)
if !ok {
logger.Warn("error processing resolve response", "result", resp.Result)
}
var claimUrl string
var claim any
for k, v := range claims {
claimUrl, claim = k, v
}
pClaim, err := arweave.ReplaceAssetUrl(baseUrl, claim, "value.thumbnail.url")
if err != nil {
logger.Warn("error replacing asset url", "err", err)
return resp, nil
}
resp.Result = map[string]any{claimUrl: pClaim}
return resp, nil
}

func sliceContains[V comparable](cont []V, items ...V) bool {
for _, t := range cont {
for _, i := range items {
Expand Down

0 comments on commit 319d528

Please sign in to comment.