From 2e00b11434e7ec3469485a7270dea37b5afbc0a0 Mon Sep 17 00:00:00 2001 From: comraderat Date: Sat, 23 May 2020 19:40:29 +0200 Subject: [PATCH] Fix some errors --- initdb_master.sql | 2 +- initdb_mysql.sql | 2 +- initdb_postgres.sql | 2 +- initdb_sqlite3.sql | 2 +- pkg/building/boards.go | 4 ++-- pkg/building/building.go | 2 +- pkg/gcsql/postsretrievalqueries.go | 17 +++++++++-------- pkg/gcsql/queries.go | 27 +++++++++++++-------------- pkg/posting/post.go | 2 +- 9 files changed, 30 insertions(+), 30 deletions(-) diff --git a/initdb_master.sql b/initdb_master.sql index 7ab8e50b..9b051d86 100644 --- a/initdb_master.sql +++ b/initdb_master.sql @@ -250,7 +250,7 @@ CREATE TABLE DBPREFIXfile_ban( staff_id {fk to serial} NOT NULL, staff_note VARCHAR(255) NOT NULL, issued_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - checksum INT NOT NULL, + checksum TEXT NOT NULL, FOREIGN KEY(board_id) REFERENCES DBPREFIXboards(id), FOREIGN KEY(staff_id) REFERENCES DBPREFIXstaff(id) ); diff --git a/initdb_mysql.sql b/initdb_mysql.sql index 1fd66d8a..74647ea5 100644 --- a/initdb_mysql.sql +++ b/initdb_mysql.sql @@ -250,7 +250,7 @@ CREATE TABLE DBPREFIXfile_ban( staff_id BIGINT NOT NULL, staff_note VARCHAR(255) NOT NULL, issued_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - checksum INT NOT NULL, + checksum TEXT NOT NULL, FOREIGN KEY(board_id) REFERENCES DBPREFIXboards(id), FOREIGN KEY(staff_id) REFERENCES DBPREFIXstaff(id) ); diff --git a/initdb_postgres.sql b/initdb_postgres.sql index b8d11683..e8f2dba5 100644 --- a/initdb_postgres.sql +++ b/initdb_postgres.sql @@ -250,7 +250,7 @@ CREATE TABLE DBPREFIXfile_ban( staff_id BIGINT NOT NULL, staff_note VARCHAR(255) NOT NULL, issued_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - checksum INT NOT NULL, + checksum TEXT NOT NULL, FOREIGN KEY(board_id) REFERENCES DBPREFIXboards(id), FOREIGN KEY(staff_id) REFERENCES DBPREFIXstaff(id) ); diff --git a/initdb_sqlite3.sql b/initdb_sqlite3.sql index f3fff311..650eaa22 100644 --- a/initdb_sqlite3.sql +++ b/initdb_sqlite3.sql @@ -250,7 +250,7 @@ CREATE TABLE DBPREFIXfile_ban( staff_id INTEGER NOT NULL, staff_note VARCHAR(255) NOT NULL, issued_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - checksum INT NOT NULL, + checksum TEXT NOT NULL, FOREIGN KEY(board_id) REFERENCES DBPREFIXboards(id), FOREIGN KEY(staff_id) REFERENCES DBPREFIXstaff(id) ); diff --git a/pkg/building/boards.go b/pkg/building/boards.go index b03cc03a..ffe004b9 100644 --- a/pkg/building/boards.go +++ b/pkg/building/boards.go @@ -50,7 +50,7 @@ func BuildBoardPages(board *gcsql.Board) (html string) { var postsInThread []gcsql.Post var replyCount, err = gcsql.GetReplyCount(op.ID) - if err == nil { + if err != nil { return html + gclog.Printf(gclog.LErrorLog, "Error getting replies to /%s/%d: %s", board.Dir, op.ID, err.Error()) + "
" @@ -58,7 +58,7 @@ func BuildBoardPages(board *gcsql.Board) (html string) { thread.NumReplies = replyCount fileCount, err := gcsql.GetReplyFileCount(op.ID) - if err == nil { + if err != nil { return html + gclog.Printf(gclog.LErrorLog, "Error getting file count to /%s/%d: %s", board.Dir, op.ID, err.Error()) + "
" diff --git a/pkg/building/building.go b/pkg/building/building.go index fb494e35..1ec21e5e 100644 --- a/pkg/building/building.go +++ b/pkg/building/building.go @@ -28,7 +28,7 @@ func BuildFrontPage() string { var recentPostsArr []gcsql.RecentPost recentPostsArr, err = gcsql.GetRecentPostsGlobal(config.Config.MaxRecentPosts, !config.Config.RecentPostsWithNoFile) - if err == nil { + if err != nil { return gclog.Print(gclog.LErrorLog, "Failed loading recent posts: "+err.Error()) + "
" } diff --git a/pkg/gcsql/postsretrievalqueries.go b/pkg/gcsql/postsretrievalqueries.go index 8995a447..f1842e97 100644 --- a/pkg/gcsql/postsretrievalqueries.go +++ b/pkg/gcsql/postsretrievalqueries.go @@ -53,10 +53,11 @@ FROM posts.message_raw, posts.password, posts.created_on, - threads.anchored, - threads.last_bump, - threads.stickied, - threads.locked + posts.is_top_post, + threads.anchored, + threads.last_bump, + threads.stickied, + threads.locked FROM DBPREFIXposts AS posts JOIN DBPREFIXthreads AS threads @@ -265,22 +266,22 @@ func getRecentPostsInternal(amount int, onlyWithFile bool, boardID int, onSpecif var err error if onlyWithFile && onSpecificBoard { - recentQueryStr += `\nWHERE singlefiles.filename IS NOT NULL AND recentposts.boardid = ? + recentQueryStr += "\n" + `WHERE singlefiles.filename IS NOT NULL AND recentposts.boardid = ? ORDER BY recentposts.created_on DESC LIMIT ?` rows, err = QuerySQL(recentQueryStr, boardID, amount) } if onlyWithFile && !onSpecificBoard { - recentQueryStr += `\nWHERE singlefiles.filename IS NOT NULL + recentQueryStr += "\n" + `WHERE singlefiles.filename IS NOT NULL ORDER BY recentposts.created_on DESC LIMIT ?` rows, err = QuerySQL(recentQueryStr, amount) } if !onlyWithFile && onSpecificBoard { - recentQueryStr += `\nWHERE recentposts.boardid = ? + recentQueryStr += "\n" + `WHERE recentposts.boardid = ? ORDER BY recentposts.created_on DESC LIMIT ?` rows, err = QuerySQL(recentQueryStr, boardID, amount) } if !onlyWithFile && !onSpecificBoard { - recentQueryStr += `\nORDER BY recentposts.created_on DESC LIMIT ?` + recentQueryStr += "\nORDER BY recentposts.created_on DESC LIMIT ?" rows, err = QuerySQL(recentQueryStr, amount) } diff --git a/pkg/gcsql/queries.go b/pkg/gcsql/queries.go index 806e0698..f52cf84d 100644 --- a/pkg/gcsql/queries.go +++ b/pkg/gcsql/queries.go @@ -520,7 +520,7 @@ func CheckBan(ip string, name string, filename string, checksum string) (*BanInf 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 DBPREFIXusername_ban WHERE username = ?` + 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)) return ban, err @@ -571,7 +571,7 @@ func SinceLastPost(postID int) (int, error) { // The code should be changed to reflect the new database design func InsertPost(post *Post, bump bool) error { const sql = `INSERT INTO DBPREFIXposts (id, thread_id, name, tripcode, is_role_signature, email, subject, ip, is_top_post, message, message_raw, banned_message, password) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` isNewThread := post.ParentID == 0 var threadID int var err error @@ -592,9 +592,7 @@ func InsertPost(post *Post, bump bool) error { if err != nil { return err } - err = QueryRowSQL(sql, - interfaceSlice(nextFreeID, threadID, post.Name, post.Tripcode, false, post.Email, post.Subject, post.IP, isNewThread, post.MessageHTML, post.MessageText, "", post.Password), - interfaceSlice()) + _, err = ExecSQL(sql, nextFreeID, threadID, post.Name, post.Tripcode, false, post.Email, post.Subject, post.IP, isNewThread, post.MessageHTML, post.MessageText, "", post.Password) isPrimaryKeyError, err = errFilterDuplicatePrimaryKey(err) if err != nil { @@ -627,7 +625,7 @@ func createThread(boardID int, locked bool, stickied bool, anchored bool, cyclic if err != nil { return 0, err } - err = QueryRowSQL(sql, interfaceSlice(boardID, locked, stickied, anchored, cyclical), interfaceSlice()) + _, err = ExecSQL(sql, boardID, locked, stickied, anchored, cyclical) isPrimaryKeyError, err = errFilterDuplicatePrimaryKey(err) if err != nil { @@ -866,13 +864,14 @@ func CreateDefaultBoardIfNoneExist() error { if err != nil { return err } - return CreateBoard( - &Board{ - Dir: "test", - Title: "Testing board", - Subtitle: "Board for testing", - Description: "Board for testing", - Section: defaultSectionID}) + var board = Board{ + Dir: "test", + Title: "Testing board", + Subtitle: "Board for testing", + Description: "Board for testing", + Section: defaultSectionID} + board.SetDefaults() + return CreateBoard(&board) } //CreateDefaultAdminIfNoStaff creates a new default admin account if no accounts exist @@ -987,7 +986,7 @@ func GetDatabaseVersion() (int, error) { } func getNextFreeID(tableName string) (ID int, err error) { - var sql = `SELECT MAX(id) + 1 FROM ` + tableName + var sql = `SELECT COALESCE(MAX(id), 0) + 1 FROM ` + tableName err = QueryRowSQL(sql, interfaceSlice(), interfaceSlice(&ID)) return ID, err } diff --git a/pkg/posting/post.go b/pkg/posting/post.go index 413fe3a3..9651527b 100644 --- a/pkg/posting/post.go +++ b/pkg/posting/post.go @@ -344,7 +344,7 @@ func MakePost(writer http.ResponseWriter, request *http.Request) { boards, _ := gcsql.GetAllBoards() postBoard, _ := gcsql.GetBoardFromID(post.BoardID) - if banStatus.IsBanned(postBoard.Dir) { + if banStatus != nil && banStatus.IsBanned(postBoard.Dir) { var banpageBuffer bytes.Buffer if err = gcutil.MinifyTemplate(gctemplates.Banpage, map[string]interface{}{