openidec

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

commit 0983ce651806480bf31021dccedf7130b25b9d7b
parent 862cf71b9b5fa06788ee5f966862052fc2b68d3f
Author: Peter Kosyh <p.kosyh@gmail.com>
Date:   Sun,  6 Sep 2020 16:50:18 +0300

query

Diffstat:
Mii-node/lib/style.css | 9+++++++++
Mii-node/tpl/profile.tpl | 5+++--
Aii-node/tpl/query.tpl | 21+++++++++++++++++++++
Mii-node/tpl/topic.tpl | 2+-
Mii-node/web.go | 60+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Mii/db.go | 25+++++++++++--------------
6 files changed, 104 insertions(+), 18 deletions(-)

diff --git a/ii-node/lib/style.css b/ii-node/lib/style.css @@ -110,6 +110,11 @@ body { #topic .echo { font-weight: bold; } + +#topic .echo a, #topic .echo a:visited { + color: #555555; +} + #topic .msg { background-color: #ffffea; margin-bottom: 1em; @@ -165,6 +170,10 @@ body { font-size: smaller; } +#topic .info a { + color: #555555; +} + #header { margin: 0; margin-left:0; diff --git a/ii-node/tpl/profile.tpl b/ii-node/tpl/profile.tpl @@ -4,7 +4,8 @@ <tr class="odd"><td>Login:</td><td>{{.User.Name}}</td></tr> <tr class="even"><td>Auth:</td><td>{{.User.Secret}}</td></tr> <tr class="odd"><td>e-mail:</td><td>{{.User.Mail}}</td></tr> -<!-- <tr class="even"><td class="links" colspan="2"><a href="/logout">Logout</a> -</td></tr> --> +<tr class="even"><td>Addr:</td><td>{{.Selected}}</td></tr> +<tr class="odd"><td class="links" colspan="2"><a href="/to/{{.User.Name}}">/to/{{.User.Name}}</a> :: <a href="/from/{{.Selected}}">/from/{{.User.Name}}</a> +</td></tr> </table> {{template "footer.tpl"}} diff --git a/ii-node/tpl/query.tpl b/ii-node/tpl/query.tpl @@ -0,0 +1,21 @@ +{{template "header.tpl" $}} +{{template "pager.tpl" $}} +<div id="topic"> +{{ range .Msg }} +<div class="msg"> +<a class="msgid" href="/{{.MsgId}}#{{.MsgId}}">#</a><span class="subj"> <a href="/{{. | repto}}#{{. | repto}}">{{with .Subj}}{{.}}{{else}}No subject{{end}}</a></span><br> +<span class="echo"><a href="/{{.Echo}}">{{.Echo}}</a></span><br> +<span class="info">{{.From}}({{.Addr}}) &mdash; {{.To}}<br>{{.Date | fdate}}</span><br> +<div class="text"> +<br> +{{with .Text}} +{{. | msg_format}} +{{end}} +<br> +</div> +</div> +{{ end }} +</div> +{{template "pager.tpl" $}} + +{{template "footer.tpl"}} diff --git a/ii-node/tpl/topic.tpl b/ii-node/tpl/topic.tpl @@ -9,7 +9,7 @@ <div class="msg"> {{end}} <a class="msgid" href="/{{.MsgId}}#{{.MsgId}}">#</a><span class="subj"> <a href="/{{. | repto}}#{{. | repto}}">{{with .Subj}}{{.}}{{else}}No subject{{end}}</a></span><br> -<span class="info">{{.From}}({{.Addr}}) &mdash; {{.To}}<br>{{.Date | fdate}}</span><br> +<span class="info"><a href="/from/{{.Addr}}">{{.From}}</a>({{.Addr}}) &mdash; {{.To}}<br>{{.Date | fdate}}</span><br> <div class="text"> <br> {{with .Text}} diff --git a/ii-node/web.go b/ii-node/web.go @@ -101,6 +101,7 @@ func www_profile(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request) ii.Error.Printf("Access denied") return errors.New("Access denied") } + ctx.Selected = fmt.Sprintf("%s,%d", www.db.Name, user.Id) err := www.tpl.ExecuteTemplate(w, "profile.tpl", ctx) return err } @@ -206,6 +207,32 @@ func makePager(ctx *WebContext, count int, page int) int { return start } +func www_query(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, q ii.Query, page int, req string) error { + db := www.db + ctx := WebContext{User: user, Echolist: www.edb, Ref: r.Header.Get("Referer")} + mis := db.LookupIDS(db.SelectIDS(q)) + ii.Trace.Printf("www query") + + sort.SliceStable(mis, func(i, j int) bool { + return mis[i].Off > mis[j].Off + }) + ctx.BasePath = req + count := len(mis) + start := makePager(&ctx, count, page) + nr := PAGE_SIZE + for i := start; i < count && nr > 0; i++ { + m := db.GetFast(mis[i].Id) + if m == nil { + ii.Error.Printf("Can't get msg: %s\n", mis[i].Id) + continue + } + ctx.Msg = append(ctx.Msg, m) + nr-- + } + err := www.tpl.ExecuteTemplate(w, "query.tpl", ctx) + return err +} + func www_topics(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, echo string, page int) error { db := www.db ctx := WebContext{User: user, Echo: echo, Echolist: www.edb, Ref: r.Header.Get("Referer")} @@ -253,6 +280,7 @@ func www_topics(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, return err } + func www_topic(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, id string, page int) error { db := www.db ctx := WebContext{User: user, Echolist: www.edb, Ref: r.Header.Get("Referer")} @@ -588,7 +616,7 @@ func msg_format(txt string) template.HTML { func msg_access(www *WWW, m ii.Msg, u ii.User) bool { addr := fmt.Sprintf("%s,%d", www.db.Name, u.Id) - return addr == m.Addr + return addr == m.Addr || u.Id == 1 } func WebInit(www *WWW) { @@ -665,6 +693,36 @@ func _handleWWW(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request) return www_topic(user, www, w, r, args[0], page) } else if path == "new" { return www_new(user, www, w, r, "") + } else if args[0] == "to" { + page := 1 + if len(args) < 2 { + return errors.New("Wrong request") + } + if len(args) > 2 { + fmt.Sscanf(args[2], "%d", &page) + } + return www_query(user, www, w, r, ii.Query { To: args[1] }, + page, "to/" + args[1]) + } else if args[0] == "from" { + page := 1 + if len(args) < 2 { + return errors.New("Wrong request") + } + if len(args) > 2 { + fmt.Sscanf(args[2], "%d", &page) + } + return www_query(user, www, w, r, ii.Query { Addr: args[1] }, + page, "from/" + args[1]) + } else if args[0] == "echo" { + page := 1 + if len(args) < 2 { + return errors.New("Wrong request") + } + if len(args) > 2 { + fmt.Sscanf(args[2], "%d", &page) + } + return www_query(user, www, w, r, ii.Query { Echo: args[1] }, + page, "echo/" + args[1]) } else if ii.IsEcho(args[0]) { page := 1 if len(args) > 1 { diff --git a/ii/db.go b/ii/db.go @@ -22,6 +22,7 @@ type MsgInfo struct { To string Off int64 Repto string + Addr string } type Index struct { @@ -143,11 +144,8 @@ func (db *DB) _CreateIndex() error { return true } repto, _ := msg.Tag("repto") - if repto != "" { - repto = ":" + repto - } - fidx.WriteString(fmt.Sprintf("%s:%s:%d:%s%s\n", - msg.MsgId, msg.Echo, off, msg.To, repto)) + fidx.WriteString(fmt.Sprintf("%s:%s:%d:%s:%s:%s\n", + msg.MsgId, msg.Echo, off, msg.To, msg.Addr, repto)) off += int64(len(line) + 1) return true }) @@ -217,18 +215,16 @@ func (db *DB) LoadIndex() error { err = f_lines(file, func(line string) bool { linenr++ info := strings.Split(line, ":") - if len(info) < 4 { + if len(info) < 6 { err2 = errors.New("Wrong format on line:" + fmt.Sprintf("%d", linenr)) return false } - mi := MsgInfo{Id: info[0], Echo: info[1], To: info[3]} + mi := MsgInfo{Id: info[0], Echo: info[1], To: info[3], Addr: info[4] } if _, err := fmt.Sscanf(info[2], "%d", &mi.Off); err != nil { err2 = errors.New("Wrong offset on line: " + fmt.Sprintf("%d", linenr)) return false } - if len(info) > 4 { - mi.Repto = info[4] - } + mi.Repto = info[5] if _, ok := Idx.Hash[mi.Id]; !ok { // new msg Idx.List = append(Idx.List, mi.Id) } @@ -366,6 +362,7 @@ func (db *DB) GetFast(Id string) *Msg { type Query struct { Echo string Repto string + Addr string To string Start int Lim int @@ -392,6 +389,9 @@ func (db *DB) Match(info MsgInfo, r Query) bool { if r.To != "" && r.To != info.To { return false } + if r.Addr != "" && r.Addr != info.Addr { + return false + } return true } @@ -579,10 +579,7 @@ func (db *DB) _Store(m *Msg, edit bool) error { return err } - if repto != "" { - repto = ":" + repto - } - rec := fmt.Sprintf("%s:%s:%d:%s%s", m.MsgId, m.Echo, off, m.To, repto) + rec := fmt.Sprintf("%s:%s:%d:%s:%s:%s", m.MsgId, m.Echo, off, m.To, m.Addr, repto) if err := append_file(db.IndexPath(), rec); err != nil { return err }