mirror of
https://codeberg.org/VnPower/PixivFE
synced 2024-12-06 19:16:23 +01:00
refactor comment-fetching logic with CommentService interface
This commit is contained in:
+3
-26
@@ -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
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user