# HG changeset patch # User Anirudh Oppiliappan # Date 1668999810 21600 # Sun Nov 20 21:03:30 2022 -0600 # Node ID 0f50e74d1ff4c7858f96766bb929c26959548004 # Parent c1a34374d771b5e828a497bb0152a6d54fc81d62 Avatar fetching, maybe? Combination of two commits by Anirudh Oppiliappan (icyphox): https://git.icyphox.sh/honk/commit/?id=63d8b46546c5ee6a19685260f3776f737dcc4525 https://git.icyphox.sh/honk/commit/?id=82ac330b1482e6616421e65634b48b1270f072b5 (Plus some minor tweaking.) diff --git a/web.go b/web.go --- a/web.go +++ b/web.go @@ -2157,16 +2157,62 @@ return fmt.Sprintf("%d", secs) } +func isURL(s string) bool { + u, err := url.Parse(s) + return err == nil && u.Scheme != "" && u.Host != "" +} + +func avatateautogen(r *http.Request) []byte { + hex := r.FormValue("hex") == "1" + n := r.FormValue("a") + return genAvatar(n, hex) +} + func avatate(w http.ResponseWriter, r *http.Request) { if develMode { loadAvatarColors() } + var a []byte n := r.FormValue("a") - hex := r.FormValue("hex") == "1" - a := genAvatar(n, hex) + + if isURL(n) { + uinfo := login.GetUserInfo(r) + j, err := GetJunkFast(uinfo.UserID, n) + if err != nil { + dlog.Println("avatating: getting junk:", err) + a = avatateautogen(r) + goto nope + } + pfpurl, _ := j.GetString("icon", "url") + res, err := http.Get(pfpurl) + if res.StatusCode != 200 { + dlog.Printf("avatating: %s: not ok: %d", n, res.StatusCode) + a = avatateautogen(r) + goto nope + } + if err != nil { + dlog.Println("avatating: getting pfp url:", err) + a = avatateautogen(r) + goto nope + } + defer res.Body.Close() + + pfpbytes, err := io.ReadAll(res.Body) + if err != nil { + dlog.Println("avatating: io.ReadAll(), whoops:", err) + a = avatateautogen(r) + goto nope + } + a = pfpbytes + } else { + a = avatateautogen(r) + } + +nope: if !develMode { w.Header().Set("Cache-Control", "max-age="+somedays()) } + w.Write(a) }