clean up the one or many arrays a little
Ted Unangst tedu@tedunangst.com
Sun, 30 Jul 2023 13:08:53 -0400
1 files changed,
17 insertions(+),
24 deletions(-)
jump to
M
activity.go
→
activity.go
@@ -468,19 +468,12 @@ return a
} func extractattrto(obj junk.Junk) string { - who, _ := obj.GetString("attributedTo") - if who != "" { - return who - } - o, ok := obj.GetMap("attributedTo") - if ok { - id, ok := o.GetString("id") + arr := oneforall(obj, "attributedTo") + for _, a := range arr { + s, ok := a.(string) if ok { - return id + return s } - } - arr, _ := obj.GetArray("attributedTo") - for _, a := range arr { o, ok := a.(junk.Junk) if ok { t, _ := o.GetString("type")@@ -489,12 +482,19 @@ if t == "Person" || t == "" {
return id } } - s, ok := a.(string) - if ok { - return s - } } return "" +} + +func oneforall(obj junk.Junk, key string) []interface{} { + if val, ok := obj.GetMap(key); ok { + return []interface{}{val} + } + if str, ok := obj.GetString(key); ok { + return []interface{}{str} + } + arr, _ := obj.GetArray(key) + return arr } func firstofmany(obj junk.Junk, key string) string {@@ -958,16 +958,13 @@ preferorig = false
} } if !preferorig { - atts, _ := obj.GetArray("attachment") + atts := oneforall(obj, "attachment") for _, atti := range atts { att, ok := atti.(junk.Junk) if !ok { ilog.Printf("attachment that wasn't map?") continue } - procatt(att) - } - if att, ok := obj.GetMap("attachment"); ok { procatt(att) } }@@ -1016,16 +1013,12 @@ m.Where, _ = tag.GetString("href")
mentions = append(mentions, m) } } - tags, _ := obj.GetArray("tag") + tags := oneforall(obj, "tag") for _, tagi := range tags { tag, ok := tagi.(junk.Junk) if !ok { continue } - proctag(tag) - } - tag, ok := obj.GetMap("tag") - if ok { proctag(tag) } if starttime, ok := obj.GetString("startTime"); ok {