Files
2024-12-05 07:17:30 +11:00

86 lines
1.8 KiB
Go

package core
import (
"net/http"
"time"
"github.com/goccy/go-json"
"codeberg.org/vnpower/pixivfe/v2/audit"
"codeberg.org/vnpower/pixivfe/v2/server/session"
)
type Comment struct {
AuthorID string `json:"userId"`
AuthorName string `json:"userName"`
Avatar string `json:"img"`
Context string `json:"comment"`
Stamp string `json:"stampId"`
Date time.Time `json:"commentDate"`
}
// UnmarshalJSON implements custom JSON unmarshaling for Comment
func (c *Comment) UnmarshalJSON(data []byte) error {
type Alias Comment // Create alias to avoid recursion
aux := &struct {
Date string `json:"commentDate"`
*Alias
}{
Alias: (*Alias)(c),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
// Parse the date string
parsedTime, err := time.Parse("2006-01-02 15:04", aux.Date)
if err != nil {
// Set to zero time if parsing fails
c.Date = time.Time{}
return nil
}
c.Date = parsedTime
return nil
}
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,
}
}