an attempt at opengraph

This commit is contained in:
Ted Unangst 2023-06-13 14:58:35 -04:00
parent 712873418b
commit e20c344f11
4 changed files with 45 additions and 10 deletions

View File

@ -25,7 +25,6 @@ import (
"io"
notrand "math/rand"
"net/http"
"net/url"
"os"
"regexp"
"strings"
@ -1602,12 +1601,7 @@ func junkuser(user *WhatAbout) junk.Junk {
a := junk.New()
a["type"] = "Image"
a["mediaType"] = "image/png"
if ava := user.Options.Avatar; ava != "" {
a["url"] = ava
} else {
u := fmt.Sprintf("https://%s/a?a=%s", serverName, url.QueryEscape(user.URL))
a["url"] = u
}
a["url"] = avatarURL(user)
j["icon"] = a
if ban := user.Options.Banner; ban != "" {
a := junk.New()

View File

@ -23,6 +23,7 @@ import (
"image"
"image/png"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
@ -103,6 +104,13 @@ func genAvatar(name string) []byte {
return buf.Bytes()
}
func avatarURL(user *WhatAbout) string {
if ava := user.Options.Avatar; ava != "" {
return ava
}
return fmt.Sprintf("https://%s/a?a=%s", serverName, url.QueryEscape(user.URL))
}
func showflag(writer http.ResponseWriter, req *http.Request) {
code := mux.Vars(req)["code"]
colors := strings.Split(code, ",")

View File

@ -7,6 +7,7 @@
<link href="/local.css{{ .LocalStyleParam }}" rel="stylesheet">
{{ end }}
{{ .APAltLink }}
{{ .Honkology }}
<link href="/icon.png" rel="icon">
<meta name="theme-color" content="#305">
<meta name="viewport" content="width=device-width">

36
web.go
View File

@ -1017,6 +1017,34 @@ func trackback(xid string, r *http.Request) {
}
}
func honkology(honk *Honk) template.HTML {
var user *WhatAbout
ok := somenumberedusers.Get(honk.UserID, &user)
if !ok {
return ""
}
title := fmt.Sprintf("%s: %s", user.Display, honk.Precis)
imgurl := avatarURL(user)
for _, d := range honk.Donks {
if d.Local && strings.HasPrefix(d.Media, "image") {
imgurl = d.URL
break
}
}
short := honk.Noise
if len(short) > 160 {
short = short[0:160] + "..."
}
return templates.Sprintf(
`<meta property="og:title" content="%s" />
<meta property="og:type" content="article" />
<meta property="article:author" content="%s" />
<meta property="og:url" content="%s" />
<meta property="og:image" content="%s" />
<meta property="og:description" content = "%s" />`,
title, user.URL, honk.XID, imgurl, short)
}
func showonehonk(w http.ResponseWriter, r *http.Request) {
name := mux.Vars(r)["name"]
user, err := butwhatabout(name)
@ -1064,19 +1092,23 @@ func showonehonk(w http.ResponseWriter, r *http.Request) {
honkpage(w, u, honks, templinfo)
return
}
templinfo := getInfo(r)
rawhonks := gethonksbyconvoy(honk.UserID, honk.Convoy, 0)
reversehonks(rawhonks)
var honks []*Honk
for _, h := range rawhonks {
if h.XID == xid && len(honks) != 0 {
if h.XID == xid {
templinfo["Honkology"] = honkology(h)
if len(honks) != 0 {
h.Style += " glow"
}
}
if h.Public && (h.Whofore == 2 || h.IsAcked()) {
honks = append(honks, h)
}
}
templinfo := getInfo(r)
templinfo["ServerMessage"] = "one honk maybe more"
templinfo["HonkCSRF"] = login.GetCSRF("honkhonk", r)
templinfo["APAltLink"] = templates.Sprintf("<link href='%s' rel='alternate' type='application/activity+json'>", xid)