package main import ( "crypto/rand" "encoding/json" "fmt" "io" "net/http" "time" "humungus.tedunangst.com/r/webs/junk" ) type NotResponseWriter struct { auth string header http.Header status int } func (w *NotResponseWriter) Header() http.Header { return w.header } func (w *NotResponseWriter) Write(b []byte) (int, error) { dlog.Println("notresponsewriter: oauth code:", string(b)) w.auth = string(b) return 0, nil } func (w *NotResponseWriter) WriteHeader(statusCode int) { w.status = statusCode } func snowflake() uint64 { ts := time.Now() return uint64(uint64(ts.UnixNano()/int64(time.Millisecond))<<16 | uint64(time.Now().Nanosecond()&0xffff)) } func badjunk(rw http.ResponseWriter, j junk.Junk, path string) { elog.Printf("%s: bad junk: %s", path, j.ToString()) http.Error(rw, fmt.Sprintf("%s: bad junk", path), http.StatusBadRequest) } func goodjunk(rw http.ResponseWriter, j junk.Junk) { rw.Header().Set("Content-Type", "application/json; charset=utf-8") rw.WriteHeader(http.StatusOK) rw.Write(j.ToBytes()) } func tokengen() string { b := make([]byte, 32) rand.Read(b) return fmt.Sprintf("%x", b) } func listjunk(w io.Writer, j []junk.Junk) error { e := json.NewEncoder(w) e.SetEscapeHTML(false) e.SetIndent("", " ") return e.Encode(j) } func honktomasto(h *Honk) junk.Junk { j := junk.Junk{} j["id"] = fmt.Sprintf("%d", h.ID) j["created_at"] = h.Date.Format("2006-01-02T15:04:05.000Z07:00") j["sensitive"] = false j["spoiler_text"] = "" j["visibility"] = "public" j["language"] = "en" j["uri"] = h.URL j["url"] = h.URL j["replies_count"] = len(h.Replies) j["reblogs_count"] = 0 j["favourites_count"] = 0 j["reblogged"] = h.IsBonked() j["favourited"] = false j["muted"] = false j["bookmarked"] = false j["content"] = h.HTML j["account"] = junk.Junk{ "id": "2019", "username": h.Username, "acct": h.Handle, "display_name": h.Username, "avatar": "https://h.icyphox.sh/a?a=" + h.Handle, "avatar_static": "https://h.icyphox.sh/a?a=" + h.Handle, "header": "https://h.icyphox.sh/meme/banner.jpg", "header_static": "https://h.icyphox.sh/meme/banner.jpg", "note": "foo bar baz", "url": h.Honker, "emojis": []junk.Junk{}, "fields": []junk.Junk{}, "bot": false, "last_status_at": "", "discoverable": false, "source": nil, "created_at": "", "statuses_count": 0, "indexable": false, "group": false, "locked": false, "followers_count": 0, "following_count": 0, } j["card"] = nil j["reblog"] = nil j["media_attachments"] = []junk.Junk{} j["tags"] = []junk.Junk{} j["emojis"] = []junk.Junk{} j["edited_at"] = nil j["poll"] = nil j["mentions"] = []junk.Junk{} j["in_reply_to_id"] = nil j["in_reply_to_account_id"] = nil if h.Replies != nil { for _, r := range h.Replies { j["mentions"] = append(j["mentions"].([]junk.Junk), junk.Junk{ "id": r.ID, "url": r.URL, "username": r.Username, "acct": r.Handle, }) j["in_reply_to_id"] = r.ID j["in_reply_to_account_id"] = nil } } return j }