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")
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 {
post.ParentID = 0
opMap[post.thread.ID] = post
@ -120,7 +120,7 @@ func BuildBoardPages(board *gcsql.Board) error {
if len(catalogThread.Posts) > maxRepliesOnBoardPage {
op := catalogThread.Posts[0]
replies := catalogThread.Posts[len(catalogThread.Posts)-maxRepliesOnBoardPage:]
catalogThread.Posts = []Post{op}
catalogThread.Posts = []*Post{op}
catalogThread.Posts = append(catalogThread.Posts, replies...)
}
catalogThread.uploads, err = thread.GetUploads()

View file

@ -13,14 +13,14 @@ import (
)
type catalogThreadData struct {
Post
Replies int `json:"replies"`
Images int `json:"images"`
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
Stickied int `json:"sticky"`
Locked int `json:"closed"`
Posts []Post `json:"-"`
*Post
Replies int `json:"replies"`
Images int `json:"images"`
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
Stickied int `json:"sticky"`
Locked int `json:"closed"`
Posts []*Post `json:"-"`
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"
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)
return nil
})

View file

@ -80,7 +80,7 @@ type Post struct {
thread gcsql.Thread
}
func (p Post) TitleText() string {
func (p *Post) TitleText() string {
title := "/" + p.BoardDir + "/ - "
if p.Subject != "" {
title += truncateString(p.Subject, 20, true)
@ -112,7 +112,7 @@ func (p *Post) ThumbnailPath() string {
return config.WebPath(p.BoardDir, "thumb", thumbnail)
}
func (p Post) UploadPath() string {
func (p *Post) UploadPath() string {
if p.Filename == "" {
return ""
}
@ -127,7 +127,7 @@ func (p *Post) Stickied() bool {
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...)
if err != nil {
return err
@ -165,7 +165,7 @@ func QueryPosts(query string, params []any, cb func(Post) error) error {
if post.Filename != "" {
post.Extension = path.Ext(post.Filename)
}
if err = cb(post); err != nil {
if err = cb(&post); err != nil {
return err
}
}
@ -206,31 +206,31 @@ func GetBuildablePost(id int, _ int) (*Post, error) {
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"
if limit > 0 {
query += " LIMIT " + strconv.Itoa(limit)
}
var posts []Post
err := QueryPosts(query, []any{ip}, func(p Post) error {
var posts []*Post
err := QueryPosts(query, []any{ip}, func(p *Post) error {
posts = append(posts, p)
return nil
})
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"
var posts []Post
err := QueryPosts(query, []any{thread.ID}, func(p Post) error {
var posts []*Post
err := QueryPosts(query, []any{thread.ID}, func(p *Post) error {
posts = append(posts, p)
return nil
})
return posts, err
}
func GetRecentPosts(boardid int, limit int) ([]Post, error) {
func GetRecentPosts(boardid int, limit int) ([]*Post, error) {
query := postQueryBase
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)
var posts []Post
err := QueryPosts(query, args, func(post Post) error {
var posts []*Post
err := QueryPosts(query, args, func(post *Post) error {
if boardid == 0 || post.BoardID == boardid {
post.Extension = path.Ext(post.Filename)
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
// TODO: make it variadic
func BuildThreads(all bool, boardid, threadid int) error {
var threads []gcsql.Post
var threads []*gcsql.Post
var err error
if all {
threads, err = gcsql.GetBoardTopPosts(boardid)
} else {
var post *gcsql.Post
post, err = gcsql.GetThreadTopPost(threadid)
threads = []gcsql.Post{*post}
threads = []*gcsql.Post{post}
}
if err != nil {
return err
}
for t := range threads {
op := &threads[t]
op := threads[t]
if err = BuildThreadPages(op); err != nil {
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)
}
threadMap := make(map[string][]Post)
threadMap := make(map[string][]*Post)
threadMap["posts"] = posts
if err = json.NewEncoder(threadJSONFile).Encode(threadMap); err != nil {

View file

@ -135,7 +135,7 @@ func GetThreadTopPost(threadID int) (*Post, error) {
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,
tripcode, is_role_signature, email, subject, message, message_raw,
password, deleted_at, is_deleted, banned_message
@ -150,7 +150,7 @@ func GetBoardTopPosts(boardID int) ([]Post, error) {
return nil, err
}
defer rows.Close()
var posts []Post
var posts []*Post
for rows.Next() {
var post Post
// var tmp int // only needed for WHERE clause in query
@ -167,7 +167,7 @@ func GetBoardTopPosts(boardID int) ([]Post, error) {
if bannedMessage != nil {
post.BannedMessage = *bannedMessage
}
posts = append(posts, post)
posts = append(posts, &post)
}
return posts, nil
}

View file

@ -81,7 +81,7 @@ func recentPostsCallback(_ http.ResponseWriter, request *http.Request, _ *gcsql.
}
}
boardidStr := request.FormValue("boardid")
var recentposts []building.Post
var recentposts []*building.Post
var boardid int
if boardidStr != "" {
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
}
fmt.Println("Flag/Country:", post.Flag, post.Country)
return nil
}

View file

@ -15,17 +15,17 @@
{{- if ne .post.Email ""}}</a>{{end}}</span>
{{- 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 -}}
</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">
{{- 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.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 -}}
</span>
{{if $.is_board_page -}}
[<a href="{{.ThreadPath}}">View</a>]
[<a href="{{.post.ThreadPath}}">View</a>]
{{end}}<br />
{{- end -}}
{{- if $.post.IsTopPost -}}
<div class="op-post post" id="op{{$.post.ID}}">
<div class="op-post post" id="op{{.post.ID}}">
{{- else -}}
<div id="replycontainer{{.post.ID}}" class="reply-container">
<div class="reply" id="reply{{.post.ID}}">