From bed7d662bba221788cd625d036303ea4b35c2e65 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Sun, 30 Jul 2023 13:08:53 -0400 Subject: [PATCH] clean up the one or many arrays a little --- activity.go | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/activity.go b/activity.go index 7ccacf1..6f5599b 100644 --- a/activity.go +++ b/activity.go @@ -468,19 +468,12 @@ func newphone(a []string, obj junk.Junk) []string { } 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") - if ok { - return id - } - } - arr, _ := obj.GetArray("attributedTo") + arr := oneforall(obj, "attributedTo") for _, a := range arr { + s, ok := a.(string) + if ok { + return s + } o, ok := a.(junk.Junk) if ok { t, _ := o.GetString("type") @@ -489,14 +482,21 @@ func extractattrto(obj junk.Junk) string { 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 { if val, _ := obj.GetString(key); val != "" { return val @@ -958,7 +958,7 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk { } } if !preferorig { - atts, _ := obj.GetArray("attachment") + atts := oneforall(obj, "attachment") for _, atti := range atts { att, ok := atti.(junk.Junk) if !ok { @@ -967,9 +967,6 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk { } procatt(att) } - if att, ok := obj.GetMap("attachment"); ok { - procatt(att) - } } proctag := func(tag junk.Junk) { tt, _ := tag.GetString("type") @@ -1016,7 +1013,7 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk { mentions = append(mentions, m) } } - tags, _ := obj.GetArray("tag") + tags := oneforall(obj, "tag") for _, tagi := range tags { tag, ok := tagi.(junk.Junk) if !ok { @@ -1024,10 +1021,6 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk { } proctag(tag) } - tag, ok := obj.GetMap("tag") - if ok { - proctag(tag) - } if starttime, ok := obj.GetString("startTime"); ok { if start, err := time.Parse(time.RFC3339, starttime); err == nil { t := new(Time)