From bbf4f2f2b1468ab38fc264dc86b8245682b3b334 Mon Sep 17 00:00:00 2001 From: iacore Date: Tue, 27 Aug 2024 21:12:32 +0000 Subject: [PATCH] clean up --- core/artwork.go | 15 +++++++-------- core/rankingCalendar.go | 6 +++--- core/types.go | 7 +++++++ core/user.go | 3 +-- routes/actions.go | 6 +++--- routes/helpers.go | 1 - routes/personal.go | 4 ++-- routes/rankingCalendar.go | 6 +++--- routes/tag.go | 2 +- routes/types.go | 3 +-- template/templateFunctions.go | 27 ++++++++++++++------------- utils/http.go | 6 +++--- 12 files changed, 45 insertions(+), 41 deletions(-) create mode 100644 core/types.go diff --git a/core/artwork.go b/core/artwork.go index b9b80f2..9663f58 100644 --- a/core/artwork.go +++ b/core/artwork.go @@ -3,7 +3,6 @@ package core import ( "errors" "fmt" - "html/template" "sort" "strings" "sync" @@ -103,13 +102,13 @@ type ArtworkBrief struct { } type Illust struct { - ID string `json:"id"` - Title string `json:"title"` - Description template.HTML `json:"description"` - UserID string `json:"userId"` - UserName string `json:"userName"` - UserAccount string `json:"userAccount"` - Date time.Time `json:"uploadDate"` + ID string `json:"id"` + Title string `json:"title"` + Description HTML `json:"description"` + UserID string `json:"userId"` + UserName string `json:"userName"` + UserAccount string `json:"userAccount"` + Date time.Time `json:"uploadDate"` Images []Image Tags []Tag `json:"tags"` Pages int `json:"pageCount"` diff --git a/core/rankingCalendar.go b/core/rankingCalendar.go index b36d0fa..c953b44 100644 --- a/core/rankingCalendar.go +++ b/core/rankingCalendar.go @@ -40,9 +40,9 @@ func GetRankingCalendar(r *http.Request, mode string, year, month int) (template URL := GetRankingCalendarURL(mode, year, month) req, err := http.NewRequestWithContext(r.Context(), "GET", URL, nil) - if err != nil { - return template.HTML(""), err - } + if err != nil { + return template.HTML(""), err + } req.Header.Add("User-Agent", "Mozilla/5.0") req.Header.Add("Cookie", "PHPSESSID="+token) // req.AddCookie(&http.Cookie{ diff --git a/core/types.go b/core/types.go new file mode 100644 index 0000000..63b3176 --- /dev/null +++ b/core/types.go @@ -0,0 +1,7 @@ +package core + +import ( + html "html/template" +) + +type HTML = html.HTML diff --git a/core/user.go b/core/user.go index 45b4ae5..0689940 100644 --- a/core/user.go +++ b/core/user.go @@ -3,7 +3,6 @@ package core import ( "errors" "fmt" - "html/template" "math" "sort" @@ -48,7 +47,7 @@ type User struct { Avatar string `json:"imageBig"` Following int `json:"following"` MyPixiv int `json:"mypixivCount"` - Comment template.HTML `json:"commentHtml"` + Comment HTML `json:"commentHtml"` Webpage string `json:"webpage"` SocialRaw json.RawMessage `json:"social"` Artworks []ArtworkBrief `json:"artworks"` diff --git a/routes/actions.go b/routes/actions.go index 67c869e..96da1ec 100644 --- a/routes/actions.go +++ b/routes/actions.go @@ -16,9 +16,9 @@ func pixivPostRequest(r *http.Request, url, payload, token, csrf string, isJSON requestBody := []byte(payload) req, err := http.NewRequestWithContext(r.Context(), "POST", url, bytes.NewBuffer(requestBody)) - if err != nil { - return err - } + if err != nil { + return err + } req.Header.Add("User-Agent", "Mozilla/5.0") req.Header.Add("Accept", "application/json") req.Header.Add("Cookie", "PHPSESSID="+token) diff --git a/routes/helpers.go b/routes/helpers.go index 3ae4def..403a578 100644 --- a/routes/helpers.go +++ b/routes/helpers.go @@ -6,7 +6,6 @@ import ( "github.com/gorilla/mux" ) - func GetQueryParam(r *http.Request, name string, defaultValue ...string) string { if v := r.URL.Query().Get(name); v != "" { return v diff --git a/routes/personal.go b/routes/personal.go index 58c8cc3..782d727 100644 --- a/routes/personal.go +++ b/routes/personal.go @@ -28,7 +28,7 @@ func LoginUserPage(w http.ResponseWriter, r *http.Request) error { // The left part of the token is the member ID userId := strings.Split(token, "_") - http.Redirect(w, r, "/users/" + userId[0], http.StatusSeeOther) + http.Redirect(w, r, "/users/"+userId[0], http.StatusSeeOther) return nil } @@ -41,7 +41,7 @@ func LoginBookmarkPage(w http.ResponseWriter, r *http.Request) error { // The left part of the token is the member ID userId := strings.Split(token, "_") - http.Redirect(w, r, "/users/" + userId[0] + "/bookmarks#checkpoint", http.StatusSeeOther) + http.Redirect(w, r, "/users/"+userId[0]+"/bookmarks#checkpoint", http.StatusSeeOther) return nil } diff --git a/routes/rankingCalendar.go b/routes/rankingCalendar.go index ea58ab7..cbf0b38 100644 --- a/routes/rankingCalendar.go +++ b/routes/rankingCalendar.go @@ -42,9 +42,9 @@ func RankingCalendarPicker(w http.ResponseWriter, r *http.Request) error { } date := r.FormValue("date") - return utils.RedirectTo(w, r,"/rankingCalendar", map[string]string{ - "mode": mode, - "date": date, + return utils.RedirectTo(w, r, "/rankingCalendar", map[string]string{ + "mode": mode, + "date": date, }) } diff --git a/routes/tag.go b/routes/tag.go index 7e6fbd5..a0fe614 100644 --- a/routes/tag.go +++ b/routes/tag.go @@ -56,7 +56,7 @@ func TagPage(w http.ResponseWriter, r *http.Request) error { } func AdvancedTagPost(w http.ResponseWriter, r *http.Request) error { - return utils.RedirectTo(w, r,"/tags", map[string]string{ + return utils.RedirectTo(w, r, "/tags", map[string]string{ "name": GetQueryParam(r, "name", r.FormValue("name")), "category": GetQueryParam(r, "category", "artworks"), "order": GetQueryParam(r, "order", "date_d"), diff --git a/routes/types.go b/routes/types.go index 74ee9d6..6abe077 100644 --- a/routes/types.go +++ b/routes/types.go @@ -1,7 +1,6 @@ package routes import ( - html "html/template" "net/http" "codeberg.org/vnpower/pixivfe/v2/core" @@ -102,7 +101,7 @@ type Data_rank struct { } type Data_rankingCalendar struct { Title string - Render html.HTML + Render core.HTML Mode string Year int MonthBefore DateWrap diff --git a/template/templateFunctions.go b/template/templateFunctions.go index d770ca8..c92ada1 100644 --- a/template/templateFunctions.go +++ b/template/templateFunctions.go @@ -2,7 +2,6 @@ package template import ( "fmt" - "html/template" "math" "math/rand" "net/url" @@ -13,6 +12,8 @@ import ( "codeberg.org/vnpower/pixivfe/v2/core" ) +type HTML = core.HTML + func GetRandomColor() string { // Some color shade I stole colors := []string{ @@ -42,7 +43,7 @@ func GetRandomColor() string { return colors[rand.Intn(len(colors))] } -func ParseEmojis(s string) template.HTML { +func ParseEmojis(s string) HTML { emojiList := map[string]string{ "normal": "101", "surprise": "102", @@ -92,10 +93,10 @@ func ParseEmojis(s string) template.HTML { return fmt.Sprintf(`(%s)`, id, s) }) - return template.HTML(parsedString) + return HTML(parsedString) } -func ParsePixivRedirect(s string) template.HTML { +func ParsePixivRedirect(s string) HTML { regex := regexp.MustCompile(`\/jump\.php\?(http[^"]+)`) parsedString := regex.ReplaceAllStringFunc(s, func(s string) string { @@ -104,9 +105,9 @@ func ParsePixivRedirect(s string) template.HTML { }) escaped, err := url.QueryUnescape(parsedString) if err != nil { - return template.HTML(s) + return HTML(s) } - return template.HTML(escaped) + return HTML(escaped) } func EscapeString(s string) string { @@ -122,7 +123,7 @@ func ParseTimeCustomFormat(date time.Time, format string) string { return date.Format(format) } -func CreatePaginator(base, ending string, current_page, max_page int) template.HTML { +func CreatePaginator(base, ending string, current_page, max_page int) HTML { pageUrl := func(page int) string { return fmt.Sprintf(`%s%d%s`, base, page, ending) } @@ -194,7 +195,7 @@ func CreatePaginator(base, ending string, current_page, max_page int) template.H } pages += `` - return template.HTML(pages) + return HTML(pages) } func GetNovelGenre(s string) string { @@ -249,11 +250,11 @@ func SwitchButtonAttributes(baseURL, selection, currentSelection string) string func GetTemplateFunctions() map[string]any { return map[string]any{ - "parseEmojis": func(s string) template.HTML { + "parseEmojis": func(s string) HTML { return ParseEmojis(s) }, - "parsePixivRedirect": func(s string) template.HTML { + "parsePixivRedirect": func(s string) HTML { return ParsePixivRedirect(s) }, "escapeString": func(s string) string { @@ -289,7 +290,7 @@ func GetTemplateFunctions() map[string]any { "parseTimeCustomFormat": func(date time.Time, format string) string { return ParseTimeCustomFormat(date, format) }, - "createPaginator": func(base, ending string, current_page, max_page int) template.HTML { + "createPaginator": func(base, ending string, current_page, max_page int) HTML { return CreatePaginator(base, ending, current_page, max_page) }, "joinArtworkIds": func(artworks []core.ArtworkBrief) string { @@ -303,10 +304,10 @@ func GetTemplateFunctions() map[string]any { // this is stupid return s[:len(s)-6] }, - "renderNovel": func(s string) template.HTML { + "renderNovel": func(s string) HTML { s = strings.ReplaceAll(s, "\n", "
") s = strings.ReplaceAll(s, "[newpage]", "Insert page separator here.") - return template.HTML(s) + return HTML(s) }, "novelGenre": GetNovelGenre, "floor": func(i float64) int { diff --git a/utils/http.go b/utils/http.go index 35205b8..972a63e 100644 --- a/utils/http.go +++ b/utils/http.go @@ -1,16 +1,16 @@ package utils import ( - "encoding/json" "net/http" "net/url" "strings" -) + "github.com/goccy/go-json" +) func SendString(w http.ResponseWriter, text string) error { w.Header().Set("content-type", "text/plain") - _, err := w.Write([]byte(text)) + _, err := w.Write([]byte(text)) return err }