1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-06 21:46:24 -07:00

Add ability to update thread attributes from manage page

This commit is contained in:
Eggbertx 2023-02-02 11:12:39 -08:00
parent 0f0c9362eb
commit 34cab06311
5 changed files with 174 additions and 70 deletions

View file

@ -86,21 +86,28 @@ func GetTopPostInThread(postID int) (int, error) {
return id, err
}
func GetTopPostIDsInThreadIDs(threads ...interface{}) ([]int, error) {
// GetTopPostIDsInThreadIDs takes a variable number of threads and returns a map[threadID]topPostID
func GetTopPostIDsInThreadIDs(threads ...interface{}) (map[interface{}]int, error) {
ids := make(map[interface{}]int)
if threads == nil {
return ids, nil
}
params := createArrayPlaceholder(threads)
query := `SELECT id FROM DBPREFIXposts WHERE thread_id in ` + params
query := `SELECT id FROM DBPREFIXposts WHERE thread_id in ` + params + " AND is_top_post"
rows, err := QuerySQL(query, threads...)
if err != nil {
return nil, err
}
defer rows.Close()
var ids []int
var i int
for rows.Next() {
var id int
if err = rows.Scan(&id); err != nil {
return nil, err
}
ids = append(ids, id)
ids[threads[i]] = id
i++
}
return ids, nil
}

View file

@ -3,6 +3,7 @@ package gcsql
import (
"database/sql"
"errors"
"fmt"
"strconv"
)
@ -41,7 +42,7 @@ func createThread(tx *sql.Tx, boardID int, locked bool, stickied bool, anchored
return threadID, err
}
// GetThread returns a a thread object from the database
// GetThread returns a a thread object from the database, given its ID
func GetThread(threadID int) (*Thread, error) {
const query = selectThreadsBaseSQL + `WHERE id = ?`
thread := new(Thread)
@ -52,6 +53,17 @@ func GetThread(threadID int) (*Thread, error) {
return thread, err
}
// GetPostThread returns a thread object from the database, given the ID of a post in the thread
func GetPostThread(opID int) (*Thread, error) {
const query = selectThreadsBaseSQL + `WHERE id = (SELECT thread_id FROM DBPREFIXposts WHERE id = ? LIMIT 1)`
thread := new(Thread)
err := QueryRowSQL(query, interfaceSlice(opID), interfaceSlice(
&thread.ID, &thread.BoardID, &thread.Locked, &thread.Stickied, &thread.Anchored, &thread.Cyclical,
&thread.LastBump, &thread.DeletedAt, &thread.IsDeleted,
))
return thread, err
}
// GetTopPostThreadID gets the thread ID from the database, given the post ID of a top post
func GetTopPostThreadID(opID int) (int, error) {
const query = `SELECT thread_id FROM DBPREFIXposts WHERE id = ? and is_top_post`
@ -195,6 +207,23 @@ func (t *Thread) GetUploads() ([]Upload, error) {
return uploads, nil
}
// UpdateThreadAttribute updates the given attribute (valid attribute values are "locked", "stickied, "anchored",
// or "cyclical") for the thread with the given top post ID
func UpdateThreadAttribute(opID int, attribute string, value bool) error {
updateSQL := "UPDATE DBPREFIXthreads SET "
if attribute == "locked" || attribute == "stickied" || attribute == "anchored" || attribute == "cyclical" {
updateSQL += attribute + " = ? WHERE id = ?"
} else {
return fmt.Errorf("invalid thread attribute %q", attribute)
}
threadID, err := GetTopPostThreadID(opID)
if err != nil {
return err
}
_, err = ExecSQL(updateSQL, value, threadID)
return err
}
// deleteThread updates the thread and sets it as deleted, as well as the posts where thread_id = threadID
func deleteThread(threadID int) error {
const deletePostsSQL = `UPDATE DBPREFIXposts SET is_deleted = TRUE, deleted_at = CURRENT_TIMESTAMP WHERE thread_id = ?`