better handle AP usernames. don't require manual entry

This commit is contained in:
Ted Unangst 2019-08-15 23:27:55 -04:00
parent 988f16aca8
commit 617cdd5fd8
5 changed files with 40 additions and 21 deletions

View File

@ -990,26 +990,28 @@ func isactor(t string) bool {
return true
}
func investigate(name string) string {
func investigate(name string) *Honker {
if name == "" {
return ""
return nil
}
if name[0] == '@' {
name = gofish(name)
}
if name == "" {
return ""
return nil
}
log.Printf("digging up some info on %s", name)
obj, err := GetJunkFast(name)
if err != nil {
log.Printf("error investigating honker: %s", err)
return ""
return nil
}
t, _ := obj.GetString("type")
if !isactor(t) {
log.Printf("it's not a person! %s", name)
return ""
return nil
}
id, _ := obj.GetString("id")
return id
xid, _ := obj.GetString("id")
handle, _ := obj.GetString("preferredUsername")
return &Honker{XID: xid, Handle: handle}
}

View File

@ -2,6 +2,8 @@ changelog
-- next
+ Better understanding of ActivityPub Persons. No need to enter names!
+ More robust retries for fetching objects.
+ Don't decode excessively large images and run out of memory.

25
fun.go
View File

@ -52,9 +52,9 @@ func reverbolate(userid int64, honks []*Honk) {
bloat_counterhtml(h)
}
}
h.Username, h.Handle = honkerhandle(h.Honker)
h.Username, h.Handle = handles(h.Honker)
} else {
_, h.Handle = honkerhandle(h.Honker)
_, h.Handle = handles(h.Honker)
h.Username = h.Handle
if len(h.Username) > 20 {
h.Username = h.Username[:20] + ".."
@ -64,7 +64,7 @@ func reverbolate(userid int64, honks []*Honk) {
}
}
if h.Oonker != "" {
_, h.Oondle = honkerhandle(h.Oonker)
_, h.Oondle = handles(h.Oonker)
}
zap := make(map[*Donk]bool)
h.Noise = unpucker(h.Noise)
@ -374,12 +374,21 @@ func originate(u string) string {
return ""
}
func honkerhandle(h string) (string, string) {
m := re_unurl.FindStringSubmatch(h)
if len(m) > 2 {
return m[2], fmt.Sprintf("%s@%s", m[2], m[1])
// handle, handle@host
func handles(xid string) (string, string) {
row := stmtGetXonker.QueryRow(xid, "handle")
var handle string
err := row.Scan(&handle)
if err == nil {
return handle, handle + "@" + originate(xid)
}
return h, h
p := investigate(xid)
if p == nil {
return xid, xid
}
handle = p.Handle
stmtSaveXonker.Exec(xid, handle, "handle")
return handle, handle + "@" + originate(xid)
}
func prepend(s string, x []string) []string {

16
honk.go
View File

@ -92,6 +92,7 @@ type Honker struct {
UserID int64
Name string
XID string
Handle string
Flavor string
Combos []string
}
@ -436,9 +437,9 @@ func inbox(w http.ResponseWriter, r *http.Request) {
func ximport(w http.ResponseWriter, r *http.Request) {
xid := r.FormValue("xid")
x := investigate(xid)
if x != "" {
xid = x
p := investigate(xid)
if p != nil {
xid = p.XID
}
j, err := GetJunk(xid)
if err != nil {
@ -1232,10 +1233,15 @@ func savehonker(w http.ResponseWriter, r *http.Request) {
if peep == "peep" {
flavor = "peep"
}
url = investigate(url)
if url == "" {
p := investigate(url)
if p == nil {
log.Printf("failed to investigate honker")
return
}
url = p.XID
if name == "" {
name = p.Handle
}
_, err := stmtSaveHonker.Exec(u.UserID, name, url, flavor, combos)
if err != nil {
log.Print(err)

View File

@ -5,8 +5,8 @@
<form action="/savehonker" method="POST">
<span class="title">add new honker</span>
<input type="hidden" name="CSRF" value="{{ .HonkerCSRF }}">
<p><input tabindex=1 type="text" name="name" value="" autocomplete=off> - name
<p><input tabindex=1 type="text" name="url" value="" autocomplete=off> - url
<p><input tabindex=1 type="text" name="name" value="" placeholder="optional" autocomplete=off> - name
<p><span><label for="peep">just peeping:</label>
<input tabindex=1 type="checkbox" id="peep" name="peep" value="peep"><span></span></span>
<p><input tabindex=1 type="submit" name="add honker" value="add honker">