openidec

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

commit 1fa4ba8131e76a5388cc51da708b3828e0136e2e
parent 449217ad0e6c2d36fcebea4d8d74c663bcc37228
Author: Peter Kosyh <p.kosyh@gmail.com>
Date:   Thu,  3 Sep 2020 17:49:36 +0300

sorting on index fix

Diffstat:
Mii-node/tpl/index.tpl | 10+++++-----
Mii-node/web.go | 19+++++--------------
Mii/db.go | 24+++++++++++++++++-------
3 files changed, 27 insertions(+), 26 deletions(-)

diff --git a/ii-node/tpl/index.tpl b/ii-node/tpl/index.tpl @@ -7,12 +7,12 @@ <th>Posts</th> <th>Last</th> </tr> -{{range $k, $v := .Echoes }} +{{range .Echoes }} <tr> -<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> +<td><a href="{{.Name}}/-1">{{.Name}}</a></td> +<td>{{.Topics}}</td> +<td>{{.Count}}</td> +<td>{{with .Msg}}{{.Date | fdate}}[{{.From}}] {{.Subj}}{{end}}</td> </tr> {{ end }} </table> diff --git a/ii-node/web.go b/ii-node/web.go @@ -12,9 +12,9 @@ import ( ) type WebContext struct { - Echoes []ii.Echo - Topics []Topic - Msg []ii.Msg + Echoes []*ii.Echo + Topics []*Topic + Msg []*ii.Msg Echo string Page int Pages int @@ -59,15 +59,6 @@ func www_index(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request) e ii.Trace.Printf("www index") ctx.Echoes = www.db.Echoes(nil) - 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 } @@ -177,7 +168,7 @@ func www_topics(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, ii.Error.Printf("Skip wrong message: %s\n", t.Ids[0]) continue } - ctx.Topics = append(ctx.Topics, *topics[i]) + ctx.Topics = append(ctx.Topics, topics[i]) nr -- } ii.Trace.Printf("Stop to generate topics") @@ -205,7 +196,7 @@ func www_topic(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, i ii.Error.Printf("Skip wrong message: %s", id) continue } - ctx.Msg = append(ctx.Msg, *m) + ctx.Msg = append(ctx.Msg, m) nr -- } ctx.BasePath = id diff --git a/ii/db.go b/ii/db.go @@ -392,14 +392,15 @@ type Echo struct { Count int Topics int Last MsgInfo + Msg *Msg } -func (db *DB) Echoes(names []string) []Echo { +func (db *DB) Echoes(names []string) []*Echo { db.Sync.Lock() defer db.Sync.Unlock() db.Lock() defer db.Unlock() - var list []Echo + var list []*Echo filter := make(map[string]bool) for _, n := range names { @@ -441,16 +442,25 @@ func (db *DB) Echoes(names []string) []Echo { } if names != nil { for _, v := range names { - list = append(list, hash[v]) + n := hash[v] + list = append(list, &n) } } else { for _, v := range hash { - list = append(list, v) + n := v + list = append(list, &n) } - sort.SliceStable(list, func(i, j int) bool { - return list[i].Last.Off < list[j].Last.Off - }) } + for _, v := range list { + v.Msg = db.GetFast(v.Last.Id) + if v.Msg == nil { + Error.Printf("Can not get echo last message: %s", v.Last.Id) + v.Msg = &Msg {} + } + } + sort.SliceStable(list, func(i, j int) bool { + return list[i].Msg.Date > list[j].Msg.Date + }) return list }