summaryrefslogtreecommitdiff
path: root/internal/ap/extract.go
diff options
context:
space:
mode:
authorLibravatar kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>2023-11-04 20:21:20 +0000
committerLibravatar GitHub <noreply@github.com>2023-11-04 20:21:20 +0000
commit41435a6c4ee0a5b52528890edf3fbf5a9dc0a6c8 (patch)
tree987b5d7787b24f6f6e340bbcf7fd1b052fe40dfc /internal/ap/extract.go
parent[docs/bugfix] fix link to swagger yaml (#2333) (diff)
downloadgotosocial-41435a6c4ee0a5b52528890edf3fbf5a9dc0a6c8.tar.xz
[feature] support canceling scheduled tasks, some federation API performance improvements (#2329)
Diffstat (limited to 'internal/ap/extract.go')
-rw-r--r--internal/ap/extract.go101
1 files changed, 100 insertions, 1 deletions
diff --git a/internal/ap/extract.go b/internal/ap/extract.go
index 74c4497b3..332550578 100644
--- a/internal/ap/extract.go
+++ b/internal/ap/extract.go
@@ -91,6 +91,105 @@ func ExtractActivityData(activity pub.Activity, rawJSON map[string]any) ([]TypeO
}
}
+// ExtractAccountables extracts Accountable objects from a slice TypeOrIRI, returning extracted and remaining TypeOrIRIs.
+func ExtractAccountables(arr []TypeOrIRI) ([]Accountable, []TypeOrIRI) {
+ var accounts []Accountable
+
+ for i := 0; i < len(arr); i++ {
+ elem := arr[i]
+
+ if elem.IsIRI() {
+ // skip IRIs
+ continue
+ }
+
+ // Extract AS vocab type
+ // associated with elem.
+ t := elem.GetType()
+
+ // Try cast AS type as Accountable.
+ account, ok := ToAccountable(t)
+ if !ok {
+ continue
+ }
+
+ // Add casted accountable type.
+ accounts = append(accounts, account)
+
+ // Drop elem from slice.
+ copy(arr[:i], arr[i+1:])
+ arr = arr[:len(arr)-1]
+ }
+
+ return accounts, arr
+}
+
+// ExtractStatusables extracts Statusable objects from a slice TypeOrIRI, returning extracted and remaining TypeOrIRIs.
+func ExtractStatusables(arr []TypeOrIRI) ([]Statusable, []TypeOrIRI) {
+ var statuses []Statusable
+
+ for i := 0; i < len(arr); i++ {
+ elem := arr[i]
+
+ if elem.IsIRI() {
+ // skip IRIs
+ continue
+ }
+
+ // Extract AS vocab type
+ // associated with elem.
+ t := elem.GetType()
+
+ // Try cast AS type as Statusable.
+ status, ok := ToStatusable(t)
+ if !ok {
+ continue
+ }
+
+ // Add casted Statusable type.
+ statuses = append(statuses, status)
+
+ // Drop elem from slice.
+ copy(arr[:i], arr[i+1:])
+ arr = arr[:len(arr)-1]
+ }
+
+ return statuses, arr
+}
+
+// ExtractPollOptionables extracts PollOptionable objects from a slice TypeOrIRI, returning extracted and remaining TypeOrIRIs.
+func ExtractPollOptionables(arr []TypeOrIRI) ([]PollOptionable, []TypeOrIRI) {
+ var options []PollOptionable
+
+ for i := 0; i < len(arr); i++ {
+ elem := arr[i]
+
+ if elem.IsIRI() {
+ // skip IRIs
+ continue
+ }
+
+ // Extract AS vocab type
+ // associated with elem.
+ t := elem.GetType()
+
+ // Try cast as PollOptionable.
+ option, ok := ToPollOptionable(t)
+ if !ok {
+ continue
+ }
+
+ // Add casted PollOptionable type.
+ options = append(options, option)
+
+ // Drop elem from slice.
+ copy(arr[:i], arr[i+1:])
+ arr = arr[:len(arr)-1]
+ }
+
+ return options, arr
+}
+
// ExtractPreferredUsername returns a string representation of
// an interface's preferredUsername property. Will return an
// error if preferredUsername is nil, not a string, or empty.
@@ -192,7 +291,7 @@ func ExtractToURIs(i WithTo) []*url.URL {
// ExtractCcURIs returns a slice of URIs
// that the given WithCC addresses as Cc.
-func ExtractCcURIs(i WithCC) []*url.URL {
+func ExtractCcURIs(i WithCc) []*url.URL {
ccProp := i.GetActivityStreamsCc()
if ccProp == nil {
return nil