Debian Codegraph @

item_collection.go

raw symbols SCIP
1 package activitypub
2
3 import (
4 "slices"
5 "sort"
6 )
7
8 // ItemCollection represents an array of items
9 type ItemCollection []Item
10
11 // GetID returns the ID corresponding to ItemCollection
12 func (i ItemCollection) GetID() ID {
13 return EmptyID
14 }
15
16 // GetLink returns the empty IRI
17 func (i ItemCollection) GetLink() IRI {
18 return EmptyIRI
19 }
20
21 // GetType returns the ItemCollection's type
22 func (i ItemCollection) GetType() Typer {
23 return CollectionOfItems
24 }
25
26 // Match returns whether the receiver matches the ActivityVocabularyType arguments.
27 func (i ItemCollection) Match(tt ...ActivityVocabularyType) bool {
28 return ActivityVocabularyTypes(tt).Match(i.GetType())
29 }
30
31 // IsLink returns false for an ItemCollection object
32 func (i ItemCollection) IsLink() bool {
33 return false
34 }
35
36 // IsObject returns true for a ItemCollection object
37 func (i ItemCollection) IsObject() bool {
38 return false
39 }
40
41 func (i ItemCollection) IRIs() IRIs {
42 if i == nil {
43 return nil
44 }
45
46 iris := make(IRIs, 0, len(i))
47 for _, it := range i {
48 if IsNil(it) {
49 continue
50 }
51 iris = append(iris, it.GetLink())
52 }
53 return iris
54 }
55
56 // MarshalJSON encodes the receiver object to a JSON document.
57 func (i ItemCollection) MarshalJSON() ([]byte, error) {
58 if i == nil {
59 return nil, nil
60 }
61 b := make([]byte, 0)
62 JSONWriteItemCollectionValue(&b, i, true)
63 return b, nil
64 }
65
66 // Append facilitates adding elements to Item arrays
67 // and ensures ItemCollection implements the Collection interface
68 func (i *ItemCollection) Append(it ...Item) error {
69 for _, ob := range it {
70 if i.Contains(ob) {
71 continue
72 }
73 *i = append(*i, ob)
74 }
75 return nil
76 }
77
78 // Count returns the length of Items in the item collection
79 func (i *ItemCollection) Count() uint {
80 if i == nil {
81 return 0
82 }
83 return uint(len(*i))
84 }
85
86 // First returns the ID corresponding to ItemCollection
87 func (i ItemCollection) First() Item {
88 if len(i) == 0 {
89 return nil
90 }
91 return i[0]
92 }
93
94 // Normalize returns the first item if the collection contains only one,
95 // the full collection if the collection contains more than one item,
96 // or nil
97 func (i ItemCollection) Normalize() Item {
98 if len(i) == 0 {
99 return nil
100 }
101 if len(i) == 1 {
102 return i[0]
103 }
104 return i
105 }
106
107 // Collection returns the current object as collection interface
108 func (i *ItemCollection) Collection() ItemCollection {
109 return *i
110 }
111
112 // IsCollection returns true for ItemCollection arrays
113 func (i ItemCollection) IsCollection() bool {
114 return true
115 }
116
117 // Contains verifies if IRIs array contains the received one
118 func (i ItemCollection) Contains(r Item) bool {
119 if len(i) == 0 {
120 return false
121 }
122 for _, it := range i {
123 if ItemsEqual(it, r) {
124 return true
125 }
126 }
127 return false
128 }
129
130 // Remove removes the items from the ItemCollection
131 func (i *ItemCollection) Remove(it ...Item) {
132 *i = slices.DeleteFunc(*i, ItemCollection(it).Contains)
133 }
134
135 // ItemCollectionDeduplication normalizes the received arguments lists into a single unified one
136 func ItemCollectionDeduplication(recCols ...*ItemCollection) ItemCollection {
137 rec := make(ItemCollection, 0)
138
139 for _, recCol := range recCols {
140 if recCol == nil {
141 continue
142 }
143
144 toRemove := make([]int, 0)
145 for i, cur := range *recCol {
146 save := true
147 if cur == nil {
148 continue
149 }
150 var testIt IRI
151 if cur.IsObject() {
152 testIt = cur.GetID()
153 } else if cur.IsLink() {
154 testIt = cur.GetLink()
155 } else {
156 continue
157 }
158 for _, it := range rec {
159 if testIt.Equal(it.GetID()) {
160 // mark the element for removal
161 toRemove = append(toRemove, i)
162 save = false
163 }
164 }
165 if save {
166 rec = append(rec, testIt)
167 }
168 }
169
170 sort.Sort(sort.Reverse(sort.IntSlice(toRemove)))
171 for _, idx := range toRemove {
172 *recCol = append((*recCol)[:idx], (*recCol)[idx+1:]...)
173 }
174 }
175 return rec
176 }
177
178 // ToItemCollection returns the item collection contained as part of OrderedCollection, OrderedCollectionPage,
179 // Collection and CollectionPage.
180 // It also converts an IRI slice into an equivalent ItemCollection.
181 func ToItemCollection(it LinkOrIRI) (*ItemCollection, error) {
182 switch i := it.(type) {
183 case *ItemCollection:
184 return i, nil
185 case ItemCollection:
186 return &i, nil
187 case *OrderedCollection:
188 return &i.OrderedItems, nil
189 case *OrderedCollectionPage:
190 return &i.OrderedItems, nil
191 case *Collection:
192 return &i.Items, nil
193 case *CollectionPage:
194 return &i.Items, nil
195 case IRIs:
196 iris := make(ItemCollection, len(i))
197 for j, ob := range i {
198 iris[j] = ob
199 }
200 return &iris, nil
201 case *IRIs:
202 iris := make(ItemCollection, len(*i))
203 for j, ob := range *i {
204 iris[j] = ob
205 }
206 return &iris, nil
207 default:
208 }
209 return reflectItemToType[ItemCollection](it)
210 }
211
212 // ToIRIs
213 func ToIRIs(it Item) (*IRIs, error) {
214 switch i := it.(type) {
215 case *IRIs:
216 return i, nil
217 case IRIs:
218 return &i, nil
219 case ItemCollection:
220 iris := i.IRIs()
221 return &iris, nil
222 case *ItemCollection:
223 iris := make(IRIs, len(*i))
224 for j, ob := range *i {
225 iris[j] = ob.GetLink()
226 }
227 return &iris, nil
228 default:
229 return reflectItemToType[IRIs](it)
230 }
231 return nil, ErrorInvalidType[IRIs](it)
232 }
233
234 // ItemsMatch
235 func (i ItemCollection) ItemsMatch(col ...Item) bool {
236 for _, it := range col {
237 if match := i.Contains(it); !match {
238 return false
239 }
240 }
241 return true
242 }
243
244 // Equals verifies if our receiver ItemCollection is equals with the "with" Item
245 func (i *ItemCollection) Equals(with Item) bool {
246 if IsNil(with) {
247 return i == nil
248 }
249 if !with.IsCollection() {
250 return false
251 }
252 if !CollectionOfItems.Match(with.GetType()) {
253 return false
254 }
255 itemCollection, err := ToItemCollection(with)
256 if err != nil {
257 return false
258 }
259 return i.equal(*itemCollection)
260 }
261
262 // equal verifies if our receiver ItemCollection is equals with the "with" ItemCollection
263 func (i ItemCollection) equal(with ItemCollection) bool {
264 if len(i) != len(with) {
265 return false
266 }
267 result := true
268 for _, it := range i {
269 if !with.Contains(it.GetLink()) {
270 result = false
271 }
272 }
273 return result
274 }
275
276 // Clean removes Bto and BCC properties on all the members of the collection
277 func (i ItemCollection) Clean() {
278 for j, it := range i {
279 i[j] = CleanRecipients(it)
280 }
281 }
282
283 func (i ItemCollection) Recipients() ItemCollection {
284 all := make(ItemCollection, 0)
285 for _, it := range i {
286 _ = OnObject(it, func(ob *Object) error {
287 aud := ob.Audience
288 _ = all.Append(ItemCollectionDeduplication(&ob.To, &ob.CC, &ob.Bto, &ob.BCC, &aud)...)
289 return nil
290 })
291 }
292 return ItemCollectionDeduplication(&all)
293 }