From ae2a4809ba136ac5cb5e74b1662184cb294c3566 Mon Sep 17 00:00:00 2001 From: Azareal Date: Tue, 11 Feb 2020 17:01:13 +1000 Subject: [PATCH] carry over the recipient when creating a convo via profile hide the convo reply form when we don't have permission to create a reply --- common/pages.go | 7 ++++--- common/template_init.go | 36 ++++++++++++++++++------------------ routes/convos.go | 27 ++++++++++++++++++++++++--- templates/convo.html | 2 +- templates/profile.html | 2 +- 5 files changed, 48 insertions(+), 26 deletions(-) diff --git a/common/pages.go b/common/pages.go index 54be0736..d9fde7a3 100644 --- a/common/pages.go +++ b/common/pages.go @@ -300,9 +300,10 @@ type ConvoViewRow struct { type ConvoViewPage struct { *Header - Convo *Conversation - Posts []ConvoViewRow - Users []*User + Convo *Conversation + Posts []ConvoViewRow + Users []*User + CanReply bool Paginator } diff --git a/common/template_init.go b/common/template_init.go index 4490139d..46418ffb 100644 --- a/common/template_init.go +++ b/common/template_init.go @@ -93,7 +93,7 @@ var Template_account_handle = genIntTmpl("account") func tmplInitUsers() (User, User, User) { avatar, microAvatar := BuildAvatar(62, "") - user := User{62, BuildProfileURL("fake-user", 62), "Fake User", "compiler@localhost", 0, false, false, false, false, false, false, GuestPerms, make(map[string]bool), "", false, "", avatar, microAvatar, "", "", 0, 0, 0, 0, StartTime,"0.0.0.0.0", "", 0, nil} + user := User{62, BuildProfileURL("fake-user", 62), "Fake User", "compiler@localhost", 0, false, false, false, false, false, false, GuestPerms, make(map[string]bool), "", false, "", avatar, microAvatar, "", "", 0, 0, 0, 0, StartTime, "0.0.0.0.0", "", 0, nil} // TODO: Do a more accurate level calculation for this? avatar, microAvatar = BuildAvatar(1, "") @@ -104,7 +104,7 @@ func tmplInitUsers() (User, User, User) { return user, user2, user3 } -func tmplInitHeaders(user User, user2 User, user3 User) (*Header, *Header, *Header) { +func tmplInitHeaders(user, user2, user3 User) (*Header, *Header, *Header) { header := &Header{ Site: Site, Settings: SettingBox.Load().(SettingMap), @@ -147,11 +147,11 @@ type TItem struct { type TItemHold map[string]TItem -func (h TItemHold) Add(name string, expects string, expectsInt interface{}) { +func (h TItemHold) Add(name, expects string, expectsInt interface{}) { h[name] = TItem{expects, expectsInt, true} } -func (h TItemHold) AddStd(name string, expects string, expectsInt interface{}) { +func (h TItemHold) AddStd(name, expects string, expectsInt interface{}) { h[name] = TItem{expects, expectsInt, false} } @@ -208,7 +208,7 @@ func CompileTemplates() error { return nil } -func compileCommons(c *tmpl.CTemplateSet, head *Header, head2 *Header, forumList []Forum, o TItemHold) error { +func compileCommons(c *tmpl.CTemplateSet, head, head2 *Header, forumList []Forum, o TItemHold) error { // TODO: Add support for interface{}s _, user2, user3 := tmplInitUsers() now := time.Now() @@ -352,7 +352,7 @@ func compileTemplates(wg *sync.WaitGroup, c *tmpl.CTemplateSet, themeName string parti := []*User{&user} convo := &Conversation{1, user.ID, time.Now(), 0, time.Now()} convoItems := []ConvoViewRow{ConvoViewRow{&ConversationPost{1, 1, "hey", "", user.ID}, &user, "", 4, true}} - convoPage := ConvoViewPage{header, convo, convoItems, parti, Paginator{[]int{1}, 1, 1}} + convoPage := ConvoViewPage{header, convo, convoItems, parti, true, Paginator{[]int{1}, 1, 1}} t.AddStd("convo", "c.ConvoViewPage", convoPage) convos := []*ConversationExtra{&ConversationExtra{&Conversation{}, []*User{&user}}} @@ -377,7 +377,7 @@ func compileTemplates(wg *sync.WaitGroup, c *tmpl.CTemplateSet, themeName string writeTemplate := func(name string, content interface{}) { log.Print("Writing template '" + name + "'") - writeTmpl := func(name string, content string) { + writeTmpl := func(name, content string) { if content == "" { return //log.Fatal("No content body for " + name) } @@ -531,7 +531,7 @@ func compileJSTemplates(wg *sync.WaitGroup, c *tmpl.CTemplateSet, themeName stri }, VoteCount: 7} avatar, microAvatar := BuildAvatar(62, "") miniAttach := []*MiniAttachment{&MiniAttachment{Path: "/"}} - topic := TopicUser{1, "blah", "Blah", "Hey there!", 62, false, false, now, now, 1, 1, 0, "", "127.0.0.1", 1, 0, 1, 0, "classname", poll.ID, "weird-data", BuildProfileURL("fake-user", 62), "Fake User", Config.DefaultGroup, avatar, microAvatar, 0, "","","", 58, false, miniAttach, nil, false} + topic := TopicUser{1, "blah", "Blah", "Hey there!", 62, false, false, now, now, 1, 1, 0, "", "127.0.0.1", 1, 0, 1, 0, "classname", poll.ID, "weird-data", BuildProfileURL("fake-user", 62), "Fake User", Config.DefaultGroup, avatar, microAvatar, 0, "", "", "", 58, false, miniAttach, nil, false} var replyList []*ReplyUser // TODO: Do we really want the UID here to be zero? avatar, microAvatar = BuildAvatar(0, "") @@ -559,13 +559,13 @@ func compileJSTemplates(wg *sync.WaitGroup, c *tmpl.CTemplateSet, themeName stri parti := []*User{&user} convo := &Conversation{1, user.ID, time.Now(), 0, time.Now()} convoItems := []ConvoViewRow{ConvoViewRow{&ConversationPost{1, 1, "hey", "", user.ID}, &user, "", 4, true}} - convoPage := ConvoViewPage{header, convo, convoItems, parti, Paginator{[]int{1}, 1, 1}} + convoPage := ConvoViewPage{header, convo, convoItems, parti, true, Paginator{[]int{1}, 1, 1}} t.AddStd("convo", "c.ConvoViewPage", convoPage) t.AddStd("notice", "string", "nonono") dirPrefix := "./tmpl_client/" - writeTemplate := func(name string, content string) { + writeTemplate := func(name, content string) { log.Print("Writing template '" + name + "'") if content == "" { return //log.Fatal("No content body") @@ -695,29 +695,29 @@ func arithToInt64(in interface{}) (out int64) { return out } -func arithDuoToInt64(left interface{}, right interface{}) (leftInt int64, rightInt int64) { +func arithDuoToInt64(left, right interface{}) (leftInt, rightInt int64) { return arithToInt64(left), arithToInt64(right) } func initDefaultTmplFuncMap() { // TODO: Add support for floats fmap := make(map[string]interface{}) - fmap["add"] = func(left interface{}, right interface{}) interface{} { + fmap["add"] = func(left, right interface{}) interface{} { leftInt, rightInt := arithDuoToInt64(left, right) return leftInt + rightInt } - fmap["subtract"] = func(left interface{}, right interface{}) interface{} { + fmap["subtract"] = func(left, right interface{}) interface{} { leftInt, rightInt := arithDuoToInt64(left, right) return leftInt - rightInt } - fmap["multiply"] = func(left interface{}, right interface{}) interface{} { + fmap["multiply"] = func(left, right interface{}) interface{} { leftInt, rightInt := arithDuoToInt64(left, right) return leftInt * rightInt } - fmap["divide"] = func(left interface{}, right interface{}) interface{} { + fmap["divide"] = func(left, right interface{}) interface{} { leftInt, rightInt := arithDuoToInt64(left, right) if leftInt == 0 || rightInt == 0 { return 0 @@ -725,11 +725,11 @@ func initDefaultTmplFuncMap() { return leftInt / rightInt } - fmap["dock"] = func(dock interface{}, headerInt interface{}) interface{} { + fmap["dock"] = func(dock, headerInt interface{}) interface{} { return template.HTML(BuildWidget(dock.(string), headerInt.(*Header))) } - fmap["hasWidgets"] = func(dock interface{}, headerInt interface{}) interface{} { + fmap["hasWidgets"] = func(dock, headerInt interface{}) interface{} { return HasWidgets(dock.(string), headerInt.(*Header)) } @@ -803,7 +803,7 @@ func initDefaultTmplFuncMap() { return "" } - fmap["dyntmpl"] = func(nameInt interface{}, pageInt interface{}, headerInt interface{}) interface{} { + fmap["dyntmpl"] = func(nameInt, pageInt, headerInt interface{}) interface{} { header := headerInt.(*Header) err := header.Theme.RunTmpl(nameInt.(string), pageInt, header.Writer) if err != nil { diff --git a/routes/convos.go b/routes/convos.go index 994f13a8..11722770 100644 --- a/routes/convos.go +++ b/routes/convos.go @@ -3,6 +3,7 @@ package routes import ( "database/sql" "errors" + "html" "net/http" "strconv" "strings" @@ -94,7 +95,18 @@ func Convo(w http.ResponseWriter, r *http.Request, user c.User, header *c.Header pitems[i] = c.ConvoViewRow{post, uuser, "", 4, canModify} } - pi := c.Account{header, "dashboard", "convo", c.ConvoViewPage{header, convo, pitems, users, c.Paginator{pageList, page, lastPage}}} + canReply := user.Perms.UseConvos || user.Perms.UseConvosOnlyWithMod + if !user.Perms.UseConvos && user.Perms.UseConvosOnlyWithMod { + u, err := c.Users.Get(convo.CreatedBy) + if err != nil { + return c.InternalError(err, w, r) + } + if !u.IsSuperMod { + canReply = false + } + } + + pi := c.Account{header, "dashboard", "convo", c.ConvoViewPage{header, convo, pitems, users, canReply, c.Paginator{pageList, page, lastPage}}} return renderTemplate("account", w, r, header, pi) } @@ -104,8 +116,16 @@ func ConvosCreate(w http.ResponseWriter, r *http.Request, user c.User, h *c.Head return c.NoPermissions(w, r, user) } h.AddNotice("convo_dev") - recpName := "" - pi := c.Account{h, "dashboard", "create_convo", c.ConvoCreatePage{h, recpName}} + uid, err := strconv.Atoi(r.FormValue("with")) + if err != nil { + return c.LocalError("invalid integer in parameter with", w, r, user) + } + u, err := c.Users.Get(uid) + if err != nil { + return c.LocalError("Unable to fetch user", w, r, user) + } + // TODO: Avoid potential double escape? + pi := c.Account{h, "dashboard", "create_convo", c.ConvoCreatePage{h, html.EscapeString(u.Name)}} return renderTemplate("account", w, r, h, pi) } @@ -206,6 +226,7 @@ func ConvosCreateReplySubmit(w http.ResponseWriter, r *http.Request, user c.User if !convo.Has(user.ID) { return c.LocalError("You are not in this conversation.", w, r, user) } + // TODO: Let the user reply if they're the convo creator in a convo with a mod if !user.Perms.UseConvos && user.Perms.UseConvosOnlyWithMod { u, err := c.Users.Get(convo.CreatedBy) if err != nil { diff --git a/templates/convo.html b/templates/convo.html index 032c9fab..fc6e5e17 100644 --- a/templates/convo.html +++ b/templates/convo.html @@ -10,7 +10,7 @@
{{template "convo_row.html" .}}
-{{if not .CurrentUser.IsBanned}} +{{if .CanReply}}
diff --git a/templates/profile.html b/templates/profile.html index 1ed9073a..9f44f495 100644 --- a/templates/profile.html +++ b/templates/profile.html @@ -28,7 +28,7 @@ {{lang "profile.login_for_options"}}
{{else}} {{if .CanMessage}}{{end}}