commit fd4fb4049fa7a27497a672ae9bd6d66569aa1391
parent 6f9a54291bb570624c6316679c3d4a312be34986
Author: Peter Kosyh <p.kosyh@gmail.com>
Date: Tue, 8 Sep 2020 14:16:17 +0300
private messages
Diffstat:
4 files changed, 46 insertions(+), 11 deletions(-)
diff --git a/ii-node/main.go b/ii-node/main.go
@@ -87,9 +87,11 @@ func main() {
http.Handle("/lib/", http.StripPrefix("/lib/", fs))
http.HandleFunc("/list.txt", func(w http.ResponseWriter, r *http.Request) {
- echoes := db.Echoes(nil)
+ echoes := db.Echoes(nil, ii.Query {})
for _, v := range echoes {
- fmt.Fprintf(w, "%s:%d:%s\n", v.Name, v.Count, www.edb.Info[v.Name])
+ if !ii.IsPrivate(v.Name) {
+ fmt.Fprintf(w, "%s:%d:%s\n", v.Name, v.Count, www.edb.Info[v.Name])
+ }
}
})
http.HandleFunc("/blacklist.txt", func(w http.ResponseWriter, r *http.Request) {
@@ -135,9 +137,11 @@ func main() {
})
http.HandleFunc("/x/c/", func(w http.ResponseWriter, r *http.Request) {
enames := strings.Split(r.URL.Path[5:], "/")
- echoes := db.Echoes(enames)
+ echoes := db.Echoes(enames, ii.Query {})
for _, v := range echoes {
- fmt.Fprintf(w, "%s:%d:\n", v.Name, v.Count)
+ if !ii.IsPrivate(v.Name) {
+ fmt.Fprintf(w, "%s:%d:\n", v.Name, v.Count)
+ }
}
})
http.HandleFunc("/u/m/", func(w http.ResponseWriter, r *http.Request) {
@@ -163,7 +167,7 @@ func main() {
}
for _, e := range echoes {
- if !ii.IsEcho(e) {
+ if !ii.IsEcho(e) || ii.IsPrivate(e) {
continue
}
fmt.Fprintf(w, "%s\n", e)
@@ -186,7 +190,7 @@ func main() {
})
http.HandleFunc("/e/", func(w http.ResponseWriter, r *http.Request) {
e := r.URL.Path[3:]
- if !ii.IsEcho(e) {
+ if !ii.IsEcho(e) || ii.IsPrivate(e) {
return
}
ids := db.SelectIDS(ii.Query{Echo: e})
diff --git a/ii-node/web.go b/ii-node/web.go
@@ -127,7 +127,7 @@ func www_logout(ctx *WebContext, w http.ResponseWriter, r *http.Request) error {
func www_index(ctx *WebContext, w http.ResponseWriter, r *http.Request) error {
ii.Trace.Printf("www index")
- ctx.Echoes = ctx.www.db.Echoes(nil)
+ ctx.Echoes = ctx.www.db.Echoes(nil, ii.Query { User: *ctx.User })
err := ctx.www.tpl.ExecuteTemplate(w, "index.tpl", ctx)
return err
}
@@ -314,6 +314,17 @@ func makePager(ctx *WebContext, count int, page int) int {
return start
}
+func Select(ctx *WebContext, q ii.Query) []string {
+ if ii.IsPrivate(q.Echo) { /* private */
+ if ctx.User.Name == "" {
+ var l []string
+ return l
+ }
+ q.User = *ctx.User
+ }
+ return ctx.www.db.SelectIDS(q)
+}
+
func www_query(ctx *WebContext, w http.ResponseWriter, r *http.Request, q ii.Query, page int, rss bool) error {
db := ctx.www.db
req := ctx.BasePath
@@ -321,7 +332,7 @@ func www_query(ctx *WebContext, w http.ResponseWriter, r *http.Request, q ii.Que
if rss {
q.Start = -100
}
- mis := db.LookupIDS(db.SelectIDS(q))
+ mis := db.LookupIDS(Select(ctx, q))
ii.Trace.Printf("www query")
sort.SliceStable(mis, func(i, j int) bool {
@@ -380,7 +391,7 @@ func www_topics(ctx *WebContext, w http.ResponseWriter, r *http.Request, page i
db := ctx.www.db
echo := ctx.BasePath
ctx.Echo = echo
- mis := db.LookupIDS(db.SelectIDS(ii.Query{Echo: echo}))
+ mis := db.LookupIDS(Select(ctx, ii.Query{Echo: echo}))
ii.Trace.Printf("www topics: %s", echo)
topicsIds := db.GetTopics(mis)
var topics []*Topic
@@ -437,7 +448,7 @@ func www_topic(ctx *WebContext, w http.ResponseWriter, r *http.Request, page int
ctx.Selected = id
}
ctx.Echo = mi.Echo
- mis := db.LookupIDS(db.SelectIDS(ii.Query{Echo: mi.Echo}))
+ mis := db.LookupIDS(Select(ctx, ii.Query{Echo: mi.Echo}))
topic := mi.Id
for p := mi; p != nil; p = db.LookupFast(p.Repto, false) {
diff --git a/ii/db.go b/ii/db.go
@@ -370,6 +370,8 @@ type Query struct {
Start int
Lim int
Blacklisted bool
+ User User
+ Match func(mi MsgInfo, q Query) bool
}
func prependStr(x []string, y string) []string {
@@ -383,6 +385,9 @@ func (db *DB) Match(info MsgInfo, r Query) bool {
if r.Blacklisted {
return info.Off < 0
}
+ if r.Match != nil {
+ return r.Match(info, r)
+ }
if r.Echo != "" && r.Echo != info.Echo {
return false
}
@@ -395,6 +400,14 @@ func (db *DB) Match(info MsgInfo, r Query) bool {
if r.From != "" && r.From != info.From {
return false
}
+ if IsPrivate(info.Echo) {
+ if r.User.Name == "" {
+ return false
+ }
+ if info.To != "All" && info.From != r.User.Name && info.To != r.User.Name {
+ return false
+ }
+ }
return true
}
@@ -406,7 +419,7 @@ type Echo struct {
Msg *Msg
}
-func (db *DB) Echoes(names []string) []*Echo {
+func (db *DB) Echoes(names []string, q Query) []*Echo {
db.Sync.Lock()
defer db.Sync.Unlock()
db.Lock()
@@ -433,6 +446,9 @@ func (db *DB) Echoes(names []string) []*Echo {
if info.Off < 0 {
continue
}
+ if !db.Match(info, q) {
+ continue
+ }
e := info.Echo
if names != nil { // filter?
if _, ok := filter[e]; !ok {
diff --git a/ii/msg.go b/ii/msg.go
@@ -38,6 +38,10 @@ func IsMsgId(id string) bool {
return len(id) == 20 && !strings.Contains(id, ".")
}
+func IsPrivate(e string) bool {
+ return strings.HasPrefix(e, ".")
+}
+
func IsEcho(e string) bool {
l := len(e)
return l >= 3 && l <= 120 && strings.Contains(e, ".") && !strings.Contains(e, ":")