diff --git a/core/artwork.go b/core/artwork.go index 23d3bb2..21122ef 100644 --- a/core/artwork.go +++ b/core/artwork.go @@ -16,6 +16,8 @@ import ( "codeberg.org/vnpower/pixivfe/v2/server/session" ) +var artworkCommentService = NewCommentService(GetArtworkCommentsURL) + // Pixiv returns 0, 1, 2 to filter SFW and/or NSFW artworks. // Those values are saved in `XRestrict` // 0: Safe @@ -234,30 +236,6 @@ func GetArtworkImages(auditor *audit.Auditor, r *http.Request, id string, illust return images, nil } -func GetArtworkComments(auditor *audit.Auditor, r *http.Request, id string) ([]Comment, error) { - var body struct { - Comments []Comment `json:"comments"` - } - - URL := GetArtworkCommentsURL(id) - - resp, err := API_GET_UnwrapJson(r.Context(), auditor, URL, "", r.Header) - if err != nil { - return nil, err - } - resp, err = session.ProxyImageUrl(r, resp) - if err != nil { - return nil, err - } - - err = json.Unmarshal([]byte(resp), &body) - if err != nil { - return nil, err - } - - return body.Comments, nil -} - func GetRelatedArtworks(auditor *audit.Auditor, r *http.Request, id string) ([]ArtworkBrief, error) { var body struct { Illusts []ArtworkBrief `json:"illusts"` @@ -452,8 +430,7 @@ func GetArtworkByID(auditor *audit.Auditor, r *http.Request, id string, full boo go func() { defer wg.Done() - // Retrieve a list of comments for this artwork, if comments are not disabled. - comments, err := GetArtworkComments(auditor, r, id) + comments, err := artworkCommentService.GetComments(auditor, r, id) if err != nil { cerr <- err return diff --git a/core/comments.go b/core/comments.go new file mode 100644 index 0000000..d69a7d9 --- /dev/null +++ b/core/comments.go @@ -0,0 +1,49 @@ +package core + +import ( + "net/http" + + "github.com/goccy/go-json" + + "codeberg.org/vnpower/pixivfe/v2/audit" + "codeberg.org/vnpower/pixivfe/v2/server/session" +) + +type CommentService interface { + GetComments(auditor *audit.Auditor, r *http.Request, id string) ([]Comment, error) +} + +type commentService struct { + getCommentsURL func(id string) string +} + +func (cs *commentService) GetComments(auditor *audit.Auditor, r *http.Request, id string) ([]Comment, error) { + var body struct { + Comments []Comment `json:"comments"` + } + + url := cs.getCommentsURL(id) + + resp, err := API_GET_UnwrapJson(r.Context(), auditor, url, "", r.Header) + if err != nil { + return nil, err + } + + resp, err = session.ProxyImageUrl(r, resp) + if err != nil { + return nil, err + } + + err = json.Unmarshal([]byte(resp), &body) + if err != nil { + return nil, err + } + + return body.Comments, nil +} + +func NewCommentService(getCommentsURL func(id string) string) CommentService { + return &commentService{ + getCommentsURL: getCommentsURL, + } +} diff --git a/core/novel.go b/core/novel.go index 4b7c65a..582e139 100644 --- a/core/novel.go +++ b/core/novel.go @@ -252,28 +252,3 @@ func GetNovelRelated(auditor *audit.Auditor, r *http.Request, id string) ([]Nove return novels.List, nil } - -func GetNovelComments(auditor *audit.Auditor, r *http.Request, id string) ([]Comment, error) { - var body struct { - Comments []Comment `json:"comments"` - } - - URL := GetNovelCommentsURL(id) - - resp, err := API_GET_UnwrapJson(r.Context(), auditor, URL, "", r.Header) - if err != nil { - return nil, err - } - - resp, err = session.ProxyImageUrl(r, resp) - if err != nil { - return nil, err - } - - err = json.Unmarshal([]byte(resp), &body) - if err != nil { - return nil, err - } - - return body.Comments, nil -} diff --git a/server/routes/novel.go b/server/routes/novel.go index 893b6fd..1b54eef 100644 --- a/server/routes/novel.go +++ b/server/routes/novel.go @@ -12,6 +12,8 @@ import ( "codeberg.org/vnpower/pixivfe/v2/server/session" ) +var novelCommentService = core.NewCommentService(core.GetNovelCommentsURL) + func NovelPage(auditor *audit.Auditor, w http.ResponseWriter, r *http.Request) error { id := GetPathVar(r, "id") if _, err := strconv.Atoi(id); err != nil { @@ -35,12 +37,14 @@ func NovelPage(auditor *audit.Auditor, w http.ResponseWriter, r *http.Request) e contentTitles, _ = core.GetNovelSeriesContentTitlesByID(auditor, r, novel.SeriesNavData.SeriesID) } - if novel.CommentOff == 0 { - // TODO should use token only if R-18/R-18G - comments, err := core.GetNovelComments(auditor, r, id) - if err == nil { - novel.CommentsList = comments + // Fetch comments if they are not disabled + if novel.CommentOff != 1 { + // TODO: should use token only if R-18/R-18G + comments, err := novelCommentService.GetComments(auditor, r, id) + if err != nil { + return err } + novel.CommentsList = comments } user, err := core.GetUserBasicInformation(auditor, r, novel.UserID)