mirror of
https://codeberg.org/VnPower/PixivFE
synced 2024-12-06 19:16:23 +01:00
refactor for a separate populateFrequentTags()
This commit is contained in:
+113
-43
@@ -249,49 +249,9 @@ func getPopulatedWorks(auditor *audit.Auditor, r *http.Request, user *User, id s
|
||||
|
||||
// Fetch a user's frequently used tags if requested
|
||||
if getTags {
|
||||
var tagsIDs []string
|
||||
|
||||
// Since category is pre-validated, we don't need to handle unexpected categories
|
||||
if category.Value == CategoryAny.Value ||
|
||||
category.Value == CategoryAnyAlt.Value ||
|
||||
category.Value == CategoryIllustration.Value ||
|
||||
category.Value == CategoryManga.Value {
|
||||
// Append illustrationIDs and mangaIDs if they are not empty
|
||||
if illustrationIDs != "" {
|
||||
auditor.Logger.Debug("illustrationIDs found, appending to tagsIDs")
|
||||
tagsIDs = append(tagsIDs, illustrationIDs)
|
||||
}
|
||||
if mangaIDs != "" {
|
||||
auditor.Logger.Debug("mangaIDs found, appending to tagsIDs")
|
||||
tagsIDs = append(tagsIDs, mangaIDs)
|
||||
}
|
||||
} else if category.Value == CategoryNovels.Value {
|
||||
auditor.Logger.Debug("Using CategoryNovels to fetch tags")
|
||||
if novelIDs != "" {
|
||||
auditor.Logger.Debug("novelIDs found, appending to tagsIDs")
|
||||
tagsIDs = append(tagsIDs, novelIDs)
|
||||
}
|
||||
} else {
|
||||
auditor.Logger.Warn("No recognized category found")
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(tagsIDs) == 0 {
|
||||
auditor.Logger.Warn("No tags found to fetch, setting FrequentTags to empty slice",
|
||||
zap.String("category", category.Value),
|
||||
zap.Strings("tagsIDs", tagsIDs),
|
||||
zap.String("illustrationIDs", illustrationIDs),
|
||||
zap.String("mangaIDs", mangaIDs),
|
||||
zap.String("novelIDs", novelIDs),
|
||||
)
|
||||
user.FrequentTags = []FrequentTag{}
|
||||
} else {
|
||||
// Concatenate IDs to form a single string
|
||||
ids := strings.Join(tagsIDs, "")
|
||||
user.FrequentTags, err = fetchFrequentTags(auditor, r, ids, category)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get user frequent tags: %w", err)
|
||||
}
|
||||
user.FrequentTags, err = populateFrequentTags(auditor, r, id, category, illustrationIDs, mangaIDs, novelIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,6 +262,116 @@ func getPopulatedWorks(auditor *audit.Auditor, r *http.Request, user *User, id s
|
||||
return nil
|
||||
}
|
||||
|
||||
// populateFrequentTags fetches and populates a user's frequently used tags based on the category and workIDs.
|
||||
func populateFrequentTags(
|
||||
auditor *audit.Auditor,
|
||||
r *http.Request,
|
||||
id string,
|
||||
category *UserWorkCategory,
|
||||
illustrationIDs string,
|
||||
mangaIDs string,
|
||||
novelIDs string,
|
||||
) ([]FrequentTag, error) {
|
||||
var (
|
||||
primaryTagsIDs []string // For illustrationIDs and mangaIDs
|
||||
secondaryTagsIDs []string // For novelIDs
|
||||
)
|
||||
|
||||
// Append illustrationIDs to primaryTagsIDs if applicable
|
||||
if illustrationIDs != "" {
|
||||
primaryTagsIDs = append(primaryTagsIDs, illustrationIDs)
|
||||
auditor.Logger.Debug(
|
||||
"illustrationIDs found, appended to primaryTagsIDs",
|
||||
zap.Strings("primaryTagsIDs", primaryTagsIDs),
|
||||
zap.String("illustrationIDs", illustrationIDs),
|
||||
)
|
||||
}
|
||||
|
||||
// Append mangaIDs to primaryTagsIDs if applicable
|
||||
if mangaIDs != "" {
|
||||
primaryTagsIDs = append(primaryTagsIDs, mangaIDs)
|
||||
auditor.Logger.Debug(
|
||||
"mangaIDs found, appended to primaryTagsIDs",
|
||||
zap.Strings("primaryTagsIDs", primaryTagsIDs),
|
||||
zap.String("mangaIDs", mangaIDs),
|
||||
)
|
||||
}
|
||||
|
||||
// Append novelIDs to secondaryTagsIDs if applicable
|
||||
if novelIDs != "" {
|
||||
secondaryTagsIDs = append(secondaryTagsIDs, novelIDs)
|
||||
auditor.Logger.Debug(
|
||||
"novelIDs found, appended to secondaryTagsIDs",
|
||||
zap.Strings("secondaryTagsIDs", secondaryTagsIDs),
|
||||
zap.String("novelIDs", novelIDs),
|
||||
)
|
||||
}
|
||||
|
||||
// Function to count the number of items based on delimiter
|
||||
countIDs := func(ids []string) int {
|
||||
count := 0
|
||||
for _, idStr := range ids {
|
||||
// Each idStr contains multiple "&ids[]=" entries, so we split by "&ids[]=" and count non-empty parts
|
||||
parts := strings.Split(idStr, "&ids[]=")
|
||||
for _, part := range parts {
|
||||
if part != "" {
|
||||
count++
|
||||
}
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// Count the number of items in each group
|
||||
primaryCount := countIDs(primaryTagsIDs)
|
||||
secondaryCount := countIDs(secondaryTagsIDs)
|
||||
|
||||
auditor.Logger.Debug(
|
||||
"Counts of primary and secondary tags",
|
||||
zap.Int("primaryCount", primaryCount),
|
||||
zap.Int("secondaryCount", secondaryCount),
|
||||
)
|
||||
|
||||
var finalTagsIDs []string
|
||||
|
||||
// Decide which tagsIDs to use based on the count
|
||||
if primaryCount > secondaryCount {
|
||||
finalTagsIDs = primaryTagsIDs
|
||||
auditor.Logger.Debug(
|
||||
"Selected primaryTagsIDs as finalTagsIDs",
|
||||
zap.Strings("finalTagsIDs", finalTagsIDs),
|
||||
)
|
||||
} else {
|
||||
finalTagsIDs = secondaryTagsIDs
|
||||
auditor.Logger.Debug(
|
||||
"Selected secondaryTagsIDs as finalTagsIDs",
|
||||
zap.Strings("finalTagsIDs", finalTagsIDs),
|
||||
)
|
||||
}
|
||||
|
||||
// Check if any tags were found
|
||||
if len(finalTagsIDs) != 0 {
|
||||
// Concatenate IDs to form a single string
|
||||
ids := strings.Join(finalTagsIDs, "")
|
||||
frequentTags, err := fetchFrequentTags(auditor, r, ids, category)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get user frequent tags: %w", err)
|
||||
}
|
||||
return frequentTags, nil
|
||||
}
|
||||
|
||||
auditor.Logger.Warn(
|
||||
"No tags found to fetch, setting FrequentTags to empty slice",
|
||||
zap.String("category", category.Value),
|
||||
zap.Strings("primaryTagsIDs", primaryTagsIDs),
|
||||
zap.Strings("secondaryTagsIDs", secondaryTagsIDs),
|
||||
zap.String("illustrationIDs", illustrationIDs),
|
||||
zap.String("mangaIDs", mangaIDs),
|
||||
zap.String("novelIDs", novelIDs),
|
||||
)
|
||||
return []FrequentTag{}, nil
|
||||
}
|
||||
|
||||
// The HasID interface allows for generic handling of the ArtworkBrief
|
||||
// and NovelBrief structs, which both contain an ID string.
|
||||
type HasID interface {
|
||||
|
||||
Reference in New Issue
Block a user