Index: backend for completed requests

This commit is contained in:
VnPower
2024-12-02 20:49:03 +07:00
parent 49ec04851f
commit 81cf328d90
+65 -16
View File
@@ -19,12 +19,12 @@ import (
type Pages struct {
Pixivision []Pixivision `json:"pixivision"`
Follow []int `json:"follow"`
Recommended RecommendedSN `json:"recommend"`
RecommendedByTag []RecommendByTagSN `json:"recommendByTag"`
RecommendUser []RecommendUserSN `json:"recommendUser"`
TrendingTag []TrendingTagSN `json:"trendingTags"`
Newest []string `json:"newPost"`
Recommended RecommendedSN `json:"recommend"`
RecommendedByTag []RecommendByTagSN `json:"recommendByTag"`
RecommendUser []RecommendUserSN `json:"recommendUser"`
TrendingTag []TrendingTagSN `json:"trendingTags"`
Newest []string `json:"newPost"`
// Commented out fields that aren't currently implemented in the frontend
// EditorRecommended []any `json:"editorRecommend"`
// RecommendedUsers []RecommendedUser `json:"recommendUser"`
@@ -49,9 +49,9 @@ type Pages struct {
}
type TrendingTagSN struct {
Name string `json:"tag"`
TrendingRate int `json:"trendingRate"`
IDs []int `json:"ids"`
Name string `json:"tag"`
TrendingRate int `json:"trendingRate"`
IDs []int `json:"ids"`
}
type RecommendedSN struct {
@@ -63,6 +63,19 @@ type RecommendByTagSN struct {
IDs []string `json:"ids"`
}
type RequestSN struct {
RequestID string `json:"requestId"`
PlanID string `json:"planId"`
CreatorUserID string `json:"creatorUserId"`
RequestTags []string `json:"requestTags"`
RequestProposal struct {
RequestProposalHTML string `json:"requestOriginalProposalHtml"`
}
PostWork struct {
PostWorkID string `json:"postWorkId"`
} `json:"postWork"`
}
// Pixivision represents a Pixivision article as returned by the landing page endpoint.
//
// Note that this type is less complete than the PixivisionArticle type.
@@ -89,10 +102,16 @@ type RecommendedTags struct {
}
type PopularTag struct {
Name string
Name string
TrendingRate int
IDs []int
Artworks []ArtworkBrief
IDs []int
Artworks []ArtworkBrief
}
type Request struct {
Description string
Tags []string
Artwork ArtworkBrief
}
// LandingArtworks aggregates various categories of artworks and other related data
@@ -108,9 +127,9 @@ type LandingArtworks struct {
RecommendByTags []RecommendedTags
RecommendUser []User
PopularTag []PopularTag
Requests []Request
}
// Constants for calling GetNewestFromFollowing when len(followIDs) == 0
const (
// FollowingMode = "all"
@@ -185,6 +204,11 @@ func GetLanding(auditor *audit.Auditor, r *http.Request, mode string, isLoggedIn
landing.Recommended = populateArtworks(pages.Recommended.IDs, artworks)
landing.RecommendUser = populateRecommendUsers(pages.RecommendUser, users, artworks)
landing.PopularTag = populatePopularTags(pages.TrendingTag, artworks)
landing.Requests, err = parseRequests(resp, artworks)
if err != nil {
return nil, err
}
}
landing.Rankings, err = ctx.fetchRankings(mode)
@@ -322,18 +346,43 @@ func populatePopularTags(tags []TrendingTagSN, artworks map[string]ArtworkBrief)
for _, workID := range tag.IDs {
ids = append(ids, fmt.Sprint(workID))
}
artworksList := populateArtworks(ids, artworks)
populated = append(populated, PopularTag{
Name: tag.Name,
Name: tag.Name,
TrendingRate: tag.TrendingRate,
Artworks: artworksList,
Artworks: artworksList,
})
}
return populated
}
func parseRequests(resp string, artworks map[string]ArtworkBrief) ([]Request, error) {
var requests []RequestSN
pixivRequests := gjson.Get(resp, "requests").String()
if err := json.Unmarshal([]byte(pixivRequests), &requests); err != nil {
return nil, err
}
populated := make([]Request, 0, len(requests))
for _, request := range requests {
desc := request.RequestProposal.RequestProposalHTML
tags := request.RequestTags
artwork := artworks[request.PostWork.PostWorkID]
populated = append(populated, Request{
Description: desc,
Tags: tags,
Artwork: artwork,
})
}
return populated, nil
}
// fetchRankings retrieves the current rankings based on the selected mode.
//
// It maps the landing page mode to the appropriate ranking mode and fetches the ranking data.