all repos — honk @ 46d0becbc1dca5718961e1ef9c7a78467807483f

my fork of honk

masto_util.go (view raw)

  1package main
  2
  3import (
  4	"crypto/rand"
  5	"encoding/json"
  6	"fmt"
  7	"io"
  8	"net/http"
  9	"time"
 10
 11	"humungus.tedunangst.com/r/webs/junk"
 12)
 13
 14type NotResponseWriter struct {
 15	auth   string
 16	header http.Header
 17	status int
 18}
 19
 20func (w *NotResponseWriter) Header() http.Header {
 21	return w.header
 22}
 23
 24func (w *NotResponseWriter) Write(b []byte) (int, error) {
 25	dlog.Println("notresponsewriter: oauth code:", string(b))
 26	w.auth = string(b)
 27	return 0, nil
 28}
 29
 30func (w *NotResponseWriter) WriteHeader(statusCode int) {
 31	w.status = statusCode
 32}
 33
 34func snowflake() uint64 {
 35	ts := time.Now()
 36	return uint64(uint64(ts.UnixNano()/int64(time.Millisecond))<<16 | uint64(time.Now().Nanosecond()&0xffff))
 37}
 38
 39func badjunk(rw http.ResponseWriter, j junk.Junk, path string) {
 40	elog.Printf("%s: bad junk: %s", path, j.ToString())
 41	http.Error(rw, fmt.Sprintf("%s: bad junk", path), http.StatusBadRequest)
 42}
 43
 44func goodjunk(rw http.ResponseWriter, j junk.Junk) {
 45	rw.Header().Set("Content-Type", "application/json; charset=utf-8")
 46	rw.WriteHeader(http.StatusOK)
 47	rw.Write(j.ToBytes())
 48}
 49
 50func tokengen() string {
 51	b := make([]byte, 32)
 52	rand.Read(b)
 53	return fmt.Sprintf("%x", b)
 54}
 55
 56func listjunk(w io.Writer, j []junk.Junk) error {
 57	e := json.NewEncoder(w)
 58	e.SetEscapeHTML(false)
 59	e.SetIndent("", "  ")
 60	return e.Encode(j)
 61}
 62
 63func honktomasto(h *Honk) junk.Junk {
 64	j := junk.Junk{}
 65
 66	j["id"] = fmt.Sprintf("%d", h.ID)
 67	j["created_at"] = h.Date.Format("2006-01-02T15:04:05.000Z07:00")
 68	j["sensitive"] = false
 69	j["spoiler_text"] = ""
 70	j["visibility"] = "public"
 71	j["language"] = "en"
 72	j["uri"] = h.URL
 73	j["url"] = h.URL
 74	j["replies_count"] = len(h.Replies)
 75	j["reblogs_count"] = 0
 76	j["favourites_count"] = 0
 77	j["reblogged"] = h.IsBonked()
 78	j["favourited"] = false
 79	j["muted"] = false
 80	j["bookmarked"] = false
 81	j["content"] = h.HTML
 82	j["account"] = junk.Junk{
 83		"id":              "2019",
 84		"username":        h.Username,
 85		"acct":            h.Handle,
 86		"display_name":    h.Username,
 87		"avatar":          "https://h.icyphox.sh/a?a=" + h.Handle,
 88		"avatar_static":   "https://h.icyphox.sh/a?a=" + h.Handle,
 89		"header":          "https://h.icyphox.sh/meme/banner.jpg",
 90		"header_static":   "https://h.icyphox.sh/meme/banner.jpg",
 91		"note":            "foo bar baz",
 92		"url":             h.Honker,
 93		"emojis":          []junk.Junk{},
 94		"fields":          []junk.Junk{},
 95		"bot":             false,
 96		"last_status_at":  "",
 97		"discoverable":    false,
 98		"source":          nil,
 99		"created_at":      "",
100		"statuses_count":  0,
101		"indexable":       false,
102		"group":           false,
103		"locked":          false,
104		"followers_count": 0,
105		"following_count": 0,
106	}
107
108	j["card"] = nil
109	j["reblog"] = nil
110	j["media_attachments"] = []junk.Junk{}
111	j["tags"] = []junk.Junk{}
112	j["emojis"] = []junk.Junk{}
113	j["edited_at"] = nil
114	j["poll"] = nil
115	j["mentions"] = []junk.Junk{}
116	j["in_reply_to_id"] = nil
117	j["in_reply_to_account_id"] = nil
118
119	if h.Replies != nil {
120		for _, r := range h.Replies {
121			j["mentions"] = append(j["mentions"].([]junk.Junk), junk.Junk{
122				"id":       r.ID,
123				"url":      r.URL,
124				"username": r.Username,
125				"acct":     r.Handle,
126			})
127			j["in_reply_to_id"] = r.ID
128			j["in_reply_to_account_id"] = nil
129		}
130	}
131
132	return j
133}