From 21ca139a310778a4ac87864eb519afa25e3061c0 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Thu, 15 Aug 2019 09:25:36 -0400 Subject: [PATCH] bloat: counter.cgi --- bloat.go | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ fun.go | 3 ++ honk.go | 12 ++++++- util.go | 5 +++ 4 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 bloat.go diff --git a/bloat.go b/bloat.go new file mode 100644 index 0000000..6460b26 --- /dev/null +++ b/bloat.go @@ -0,0 +1,102 @@ +// +// Copyright (c) 2019 Ted Unangst +// +// 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>") +} + diff --git a/fun.go b/fun.go index 1521259..54ceda8 100644 --- a/fun.go +++ b/fun.go @@ -48,6 +48,9 @@ func reverbolate(userid int64, honks []*Honk) { h.URL = h.XID if h.What != "bonked" { h.Noise = mentionize(h.Noise) + if bloat_iscounter(h) { + bloat_counterhtml(h) + } } h.Username, h.Handle = honkerhandle(h.Honker) } else { diff --git a/honk.go b/honk.go index de443ec..96a30e4 100644 --- a/honk.go +++ b/honk.go @@ -506,6 +506,9 @@ func outbox(w http.ResponseWriter, r *http.Request) { 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 @@ func showhonk(w http.ResponseWriter, r *http.Request) { } 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 @@ func savehonk(w http.ResponseWriter, r *http.Request) { } } - 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) } diff --git a/util.go b/util.go index 2a509a3..429d4e1 100644 --- a/util.go +++ b/util.go @@ -286,6 +286,11 @@ func getconfig(key string, value interface{}) error { 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)