2016-12-03 13:45:08 +00:00
|
|
|
package main
|
2017-03-27 07:09:28 +00:00
|
|
|
|
2017-06-14 09:55:47 +00:00
|
|
|
import "log"
|
2017-03-14 10:57:40 +00:00
|
|
|
//import "fmt"
|
2017-03-27 07:09:28 +00:00
|
|
|
import "sync"
|
2017-03-15 08:34:14 +00:00
|
|
|
import "strconv"
|
2017-01-26 13:37:50 +00:00
|
|
|
import "database/sql"
|
|
|
|
import _ "github.com/go-sql-driver/mysql"
|
2016-12-03 13:45:08 +00:00
|
|
|
|
2017-02-04 15:49:24 +00:00
|
|
|
type ForumAdmin struct
|
|
|
|
{
|
|
|
|
ID int
|
|
|
|
Name string
|
2017-06-05 11:57:27 +00:00
|
|
|
Desc string
|
2017-02-04 15:49:24 +00:00
|
|
|
Active bool
|
|
|
|
Preset string
|
|
|
|
TopicCount int
|
|
|
|
PresetLang string
|
|
|
|
}
|
|
|
|
|
2016-12-03 13:45:08 +00:00
|
|
|
type Forum struct
|
|
|
|
{
|
|
|
|
ID int
|
|
|
|
Name string
|
2017-06-05 11:57:27 +00:00
|
|
|
Desc string
|
2016-12-06 10:26:48 +00:00
|
|
|
Active bool
|
2017-02-04 15:49:24 +00:00
|
|
|
Preset string
|
2017-01-26 13:37:50 +00:00
|
|
|
TopicCount int
|
2016-12-03 13:45:08 +00:00
|
|
|
LastTopic string
|
|
|
|
LastTopicID int
|
|
|
|
LastReplyer string
|
|
|
|
LastReplyerID int
|
|
|
|
LastTopicTime string
|
|
|
|
}
|
2016-12-04 10:44:28 +00:00
|
|
|
|
|
|
|
type ForumSimple struct
|
|
|
|
{
|
|
|
|
ID int
|
|
|
|
Name string
|
2016-12-07 09:34:09 +00:00
|
|
|
Active bool
|
2017-02-04 15:49:24 +00:00
|
|
|
Preset string
|
2017-01-01 15:45:43 +00:00
|
|
|
}
|
2017-01-26 13:37:50 +00:00
|
|
|
|
2017-06-14 09:55:47 +00:00
|
|
|
func LoadForums() error {
|
|
|
|
//if debug {
|
|
|
|
log.Print("Adding the uncategorised forum")
|
|
|
|
//}
|
|
|
|
forums = append(forums, Forum{0,"Uncategorised","",uncategorised_forum_visible,"all",0,"",0,"",0,""})
|
|
|
|
|
|
|
|
rows, err := get_forums_stmt.Query()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var i int = 1
|
|
|
|
for ;rows.Next();i++ {
|
|
|
|
forum := Forum{ID:0,Name:"",Active:true,Preset:"all"}
|
|
|
|
err = rows.Scan(&forum.ID, &forum.Name, &forum.Desc, &forum.Active, &forum.Preset, &forum.TopicCount, &forum.LastTopic, &forum.LastTopicID, &forum.LastReplyer, &forum.LastReplyerID, &forum.LastTopicTime)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ugh, you really shouldn't physically delete these items, it makes a big mess of things
|
|
|
|
if forum.ID != i {
|
|
|
|
log.Print("Stop physically deleting forums. You are messing up the IDs. Use the Forum Manager or delete_forum() instead x.x")
|
|
|
|
fill_forum_id_gap(i, forum.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if forum.Name == "" {
|
|
|
|
if debug {
|
|
|
|
log.Print("Adding a placeholder forum")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Print("Adding the " + forum.Name + " forum")
|
|
|
|
}
|
|
|
|
forums = append(forums,forum)
|
|
|
|
}
|
|
|
|
err = rows.Err()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
forumCapCount = i
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-27 07:09:28 +00:00
|
|
|
var forum_update_mutex sync.Mutex
|
2017-06-15 11:40:35 +00:00
|
|
|
var forum_create_mutex sync.Mutex
|
2017-06-05 11:57:27 +00:00
|
|
|
func create_forum(forum_name string, forum_desc string, active bool, preset string) (int, error) {
|
2017-01-26 13:37:50 +00:00
|
|
|
var fid int
|
|
|
|
err := forum_entry_exists_stmt.QueryRow().Scan(&fid)
|
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if err != sql.ErrNoRows {
|
2017-03-27 07:09:28 +00:00
|
|
|
forum_update_mutex.Lock()
|
2017-06-05 11:57:27 +00:00
|
|
|
_, err = update_forum_stmt.Exec(forum_name, forum_desc, active, preset, fid)
|
2017-01-26 13:37:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return fid, err
|
|
|
|
}
|
|
|
|
forums[fid].Name = forum_name
|
2017-06-05 11:57:27 +00:00
|
|
|
forums[fid].Desc = forum_desc
|
2017-01-26 13:37:50 +00:00
|
|
|
forums[fid].Active = active
|
2017-02-04 15:49:24 +00:00
|
|
|
forums[fid].Preset = preset
|
2017-03-27 07:09:28 +00:00
|
|
|
forum_update_mutex.Unlock()
|
2017-01-26 13:37:50 +00:00
|
|
|
return fid, nil
|
|
|
|
}
|
2017-06-05 11:57:27 +00:00
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
forum_create_mutex.Lock()
|
2017-06-05 11:57:27 +00:00
|
|
|
res, err := create_forum_stmt.Exec(forum_name, forum_desc, active, preset)
|
2017-01-26 13:37:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-06-05 11:57:27 +00:00
|
|
|
|
2017-01-26 13:37:50 +00:00
|
|
|
fid64, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-02-04 06:19:55 +00:00
|
|
|
fid = int(fid64)
|
2017-06-05 11:57:27 +00:00
|
|
|
|
|
|
|
forums = append(forums, Forum{fid,forum_name,forum_desc,active,preset,0,"",0,"",0,""})
|
2017-06-15 11:40:35 +00:00
|
|
|
forum_create_mutex.Unlock()
|
2017-01-26 13:37:50 +00:00
|
|
|
return fid, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func delete_forum(fid int) error {
|
2017-06-15 11:40:35 +00:00
|
|
|
forum_update_mutex.Lock()
|
2017-01-26 13:37:50 +00:00
|
|
|
_, err := delete_forum_stmt.Exec(fid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
forums[fid].Name = ""
|
2017-06-15 11:40:35 +00:00
|
|
|
forum_update_mutex.Unlock()
|
2017-01-26 13:37:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-03-14 10:57:40 +00:00
|
|
|
|
2017-03-15 08:34:14 +00:00
|
|
|
func get_forum(fid int) (forum *Forum, res bool) {
|
|
|
|
if !((fid <= forumCapCount) && (fid >= 0) && forums[fid].Name!="") {
|
|
|
|
return forum, false
|
|
|
|
}
|
|
|
|
return &forums[fid], true
|
|
|
|
}
|
|
|
|
|
|
|
|
func get_forum_copy(fid int) (forum Forum, res bool) {
|
2017-06-05 11:57:27 +00:00
|
|
|
if !((fid <= forumCapCount) && (fid >= 0) && forums[fid].Name != "") {
|
2017-03-15 08:34:14 +00:00
|
|
|
return forum, false
|
|
|
|
}
|
|
|
|
return forums[fid], true
|
|
|
|
}
|
|
|
|
|
2017-03-14 10:57:40 +00:00
|
|
|
func forum_exists(fid int) bool {
|
2017-06-05 11:57:27 +00:00
|
|
|
return (fid <= forumCapCount) && (fid >= 0) && forums[fid].Name != ""
|
2017-03-14 10:57:40 +00:00
|
|
|
}
|
2017-03-15 08:34:14 +00:00
|
|
|
|
|
|
|
func build_forum_url(fid int) string {
|
|
|
|
return "/forum/" + strconv.Itoa(fid)
|
|
|
|
}
|