2022-10-11 14:26:31 -07:00
|
|
|
package building
|
|
|
|
|
2022-11-28 16:15:20 -08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/gochan-org/gochan/pkg/config"
|
|
|
|
"github.com/gochan-org/gochan/pkg/gcsql"
|
|
|
|
"github.com/gochan-org/gochan/pkg/gctemplates"
|
|
|
|
"github.com/gochan-org/gochan/pkg/gcutil"
|
|
|
|
"github.com/gochan-org/gochan/pkg/serverutil"
|
|
|
|
)
|
2022-10-31 12:41:17 -07:00
|
|
|
|
2022-10-11 14:26:31 -07:00
|
|
|
type catalogThreadData struct {
|
2022-11-28 16:15:20 -08:00
|
|
|
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
|
|
|
|
Sticky int `json:"sticky"`
|
|
|
|
Locked int `json:"locked"`
|
|
|
|
Posts []Post `json:"-"`
|
|
|
|
uploads []gcsql.Upload
|
2022-10-11 14:26:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type catalogPage struct {
|
|
|
|
PageNum int `json:"page"`
|
|
|
|
Threads []catalogThreadData `json:"threads"`
|
|
|
|
}
|
|
|
|
|
2022-10-31 12:41:17 -07:00
|
|
|
type boardCatalog struct {
|
|
|
|
pages []catalogPage // this array gets marshalled, not the boardCatalog object
|
|
|
|
numPages int
|
|
|
|
currentPage int
|
|
|
|
}
|
|
|
|
|
|
|
|
// fillPages fills the catalog's pages array with pages of the specified size, with the remainder
|
|
|
|
// on the last page
|
|
|
|
func (catalog *boardCatalog) fillPages(threadsPerPage int, threads []catalogThreadData) {
|
|
|
|
catalog.pages = []catalogPage{} // clear the array if it isn't already
|
|
|
|
catalog.numPages = len(threads) / threadsPerPage
|
|
|
|
remainder := len(threads) % threadsPerPage
|
|
|
|
currentThreadIndex := 0
|
|
|
|
var i int
|
|
|
|
for i = 0; i < catalog.numPages; i++ {
|
|
|
|
catalog.pages = append(catalog.pages,
|
|
|
|
catalogPage{
|
|
|
|
PageNum: i + 1,
|
|
|
|
Threads: threads[currentThreadIndex : currentThreadIndex+threadsPerPage],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
currentThreadIndex += threadsPerPage
|
|
|
|
}
|
|
|
|
if remainder > 0 {
|
|
|
|
catalog.pages = append(catalog.pages,
|
|
|
|
catalogPage{
|
|
|
|
PageNum: i + 1,
|
|
|
|
Threads: threads[len(threads)-remainder:],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-28 16:15:20 -08:00
|
|
|
// BuildCatalog builds the catalog for a board with a given id
|
|
|
|
func BuildCatalog(boardID int) error {
|
|
|
|
errEv := gcutil.LogError(nil).
|
|
|
|
Str("building", "catalog").
|
|
|
|
Int("boardID", boardID)
|
|
|
|
err := gctemplates.InitTemplates("catalog")
|
|
|
|
if err != nil {
|
|
|
|
errEv.Err(err).Send()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
board, err := gcsql.GetBoardFromID(boardID)
|
|
|
|
if err != nil {
|
|
|
|
errEv.Err(err).
|
|
|
|
Caller().Msg("Unable to get board information")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
errEv.Str("boardDir", board.Dir)
|
|
|
|
criticalCfg := config.GetSystemCriticalConfig()
|
|
|
|
catalogPath := path.Join(criticalCfg.DocumentRoot, board.Dir, "catalog.html")
|
|
|
|
catalogFile, err := os.OpenFile(catalogPath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0777)
|
|
|
|
if err != nil {
|
|
|
|
errEv.Err(err).Caller().Send()
|
2022-12-13 16:23:16 -08:00
|
|
|
return fmt.Errorf("failed opening /%s/catalog.html: %s", board.Dir, err.Error())
|
2022-11-28 16:15:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
threadOPs, err := getBoardTopPosts(boardID)
|
|
|
|
if err != nil {
|
|
|
|
errEv.Err(err).Caller().Send()
|
2022-12-13 16:23:16 -08:00
|
|
|
return fmt.Errorf("failed building catalog for /%s/: %s", board.Dir, err.Error())
|
2022-11-28 16:15:20 -08:00
|
|
|
}
|
|
|
|
boardConfig := config.GetBoardConfig(board.Dir)
|
|
|
|
|
|
|
|
if err = serverutil.MinifyTemplate(gctemplates.Catalog, map[string]interface{}{
|
2022-12-22 12:58:18 -08:00
|
|
|
"boards": gcsql.AllBoards,
|
|
|
|
"webroot": criticalCfg.WebRoot,
|
|
|
|
"board": board,
|
|
|
|
"boardConfig": boardConfig,
|
|
|
|
"sections": gcsql.AllSections,
|
|
|
|
"threads": threadOPs,
|
2022-11-28 16:15:20 -08:00
|
|
|
}, catalogFile, "text/html"); err != nil {
|
|
|
|
errEv.Err(err).Caller().Send()
|
|
|
|
return fmt.Errorf("failed building catalog for /%s/: %s", board.Dir, err.Error())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|