openidec

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

commit 987ce0e960e1ae7032e66981d4630114cb3f27ae
parent 754a7cdcffa2283de1e06111e9a7694d4f6bd618
Author: Peter Kosyh <p.kosyh@gmail.com>
Date:   Thu,  3 Sep 2020 15:27:05 +0300

cleanups

Diffstat:
Mii-node/main.go | 6+++---
Mii-node/tpl/index.tpl | 10++++++----
Mii-node/tpl/pager.tpl | 2+-
Mii-node/tpl/topic.tpl | 2+-
Mii-node/tpl/topics.tpl | 2+-
Mii-node/web.go | 44+++++++++++++++++++++++++++++++-------------
Mii/db.go | 1-
7 files changed, 43 insertions(+), 24 deletions(-)

diff --git a/ii-node/main.go b/ii-node/main.go @@ -72,10 +72,10 @@ func main() { db.Name = *sysname_opt - www.tpl = template.Must(template.ParseGlob("tpl/*.tpl")) - www.db = db + WebInit(&www, db) + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - err := Web(www, w, r) + err := handleWWW(www, w, r) if err != nil { fmt.Fprintf(w, "%s\n", err) } diff --git a/ii-node/tpl/index.tpl b/ii-node/tpl/index.tpl @@ -5,12 +5,14 @@ <th>Name</th> <th>Topics</th> <th>Posts</th> +<th>Last</th> </tr> -{{range .Echoes }} +{{range $k, $v := .Echoes }} <tr> -<td><a href="{{.Name}}">{{.Name}}</a></td> -<td>{{.Topics}}</td> -<td>{{.Count}}</td> +<td><a href="{{$v.Name}}/-1">{{$v.Name}}</a></td> +<td>{{$v.Topics}}</td> +<td>{{$v.Count}}</td> +<td>{{with index $.Msg $k}}[{{.From}}] {{.Subj}}{{end}}</td> </tr> {{ end }} </table> diff --git a/ii-node/tpl/pager.tpl b/ii-node/tpl/pager.tpl @@ -1,6 +1,6 @@ {{ range .Pager }} {{ if eq . $.Page }} -<a href="/{{$.BasePath}}/{{.}}">[{{.}}]</a> +[{{.}}] {{ else }} <a href="/{{$.BasePath}}/{{.}}">{{.}}</a> {{ end }} diff --git a/ii-node/tpl/topic.tpl b/ii-node/tpl/topic.tpl @@ -10,7 +10,7 @@ <br/> <span class="msgtext"> {{with .Text}} -{{call $.Render .}} +{{. | msg_format}} {{end}} </span> </div> diff --git a/ii-node/tpl/topics.tpl b/ii-node/tpl/topics.tpl @@ -10,7 +10,7 @@ <tr> <td><a href="/{{.Head.MsgId}}">{{.Head.Subj}}</a></td> <td>{{.Count}}</td> -<td>{{.Date}}</td> +<td>{{.Tail.Date | fdate}} {{.Tail.From}}</td> </tr> {{ end }} </table> diff --git a/ii-node/web.go b/ii-node/web.go @@ -15,7 +15,6 @@ type WebContext struct { Echoes []ii.Echo Topics []Topic Msg []ii.Msg - Render func(string) template.HTML Echo string Page int Pages int @@ -26,11 +25,17 @@ type WebContext struct { func www_index(www WWW, w http.ResponseWriter, r *http.Request) error { var ctx WebContext ii.Trace.Printf("www index") + ctx.Echoes = www.db.Echoes(nil) - // ctx.Msg = make([]*ii.Msg, len(ctx.Echoes)) - // for k, e := range ctx.Echoes { - // ctx.Msg[k] = db.Get(e.Last.Id) - // } + ctx.Msg = make([]ii.Msg, len(ctx.Echoes)) + www.db.LoadIndex() + www.db.Sync.RLock() + defer www.db.Sync.RUnlock() + for k, e := range ctx.Echoes { + if m := www.db.GetFast(e.Last.Id); m != nil { + ctx.Msg[k] = *m + } + } err := www.tpl.ExecuteTemplate(w, "index.tpl", ctx) return err } @@ -70,7 +75,6 @@ func getTopics(db *ii.DB, mi []*ii.MsgInfo) map[string][]string { type Topic struct { Ids []string Count int - Date string Last *ii.MsgInfo Head *ii.Msg Tail *ii.Msg @@ -104,6 +108,7 @@ func makePager(ctx *WebContext, count int, page int) int { func www_topics(www WWW, w http.ResponseWriter, r *http.Request, echo string, page int) error { db := www.db var ctx WebContext + mis := db.LookupIDS(db.SelectIDS(ii.Query{Echo: echo})) ii.Trace.Printf("www topics: %s", echo) topicsIds := getTopics(db, mis) @@ -140,7 +145,6 @@ func www_topics(www WWW, w http.ResponseWriter, r *http.Request, echo string, pa ii.Error.Printf("Skip wrong message: %s\n", t.Ids[0]) continue } - t.Date = time.Unix(t.Tail.Date, 0).Format("2006-01-02 15:04:05") ctx.Topics = append(ctx.Topics, *topics[i]) nr -- } @@ -148,16 +152,19 @@ func www_topics(www WWW, w http.ResponseWriter, r *http.Request, echo string, pa err := www.tpl.ExecuteTemplate(w, "topics.tpl", ctx) return err } + func msg_format(txt string) template.HTML { txt = strings.Replace(txt, "&", "&amp;", -1) txt = strings.Replace(txt, "<", "&lt;", -1) txt = strings.Replace(txt, ">", "&gt;", -1) return template.HTML(strings.Replace(txt, "\n", "<br/>", -1)) } + func www_topic(www WWW, w http.ResponseWriter, r *http.Request, id string, page int) error { db := www.db + var ctx WebContext - ctx.Render = msg_format + mi := db.Lookup(id) if mi == nil { return errors.New("No such message") @@ -182,25 +189,36 @@ func www_topic(www WWW, w http.ResponseWriter, r *http.Request, id string, page return err } -func Web(www WWW, w http.ResponseWriter, r *http.Request) error { +func WebInit(www *WWW, db *ii.DB) { + funcMap := template.FuncMap{ + "fdate": func (date int64) string { + return time.Unix(date, 0).Format("2006-01-02 15:04:05") + }, + "msg_format": msg_format, + } + www.db = db + www.tpl = template.Must(template.New("main").Funcs(funcMap).ParseGlob("tpl/*.tpl")) +} + +func handleWWW(www WWW, w http.ResponseWriter, r *http.Request) error { path := strings.TrimPrefix(r.URL.Path, "/") args := strings.Split(path, "/") if path == "" { return www_index(www, w, r) - } - if ii.IsMsgId(args[0]) { + } else if ii.IsMsgId(args[0]) { page := 1 if len(args) > 1 { fmt.Sscanf(args[1], "%d", &page) } return www_topic(www, w, r, args[0], page) - } - if ii.IsEcho(args[0]) { + } else if ii.IsEcho(args[0]) { page := 1 if len(args) > 1 { fmt.Sscanf(args[1], "%d", &page) } return www_topics(www, w, r, args[0], page) + } else { + w.WriteHeader(http.StatusNotFound) } return nil } diff --git a/ii/db.go b/ii/db.go @@ -454,7 +454,6 @@ func (db *DB) Echoes(names []string) []Echo { } sort.SliceStable(list, func(i, j int) bool { return list[i].Last.Off > list[j].Last.Off - // return list[i].Name < list[j].Name }) } return list