Fixed topic creation.

Fixed a text overflow bug.
Fixed a newline formatting bug.
Fixed an avatar rendering bug.
This commit is contained in:
Azareal 2016-12-03 04:50:35 +00:00
parent 7a49ff5194
commit 2c6a20f259
8 changed files with 70 additions and 83 deletions

View File

@ -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)

View File

@ -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("<textarea style='width: 100%;' name='edit_item'>" + block.html() + "</textarea><br /><a href='" + $(this).closest('a').attr("href") + "'><button class='submit_edit' type='submit'>Update</button></a>");
block.html("<textarea style='width: 99%;' name='edit_item'>" + block.html() + "</textarea><br /><a href='" + $(this).closest('a').attr("href") + "'><button class='submit_edit' type='submit'>Update</button></a>");
$(".submit_edit").click(function(event)
{

View File

@ -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
{

View File

@ -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
}

View File

@ -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] == '.' {
if replyAvatar != "" && replyAvatar[0] == '.' {
replyAvatar = "/uploads/avatar_" + strconv.Itoa(user.ID) + replyAvatar
}
} else {
replyHasAvatar = false
}
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","<br>",-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","<br>",-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","<br>",-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","<br>",-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","<br>",-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}

View File

@ -1,8 +1,8 @@
{{template "header.html" . }}
<div class="colblock_left">
<div class="rowitem"><a>My Account</a></div>
<div class="rowitem passive"><a href="/user/edit/critical/">Edit Password</a></div>
<div class="rowitem passive"><a href="/user/edit/avatar/">Edit Avatar</a></div>
<div class="rowitem passive"><a href="/user/edit/critical/">Edit Password</a></div>
<div class="rowitem passive"><a>Coming Soon</a></div>
<div class="rowitem passive"><a>Coming Soon</a></div>
<div class="rowitem passive"><a>Coming Soon</a></div>
@ -10,7 +10,7 @@
<div class="colblock_right">
<div class="rowitem"><a>Edit Avatar</a></div>
</div>
{{ if .CurrentUser.HasAvatar }}
{{ if .CurrentUser.Avatar }}
<div class="colblock_right">
<div class="rowitem"><img src="{{.CurrentUser.Avatar}}" height="128px" max-width="128px" /></div>
</div>

View File

@ -1,13 +1,13 @@
{{template "header.html" . }}
<div class="rowblock">
<form action='/topic/edit/submit/{{index .Something "tid"}}/' method="post">
<form action='/topic/edit/submit/{{.Something.ID}}' method="post">
<div class="rowitem">
<a class='topic_name hide_on_edit'>{{index .Something "title"}}</a>
<span class='topic_status topic_status_e topic_status_{{index .Something "status"}} hide_on_edit'>{{index .Something "status"}}</span>
<a href='/topic/edit/{{index .Something "ID"}}/' class="topic_status hide_on_edit open_edit">Edit</a>
<a href='/topic/delete/{{index .Something "ID"}}/' class="topic_status">Delete</a>
<a class='topic_name hide_on_edit'>{{.Something.Title}}</a>
<span class='topic_status topic_status_e topic_status_{{.Something.Status}} hide_on_edit'>{{.Something.Status}}</span>
<a href='/topic/edit/{{.Something.ID}}' class="topic_status hide_on_edit open_edit">Edit</a>
<a href='/topic/delete/{{.Something.ID}}' class="topic_status">Delete</a>
<input class='show_on_edit topic_name_input' name="topic_name" value='{{index .Something "title"}}' type="text" />
<input class='show_on_edit topic_name_input' name="topic_name" value='{{.Something.Title}}' type="text" />
<select name="topic_status" class='show_on_edit topic_status_input'>
<option>closed</option>
<option>open</option>
@ -17,26 +17,26 @@
</form>
</div>
<div class="rowblock">
<div class="rowitem passive editable_parent" style="border-bottom: none;{{ if .CurrentUser.HasAvatar }}background-image: url({{ .CurrentUser.Avatar }});background-position: left;background-repeat: no-repeat;background-size: 128px;padding-left: 136px;{{end}}">
<span class="hide_on_edit topic_content">{{index .Something "content"}}</span>
<textarea name="topic_content" class="show_on_edit topic_content_input">{{index .Something "content"}}</textarea>
<div class="rowitem passive editable_parent" style="border-bottom: none;{{ if .Something.Avatar }}background-image: url({{ .Something.Avatar }});background-position: left;background-repeat: no-repeat;background-size: 128px;padding-left: 136px;{{end}}">
<span class="hide_on_edit topic_content">{{.Something.Content}}</span>
<textarea name="topic_content" class="show_on_edit topic_content_input">{{.Something.Content}}</textarea>
<br /><br />
<a class="topic_status" style="padding-left: 0px;margin-left: 0px;">{{index .Something "createdByName"}}<a/>
<a class="topic_status" style="padding-left: 0px;margin-left: 0px;">{{.Something.CreatedByName}}<a/>
</div>
</div><br />
<div class="rowblock" style="overflow: hidden;">
{{range $index, $element := .ItemList}}
<div class="rowitem passive deletable_block editable_parent" style="{{ if $element.HasAvatar }}background-image: url({{ $element.Avatar }});background-position: left;background-repeat: no-repeat;background-size: 128px;padding-left: 136px;{{end}}">
<span class="editable_block">{{$element.Content}}</span>
<div class="rowitem passive deletable_block editable_parent" style="{{ if $element.Avatar }}background-image: url({{ $element.Avatar }});background-position: left;background-repeat: no-repeat;background-size: 128px;padding-left: 136px;{{end}}">
<span class="editable_block">{{$element.ContentHtml}}</span>
<br /><br />
<a class="topic_status" style="padding-left: 0px;margin-left: 0px;">{{$element.CreatedByName}}<a/>
<a href="/reply/edit/submit/{{$element.ID}}/"><button class="topic_status edit_item">Edit</button></a>
<a href="/reply/delete/submit/{{$element.ID}}/"><button class="topic_status delete_item">Delete</button></a>
<a href="/reply/edit/submit/{{$element.ID}}"><button class="topic_status edit_item">Edit</button></a>
<a href="/reply/delete/submit/{{$element.ID}}"><button class="topic_status delete_item">Delete</button></a>
</div>{{ end }}
</div>
<div class="rowblock">
<form action="/reply/create/" method="post">
<input name="tid" value='{{index .Something "tid"}}' type="hidden" />
<input name="tid" value='{{.Something.ID}}' type="hidden" />
<div class="formrow">
<div class="formitem"><textarea name="reply-content" placeholder="Insert reply here"></textarea></div>
</div>

View File

@ -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] == '.' {
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
}