From df5f70ee6b8cd5057c95c4855410d38fa8038c4a Mon Sep 17 00:00:00 2001 From: Azareal Date: Sun, 5 Feb 2017 14:41:53 +0000 Subject: [PATCH] You can now post in any forum you want via the Board selector. Added a lock indicator for forums you don't have permission to post in. Added a New Topic option in forums you have permission to post in. General is now the default forum for topics. /topics/ no longer shows topics for forums you aren't able to access. --- data.sql | 2 +- main.go | 1 + mysql.go | 2 +- pages.go | 10 ++++++ routes.go | 68 ++++++++++++++++++++++++++++++------- template_forum.go | 44 ++++++++++++++++-------- template_list.go | 34 ++++++++++++------- template_profile.go | 2 +- templates/create-topic.html | 6 ++++ templates/forum.html | 5 +-- 10 files changed, 130 insertions(+), 44 deletions(-) diff --git a/data.sql b/data.sql index e03870cc..2eb810ad 100644 --- a/data.sql +++ b/data.sql @@ -74,7 +74,7 @@ CREATE TABLE `topics`( `createdBy` int not null, `is_closed` tinyint DEFAULT 0 not null, `sticky` tinyint DEFAULT 0 not null, - `parentID` int DEFAULT 1 not null, + `parentID` int DEFAULT 2 not null, `ipaddress` varchar(200) DEFAULT '0.0.0.0.0' not null, `postCount` int DEFAULT 1 not null, `data` varchar(200) DEFAULT '' not null, diff --git a/main.go b/main.go index 968972f2..51afc0c0 100644 --- a/main.go +++ b/main.go @@ -42,6 +42,7 @@ var template_topics_handle func(TopicsPage,io.Writer) = nil var template_forum_handle func(ForumPage,io.Writer) = nil var template_forums_handle func(ForumsPage,io.Writer) = nil var template_profile_handle func(ProfilePage,io.Writer) = nil +var template_create_topic_handle func(CreateTopicPage,io.Writer) = nil func compile_templates() { var c CTemplateSet diff --git a/mysql.go b/mysql.go index 75201444..0edb2ead 100644 --- a/mysql.go +++ b/mysql.go @@ -130,7 +130,7 @@ func init_database(err error) { } log.Print("Preparing create_topic statement.") - create_topic_stmt, err = db.Prepare("insert into topics(title,content,parsed_content,createdAt,ipaddress,createdBy) VALUES(?,?,?,NOW(),?,?)") + create_topic_stmt, err = db.Prepare("insert into topics(parentID,title,content,parsed_content,createdAt,ipaddress,createdBy) VALUES(?,?,?,?,NOW(),?,?)") if err != nil { log.Fatal(err) } diff --git a/pages.go b/pages.go index 4ced1fec..579e77a3 100644 --- a/pages.go +++ b/pages.go @@ -63,6 +63,16 @@ type ProfilePage struct ExtData interface{} } +type CreateTopicPage struct +{ + Title string + CurrentUser User + NoticeList []string + ItemList []Forum + FID int + ExtData interface{} +} + type PageSimple struct { Title string diff --git a/routes.go b/routes.go index 472eafb1..2074481e 100644 --- a/routes.go +++ b/routes.go @@ -74,7 +74,6 @@ func route_custom_page(w http.ResponseWriter, r *http.Request){ if !ok { return } - name := r.URL.Path[len("/pages/"):] if templates.Lookup("page_" + name) == nil { NotFound(w,r,user) @@ -93,8 +92,17 @@ func route_topics(w http.ResponseWriter, r *http.Request){ return } + var fidList []string + group := groups[user.Group] + for _, fid := range group.CanSee { + if forums[fid].Name != "" { + fidList = append(fidList,strconv.Itoa(fid)) + } + } + var topicList []TopicUser - rows, err := get_topic_list_stmt.Query() + rows, err := db.Query("select topics.tid, topics.title, topics.content, topics.createdBy, topics.is_closed, topics.sticky, topics.createdAt, topics.parentID, users.name, users.avatar from topics left join users ON topics.createdBy = users.uid where parentID in("+strings.Join(fidList,",")+") order by topics.sticky DESC, topics.lastReplyAt DESC, topics.createdBy DESC") + //rows, err := get_topic_list_stmt.Query() if err != nil { InternalError(err,w,r,user) return @@ -530,9 +538,9 @@ func route_profile(w http.ResponseWriter, r *http.Request){ if template_profile_handle != nil { template_profile_handle(ppage,w) } else { - err = templates.ExecuteTemplate(w,"profile.html", ppage) + err = templates.ExecuteTemplate(w,"profile.html",ppage) if err != nil { - InternalError(err, w, r, user) + InternalError(err,w,r,user) } } } @@ -546,8 +554,35 @@ func route_topic_create(w http.ResponseWriter, r *http.Request){ NoPermissions(w,r,user) return } - pi := Page{"Create Topic",user,noticeList,tList,0} - templates.ExecuteTemplate(w,"create-topic.html", pi) + + var fid int + var err error + sfid := r.URL.Path[len("/topics/create/"):] + if sfid != "" { + fid, err = strconv.Atoi(sfid) + if err != nil { + LocalError("The provided ForumID is not a valid number.",w,r,user) + return + } + } + + var forumList []Forum + group := groups[user.Group] + for _, fid := range group.CanSee { + if forums[fid].Active && forums[fid].Name != "" { + forumList = append(forumList, forums[fid]) + } + } + + ctpage := CreateTopicPage{"Create Topic",user,noticeList,forumList,fid,nil} + if template_create_topic_handle != nil { + template_create_topic_handle(ctpage,w) + } else { + err = templates.ExecuteTemplate(w,"create-topic.html",ctpage) + if err != nil { + InternalError(err,w,r,user) + } + } } // POST functions. Authorised users only. @@ -563,11 +598,15 @@ func route_create_topic(w http.ResponseWriter, r *http.Request) { err := r.ParseForm() if err != nil { - LocalError("Bad Form", w, r, user) + LocalError("Bad Form",w,r,user) return } - fid := 2 + fid, err := strconv.Atoi(r.PostFormValue("topic-board")) + if err != nil { + LocalError("The provided ForumID is not a valid number.",w,r,user) + return + } topic_name := html.EscapeString(r.PostFormValue("topic-name")) content := html.EscapeString(preparse_message(r.PostFormValue("topic-content"))) ipaddress, _, err := net.SplitHostPort(r.RemoteAddr) @@ -581,7 +620,7 @@ func route_create_topic(w http.ResponseWriter, r *http.Request) { return } - res, err := create_topic_stmt.Exec(topic_name,content,parse_message(content),ipaddress,user.ID) + res, err := create_topic_stmt.Exec(fid,topic_name,content,parse_message(content),ipaddress,user.ID) if err != nil { InternalError(err,w,r,user) return @@ -593,22 +632,27 @@ func route_create_topic(w http.ResponseWriter, r *http.Request) { return } - _, err = add_topics_to_forum_stmt.Exec(1, fid) + _, err = add_topics_to_forum_stmt.Exec(1,fid) if err != nil { InternalError(err,w,r,user) return } forums[fid].TopicCount -= 1 - _, err = update_forum_cache_stmt.Exec(topic_name, lastId, user.Name, user.ID, fid) + _, err = update_forum_cache_stmt.Exec(topic_name,lastId,user.Name,user.ID,fid) if err != nil { InternalError(err,w,r,user) return } + forums[fid].LastTopic = topic_name + forums[fid].LastTopicID = int(lastId) + forums[fid].LastReplyer = user.Name + forums[fid].LastReplyerID = user.ID + forums[fid].LastTopicTime = "" http.Redirect(w, r, "/topic/" + strconv.FormatInt(lastId,10), http.StatusSeeOther) wcount := word_count(content) - err = increase_post_user_stats(wcount, user.ID, true, user) + err = increase_post_user_stats(wcount,user.ID,true,user) if err != nil { InternalError(err,w,r,user) return diff --git a/template_forum.go b/template_forum.go index be038155..ffb4f55c 100644 --- a/template_forum.go +++ b/template_forum.go @@ -1,7 +1,7 @@ /* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */ package main -import "io" import "strconv" +import "io" func init() { template_forum_handle = template_forum @@ -61,34 +61,50 @@ w.Write(forum_7) w.Write(forum_8) w.Write([]byte(tmpl_forum_vars.Title)) w.Write(forum_9) -if len(tmpl_forum_vars.ItemList) != 0 { -for _, item := range tmpl_forum_vars.ItemList { +if tmpl_forum_vars.CurrentUser.ID != 0 { +if !tmpl_forum_vars.CurrentUser.Perms.CreateTopic { w.Write(forum_10) -if item.Avatar != "" { +} else { w.Write(forum_11) -w.Write([]byte(item.Avatar)) +w.Write([]byte(strconv.Itoa(tmpl_forum_vars.Forum.ID))) w.Write(forum_12) } -if item.Sticky { +} w.Write(forum_13) -} else { -if item.Is_Closed { +if len(tmpl_forum_vars.ItemList) != 0 { +for _, item := range tmpl_forum_vars.ItemList { w.Write(forum_14) -} -} +if item.Avatar != "" { w.Write(forum_15) -w.Write([]byte(strconv.Itoa(item.ID))) +w.Write([]byte(item.Avatar)) w.Write(forum_16) -w.Write([]byte(item.Title)) +} +if item.Sticky { w.Write(forum_17) +} else { if item.Is_Closed { w.Write(forum_18) } +} w.Write(forum_19) +w.Write([]byte(strconv.Itoa(item.ID))) +w.Write(forum_20) +w.Write([]byte(item.Title)) +w.Write(forum_21) +if item.Is_Closed { +w.Write(forum_22) +} +w.Write(forum_23) } } else { -w.Write(forum_20) +w.Write(forum_24) +if tmpl_forum_vars.CurrentUser.Perms.CreateTopic { +w.Write(forum_25) +w.Write([]byte(strconv.Itoa(tmpl_forum_vars.Forum.ID))) +w.Write(forum_26) } -w.Write(forum_21) +w.Write(forum_27) +} +w.Write(forum_28) w.Write(footer_0) } diff --git a/template_list.go b/template_list.go index edc18c40..543afa80 100644 --- a/template_list.go +++ b/template_list.go @@ -425,24 +425,32 @@ var forum_7 []byte = []byte(`">>`) var forum_8 []byte = []byte(`
+var forum_9 []byte = []byte(` + `) +var forum_10 []byte = []byte(`🔒︎`) +var forum_11 []byte = []byte(`New Topic`) +var forum_13 []byte = []byte(`
`) -var forum_10 []byte = []byte(`
+var forum_14 []byte = []byte(`
`) -var forum_17 []byte = []byte(` `) -var forum_18 []byte = []byte(`🔒︎`) -var forum_19 []byte = []byte(` +var forum_20 []byte = []byte(`">`) +var forum_21 []byte = []byte(` `) +var forum_22 []byte = []byte(`🔒︎`) +var forum_23 []byte = []byte(`
`) -var forum_20 []byte = []byte(`
There aren't any topics in this forum yet.
`) -var forum_21 []byte = []byte(` +var forum_24 []byte = []byte(`
There aren't any topics in this forum yet.`) +var forum_25 []byte = []byte(` Start one?`) +var forum_27 []byte = []byte(`
`) +var forum_28 []byte = []byte(`
`) diff --git a/template_profile.go b/template_profile.go index 609431b3..e71179bf 100644 --- a/template_profile.go +++ b/template_profile.go @@ -1,7 +1,7 @@ /* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */ package main -import "io" import "strconv" +import "io" func init() { template_profile_handle = template_profile diff --git a/templates/create-topic.html b/templates/create-topic.html index 5551533d..36ffaeb2 100644 --- a/templates/create-topic.html +++ b/templates/create-topic.html @@ -5,6 +5,12 @@
+ +
+
+ diff --git a/templates/forum.html b/templates/forum.html index cf193f54..a9915af1 100644 --- a/templates/forum.html +++ b/templates/forum.html @@ -3,12 +3,13 @@ {{if ne .LastPage .Page}} {{end}}
- +
{{.Title}} + {{if ne .CurrentUser.ID 0}}{{if not .CurrentUser.Perms.CreateTopic}}🔒︎{{else}}New Topic{{end}}{{end}}
{{range .ItemList}}
{{.Title}} {{if .Is_Closed}}🔒︎{{end}}
- {{else}}
There aren't any topics in this forum yet.
{{end}} + {{else}}
There aren't any topics in this forum yet.{{if .CurrentUser.Perms.CreateTopic}} Start one?{{end}}
{{end}}
{{template "footer.html" . }} \ No newline at end of file