1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-02 06:46:24 -07:00

Fix template issues with pointer/non-pointer receivers

This commit is contained in:
Eggbertx 2024-03-27 11:13:31 -07:00
parent 34e6887490
commit 8464c2447c
8 changed files with 39 additions and 40 deletions

View file

@ -74,7 +74,7 @@ func BuildBoardPages(board *gcsql.Board) error {
errEv.Err(err).Caller().Msg("Failed getting board threads") errEv.Err(err).Caller().Msg("Failed getting board threads")
return fmt.Errorf("error getting OP posts for /%s/: %s", board.Dir, err.Error()) return fmt.Errorf("error getting OP posts for /%s/: %s", board.Dir, err.Error())
} }
opMap := make(map[int]Post) opMap := make(map[int]*Post)
for _, post := range topPosts { for _, post := range topPosts {
post.ParentID = 0 post.ParentID = 0
opMap[post.thread.ID] = post opMap[post.thread.ID] = post
@ -120,7 +120,7 @@ func BuildBoardPages(board *gcsql.Board) error {
if len(catalogThread.Posts) > maxRepliesOnBoardPage { if len(catalogThread.Posts) > maxRepliesOnBoardPage {
op := catalogThread.Posts[0] op := catalogThread.Posts[0]
replies := catalogThread.Posts[len(catalogThread.Posts)-maxRepliesOnBoardPage:] replies := catalogThread.Posts[len(catalogThread.Posts)-maxRepliesOnBoardPage:]
catalogThread.Posts = []Post{op} catalogThread.Posts = []*Post{op}
catalogThread.Posts = append(catalogThread.Posts, replies...) catalogThread.Posts = append(catalogThread.Posts, replies...)
} }
catalogThread.uploads, err = thread.GetUploads() catalogThread.uploads, err = thread.GetUploads()

View file

@ -13,14 +13,14 @@ import (
) )
type catalogThreadData struct { type catalogThreadData struct {
Post *Post
Replies int `json:"replies"` Replies int `json:"replies"`
Images int `json:"images"` Images int `json:"images"`
OmittedPosts int `json:"omitted_posts"` // posts in the thread but not shown on the board page OmittedPosts int `json:"omitted_posts"` // posts in the thread but not shown on the board page
OmittedImages int `json:"omitted_images"` // uploads in the thread but not shown on the board page OmittedImages int `json:"omitted_images"` // uploads in the thread but not shown on the board page
Stickied int `json:"sticky"` Stickied int `json:"sticky"`
Locked int `json:"closed"` Locked int `json:"closed"`
Posts []Post `json:"-"` Posts []*Post `json:"-"`
uploads []gcsql.Upload uploads []gcsql.Upload
} }
@ -62,11 +62,11 @@ func (catalog *boardCatalog) fillPages(threadsPerPage int, threads []catalogThre
} }
} }
func getBoardTopPosts(boardID int) ([]Post, error) { func getBoardTopPosts(boardID int) ([]*Post, error) {
const query = postQueryBase + " AND is_top_post AND t.board_id = ? ORDER BY t.stickied DESC, last_bump DESC" const query = postQueryBase + " AND is_top_post AND t.board_id = ? ORDER BY t.stickied DESC, last_bump DESC"
var posts []Post var posts []*Post
err := QueryPosts(query, []any{boardID}, func(p Post) error { err := QueryPosts(query, []any{boardID}, func(p *Post) error {
posts = append(posts, p) posts = append(posts, p)
return nil return nil
}) })

View file

@ -80,7 +80,7 @@ type Post struct {
thread gcsql.Thread thread gcsql.Thread
} }
func (p Post) TitleText() string { func (p *Post) TitleText() string {
title := "/" + p.BoardDir + "/ - " title := "/" + p.BoardDir + "/ - "
if p.Subject != "" { if p.Subject != "" {
title += truncateString(p.Subject, 20, true) title += truncateString(p.Subject, 20, true)
@ -112,7 +112,7 @@ func (p *Post) ThumbnailPath() string {
return config.WebPath(p.BoardDir, "thumb", thumbnail) return config.WebPath(p.BoardDir, "thumb", thumbnail)
} }
func (p Post) UploadPath() string { func (p *Post) UploadPath() string {
if p.Filename == "" { if p.Filename == "" {
return "" return ""
} }
@ -127,7 +127,7 @@ func (p *Post) Stickied() bool {
return p.thread.Stickied return p.thread.Stickied
} }
func QueryPosts(query string, params []any, cb func(Post) error) error { func QueryPosts(query string, params []any, cb func(*Post) error) error {
rows, err := gcsql.QuerySQL(query, params...) rows, err := gcsql.QuerySQL(query, params...)
if err != nil { if err != nil {
return err return err
@ -165,7 +165,7 @@ func QueryPosts(query string, params []any, cb func(Post) error) error {
if post.Filename != "" { if post.Filename != "" {
post.Extension = path.Ext(post.Filename) post.Extension = path.Ext(post.Filename)
} }
if err = cb(post); err != nil { if err = cb(&post); err != nil {
return err return err
} }
} }
@ -206,31 +206,31 @@ func GetBuildablePost(id int, _ int) (*Post, error) {
return &post, nil return &post, nil
} }
func GetBuildablePostsByIP(ip string, limit int) ([]Post, error) { func GetBuildablePostsByIP(ip string, limit int) ([]*Post, error) {
query := postQueryBase + " AND DBPREFIXposts.ip = ? ORDER BY DBPREFIXposts.id DESC" query := postQueryBase + " AND DBPREFIXposts.ip = ? ORDER BY DBPREFIXposts.id DESC"
if limit > 0 { if limit > 0 {
query += " LIMIT " + strconv.Itoa(limit) query += " LIMIT " + strconv.Itoa(limit)
} }
var posts []Post var posts []*Post
err := QueryPosts(query, []any{ip}, func(p Post) error { err := QueryPosts(query, []any{ip}, func(p *Post) error {
posts = append(posts, p) posts = append(posts, p)
return nil return nil
}) })
return posts, err return posts, err
} }
func getThreadPosts(thread *gcsql.Thread) ([]Post, error) { func getThreadPosts(thread *gcsql.Thread) ([]*Post, error) {
const query = postQueryBase + " AND DBPREFIXposts.thread_id = ? ORDER BY DBPREFIXposts.id ASC" const query = postQueryBase + " AND DBPREFIXposts.thread_id = ? ORDER BY DBPREFIXposts.id ASC"
var posts []Post var posts []*Post
err := QueryPosts(query, []any{thread.ID}, func(p Post) error { err := QueryPosts(query, []any{thread.ID}, func(p *Post) error {
posts = append(posts, p) posts = append(posts, p)
return nil return nil
}) })
return posts, err return posts, err
} }
func GetRecentPosts(boardid int, limit int) ([]Post, error) { func GetRecentPosts(boardid int, limit int) ([]*Post, error) {
query := postQueryBase query := postQueryBase
var args []any var args []any
@ -241,8 +241,8 @@ func GetRecentPosts(boardid int, limit int) ([]Post, error) {
query += " ORDER BY DBPREFIXposts.id DESC LIMIT " + strconv.Itoa(limit) query += " ORDER BY DBPREFIXposts.id DESC LIMIT " + strconv.Itoa(limit)
var posts []Post var posts []*Post
err := QueryPosts(query, args, func(post Post) error { err := QueryPosts(query, args, func(post *Post) error {
if boardid == 0 || post.BoardID == boardid { if boardid == 0 || post.BoardID == boardid {
post.Extension = path.Ext(post.Filename) post.Extension = path.Ext(post.Filename)
posts = append(posts, post) posts = append(posts, post)

View file

@ -19,21 +19,21 @@ import (
// if all is set to true, ignore which, otherwise, which = build only specified boardid // if all is set to true, ignore which, otherwise, which = build only specified boardid
// TODO: make it variadic // TODO: make it variadic
func BuildThreads(all bool, boardid, threadid int) error { func BuildThreads(all bool, boardid, threadid int) error {
var threads []gcsql.Post var threads []*gcsql.Post
var err error var err error
if all { if all {
threads, err = gcsql.GetBoardTopPosts(boardid) threads, err = gcsql.GetBoardTopPosts(boardid)
} else { } else {
var post *gcsql.Post var post *gcsql.Post
post, err = gcsql.GetThreadTopPost(threadid) post, err = gcsql.GetThreadTopPost(threadid)
threads = []gcsql.Post{*post} threads = []*gcsql.Post{post}
} }
if err != nil { if err != nil {
return err return err
} }
for t := range threads { for t := range threads {
op := &threads[t] op := threads[t]
if err = BuildThreadPages(op); err != nil { if err = BuildThreadPages(op); err != nil {
return err return err
} }
@ -128,7 +128,7 @@ func BuildThreadPages(op *gcsql.Post) error {
return fmt.Errorf("failed setting file permissions for /%s/res/%d.json", board.Dir, posts[0].ID) return fmt.Errorf("failed setting file permissions for /%s/res/%d.json", board.Dir, posts[0].ID)
} }
threadMap := make(map[string][]Post) threadMap := make(map[string][]*Post)
threadMap["posts"] = posts threadMap["posts"] = posts
if err = json.NewEncoder(threadJSONFile).Encode(threadMap); err != nil { if err = json.NewEncoder(threadJSONFile).Encode(threadMap); err != nil {

View file

@ -135,7 +135,7 @@ func GetThreadTopPost(threadID int) (*Post, error) {
return post, err return post, err
} }
func GetBoardTopPosts(boardID int) ([]Post, error) { func GetBoardTopPosts(boardID int) ([]*Post, error) {
query := `SELECT DBPREFIXposts.id, thread_id, is_top_post, ip, created_on, name, query := `SELECT DBPREFIXposts.id, thread_id, is_top_post, ip, created_on, name,
tripcode, is_role_signature, email, subject, message, message_raw, tripcode, is_role_signature, email, subject, message, message_raw,
password, deleted_at, is_deleted, banned_message password, deleted_at, is_deleted, banned_message
@ -150,7 +150,7 @@ func GetBoardTopPosts(boardID int) ([]Post, error) {
return nil, err return nil, err
} }
defer rows.Close() defer rows.Close()
var posts []Post var posts []*Post
for rows.Next() { for rows.Next() {
var post Post var post Post
// var tmp int // only needed for WHERE clause in query // var tmp int // only needed for WHERE clause in query
@ -167,7 +167,7 @@ func GetBoardTopPosts(boardID int) ([]Post, error) {
if bannedMessage != nil { if bannedMessage != nil {
post.BannedMessage = *bannedMessage post.BannedMessage = *bannedMessage
} }
posts = append(posts, post) posts = append(posts, &post)
} }
return posts, nil return posts, nil
} }

View file

@ -81,7 +81,7 @@ func recentPostsCallback(_ http.ResponseWriter, request *http.Request, _ *gcsql.
} }
} }
boardidStr := request.FormValue("boardid") boardidStr := request.FormValue("boardid")
var recentposts []building.Post var recentposts []*building.Post
var boardid int var boardid int
if boardidStr != "" { if boardidStr != "" {
if boardid, err = strconv.Atoi(boardidStr); err != nil { if boardid, err = strconv.Atoi(boardidStr); err != nil {

View file

@ -77,7 +77,6 @@ func attachFlag(request *http.Request, post *gcsql.Post, board string, errEv *ze
} }
post.Flag = flag post.Flag = flag
} }
fmt.Println("Flag/Country:", post.Flag, post.Country)
return nil return nil
} }

View file

@ -15,17 +15,17 @@
{{- if ne .post.Email ""}}</a>{{end}}</span> {{- if ne .post.Email ""}}</a>{{end}}</span>
{{- if ne .post.Tripcode ""}}<span class="tripcode">!{{.post.Tripcode}}</span>{{end -}} {{- if ne .post.Tripcode ""}}<span class="tripcode">!{{.post.Tripcode}}</span>{{end -}}
{{- if ne .post.Country.Flag ""}}{{template "post_flag" .post.Country}}{{end}} {{formatTimestamp .post.Timestamp -}} {{- if ne .post.Country.Flag ""}}{{template "post_flag" .post.Country}}{{end}} {{formatTimestamp .post.Timestamp -}}
</label> <a href="{{.WebPath}}">No.</a> <a href="javascript:quote({{.post.ID}})" class="backlink-click">{{.post.ID}}</a> </label> <a href="{{.post.WebPath}}">No.</a> <a href="javascript:quote({{.post.ID}})" class="backlink-click">{{.post.ID}}</a>
<span class="status-icons"> <span class="status-icons">
{{- if $.thread.Locked}}<img src="{{webPath "/static/lock.png"}}" class="locked-icon" alt="Thread locked" title="Thread locked">{{end -}} {{- if $.thread.Locked}}<img src="{{webPath `/static/lock.png`}}" class="locked-icon" alt="Thread locked" title="Thread locked">{{end -}}
{{- if $.thread.Stickied}}<img src="{{webPath "/static/sticky.png"}}" class="sticky-icon" alt="Sticky" title="Sticky">{{end -}} {{- if $.thread.Stickied}}<img src="{{webPath `/static/sticky.png`}}" class="sticky-icon" alt="Sticky" title="Sticky">{{end -}}
</span> </span>
{{if $.is_board_page -}} {{if $.is_board_page -}}
[<a href="{{.ThreadPath}}">View</a>] [<a href="{{.post.ThreadPath}}">View</a>]
{{end}}<br /> {{end}}<br />
{{- end -}} {{- end -}}
{{- if $.post.IsTopPost -}} {{- if $.post.IsTopPost -}}
<div class="op-post post" id="op{{$.post.ID}}"> <div class="op-post post" id="op{{.post.ID}}">
{{- else -}} {{- else -}}
<div id="replycontainer{{.post.ID}}" class="reply-container"> <div id="replycontainer{{.post.ID}}" class="reply-container">
<div class="reply" id="reply{{.post.ID}}"> <div class="reply" id="reply{{.post.ID}}">