1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-04 20:16:24 -07:00

Fix recent post checking

This commit is contained in:
Eggbertx 2022-11-14 12:37:28 -08:00
parent 30b5a84aab
commit fd6ca5d38c
3 changed files with 112 additions and 75 deletions

View file

@ -3,6 +3,7 @@ package building
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"html/template"
"os" "os"
"path" "path"
"strconv" "strconv"
@ -14,8 +15,9 @@ import (
) )
const ( const (
postQueryBase = `SELECT DBPREFIXposts.id AS postid, thread_id AS threadid, name, tripcode, email, subject, postQueryBase = `SELECT DBPREFIXposts.id AS postid, thread_id AS threadid, ip, name, tripcode, email, subject, created_on, created_on as last_modified,
(SELECT id FROM DBPREFIXposts WHERE thread_id = threadid AND is_top_post) AS parent_id, (SELECT id FROM DBPREFIXposts WHERE thread_id = threadid AND is_top_post) AS parent_id,
message, message_raw,
(SELECT board_id FROM DBPREFIXthreads where id = threadid LIMIT 1) AS boardid, (SELECT board_id FROM DBPREFIXthreads where id = threadid LIMIT 1) AS boardid,
(SELECT dir FROM DBPREFIXboards WHERE id = boardid LIMIT 1) AS dir, (SELECT dir FROM DBPREFIXboards WHERE id = boardid LIMIT 1) AS dir,
coalesce(DBPREFIXfiles.original_filename,'') as original_filename, coalesce(DBPREFIXfiles.original_filename,'') as original_filename,
@ -31,27 +33,52 @@ const (
) )
type PostJSON struct { type PostJSON struct {
ID int `json:"no"` ID int `json:"no"`
ParentID int `json:"resto"` ParentID int `json:"resto"`
BoardID int `json:"-"` BoardID int `json:"-"`
BoardDir string `json:"-"` BoardDir string `json:"-"`
Name string `json:"name"` IP string `json:"-"`
Trip string `json:"trip"` Name string `json:"name"`
Email string `json:"email"` Tripcode string `json:"trip"`
Subject string `json:"sub"` Email string `json:"email"`
Message string `json:"com"` Subject string `json:"sub"`
Filename string `json:"tim"` MessageRaw string `json:"com"`
OriginalFilename string `json:"filename"` Message template.HTML `json:"-"`
Checksum string `json:"md5"` Filename string `json:"tim"`
Extension string `json:"extension"` OriginalFilename string `json:"filename"`
Filesize int `json:"fsize"` Checksum string `json:"md5"`
Width int `json:"w"` Extension string `json:"extension"`
Height int `json:"h"` Filesize int `json:"fsize"`
ThumbnailWidth int `json:"tn_w"` Width int `json:"w"`
ThumbnailHeight int `json:"tn_h"` Height int `json:"h"`
Capcode string `json:"capcode"` ThumbnailWidth int `json:"tn_w"`
Time string `json:"time"` ThumbnailHeight int `json:"tn_h"`
LastModified string `json:"last_modified"` Capcode string `json:"capcode"`
Time string `json:"time"`
LastModified string `json:"last_modified"`
}
func (p *PostJSON) WebPath() string {
threadID := p.ParentID
if threadID == 0 {
threadID = p.ID
}
return config.WebPath(p.BoardDir, "res", strconv.Itoa(threadID)+".html#"+strconv.Itoa(p.ID))
}
func (p *PostJSON) ThumbnailPath() string {
if p.Filename == "" {
return ""
}
return config.WebPath(p.BoardDir, "thumb", gcutil.GetThumbnailPath("reply", p.Filename))
}
func (p *PostJSON) UploadPath() string {
if p.Filename == "" {
return ""
}
return config.WebPath(p.BoardDir, "src", p.Filename)
} }
func GetPostJSON(id int, boardid int) (*PostJSON, error) { func GetPostJSON(id int, boardid int) (*PostJSON, error) {
@ -59,19 +86,28 @@ func GetPostJSON(id int, boardid int) (*PostJSON, error) {
var post PostJSON var post PostJSON
var threadID int var threadID int
err := gcsql.QueryRowSQL(query, []interface{}{id}, []interface{}{ err := gcsql.QueryRowSQL(query, []interface{}{id}, []interface{}{
&post.ID, &threadID, &post.Name, &post.Trip, &post.Email, &post.Subject, &post.ID, &threadID, &post.IP, &post.Name, &post.Tripcode, &post.Email, &post.Subject, &post.Time, &post.LastModified,
&post.ParentID, &post.BoardID, &post.BoardDir, &post.OriginalFilename, &post.Filename, &post.ParentID, &post.Message, &post.MessageRaw, &post.BoardID, &post.BoardDir,
&post.Checksum, &post.Filesize, &post.ThumbnailWidth, &post.ThumbnailHeight, &post.OriginalFilename, &post.Filename, &post.Checksum, &post.Filesize,
&post.Width, &post.Height, &post.ThumbnailWidth, &post.ThumbnailHeight, &post.Width, &post.Height,
}) })
if err != nil { if err != nil {
return nil, err return nil, err
} }
post.Extension = path.Ext(post.Filename)
return &post, nil return &post, nil
} }
func GetRecentPosts(boardid int, limit int) ([]PostJSON, error) { func GetRecentPosts(boardid int, limit int) ([]PostJSON, error) {
rows, err := gcsql.QuerySQL(postQueryBase + " LIMIT " + strconv.Itoa(limit)) query := "SELECT * FROM (" + postQueryBase + ") posts"
var args []interface{} = []interface{}{}
if boardid > 0 {
query += " WHERE boardid = ?"
args = append(args, boardid)
}
rows, err := gcsql.QuerySQL(query+" LIMIT "+strconv.Itoa(limit), args...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -81,15 +117,16 @@ func GetRecentPosts(boardid int, limit int) ([]PostJSON, error) {
var post PostJSON var post PostJSON
var threadID int var threadID int
err = rows.Scan( err = rows.Scan(
&post.ID, &threadID, &post.Name, &post.Trip, &post.Email, &post.Subject, &post.ID, &threadID, &post.IP, &post.Name, &post.Tripcode, &post.Email, &post.Subject, &post.Time, &post.LastModified,
&post.ParentID, &post.BoardID, &post.BoardDir, &post.OriginalFilename, &post.Filename, &post.ParentID, &post.Message, &post.MessageRaw, &post.BoardID, &post.BoardDir,
&post.Checksum, &post.Filesize, &post.ThumbnailWidth, &post.ThumbnailHeight, &post.OriginalFilename, &post.Filename, &post.Checksum, &post.Filesize,
&post.Width, &post.Height, &post.ThumbnailWidth, &post.ThumbnailHeight, &post.Width, &post.Height,
) )
if err != nil { if err != nil {
return nil, err return nil, err
} }
if boardid > 0 && post.BoardID == boardid { if boardid == 0 || post.BoardID == boardid {
post.Extension = path.Ext(post.Filename)
posts = append(posts, post) posts = append(posts, post)
} }
} }

View file

@ -171,21 +171,38 @@ var actions = []Action{
Permissions: JanitorPerms, Permissions: JanitorPerms,
JSONoutput: OptionalJSON, JSONoutput: OptionalJSON,
Callback: func(writer http.ResponseWriter, request *http.Request, staff *gcsql.Staff, wantsJSON bool, infoEv, errEv *zerolog.Event) (output interface{}, err error) { Callback: func(writer http.ResponseWriter, request *http.Request, staff *gcsql.Staff, wantsJSON bool, infoEv, errEv *zerolog.Event) (output interface{}, err error) {
limit, err := getIntField("limit", staff.Username, request, 0) limit := 20
limitStr := request.FormValue("limit")
if limitStr != "" {
limit, err = strconv.Atoi(limitStr)
if err != nil {
errEv.Err(err).Caller().Send()
return "", err
}
}
boardidStr := request.FormValue("boardid")
var recentposts []building.PostJSON
var boardid int
if boardidStr != "" {
if boardid, err = strconv.Atoi(boardidStr); err != nil {
errEv.Err(err).Caller().Send()
return "", err
}
}
recentposts, err = building.GetRecentPosts(boardid, limit)
if err != nil { if err != nil {
errEv.Err(err).Caller().Send()
return "", err return "", err
} }
boardid, err := getIntField("boardid", staff.Username, request, 0) if wantsJSON {
if err != nil { return recentposts, nil
return "", err
}
recentposts, err := building.GetRecentPosts(boardid, limit)
if err != nil {
return "", err
} }
manageRecentsBuffer := bytes.NewBufferString("") manageRecentsBuffer := bytes.NewBufferString("")
if err = serverutil.MinifyTemplate(gctemplates.ManageRecentPosts, map[string]interface{}{ if err = serverutil.MinifyTemplate(gctemplates.ManageRecentPosts, map[string]interface{}{
"recentposts": recentposts, "recentposts": recentposts,
"allBoards": gcsql.AllBoards,
"boardid": boardid,
"limit": limit,
"webroot": config.GetSystemCriticalConfig().WebRoot, "webroot": config.GetSystemCriticalConfig().WebRoot,
}, manageRecentsBuffer, "text/html"); err != nil { }, manageRecentsBuffer, "text/html"); err != nil {
errEv.Err(err).Caller().Send() errEv.Err(err).Caller().Send()
@ -194,30 +211,6 @@ var actions = []Action{
return manageRecentsBuffer.String(), nil return manageRecentsBuffer.String(), nil
}, },
}, },
// {
// ID: "recentposts",
// Title: "Recent posts",
// Permissions: JanitorPerms,
// JSONoutput: OptionalJSON,
// Callback: func(writer http.ResponseWriter, request *http.Request, staff *gcsql.Staff, wantsJSON bool) (output interface{}, err error) {
// limit := gcutil.HackyStringToInt(request.FormValue("limit"))
// if limit == 0 {
// limit = 50
// }
// recentposts, err := gcsql.GetRecentPostsGlobal(limit, false) //only uses boardname, boardid, postid, parentid, message, ip and timestamp
// if wantsJSON || err != nil {
// return recentposts, err
// }
// manageRecentsBuffer := bytes.NewBufferString("")
// if err = serverutil.MinifyTemplate(gctemplates.ManageRecentPosts, map[string]interface{}{
// "recentposts": recentposts,
// "webroot": config.GetSystemCriticalConfig().WebRoot,
// }, manageRecentsBuffer, "text/html"); err != nil {
// errEv.Err(err).Caller().Send()
// return "", errors.New("Error executing ban management page template: " + err.Error())
// }
// return manageRecentsBuffer.String(), nil
// }},
/* { /* {
ID: "filebans", ID: "filebans",
Title: "File bans", Title: "File bans",

View file

@ -1,24 +1,31 @@
<!-- Limit by: <select id="limitby"> <form action="{{$.webroot}}manage" method="GET">
<option>25</option> <input type="hidden" name="action" value="recentposts">
<option>50</option> <label for="boardid">Board:</label>
<option>100</option> <select name="boardid" id="boardid">
<option>200</option> <option value="0">All boards</option>
</select><br /><br /> --> {{- range $b, $board := $.allBoards -}}
<option value="{{$board.ID}}" {{if eq $.boardid $board.ID}}selected{{end}}>/{{$board.Dir}}/ - {{$board.Title}}</option>
{{- end -}}
</select><br />
<label for="limit">Limit:</label>
<input type="number" name="limit" id="limit" value="20" min="1">
<input type="submit" />
</form><hr />
<table width="100%" border="1"> <table width="100%" border="1">
<colgroup><col width="5%"><col width="15%"><col width="60%"><col width="15%"></colgroup> <colgroup><col width="5%"><col width="15%"><col width="60%"><col width="15%"></colgroup>
<tr><th></th><th>Name</th><th>Message</th><th>Thumb</th></tr> <tr><th></th><th>Name</th><th>Message</th><th>Thumb</th></tr>
{{range $rp, $post := $.recentposts}} {{range $rp, $post := $.recentposts}}
<tr><td><a href="{{$post.BoardName}}/res/{{if eq $post.ParentID 0}}{{$post.ID}}{{else}}{{$post.ParentID}}{{end}}.html#{{$post.PostID}}" class="centered">Post</a></td> <tr><td><a href="{{$post.WebPath}}" class="centered">Post</a></td>
<td><b>Name: </b> {{- if and (eq $post.Name "") (eq $post.Tripcode "")}}<span class="postername">Anonymous</span>{{end}} <td><b>Name: </b> {{- if and (eq $post.Name "") (eq $post.Tripcode "")}}<span class="postername">Anonymous</span>{{end}}
{{- if ne $post.Name ""}}<span class="postername">{{$post.Name}}</span>{{end -}} {{- if ne $post.Name ""}}<span class="postername">{{$post.Name}}</span>{{end -}}
{{- if ne $post.Tripcode ""}}!<span class="tripcode">{{$post.Tripcode}}</span>{{end -}}<br /> {{- if ne $post.Tripcode ""}}!<span class="tripcode">{{$post.Tripcode}}</span>{{end -}}<br />
<b>IP: </b> {{$post.IP}}</td> <b>IP: </b> {{$post.IP}}<br />
<b>Board: </b>/{{$post.BoardDir}}/
</td>
<td>{{truncateMessage (stripHTML $post.Message) 300 16}}</td><td> <td>{{truncateMessage (stripHTML $post.Message) 300 16}}</td><td>
{{- if eq $post.Filename "deleted" -}} {{- if eq $post.Filename "deleted" -}}
<div class="file-deleted-box centered" style="text-align:center;">File removed</div> <div class="file-deleted-box centered" style="text-align:center;">File removed</div>
{{- else if ne $post.Filename "" -}} {{- else if ne $post.Filename "" -}}
{{- $thumbURL := stringAppend $.webroot $post.BoardName "/thumb/" (getThreadThumbnail $post.Filename) -}} <a href="{{$post.UploadPath}}" target="_blank" class="centered"><img src="{{$post.ThumbnailPath}}"></a>
{{- $uploadURL := stringAppend $.webroot $post.BoardName "/src/" $post.Filename -}}
<a href="{{$uploadURL}}" target="_blank" class="centered"><img src="{{$thumbURL}}"></a>
{{end}}</td></tr>{{end}} {{end}}</td></tr>{{end}}
</table> </table>