Skip to content

Commit

Permalink
GO-3946 Fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
KirillSto committed Sep 25, 2024
1 parent b72daf9 commit 26dd64b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 86 deletions.
2 changes: 1 addition & 1 deletion core/block/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (s *Service) CreateWorkspace(ctx context.Context, req *pb.RpcWorkspaceCreat
if err != nil {
return "", fmt.Errorf("set details for space %s: %w", newSpace.Id(), err)
}
_, err = s.builtinObjectService.ImportBuiltInUseCase(nil, newSpace.Id(), req.CachePath, req.UseCase)
_, err = s.builtinObjectService.ImportBuiltInUseCase(ctx, newSpace.Id(), req.CachePath, req.UseCase)
if err != nil {
return "", fmt.Errorf("import use-case: %w", err)
}
Expand Down
112 changes: 40 additions & 72 deletions core/gallery/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,39 +63,18 @@ func (c *cache) Name() string {
}

func (c *cache) GetIndex(timeoutInSeconds int) (*pb.RpcGalleryDownloadIndexResponse, error) {
localIndex, err := c.storage.getIndex()
if err != nil {
log.Warn("failed to read local index. Need to refetch index from remote", zap.Error(err))
}

version := ""
if localIndex != nil {
version, err = c.storage.getVersion()
if err != nil {
log.Warn("failed to read local version. Need to refetch version from remote", zap.Error(err))
}
}
return c.getRecentIndex(timeoutInSeconds, false)
}

index, newVersion, err := c.downloadGalleryIndex(timeoutInSeconds, version, false)
func (c *cache) GetManifest(downloadLink string, timeoutInSeconds int) (info *model.ManifestInfo, err error) {
index, err := c.getRecentIndex(timeoutInSeconds, true)
if err != nil {
if errors.Is(err, ErrNotModified) {
return localIndex, nil
}

if localIndex != nil {
log.Warn("failed to download index from remote. Returning local index", zap.Error(err))
return localIndex, nil
}

return nil, err
}

c.storage.save(index, newVersion)

return index, nil
return getManifestByDownloadLink(index, downloadLink)
}

func (c *cache) GetManifest(downloadLink string, timeoutInSeconds int) (info *model.ManifestInfo, err error) {
func (c *cache) getRecentIndex(timeout int, withManifestValidation bool) (*pb.RpcGalleryDownloadIndexResponse, error) {
localIndex, err := c.storage.getIndex()
if err != nil {
log.Warn("failed to read local index. Need to refetch index from remote", zap.Error(err))
Expand All @@ -109,31 +88,52 @@ func (c *cache) GetManifest(downloadLink string, timeoutInSeconds int) (info *mo
}
}

index, newVersion, err := c.downloadGalleryIndex(timeoutInSeconds, version, true)
index, newVersion, err := c.downloadGalleryIndex(timeout, version, withManifestValidation)
if err == nil {
c.storage.save(index, newVersion)

return getManifestByDownloadLink(index, downloadLink)
return index, nil
}

if errors.Is(err, ErrNotModified) {
manifest, err := getManifestByDownloadLink(localIndex, downloadLink)
if err != nil {
return nil, err
}
return manifest, nil
return localIndex, nil
}

if localIndex != nil {
log.Warn("failed to download index from remote. Returning local index", zap.Error(err))
manifest, err := getManifestByDownloadLink(localIndex, downloadLink)
if err != nil {
return nil, err
return localIndex, nil
}
return nil, err
}

func (c *cache) downloadGalleryIndex(
timeoutInSeconds int, // timeout to wait for HTTP response
version string, // Etag of gallery index, that allows to fetch index faster
withManifestValidation bool, // a flag that indicates that every manifest should be validated
) (response *pb.RpcGalleryDownloadIndexResponse, newVersion string, err error) {
raw, newVersion, err := getRawJson(c.indexURL, timeoutInSeconds, version)
if err != nil {
if errors.Is(err, ErrNotModified) {
return nil, version, err
}
return manifest, nil
return nil, "", fmt.Errorf("%w: %w", ErrDownloadIndex, err)
}

return nil, err
response = &pb.RpcGalleryDownloadIndexResponse{}
err = jsonpb.Unmarshal(bytes.NewReader(raw), response)
if err != nil {
return nil, "", fmt.Errorf("%w to get lists of categories and experiences from gallery index: %w", ErrUnmarshalJson, err)
}

if withManifestValidation {
for _, info := range response.Experiences {
stripTags(info)
if err = validateManifest(info.Schema, info); err != nil {
return nil, "", fmt.Errorf("manifest validation error: %w", err)
}
}
}

return response, newVersion, nil
}

type cacheStorage interface {
Expand Down Expand Up @@ -185,36 +185,6 @@ func (s *storage) save(index *pb.RpcGalleryDownloadIndexResponse, version string
}
}

func (c *cache) downloadGalleryIndex(
timeoutInSeconds int, // timeout to wait for HTTP response
version string, // Etag of gallery index, that allows to fetch index faster
withManifestValidation bool, // a flag that indicates that every manifest should be validated
) (response *pb.RpcGalleryDownloadIndexResponse, newVersion string, err error) {
raw, newVersion, err := getRawJson(c.indexURL, timeoutInSeconds, version)
if err != nil {
if errors.Is(err, ErrNotModified) {
return nil, version, err
}
return nil, "", fmt.Errorf("%w: %w", ErrDownloadIndex, err)
}

response = &pb.RpcGalleryDownloadIndexResponse{}
err = jsonpb.Unmarshal(bytes.NewReader(raw), response)
if err != nil {
return nil, "", fmt.Errorf("%w to get lists of categories and experiences from gallery index: %w", ErrUnmarshalJson, err)
}

if withManifestValidation {
for _, info := range response.Experiences {
if err = validateManifest(info.Schema, info); err != nil {
return nil, "", fmt.Errorf("manifest validation error: %w", err)
}
}
}

return response, newVersion, nil
}

func validateManifest(schema string, info *model.ManifestInfo) error {
if err := validateSchema(schema, info); err != nil {
return fmt.Errorf("manifest does not correspond scema: %w", err)
Expand All @@ -225,7 +195,5 @@ func validateManifest(schema string, info *model.ManifestInfo) error {
return fmt.Errorf("URL '%s' provided in manifest is not in whitelist", urlToCheck)
}
}

info.Description = stripTags(info.Description)
return nil
}
11 changes: 6 additions & 5 deletions core/gallery/gallery.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (s *service) GetManifest(url string, checkWhitelist bool) (info *model.Mani
}
}

info.Description = stripTags(info.Description)
stripTags(info)
return info, nil
}

Expand Down Expand Up @@ -182,11 +182,12 @@ func validateSchema(schema string, info *model.ManifestInfo) (err error) {
return nil
}

func stripTags(str string) string {
if _, err := html.Parse(strings.NewReader(str)); err != nil {
return str
func stripTags(info *model.ManifestInfo) {
description := info.Description
if _, err := html.Parse(strings.NewReader(description)); err != nil {
return
}
return strip.StripTags(str)
info.Description = strip.StripTags(description)
}

func buildResultError(result *gojsonschema.Result) error {
Expand Down
6 changes: 4 additions & 2 deletions core/gallery/gallery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
"github.com/stretchr/testify/assert"

"github.com/anyproto/anytype-heart/pb"
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
)

func TestStripTags(t *testing.T) {
bareString := `Links:FooBarBaz`
taggedString := `<p>Links:</p><ul><li><a href="foo">Foo</a><li><a href="/bar/baz">BarBaz</a></ul><script>Malware that will destroy yor computer</script>`
stripedString := stripTags(taggedString)
assert.Equal(t, bareString, stripedString)
info := &model.ManifestInfo{Description: taggedString}
stripTags(info)
assert.Equal(t, bareString, info.Description)
}

func TestIsInWhitelist(t *testing.T) {
Expand Down
8 changes: 2 additions & 6 deletions core/gallery/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,6 @@ func (s *service) ImportBuiltInUseCase(
}

if cachePath == "" {
if ctx == nil {
ctx = context.Background()
}
// TODO: GO-4131 Remove this call when clients support cache
return s.importUseCase(ctx, spaceID, info.Title, useCase)
// return pb.RpcObjectImportUseCaseResponseError_BAD_INPUT,
Expand Down Expand Up @@ -274,8 +271,6 @@ func readClientCache(cachePath string, indexOnly bool) (archives map[string][]by
}
defer r.Close()

archives = make(map[string][]byte)

for _, f := range r.File {
if f.Name != indexName {
continue
Expand All @@ -299,6 +294,7 @@ func readClientCache(cachePath string, indexOnly bool) (archives map[string][]by
}
downloadLinks := generateMapOfDownloadLinksByNames(index)

archives = make(map[string][]byte, len(index.Experiences))
for _, f := range r.File {
if f.Name == indexName {
continue
Expand Down Expand Up @@ -487,7 +483,7 @@ func getManifestByDownloadLink(index *pb.RpcGalleryDownloadIndexResponse, link s
}

func generateMapOfDownloadLinksByNames(index *pb.RpcGalleryDownloadIndexResponse) map[string]string {
m := make(map[string]string)
m := make(map[string]string, len(index.Experiences))
for _, manifest := range index.Experiences {
m[manifest.Name] = manifest.DownloadLink
}
Expand Down

0 comments on commit 26dd64b

Please sign in to comment.