openidec

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

commit 0cdb3037414e2b0c0394ca82ef8423fa68d3f30b
parent 81a535ac2749b1bda0b6d93d51621d562b81a0b8
Author: Peter Kosyh <p.kosyh@gmail.com>
Date:   Sun,  6 Sep 2020 17:44:55 +0300

rss feeds

Diffstat:
Mii-node/tpl/profile.tpl | 2+-
Aii-node/tpl/rss.tpl | 18++++++++++++++++++
Mii-node/tpl/topic.tpl | 2+-
Mii-node/web.go | 43+++++++++++++++++++++++++++++++++----------
Mii/db.go | 12++++++------
5 files changed, 59 insertions(+), 18 deletions(-)

diff --git a/ii-node/tpl/profile.tpl b/ii-node/tpl/profile.tpl @@ -5,7 +5,7 @@ <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>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> +<tr class="odd"><td class="links" colspan="2"><a href="/to/{{.User.Name}}">/to/{{.User.Name}}</a> :: <a href="/from/{{.User.Name}}">/from/{{.User.Name}}</a> </td></tr> </table> {{template "footer.tpl"}} diff --git a/ii-node/tpl/rss.tpl b/ii-node/tpl/rss.tpl @@ -0,0 +1,18 @@ +<rss version="2.0"> +<channel> +<title>{{.Topic}}</title> +<description>RSS feed with last messages</description> +<link>http://127.0.0.1:8080</link> + +{{ range .Msg }} +<item> + <title>{{.Subj}}</title> + <guid>{{.MsgId}}</guid> + <link>http://127.0.0.1:8080/{{.MsgId}}#{{.MsgId}}</link> + <pubDate>{{.Date | fdate }}</pubDate> + <description>{{.Text}}</description> + <author>{{.From}}</author> +</item> +{{ end }} +</channel> +</rss> 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"><a href="/from/{{.Addr}}">{{.From}}</a>({{.Addr}}) &mdash; {{.To}}<br>{{.Date | fdate}}</span><br> +<span class="info"><a href="/from/{{.From}}">{{.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 @@ -160,7 +160,7 @@ 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 { +func www_query(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, q ii.Query, page int, req string, rss bool) error { db := www.db ctx := WebContext{User: user, Echolist: www.edb, Ref: r.Header.Get("Referer")} mis := db.LookupIDS(db.SelectIDS(q)) @@ -171,6 +171,9 @@ func www_query(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, q }) ctx.BasePath = req count := len(mis) + if rss { + count = 100 + } start := makePager(&ctx, count, page) nr := PAGE_SIZE for i := start; i < count && nr > 0; i++ { @@ -182,8 +185,13 @@ func www_query(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, q ctx.Msg = append(ctx.Msg, m) nr-- } - err := www.tpl.ExecuteTemplate(w, "query.tpl", ctx) - return err + if rss { + ctx.Topic = db.Name + " :: " + req + /* yes, we should use text/template here, but it will be longer to do */ + fmt.Fprintf(w, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") + return www.tpl.ExecuteTemplate(w, "rss.tpl", ctx) + } + return www.tpl.ExecuteTemplate(w, "query.tpl", ctx) } func www_topics(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request, echo string, page int) error { @@ -648,34 +656,49 @@ func _handleWWW(user *ii.User, www WWW, w http.ResponseWriter, r *http.Request) return www_new(user, www, w, r, "") } else if args[0] == "to" { page := 1 + rss := false if len(args) < 2 { return errors.New("Wrong request") } if len(args) > 2 { - fmt.Sscanf(args[2], "%d", &page) + if args[2] == "rss" { + rss = true + } else { + fmt.Sscanf(args[2], "%d", &page) + } } return www_query(user, www, w, r, ii.Query { To: args[1] }, - page, "to/" + args[1]) + page, "to/" + args[1], rss) } else if args[0] == "from" { page := 1 + rss := false if len(args) < 2 { return errors.New("Wrong request") } if len(args) > 2 { - fmt.Sscanf(args[2], "%d", &page) + if args[2] == "rss" { + rss = true + } else { + fmt.Sscanf(args[2], "%d", &page) + } } - return www_query(user, www, w, r, ii.Query { Addr: args[1] }, - page, "from/" + args[1]) + return www_query(user, www, w, r, ii.Query { From: args[1] }, + page, "from/" + args[1], rss) } else if args[0] == "echo" { page := 1 + rss := false if len(args) < 2 { return errors.New("Wrong request") } if len(args) > 2 { - fmt.Sscanf(args[2], "%d", &page) + if args[2] == "rss" { + rss = true + } else { + fmt.Sscanf(args[2], "%d", &page) + } } return www_query(user, www, w, r, ii.Query { Echo: args[1] }, - page, "echo/" + args[1]) + page, "echo/" + args[1], rss) } else if ii.IsEcho(args[0]) { page := 1 if len(args) > 1 { diff --git a/ii/db.go b/ii/db.go @@ -22,7 +22,7 @@ type MsgInfo struct { To string Off int64 Repto string - Addr string + From string } type Index struct { @@ -145,7 +145,7 @@ func (db *DB) _CreateIndex() error { } repto, _ := msg.Tag("repto") fidx.WriteString(fmt.Sprintf("%s:%s:%d:%s:%s:%s\n", - msg.MsgId, msg.Echo, off, msg.To, msg.Addr, repto)) + msg.MsgId, msg.Echo, off, msg.To, msg.From, repto)) off += int64(len(line) + 1) return true }) @@ -219,7 +219,7 @@ func (db *DB) LoadIndex() error { err2 = errors.New("Wrong format on line:" + fmt.Sprintf("%d", linenr)) return false } - mi := MsgInfo{Id: info[0], Echo: info[1], To: info[3], Addr: info[4] } + mi := MsgInfo{Id: info[0], Echo: info[1], To: info[3], From: 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 @@ -365,7 +365,7 @@ func (db *DB) GetFast(Id string) *Msg { type Query struct { Echo string Repto string - Addr string + From string To string Start int Lim int @@ -392,7 +392,7 @@ 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 { + if r.From != "" && r.From != info.From { return false } return true @@ -625,7 +625,7 @@ func (db *DB) _Store(m *Msg, edit bool) error { return err } - rec := fmt.Sprintf("%s:%s:%d:%s:%s:%s", m.MsgId, m.Echo, off, m.To, m.Addr, repto) + rec := fmt.Sprintf("%s:%s:%d:%s:%s:%s", m.MsgId, m.Echo, off, m.To, m.From, repto) if err := append_file(db.IndexPath(), rec); err != nil { return err }