bloat: counter.cgi
Ted Unangst tedu@tedunangst.com
Thu, 15 Aug 2019 09:25:36 -0400
A
bloat.go
@@ -0,0 +1,102 @@
+// +// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com> +// +// Permission to use, copy, modify, and distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +package main + +import ( + "bytes" + "fmt" + "log" + "strings" + "sync" +) + +var bloat_mtx sync.Mutex + +func bloat_counterplusone(s string) string { + bloat_mtx.Lock() + defer bloat_mtx.Unlock() + + var bloat_counter int + getconfig("bloat_counter", &bloat_counter) + + if bloat_counter < 9001 { + bloat_counter++ + saveconfig("bloat_counter", bloat_counter) + } + // 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th + suf := "th" + switch bloat_counter % 10 { + case 1: + suf = "st" + case 2: + suf = "nd" + case 3: + suf = "rd" + } + if bloat_counter == 11 || bloat_counter == 12 || bloat_counter == 13 { + suf = "th" + } + val := fmt.Sprintf("%d%s", bloat_counter, suf) + log.Printf("now producing %s counter", val) + s = strings.Replace(s, "<bloat_counter>", val, -1) + return s +} + +func bloat_counterfixhonk(honk *Honk) { + honk.Noise = bloat_counterplusone(honk.Noise) +} + +func bloat_counterhtml(honk *Honk) { + honk.Noise = strings.Replace(honk.Noise, "<bloat_counter>", "1st", -1) +} + +func bloat_counterannounce(user *WhatAbout, honk *Honk) { + rcpts := make(map[string]bool) + for _, a := range honk.Audience { + if a != thewholeworld && a != user.URL && !strings.HasSuffix(a, "/followers") { + box, _ := getboxes(a) + if box != nil && honk.Public && box.Shared != "" { + rcpts["%"+box.Shared] = true + } else { + rcpts[a] = true + } + } + } + if honk.Public { + for _, f := range getdubs(user.ID) { + box, _ := getboxes(f.XID) + if box != nil && box.Shared != "" { + rcpts["%"+box.Shared] = true + } else { + rcpts[f.XID] = true + } + } + } + for a := range rcpts { + bloat_counterfixhonk(honk) + jonk, _ := jonkjonk(user, honk) + jonk["@context"] = itiswhatitis + var buf bytes.Buffer + jonk.Write(&buf) + msg := buf.Bytes() + go deliverate(0, user.Name, a, msg) + } +} + +func bloat_iscounter(honk *Honk) bool { + return strings.Contains(honk.Noise, "<bloat_counter>") +} +
M
honk.go
→
honk.go
@@ -506,6 +506,9 @@ honks := gethonksbyuser(name, false)
var jonks []junk.Junk for _, h := range honks { + if bloat_iscounter(h) { + continue + } j, _ := jonkjonk(user, h) jonks = append(jonks, j) }@@ -624,6 +627,9 @@ return
} if friendorfoe(r.Header.Get("Accept")) { donksforhonks([]*Honk{h}) + if bloat_iscounter(h) { + bloat_counterfixhonk(h) + } _, j := jonkjonk(user, h) j["@context"] = itiswhatitis w.Header().Set("Content-Type", theonetruename)@@ -1139,7 +1145,11 @@ return
} } - go honkworldwide(user, &honk) + if bloat_iscounter(&honk) { + go bloat_counterannounce(user, &honk) + } else { + go honkworldwide(user, &honk) + } http.Redirect(w, r, xid, http.StatusSeeOther) }
M
util.go
→
util.go
@@ -286,6 +286,11 @@ }
return err } +func saveconfig(key string, val interface{}) { + db := opendatabase() + db.Exec("update config set value = ? where key = ?", val, key) +} + func openListener() (net.Listener, error) { var listenAddr string err := getconfig("listenaddr", &listenAddr)