openidec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 61b577b25d9a94bbadfb7d5f88becd06b3705506
parent 1fa4ba8131e76a5388cc51da708b3828e0136e2e
Author: Peter Kosyh <p.kosyh@gmail.com>
Date:   Thu,  3 Sep 2020 20:25:29 +0300

send messsage, fix in sorting

Diffstat:
Mii-node/tpl/index.tpl | 2+-
Mii-node/tpl/login.tpl | 3++-
Mii-node/tpl/topics.tpl | 3+++
Mii-node/web.go | 51++++++++++++++++++++++++++++++++++++++++++++++++++-
Mii/msg.go | 2+-
5 files changed, 57 insertions(+), 4 deletions(-)

diff --git a/ii-node/tpl/index.tpl b/ii-node/tpl/index.tpl @@ -9,7 +9,7 @@ </tr> {{range .Echoes }} <tr> -<td><a href="{{.Name}}/-1">{{.Name}}</a></td> +<td><a href="{{.Name}}">{{.Name}}</a></td> <td>{{.Topics}}</td> <td>{{.Count}}</td> <td>{{with .Msg}}{{.Date | fdate}}[{{.From}}] {{.Subj}}{{end}}</td> diff --git a/ii-node/tpl/login.tpl b/ii-node/tpl/login.tpl @@ -1,6 +1,7 @@ {{template "header.tpl"}} -<form method="post" enctype="multipart/form-data" action="/login"> +<form method="post" enctype="application/x-www-form-urlencoded" action="/login"> <input type="text" name="username" class="login" placeholder="username"><br> <input type="password" name="password" class="passwd" placeholder="password"><br> <button class="form-button">Login</button> +</form> {{template "footer.tpl"}} diff --git a/ii-node/tpl/topics.tpl b/ii-node/tpl/topics.tpl @@ -1,4 +1,7 @@ {{template "header.tpl" $}} +{{if .User.Name }} +<a href="/{{.BasePath}}/new">New topic</a><br> +{{end}} {{template "pager.tpl" $}} <table class="topiclist"> <tr> diff --git a/ii-node/web.go b/ii-node/web.go @@ -87,7 +87,11 @@ func getTopics(db *ii.DB, mi []*ii.MsgInfo) map[string][]string { continue } t := l[len(l) - 1] + topics[t] = append(topics[t], t) for _, id := range l { + if id == t { + continue + } topics[t] = append(topics[t], id) intopic[id] = t } @@ -184,8 +188,9 @@ func www_topic(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, i if mi == nil { return errors.New("No such message") } - mis := db.LookupIDS(db.SelectIDS(ii.Query{Echo: mi.Echo, Repto: ""})) + mis := db.LookupIDS(db.SelectIDS(ii.Query{Echo: mi.Echo})) ids := getTopics(db, mis)[id] + fmt.Printf("%d\n", len(ids)) ii.Trace.Printf("www topic: %s", id) start := makePager(&ctx, len(ids), page) nr := PAGE_SIZE @@ -204,6 +209,47 @@ func www_topic(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, i return err } +func www_new(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, echo string) error { + ctx := WebContext{ User: user } + ctx.BasePath = echo + ctx.Echo = echo + + switch r.Method { + case "GET": + err := www.tpl.ExecuteTemplate(w, "new.tpl", ctx) + return err + case "POST": + ii.Trace.Printf("www new topic in %s", echo) + if err := r.ParseForm(); err != nil { + ii.Error.Printf("Error in POST request: %s", err) + return err + } + if user.Name == "" { + ii.Error.Printf("Access denied") + return errors.New("Access denied") + } + subj := r.FormValue("subj") + to := r.FormValue("to") + msg := r.FormValue("msg") + text := fmt.Sprintf("%s\n%s\n%s\n\n%s", echo, to, subj, msg) + fmt.Printf("%s\n", text) + m, err := ii.DecodeMsgline(text, false) + if err != nil { + ii.Error.Printf("Error while posting new topic: %s", err) + return err + } + m.From = user.Name + m.Addr = fmt.Sprintf("%s,%d", www.db.Name, user.Id) + if err = www.db.Store(m); err != nil { + ii.Error.Printf("Error while storig new topic %s: %s", m.MsgId, err) + return err + } + http.Redirect(w, r, "/"+echo+"/1", http.StatusSeeOther) + return nil + } + return nil +} + func msg_format(txt string) template.HTML { txt = strings.Replace(txt, "&", "&amp;", -1) txt = strings.Replace(txt, "<", "&lt;", -1) @@ -251,6 +297,9 @@ func handleWWW(www WWW, w http.ResponseWriter, r *http.Request) error { } else if ii.IsEcho(args[0]) { page := 1 if len(args) > 1 { + if args[1] == "new" { + return www_new(user, www, w, r, args[0]) + } fmt.Sscanf(args[1], "%d", &page) } return www_topics(user, www, w, r, args[0], page) diff --git a/ii/msg.go b/ii/msg.go @@ -57,7 +57,7 @@ func DecodeMsgline(msg string, enc bool) (*Msg, error) { data = []byte(msg) } text := strings.Split(string(data), "\n") - if len(text) <= 6 { + if len(text) < 5 { return nil, errors.New("Wrong message format") } if text[3] != "" {