all repos — honk @ 5f108eceda00a5c93368d6218beab5b2c157441c

my fork of honk

bloat.go (view raw)

  1//
  2// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
  3//
  4// Permission to use, copy, modify, and distribute this software for any
  5// purpose with or without fee is hereby granted, provided that the above
  6// copyright notice and this permission notice appear in all copies.
  7//
  8// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 10// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 11// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 12// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 15
 16package main
 17
 18import (
 19	"bytes"
 20	"fmt"
 21	"log"
 22	"strings"
 23	"sync"
 24)
 25
 26var bloat_mtx sync.Mutex
 27
 28func bloat_counterplusone(s string) string {
 29	bloat_mtx.Lock()
 30	defer bloat_mtx.Unlock()
 31
 32	var bloat_counter int
 33	getconfig("bloat_counter", &bloat_counter)
 34
 35	if bloat_counter < 9001 {
 36		bloat_counter++
 37		saveconfig("bloat_counter", bloat_counter)
 38	}
 39	// 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th
 40	suf := "th"
 41	switch bloat_counter % 10 {
 42	case 1:
 43		suf = "st"
 44	case 2:
 45		suf = "nd"
 46	case 3:
 47		suf = "rd"
 48	}
 49	if bloat_counter == 11 || bloat_counter == 12 || bloat_counter == 13 {
 50		suf = "th"
 51	}
 52	val := fmt.Sprintf("%d%s", bloat_counter, suf)
 53	log.Printf("now producing %s counter", val)
 54	s = strings.Replace(s, "&lt;bloat_counter&gt;", val, -1)
 55	return s
 56}
 57
 58func bloat_counterfixhonk(honk *Honk) {
 59	honk.Noise = bloat_counterplusone(honk.Noise)
 60}
 61
 62func bloat_counterhtml(honk *Honk) {
 63	honk.Noise = strings.Replace(honk.Noise, "&lt;bloat_counter&gt;", "1st", -1)
 64}
 65
 66func bloat_counterannounce(user *WhatAbout, honk *Honk) {
 67	rcpts := make(map[string]bool)
 68	for _, a := range honk.Audience {
 69		if a != thewholeworld && a != user.URL && !strings.HasSuffix(a, "/followers") {
 70			box, _ := getboxes(a)
 71			if box != nil && honk.Public && box.Shared != "" {
 72				rcpts["%"+box.Shared] = true
 73			} else {
 74				rcpts[a] = true
 75			}
 76		}
 77	}
 78	if honk.Public {
 79		for _, f := range getdubs(user.ID) {
 80			box, _ := getboxes(f.XID)
 81			if box != nil && box.Shared != "" {
 82				rcpts["%"+box.Shared] = true
 83			} else {
 84				rcpts[f.XID] = true
 85			}
 86		}
 87	}
 88	orignoise := honk.Noise
 89	for a := range rcpts {
 90		honk.Noise = orignoise
 91		bloat_counterfixhonk(honk)
 92		jonk, _ := jonkjonk(user, honk)
 93		jonk["@context"] = itiswhatitis
 94		var buf bytes.Buffer
 95		jonk.Write(&buf)
 96		msg := buf.Bytes()
 97		go deliverate(0, user.Name, a, msg)
 98	}
 99}
100
101func bloat_iscounter(honk *Honk) bool {
102	return strings.Contains(honk.Noise, "&lt;bloat_counter&gt;")
103}
104