1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-20 09:26:23 -07:00

Changes formatted message to template.HTML

This commit is contained in:
comraderat 2020-05-24 18:56:24 +02:00
parent 604c776dbf
commit 193913bcb4
6 changed files with 70 additions and 58 deletions

View file

@ -3,6 +3,7 @@ package gcsql
import (
"database/sql"
"errors"
"html/template"
"strconv"
)
@ -91,13 +92,15 @@ func getPostsExcecution(sql string, arguments ...interface{}) ([]Post, error) {
var posts []Post
for rows.Next() {
post := new(Post)
var messageHTML string
err = rows.Scan(&post.ID, &post.ParentID, &post.BoardID, &post.Name, &post.Tripcode, &post.Email,
&post.Subject, &post.MessageHTML, &post.MessageText, &post.Password, &post.Filename,
&post.Subject, &messageHTML, &post.MessageText, &post.Password, &post.Filename,
&post.FilenameOriginal, &post.FileChecksum, &post.Filesize, &post.ImageW, &post.ImageH,
&post.ThumbW, &post.ThumbH, &post.IP, &post.Timestamp, &post.Autosage, &post.Bumped, &post.Stickied, &post.Locked)
if err != nil {
return nil, err
}
post.MessageHTML = template.HTML(messageHTML)
post.FileExt = "placeholder"
posts = append(posts, *post)
}
@ -293,12 +296,14 @@ func getRecentPostsInternal(amount int, onlyWithFile bool, boardID int, onSpecif
for rows.Next() {
recentPost := new(RecentPost)
var formattedHTML template.HTML
if err = rows.Scan(
&recentPost.PostID, &recentPost.ParentID, &recentPost.BoardName, &recentPost.BoardID,
&recentPost.Name, &recentPost.Tripcode, &recentPost.Message, &recentPost.Filename, &recentPost.ThumbW, &recentPost.ThumbH,
&recentPost.Name, &recentPost.Tripcode, &formattedHTML, &recentPost.Filename, &recentPost.ThumbW, &recentPost.ThumbH,
); err != nil {
return nil, err
}
recentPost.Message = formattedHTML
recentPostsArr = append(recentPostsArr, *recentPost)
}

View file

@ -3,6 +3,7 @@ package gcsql
import (
"database/sql"
"errors"
"html/template"
"os"
"path"
"strings"
@ -23,10 +24,12 @@ func GetAllNondeletedMessageRaw() ([]MessagePostContainer, error) {
var messages []MessagePostContainer
for rows.Next() {
var message MessagePostContainer
err = rows.Scan(message.ID, message.Message, message.MessageRaw)
var formattedHTML template.HTML
err = rows.Scan(&message.ID, &formattedHTML, &message.MessageRaw)
if err != nil {
return nil, err
}
message.Message = template.HTML(formattedHTML)
messages = append(messages, message)
}
return messages, nil
@ -43,7 +46,7 @@ func SetFormattedInDatabase(messages []MessagePostContainer) error {
return err
}
for _, message := range messages {
_, err = stmt.Exec(message.Message, message.ID)
_, err = stmt.Exec(string(message.Message), message.ID)
if err != nil {
return err
}
@ -522,7 +525,9 @@ func checkIPBan(ip string) (*IPBan, error) {
const sql = `SELECT id, staff_id, board_id, banned_for_post_id, copy_post_text, is_thread_ban, is_active, ip, issued_at, appeal_at, expires_at, permanent, staff_note, message, can_appeal
FROM DBPREFIXip_ban WHERE ip = ?`
var ban = new(IPBan)
err := QueryRowSQL(sql, interfaceSlice(ip), interfaceSlice(&ban.ID, &ban.StaffID, &ban.BoardID, &ban.BannedForPostID, &ban.CopyPostText, &ban.IsThreadBan, &ban.IsActive, &ban.IP, &ban.IssuedAt, &ban.AppealAt, &ban.ExpiresAt, &ban.Permanent, &ban.StaffNote, &ban.Message, &ban.CanAppeal))
var formattedHTMLcopyposttest template.HTML
err := QueryRowSQL(sql, interfaceSlice(ip), interfaceSlice(&ban.ID, &ban.StaffID, &ban.BoardID, &ban.BannedForPostID, &formattedHTMLcopyposttest, &ban.IsThreadBan, &ban.IsActive, &ban.IP, &ban.IssuedAt, &ban.AppealAt, &ban.ExpiresAt, &ban.Permanent, &ban.StaffNote, &ban.Message, &ban.CanAppeal))
ban.CopyPostText = formattedHTMLcopyposttest
return ban, err
}
@ -592,7 +597,7 @@ func InsertPost(post *Post, bump bool) error {
if err != nil {
return err
}
_, err = ExecSQL(sql, nextFreeID, threadID, post.Name, post.Tripcode, false, post.Email, post.Subject, post.IP, isNewThread, post.MessageHTML, post.MessageText, "", post.Password)
_, err = ExecSQL(sql, nextFreeID, threadID, post.Name, post.Tripcode, false, post.Email, post.Subject, post.IP, isNewThread, string(post.MessageHTML), post.MessageText, "", post.Password)
isPrimaryKeyError, err = errFilterDuplicatePrimaryKey(err)
if err != nil {
@ -743,9 +748,9 @@ func GetPostPassword(postID int) (password string, err error) {
//UpdatePost updates a post with new information
// Deprecated: This method was created to support old functionality during the database refactor of april 2020
// The code should be changed to reflect the new database design
func UpdatePost(postID int, email string, subject string, message string, messageRaw string) error {
func UpdatePost(postID int, email string, subject string, message template.HTML, messageRaw string) error {
const sql = `UPDATE DBPREFIXposts SET email = ?, subject = ?, message = ?, message_raw = ? WHERE id = ?`
_, err := ExecSQL(sql, email, subject, message, messageRaw)
_, err := ExecSQL(sql, email, subject, string(message), messageRaw)
return err
}

View file

@ -3,6 +3,7 @@ package gcsql
import (
"fmt"
"html"
"html/template"
"path"
"strconv"
"strings"
@ -218,34 +219,34 @@ type BoardSection struct {
// Deprecated. Struct was made for use with old database, deprecated since refactor of april 2020.
// Please refactor all code that uses this struct to use a struct that alligns with the new database structure and functions.
type Post struct {
ID int `json:"no"`
ParentID int `json:"resto"`
CurrentPage int `json:"-"`
BoardID int `json:"-"`
Name string `json:"name"`
Tripcode string `json:"trip"`
Email string `json:"email"`
Subject string `json:"sub"`
MessageHTML string `json:"com"`
MessageText string `json:"-"`
Password string `json:"-"`
Filename string `json:"tim"`
FilenameOriginal string `json:"filename"`
FileChecksum string `json:"md5"`
FileExt string `json:"extension"`
Filesize int `json:"fsize"`
ImageW int `json:"w"`
ImageH int `json:"h"`
ThumbW int `json:"tn_w"`
ThumbH int `json:"tn_h"`
IP string `json:"-"`
Capcode string `json:"capcode"`
Timestamp time.Time `json:"time"`
Autosage bool `json:"-"`
Bumped time.Time `json:"last_modified"`
Stickied bool `json:"-"`
Locked bool `json:"-"`
Reviewed bool `json:"-"`
ID int `json:"no"`
ParentID int `json:"resto"`
CurrentPage int `json:"-"`
BoardID int `json:"-"`
Name string `json:"name"`
Tripcode string `json:"trip"`
Email string `json:"email"`
Subject string `json:"sub"`
MessageHTML template.HTML `json:"com"`
MessageText string `json:"-"`
Password string `json:"-"`
Filename string `json:"tim"`
FilenameOriginal string `json:"filename"`
FileChecksum string `json:"md5"`
FileExt string `json:"extension"`
Filesize int `json:"fsize"`
ImageW int `json:"w"`
ImageH int `json:"h"`
ThumbW int `json:"tn_w"`
ThumbH int `json:"tn_h"`
IP string `json:"-"`
Capcode string `json:"capcode"`
Timestamp time.Time `json:"time"`
Autosage bool `json:"-"`
Bumped time.Time `json:"last_modified"`
Stickied bool `json:"-"`
Locked bool `json:"-"`
Reviewed bool `json:"-"`
}
func (p *Post) GetURL(includeDomain bool) string {
@ -314,7 +315,7 @@ type BoardCooldowns struct {
type MessagePostContainer struct {
ID int
MessageRaw string
Message string
Message template.HTML
}
// Deprecated. Struct was made for use with old database, deprecated since refactor of april 2020.
@ -326,7 +327,7 @@ type RecentPost struct {
ParentID int
Name string
Tripcode string
Message string
Message template.HTML
Filename string
ThumbW int
ThumbH int
@ -408,19 +409,19 @@ type WordFilter struct {
//IPBan contains the information association with a specific ip ban
type IPBan struct {
ID int `json:id`
BoardID *int `json:"board"`
StaffID int `json:"staff_id"`
BannedForPostID *int `json:"banned_for_post_id"`
CopyPostText string `json:"copy_post_text"`
IsThreadBan bool `json:"is_thread_ban"`
IsActive bool `json:"is_active"`
IP string `json:"ip"`
IssuedAt time.Time `json:"issued_at"`
AppealAt time.Time `json:"appeal_at"`
ExpiresAt time.Time `json:"expires_at"`
Permanent bool `json:"permanent"`
StaffNote string `json:"staff_note"`
Message string `json:"message"`
CanAppeal bool `json:"can_appeal"`
ID int `json:id`
BoardID *int `json:"board"`
StaffID int `json:"staff_id"`
BannedForPostID *int `json:"banned_for_post_id"`
CopyPostText template.HTML `json:"copy_post_text"`
IsThreadBan bool `json:"is_thread_ban"`
IsActive bool `json:"is_active"`
IP string `json:"ip"`
IssuedAt time.Time `json:"issued_at"`
AppealAt time.Time `json:"appeal_at"`
ExpiresAt time.Time `json:"expires_at"`
Permanent bool `json:"permanent"`
StaffNote string `json:"staff_note"`
Message string `json:"message"`
CanAppeal bool `json:"can_appeal"`
}

View file

@ -109,8 +109,8 @@ var funcMap = template.FuncMap{
}
return msg
},
"stripHTML": func(htmlStr string) string {
dom := x_html.NewTokenizer(strings.NewReader(htmlStr))
"stripHTML": func(htmlStr template.HTML) string {
dom := x_html.NewTokenizer(strings.NewReader(string(htmlStr)))
for tokenType := dom.Next(); tokenType != x_html.ErrorToken; {
if tokenType != x_html.TextToken {
tokenType = dom.Next()

View file

@ -717,7 +717,7 @@ var manageFunctions = map[string]ManageFunction{
html += fmt.Sprintf(
`<tr><td><b>Post:</b> <a href="%s">%s/%d</a><br /><b>IP:</b> %s</td><td>%s</td><td>%s</td></tr>`,
path.Join(config.Config.SiteWebfolder, recentpost.BoardName, "/res/", strconv.Itoa(recentpost.ParentID)+".html#"+strconv.Itoa(recentpost.PostID)),
recentpost.BoardName, recentpost.PostID, recentpost.IP, recentpost.Message,
recentpost.BoardName, recentpost.PostID, recentpost.IP, string(recentpost.Message),
recentpost.Timestamp.Format("01/02/06, 15:04"),
)
}

View file

@ -1,6 +1,7 @@
package posting
import (
"html/template"
"strconv"
"strings"
"time"
@ -50,7 +51,7 @@ func (mf *MessageFormatter) Compile(msg string) string {
return mf.bbCompiler.Compile(msg)
}
func FormatMessage(message string) string {
func FormatMessage(message string) template.HTML {
message = msgfmtr.Compile(message)
// prepare each line to be formatted
postLines := strings.Split(message, "<br>")
@ -94,5 +95,5 @@ func FormatMessage(message string) string {
}
postLines[i] = line
}
return strings.Join(postLines, "<br />")
return template.HTML(strings.Join(postLines, "<br />"))
}