Skip to content

Commit

Permalink
GO-4823 Use file-specific relations map
Browse files Browse the repository at this point in the history
  • Loading branch information
KirillSto committed Jan 17, 2025
1 parent 874e86d commit 7c5284a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
33 changes: 26 additions & 7 deletions core/relationutils/recommended.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,23 @@ var (
bundle.RelationKeyLinks,
}

nonFileSpecificRelationKeys = []domain.RelationKey{
bundle.RelationKeyAddedDate,
bundle.RelationKeyOrigin,
fileSpecificRelationKeysMap = map[domain.RelationKey]struct{}{
bundle.RelationKeyFileExt: {},
bundle.RelationKeySizeInBytes: {},
bundle.RelationKeyFileMimeType: {},
bundle.RelationKeyArtist: {},
bundle.RelationKeyAudioAlbum: {},
bundle.RelationKeyAudioGenre: {},
bundle.RelationKeyAudioAlbumTrackNumber: {},
bundle.RelationKeyAudioLyrics: {},
bundle.RelationKeyReleasedYear: {},
bundle.RelationKeyHeightInPixels: {},
bundle.RelationKeyWidthInPixels: {},
bundle.RelationKeyCamera: {},
bundle.RelationKeyCameraIso: {},
bundle.RelationKeyAperture: {},
bundle.RelationKeyExposure: {},
bundle.RelationKeyFocalRatio: {},
}

errRecommendedRelationsAlreadyFilled = fmt.Errorf("recommended featured relations are already filled")
Expand All @@ -55,15 +69,20 @@ func FillRecommendedRelations(ctx context.Context, deriver ObjectIDDeriver, deta

if isFileType(details) {
// for file types we need to fill separate relation list with file-specific recommended relations
fileRecommendedRelationKeys := slices.DeleteFunc(keys, func(key domain.RelationKey) bool {
return slices.Contains(nonFileSpecificRelationKeys, key)
})
var fileRecommendedRelationKeys, other []domain.RelationKey
for _, key := range keys {
if _, found := fileSpecificRelationKeysMap[key]; found {
fileRecommendedRelationKeys = append(fileRecommendedRelationKeys, key)
continue
}
other = append(other, key)
}
fileRelationIds, err := prepareRelationIds(ctx, deriver, fileRecommendedRelationKeys)
if err != nil {
return nil, false, fmt.Errorf("prepare file recommended relation ids: %w", err)
}
details.SetStringList(bundle.RelationKeyRecommendedFileRelations, fileRelationIds)
keys = nonFileSpecificRelationKeys
keys = other
}

// we should include default system recommended relations and exclude default recommended featured relations
Expand Down
5 changes: 4 additions & 1 deletion core/relationutils/recommended_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ func TestFillRecommendedRelations(t *testing.T) {
// then
assert.NoError(t, err)
assert.False(t, isAlreadyFilled)
assert.Equal(t, buildRelationIds(append(nonFileSpecificRelationKeys, defaultRecommendedRelationKeys...)), details.GetStringList(bundle.RelationKeyRecommendedRelations))
assert.Equal(t, append([]string{
bundle.RelationKeyOrigin.URL(),
bundle.RelationKeyAddedDate.URL(),
}, buildRelationIds(defaultRecommendedRelationKeys)...), details.GetStringList(bundle.RelationKeyRecommendedRelations))
assert.Equal(t, defaultRecFeatRelIds, details.GetStringList(bundle.RelationKeyRecommendedFeaturedRelations))
assert.Equal(t, []string{
bundle.RelationKeyFileExt.URL(),
Expand Down

0 comments on commit 7c5284a

Please sign in to comment.