From 2c6a20f25969c1701953d0222e455aafec2e58c6 Mon Sep 17 00:00:00 2001 From: Azareal Date: Sat, 3 Dec 2016 04:50:35 +0000 Subject: [PATCH] Fixed topic creation. Fixed a text overflow bug. Fixed a newline formatting bug. Fixed an avatar rendering bug. --- src/main.go | 10 +-- src/public/global.js | 2 +- src/public/main.css | 9 ++- src/reply.go | 3 +- src/routes.go | 76 +++++++++------------- src/templates/account-own-edit-avatar.html | 4 +- src/templates/topic.html | 30 ++++----- src/user.go | 19 ++---- 8 files changed, 70 insertions(+), 83 deletions(-) diff --git a/src/main.go b/src/main.go index 1c201b09..4311d2e7 100644 --- a/src/main.go +++ b/src/main.go @@ -57,25 +57,25 @@ func init_database(err error) { } log.Print("Preparing create_topic statement.") - create_topic_stmt, err = db.Prepare("INSERT INTO topics(title,createdAt,lastReplyAt,createdBy) VALUES(?,?,0,?)") + create_topic_stmt, err = db.Prepare("INSERT INTO topics(title,content,parsed_content,createdAt,createdBy) VALUES(?,?,?,NOW(),?)") if err != nil { log.Fatal(err) } log.Print("Preparing create_reply statement.") - create_reply_stmt, err = db.Prepare("INSERT INTO replies(tid,content,createdAt,createdBy) VALUES(?,?,?,?)") + create_reply_stmt, err = db.Prepare("INSERT INTO replies(tid,content,parsed_content,createdAt,createdBy) VALUES(?,?,?,NOW(),?)") if err != nil { log.Fatal(err) } log.Print("Preparing edit_topic statement.") - edit_topic_stmt, err = db.Prepare("UPDATE topics SET title = ?, content = ?, is_closed = ? WHERE tid = ?") + edit_topic_stmt, err = db.Prepare("UPDATE topics SET title = ?, content = ?, parsed_content = ?, is_closed = ? WHERE tid = ?") if err != nil { log.Fatal(err) } log.Print("Preparing edit_reply statement.") - edit_reply_stmt, err = db.Prepare("UPDATE replies SET content = ? WHERE rid = ?") + edit_reply_stmt, err = db.Prepare("UPDATE replies SET content = ?, parsed_content = ? WHERE rid = ?") if err != nil { log.Fatal(err) } @@ -181,7 +181,7 @@ func main(){ //http.HandleFunc("/user/edit/", route_logout) http.HandleFunc("/user/edit/critical/", route_account_own_edit_critical) // Password & Email http.HandleFunc("/user/edit/critical/submit/", route_account_own_edit_critical_submit) - http.HandleFunc("/user/edit/avatar/", route_account_own_edit_avatar) // Password & Email + http.HandleFunc("/user/edit/avatar/", route_account_own_edit_avatar) http.HandleFunc("/user/edit/avatar/submit/", route_account_own_edit_avatar_submit) //http.HandleFunc("/user/:id/edit/", route_logout) //http.HandleFunc("/user/:id/ban/", route_logout) diff --git a/src/public/global.js b/src/public/global.js index 9c99601c..255d127b 100644 --- a/src/public/global.js +++ b/src/public/global.js @@ -63,7 +63,7 @@ $(document).ready(function(){ event.preventDefault(); var block_parent = $(this).closest('.editable_parent'); var block = block_parent.find('.editable_block').eq(0); - block.html("
"); + block.html("
"); $(".submit_edit").click(function(event) { diff --git a/src/public/main.css b/src/public/main.css index 0f0d5605..72cd67ef 100644 --- a/src/public/main.css +++ b/src/public/main.css @@ -1,3 +1,9 @@ +* { + box-sizing: border-box; + -moz-box-sizing: border-box; + -webkit-box-sizing: border-box; +} + body { font-family: arial; @@ -10,7 +16,7 @@ ul padding-bottom: 5px; padding-left: 0px; padding-right: 0px; - height: 18px; + height: 28px; list-style-type: none; } @@ -74,6 +80,7 @@ li:not(:last-child) padding-top: 0px; width: 65%; overflow: hidden; + word-wrap: break-word; } .colblock_left:empty { diff --git a/src/reply.go b/src/reply.go index b6fac089..447c8639 100644 --- a/src/reply.go +++ b/src/reply.go @@ -1,15 +1,16 @@ package main +import "html/template" type Reply struct { ID int ParentID int Content string + ContentHtml template.HTML CreatedBy int CreatedByName string CreatedAt string LastEdit int LastEditBy int Avatar string - HasAvatar bool } diff --git a/src/routes.go b/src/routes.go index 93b46896..5064630a 100644 --- a/src/routes.go +++ b/src/routes.go @@ -6,11 +6,11 @@ import "strconv" import "bytes" import "regexp" import "strings" -import "time" import "io" import "os" import "net/http" import "html" +import "html/template" import "database/sql" import _ "github.com/go-sql-driver/mysql" import "golang.org/x/crypto/bcrypt" @@ -94,20 +94,18 @@ func route_topics(w http.ResponseWriter, r *http.Request){ return } pi := Page{"Topic List","topics",user,topicList,0} - templates.ExecuteTemplate(w,"topics.html", pi) + err = templates.ExecuteTemplate(w,"topics.html", pi) + if err != nil { + InternalError(err, w, r, user) + } } func route_topic_id(w http.ResponseWriter, r *http.Request){ user := SessionCheck(w,r) var( - tid int + err error rid int - parentID int - title string content string - createdBy int - createdByName string - createdAt string replyContent string replyCreatedBy int replyCreatedByName string @@ -115,17 +113,15 @@ func route_topic_id(w http.ResponseWriter, r *http.Request){ replyLastEdit int replyLastEditBy int replyAvatar string - replyHasAvatar bool - is_closed bool - sticky bool currentID int replyList map[int]interface{} ) replyList = make(map[int]interface{}) currentID = 0 + topic := TopicUser{0,"","",0,false,false,"",0,"","",""} - tid, err := strconv.Atoi(r.URL.Path[len("/topic/"):]) + topic.ID, err = strconv.Atoi(r.URL.Path[len("/topic/"):]) if err != nil { LocalError("The provided TopicID is not a valid number.",w,r,user) return @@ -133,7 +129,7 @@ func route_topic_id(w http.ResponseWriter, r *http.Request){ // Get the topic.. //err = db.QueryRow("select title, content, createdBy, status, is_closed from topics where tid = ?", tid).Scan(&title, &content, &createdBy, &status, &is_closed) - err = db.QueryRow("select topics.title, topics.content, topics.createdBy, topics.createdAt, topics.is_closed, topics.sticky, topics.parentID, users.name from topics left join users ON topics.createdBy = users.uid where tid = ?", tid).Scan(&title, &content, &createdBy, &createdAt, &is_closed, &sticky, &parentID, &createdByName) + err = db.QueryRow("select topics.title, topics.content, topics.createdBy, topics.createdAt, topics.is_closed, topics.sticky, topics.parentID, users.name, users.avatar from topics left join users ON topics.createdBy = users.uid where tid = ?", topic.ID).Scan(&topic.Title, &content, &topic.CreatedBy, &topic.CreatedAt, &topic.Is_Closed, &topic.Sticky, &topic.ParentID, &topic.CreatedByName, &topic.Avatar) if err == sql.ErrNoRows { errmsg := "The requested topic doesn't exist." pi := Page{"Error","error",user,tList,errmsg} @@ -148,25 +144,19 @@ func route_topic_id(w http.ResponseWriter, r *http.Request){ return } - var tdata map[string]string - tdata = make(map[string]string) - tdata["tid"] = strconv.Itoa(tid) - tdata["title"] = title - tdata["content"] = content - tdata["createdBy"] = string(createdBy) - tdata["createdAt"] = string(createdAt) - tdata["parentID"] = string(parentID) - if is_closed { - tdata["status"] = "closed" + topic.Content = template.HTML(content) + if topic.Is_Closed { + topic.Status = "closed" } else { - tdata["status"] = "open" + topic.Status = "open" + } + if topic.Avatar != "" && topic.Avatar[0] == '.' { + topic.Avatar = "/uploads/avatar_" + strconv.Itoa(topic.CreatedBy) + topic.Avatar } - //tdata["sticky"] = sticky - tdata["createdByName"] = createdByName // Get the replies.. //rows, err := db.Query("select rid, content, createdBy, createdAt from replies where tid = ?", tid) - rows, err := db.Query("select replies.rid, replies.content, replies.createdBy, replies.createdAt, replies.lastEdit, replies.lastEditBy, users.avatar, users.name from replies left join users ON replies.createdBy = users.uid where tid = ?", tid) + rows, err := db.Query("select replies.rid, replies.content, replies.createdBy, replies.createdAt, replies.lastEdit, replies.lastEditBy, users.avatar, users.name from replies left join users ON replies.createdBy = users.uid where tid = ?", topic.ID) if err != nil { InternalError(err,w,r,user) return @@ -180,16 +170,11 @@ func route_topic_id(w http.ResponseWriter, r *http.Request){ return } - if replyAvatar != "" { - replyHasAvatar = true - if replyAvatar[0] == '.' { - replyAvatar = "/uploads/avatar_" + strconv.Itoa(user.ID) + replyAvatar - } - } else { - replyHasAvatar = false + if replyAvatar != "" && replyAvatar[0] == '.' { + replyAvatar = "/uploads/avatar_" + strconv.Itoa(user.ID) + replyAvatar } - replyList[currentID] = Reply{rid,tid,replyContent,replyCreatedBy,replyCreatedByName,replyCreatedAt,replyLastEdit,replyLastEditBy,replyAvatar,replyHasAvatar} + replyList[currentID] = Reply{rid,topic.ID,replyContent,template.HTML(strings.Replace(replyContent,"\n","
",-1)),replyCreatedBy,replyCreatedByName,replyCreatedAt,replyLastEdit,replyLastEditBy,replyAvatar} currentID++ } err = rows.Err() @@ -198,8 +183,11 @@ func route_topic_id(w http.ResponseWriter, r *http.Request){ return } - pi := Page{title,"topic",user,replyList,tdata} - templates.ExecuteTemplate(w,"topic.html", pi) + pi := Page{topic.Title,"topic",user,replyList,topic} + err = templates.ExecuteTemplate(w,"topic.html", pi) + if err != nil { + InternalError(err, w, r, user) + } } func route_topic_create(w http.ResponseWriter, r *http.Request){ @@ -223,7 +211,7 @@ func route_create_topic(w http.ResponseWriter, r *http.Request) { } success := 1 - res, err := create_topic_stmt.Exec(html.EscapeString(r.PostFormValue("topic-name")),html.EscapeString(r.PostFormValue("topic-content")),int32(time.Now().Unix()),user.ID) + res, err := create_topic_stmt.Exec(html.EscapeString(r.PostFormValue("topic-name")),html.EscapeString(r.PostFormValue("topic-content")),strings.Replace(html.EscapeString(r.PostFormValue("topic-content")),"\n","
",-1),user.ID) if err != nil { log.Print(err) success = 0 @@ -250,8 +238,6 @@ func route_create_topic(w http.ResponseWriter, r *http.Request) { func route_create_reply(w http.ResponseWriter, r *http.Request) { var tid int - - user := SessionCheck(w,r) if !user.Loggedin { LoginRequired(w,r,user) @@ -279,9 +265,8 @@ func route_create_reply(w http.ResponseWriter, r *http.Request) { http.Error(w,errpage,500) return } - //log.Println("A reply is being created") - _, err = create_reply_stmt.Exec(tid,html.EscapeString(r.PostFormValue("reply-content")),int32(time.Now().Unix()),user.ID) + _, err = create_reply_stmt.Exec(tid,html.EscapeString(r.PostFormValue("reply-content")),strings.Replace(html.EscapeString(r.PostFormValue("reply-content")),"\n","
",-1),user.ID) if err != nil { log.Print(err) success = 0 @@ -332,8 +317,8 @@ func route_edit_topic(w http.ResponseWriter, r *http.Request) { } else { is_closed = false } - topic_content := html.EscapeString(r.PostFormValue("topic_content")) - _, err = edit_topic_stmt.Exec(topic_name, topic_content, is_closed, tid) + topic_content := html.EscapeString(r.PostFormValue("topic-content")) + _, err = edit_topic_stmt.Exec(topic_name, topic_content, strings.Replace(topic_content,"\n","
",-1), is_closed, tid) if err != nil { InternalErrorJSQ(err,w,r,user,is_js) return @@ -371,7 +356,7 @@ func route_reply_edit_submit(w http.ResponseWriter, r *http.Request) { } content := html.EscapeString(r.PostFormValue("edit_item")) - _, err = edit_reply_stmt.Exec(content, rid) + _, err = edit_reply_stmt.Exec(content, strings.Replace(content,"\n","
",-1), rid) if err != nil { InternalError(err,w,r,user) return @@ -634,7 +619,6 @@ func route_account_own_edit_avatar_submit(w http.ResponseWriter, r *http.Request return } - user.HasAvatar = true user.Avatar = "/uploads/avatar_" + strconv.Itoa(user.ID) + "." + ext pi := Page{"Edit Avatar","account-own-edit-avatar-success",user,tList,0} diff --git a/src/templates/account-own-edit-avatar.html b/src/templates/account-own-edit-avatar.html index 819a75a3..ffdf7ac9 100644 --- a/src/templates/account-own-edit-avatar.html +++ b/src/templates/account-own-edit-avatar.html @@ -1,8 +1,8 @@ {{template "header.html" . }}
- + @@ -10,7 +10,7 @@ -{{ if .CurrentUser.HasAvatar }} +{{ if .CurrentUser.Avatar }}
diff --git a/src/templates/topic.html b/src/templates/topic.html index 00f55d83..e535d1d1 100644 --- a/src/templates/topic.html +++ b/src/templates/topic.html @@ -1,13 +1,13 @@ {{template "header.html" . }}
-
+
- {{index .Something "title"}} - {{index .Something "status"}} - Edit - Delete + {{.Something.Title}} + {{.Something.Status}} + Edit + Delete - + +
+ {{.Something.Content}} +

- {{index .Something "createdByName"}} + {{.Something.CreatedByName}}

{{range $index, $element := .ItemList}} -
- +
diff --git a/src/user.go b/src/user.go index 0df40686..650a2a76 100644 --- a/src/user.go +++ b/src/user.go @@ -16,7 +16,6 @@ type User struct Session string Loggedin bool Avatar string - HasAvatar bool } func SetPassword(uid int, password string) (error) { @@ -39,7 +38,7 @@ func SetPassword(uid int, password string) (error) { } func SessionCheck(w http.ResponseWriter, r *http.Request) (User) { - user := User{0,"",0,false,false,"",false,"",false} + user := User{0,"",0,false,false,"",false,""} var err error var cookie *http.Cookie @@ -59,8 +58,8 @@ func SessionCheck(w http.ResponseWriter, r *http.Request) (User) { return user } user.Session = cookie.Value - log.Print("ID: " + user.Name) - log.Print("Session: " + user.Session) + //log.Print("ID: " + user.Name) + //log.Print("Session: " + user.Session) // Is this session valid..? err = get_session_stmt.QueryRow(user.ID,user.Session).Scan(&user.ID, &user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Avatar) @@ -72,14 +71,11 @@ func SessionCheck(w http.ResponseWriter, r *http.Request) (User) { return user } user.Is_Admin = user.Is_Super_Admin - if user.Avatar != "" { - user.HasAvatar = true - if user.Avatar[0] == '.' { - user.Avatar = "/uploads/avatar_" + strconv.Itoa(user.ID) + user.Avatar - } + if user.Avatar != "" && user.Avatar[0] == '.' { + user.Avatar = "/uploads/avatar_" + strconv.Itoa(user.ID) + user.Avatar } user.Loggedin = true - log.Print("Logged in") + /*log.Print("Logged in") log.Print("ID: " + strconv.Itoa(user.ID)) log.Print("Group: " + strconv.Itoa(user.Group)) log.Print("Name: " + user.Name) @@ -92,7 +88,6 @@ func SessionCheck(w http.ResponseWriter, r *http.Request) (User) { log.Print("Is_Admin: true") } else { log.Print("Is_Admin: false") - } - log.Print("Session: " + user.Session) + }*/ return user } \ No newline at end of file