all repos — honk @ 1d045629c87909e2cf087471e989cfc89f518bab

my fork of honk

make it possible for filters to expire
Ted Unangst tedu@tedunangst.com
Mon, 21 Oct 2019 02:28:35 -0400
commit

1d045629c87909e2cf087471e989cfc89f518bab

parent

b162076c4d37bcff6770ff1ca9299b66d5752b2b

4 files changed, 53 insertions(+), 4 deletions(-)

jump to
M hfcs.gohfcs.go

@@ -43,6 +43,7 @@ Collapse bool `json:",omitempty"`

Rewrite string `json:",omitempty"` re_rewrite *regexp.Regexp Replace string `json:",omitempty"` + Expiration time.Time } type filtType uint

@@ -65,13 +66,24 @@ }

type afiltermap map[filtType][]*Filter -var filtcache = cache.New(cache.Options{Filler: func(userid int64) (afiltermap, bool) { +var filtcache *cache.Cache + +func init() { + // resolve init loop + filtcache = cache.New(cache.Options{Filler: filtcachefiller}) +} + +func filtcachefiller(userid int64) (afiltermap, bool) { rows, err := stmtGetFilters.Query(userid) if err != nil { log.Printf("error querying filters: %s", err) return nil, false } defer rows.Close() + + now := time.Now() + + var expflush time.Time filtmap := make(afiltermap) for rows.Next() {

@@ -86,6 +98,14 @@ if err != nil {

log.Printf("error scanning filter: %s", err) continue } + if !filt.Expiration.IsZero() { + if filt.Expiration.Before(now) { + continue + } + if expflush.IsZero() || filt.Expiration.Before(expflush) { + expflush = filt.Expiration + } + } if filt.Text != "" { filt.re_text, err = regexp.Compile("\\b(?i:" + filt.Text + ")\\b") if err != nil {

@@ -127,8 +147,17 @@ sorting := filtmap[filtAny]

sort.Slice(filtmap[filtAny], func(i, j int) bool { return sorting[i].Name < sorting[j].Name }) + if !expflush.IsZero() { + dur := expflush.Sub(now) + go filtcacheclear(userid, dur) + } return filtmap, true -}}) +} + +func filtcacheclear(userid int64, dur time.Duration) { + time.Sleep(dur + time.Second) + filtcache.Clear(userid) +} func getfilters(userid int64, scope filtType) []*Filter { var filtmap afiltermap
M honk.gohonk.go

@@ -20,6 +20,7 @@ "fmt"

"html/template" "log" "os" + "strconv" "strings" "time" )

@@ -117,6 +118,17 @@ if strings.HasSuffix(s, "h0m") {

s = s[:len(s)-2] } return s +} + +func parseDuration(s string) time.Duration { + didx := strings.IndexByte(s, 'd') + if didx != -1 { + days, _ := strconv.ParseInt(s[:didx], 10, 0) + dur, _ := time.ParseDuration(s[didx:]) + return dur + 24*time.Hour*time.Duration(days) + } + dur, _ := time.ParseDuration(s) + return dur } type Time struct {
M views/hfcs.htmlviews/hfcs.html

@@ -36,6 +36,10 @@ <input tabindex=1 type="text" name="filtrewrite" value="" autocomplete=off>

<p><label for="replace">replace:</label><br> <input tabindex=1 type="text" name="filtreplace" value="" autocomplete=off> <hr> +<h3>expiration</h3> +<p><label for="filtduration">duration:</label><br> +<input tabindex=1 type="text" name="filtduration" value="" autocomplete=off> +<hr> <p><button>impose your will</button> </form> </div>

@@ -49,6 +53,7 @@ {{ with .Text }}<p>Text: {{ . }}{{ end }}

<p>Actions: {{ range .Actions }} {{ . }} {{ end }} {{ with .Rewrite }}<p>Rewrite: {{ . }}{{ end }} {{ with .Replace }}<p>Replace: {{ . }}{{ end }} +{{ if not .Expiration.IsZero }}<p>Expiration: {{ .Expiration.Format "2006-01-02 03:04" }}{{ end }} <form action="/savehfcs" method="POST"> <input type="hidden" name="CSRF" value="{{ $csrf }}"> <input type="hidden" name="hfcsid" value="{{ .ID }}">
M web.goweb.go

@@ -1217,8 +1217,8 @@ break

} } timeend := r.FormValue("timeend") - dur, err := time.ParseDuration(timeend) - if err == nil { + dur := parseDuration(timeend) + if dur != 0 { t.Duration = Duration(dur) } if !t.StartTime.IsZero() {

@@ -1460,6 +1460,9 @@ filt.Hide = r.FormValue("dohide") == "yes"

filt.Collapse = r.FormValue("docollapse") == "yes" filt.Rewrite = strings.TrimSpace(r.FormValue("filtrewrite")) filt.Replace = strings.TrimSpace(r.FormValue("filtreplace")) + if dur := parseDuration(r.FormValue("filtduration")); dur > 0 { + filt.Expiration = time.Now().UTC().Add(dur) + } if filt.Actor == "" && filt.Text == "" { log.Printf("blank filter")