| 1 | package activitypub |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/gob" |
| 6 | "fmt" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/valyala/fastjson" |
| 10 | ) |
| 11 | |
| 12 | // Tombstone a Tombstone represents a content object that has been deleted. |
| 13 | // It can be used in Collections to signify that there used to be an object at this position, |
| 14 | // but it has been deleted. |
| 15 | type Tombstone struct { |
| 16 | // ID provides the globally unique identifier for anActivity Pub Object or Link. |
| 17 | ID ID `jsonld:"id,omitempty"` |
| 18 | // Type identifies the Activity Pub Object or Link type. Multiple values may be specified. |
| 19 | Type Typer `jsonld:"type,omitempty"` |
| 20 | // Name a simple, human-readable, plain-text name for the object. |
| 21 | // HTML markup MUST NOT be included. The name MAY be expressed using multiple language-tagged values. |
| 22 | Name NaturalLanguageValues `jsonld:"name,omitempty,collapsible"` |
| 23 | // Attachment identifies a resource attached or related to an object that potentially requires special handling. |
| 24 | // The intent is to provide a model that is at least semantically similar to attachments in email. |
| 25 | Attachment Item `jsonld:"attachment,omitempty"` |
| 26 | // AttributedTo identifies one or more entities to which this object is attributed. The attributed entities might not be Actors. |
| 27 | // For instance, an object might be attributed to the completion of another activity. |
| 28 | AttributedTo Item `jsonld:"attributedTo,omitempty"` |
| 29 | // Audience identifies one or more entities that represent the total population of entities |
| 30 | // for which the object can considered to be relevant. |
| 31 | Audience ItemCollection `jsonld:"audience,omitempty"` |
| 32 | // Content or textual representation of the Activity Pub Object encoded as a JSON string. |
| 33 | // By default, the value of content is HTML. |
| 34 | // The mediaType property can be used in the object to indicate a different content type. |
| 35 | // (The content MAY be expressed using multiple language-tagged values.) |
| 36 | Content NaturalLanguageValues `jsonld:"content,omitempty,collapsible"` |
| 37 | // Context identifies the context within which the object exists or an activity was performed. |
| 38 | // The notion of "context" used is intentionally vague. |
| 39 | // The intended function is to serve as a means of grouping objects and activities that share a |
| 40 | // common originating context or purpose. An example could be all activities relating to a common project or event. |
| 41 | Context Item `jsonld:"context,omitempty"` |
| 42 | // MediaType when used on an Object, identifies the MIME media type of the value of the content property. |
| 43 | // If not specified, the content property is assumed to contain text/html content. |
| 44 | MediaType MimeType `jsonld:"mediaType,omitempty"` |
| 45 | // EndTime the date and time describing the actual or expected ending time of the object. |
| 46 | // When used with an Activity object, for instance, the endTime property specifies the moment |
| 47 | // the activity concluded or is expected to conclude. |
| 48 | EndTime time.Time `jsonld:"endTime,omitempty"` |
| 49 | // Generator identifies the entity (e.g. an application) that generated the object. |
| 50 | Generator Item `jsonld:"generator,omitempty"` |
| 51 | // Icon indicates an entity that describes an icon for this object. |
| 52 | // The image should have an aspect ratio of one (horizontal) to one (vertical) |
| 53 | // and should be suitable for presentation at a small size. |
| 54 | Icon Item `jsonld:"icon,omitempty"` |
| 55 | // Image indicates an entity that describes an image for this object. |
| 56 | // Unlike the icon property, there are no aspect ratio or display size limitations assumed. |
| 57 | Image Item `jsonld:"image,omitempty"` |
| 58 | // InReplyTo indicates one or more entities for which this object is considered a response. |
| 59 | InReplyTo Item `jsonld:"inReplyTo,omitempty"` |
| 60 | // Location indicates one or more physical or logical locations associated with the object. |
| 61 | Location Item `jsonld:"location,omitempty"` |
| 62 | // Preview identifies an entity that provides a preview of this object. |
| 63 | Preview Item `jsonld:"preview,omitempty"` |
| 64 | // Published the date and time at which the object was published |
| 65 | Published time.Time `jsonld:"published,omitempty"` |
| 66 | // Replies identifies a Collection containing objects considered to be responses to this object. |
| 67 | Replies Item `jsonld:"replies,omitempty"` |
| 68 | // StartTime the date and time describing the actual or expected starting time of the object. |
| 69 | // When used with an Activity object, for instance, the startTime property specifies |
| 70 | // the moment the activity began or is scheduled to begin. |
| 71 | StartTime time.Time `jsonld:"startTime,omitempty"` |
| 72 | // Summary a natural language summarization of the object encoded as HTML. |
| 73 | // *Multiple language tagged summaries may be provided.) |
| 74 | Summary NaturalLanguageValues `jsonld:"summary,omitempty,collapsible"` |
| 75 | // Tag one or more "tags" that have been associated with an objects. A tag can be any kind of Activity Pub Object. |
| 76 | // The key difference between attachment and tag is that the former implies association by inclusion, |
| 77 | // while the latter implies associated by reference. |
| 78 | Tag ItemCollection `jsonld:"tag,omitempty"` |
| 79 | // Updated the date and time at which the object was updated |
| 80 | Updated time.Time `jsonld:"updated,omitempty"` |
| 81 | // URL identifies one or more links to representations of the object |
| 82 | URL Item `jsonld:"url,omitempty"` |
| 83 | // To identifies an entity considered to be part of the public primary audience of an Activity Pub Object |
| 84 | To ItemCollection `jsonld:"to,omitempty"` |
| 85 | // Bto identifies anActivity Pub Object that is part of the private primary audience of this Activity Pub Object. |
| 86 | Bto ItemCollection `jsonld:"bto,omitempty"` |
| 87 | // CC identifies anActivity Pub Object that is part of the public secondary audience of this Activity Pub Object. |
| 88 | CC ItemCollection `jsonld:"cc,omitempty"` |
| 89 | // BCC identifies one or more Objects that are part of the private secondary audience of this Activity Pub Object. |
| 90 | BCC ItemCollection `jsonld:"bcc,omitempty"` |
| 91 | // Duration when the object describes a time-bound resource, such as an audio or video, a meeting, etc, |
| 92 | // the duration property indicates the object's approximate duration. |
| 93 | // The value must be expressed as an xsd:duration as defined by [ xmlschema11-2], |
| 94 | // section 3.3.6 (e.g. a period of 5 seconds is represented as "PT5S"). |
| 95 | Duration time.Duration `jsonld:"duration,omitempty"` |
| 96 | // This is a list of all Like activities with this object as the object property, added as a side effect. |
| 97 | // The likes collection MUST be either an OrderedCollection or a Collection and MAY be filtered on privileges |
| 98 | // of an authenticated user or as appropriate when no authentication is given. |
| 99 | Likes Item `jsonld:"likes,omitempty"` |
| 100 | // This is a list of all Announce activities with this object as the object property, added as a side effect. |
| 101 | // The shares collection MUST be either an OrderedCollection or a Collection and MAY be filtered on privileges |
| 102 | // of an authenticated user or as appropriate when no authentication is given. |
| 103 | Shares Item `jsonld:"shares,omitempty"` |
| 104 | // Source property is intended to convey some sort of source from which the content markup was derived, |
| 105 | // as a form of provenance, or to support future editing by clients. |
| 106 | // In general, clients do the conversion from source to content, not the other way around. |
| 107 | Source Source `jsonld:"source,omitempty"` |
| 108 | // FormerType On a Tombstone object, the formerType property identifies the type of the object that was deleted. |
| 109 | FormerType Typer `jsonld:"formerType,omitempty"` |
| 110 | // Deleted On a Tombstone object, the deleted property is a timestamp for when the object was deleted. |
| 111 | Deleted time.Time `jsonld:"deleted,omitempty"` |
| 112 | } |
| 113 | |
| 114 | // IsLink returns false for Tombstone objects |
| 115 | func (t Tombstone) IsLink() bool { |
| 116 | return false |
| 117 | } |
| 118 | |
| 119 | // IsObject returns true for Tombstone objects |
| 120 | func (t Tombstone) IsObject() bool { |
| 121 | return true |
| 122 | } |
| 123 | |
| 124 | // IsCollection returns false for Tombstone objects |
| 125 | func (t Tombstone) IsCollection() bool { |
| 126 | return false |
| 127 | } |
| 128 | |
| 129 | // GetLink returns the IRI corresponding to the current Tombstone object |
| 130 | func (t Tombstone) GetLink() IRI { |
| 131 | return IRI(t.ID) |
| 132 | } |
| 133 | |
| 134 | // GetType returns the type of the current Tombstone |
| 135 | func (t Tombstone) GetType() Typer { |
| 136 | return t.Type |
| 137 | } |
| 138 | |
| 139 | // GetID returns the ID corresponding to the current Tombstone |
| 140 | func (t Tombstone) GetID() ID { |
| 141 | return t.ID |
| 142 | } |
| 143 | |
| 144 | // Matches returns whether the receiver matches the ActivityVocabularyType arguments. |
| 145 | func (t Tombstone) Match(tt ...ActivityVocabularyType) bool { |
| 146 | return ActivityVocabularyTypes(tt).Match(t.Type) || ActivityVocabularyTypes(tt).Match(t.FormerType) |
| 147 | } |
| 148 | |
| 149 | // UnmarshalJSON decodes an incoming JSON document into the receiver object. |
| 150 | func (t *Tombstone) UnmarshalJSON(data []byte) error { |
| 151 | par := fastjson.Parser{} |
| 152 | val, err := par.ParseBytes(data) |
| 153 | if err != nil { |
| 154 | return err |
| 155 | } |
| 156 | return JSONLoadTombstone(val, t) |
| 157 | } |
| 158 | |
| 159 | // MarshalJSON encodes the receiver object to a JSON document. |
| 160 | func (t Tombstone) MarshalJSON() ([]byte, error) { |
| 161 | b := make([]byte, 0) |
| 162 | notEmpty := false |
| 163 | JSONWrite(&b, '{') |
| 164 | |
| 165 | _ = OnObject(t, func(o *Object) error { |
| 166 | notEmpty = JSONWriteObjectValue(&b, *o) |
| 167 | return nil |
| 168 | }) |
| 169 | if t.FormerType != nil { |
| 170 | notEmpty = JSONWriteTypes(&b, "formerType", t.FormerType) || notEmpty |
| 171 | } |
| 172 | if !t.Deleted.IsZero() { |
| 173 | notEmpty = JSONWriteTimeProp(&b, "deleted", t.Deleted) || notEmpty |
| 174 | } |
| 175 | if notEmpty { |
| 176 | JSONWrite(&b, '}') |
| 177 | return b, nil |
| 178 | } |
| 179 | return nil, nil |
| 180 | } |
| 181 | |
| 182 | // UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. |
| 183 | func (t *Tombstone) UnmarshalBinary(data []byte) error { |
| 184 | return t.GobDecode(data) |
| 185 | } |
| 186 | |
| 187 | // MarshalBinary implements the encoding.BinaryMarshaler interface. |
| 188 | func (t Tombstone) MarshalBinary() ([]byte, error) { |
| 189 | return t.GobEncode() |
| 190 | } |
| 191 | |
| 192 | // GobEncode |
| 193 | func (t Tombstone) GobEncode() ([]byte, error) { |
| 194 | mm := make(map[string][]byte) |
| 195 | hasData, err := mapTombstoneProperties(mm, t) |
| 196 | if err != nil { |
| 197 | return nil, err |
| 198 | } |
| 199 | if !hasData { |
| 200 | return []byte{}, nil |
| 201 | } |
| 202 | bb := bytes.Buffer{} |
| 203 | g := gob.NewEncoder(&bb) |
| 204 | if err := g.Encode(mm); err != nil { |
| 205 | return nil, err |
| 206 | } |
| 207 | return bb.Bytes(), nil |
| 208 | } |
| 209 | |
| 210 | // GobDecode |
| 211 | func (t *Tombstone) GobDecode(data []byte) error { |
| 212 | if len(data) == 0 { |
| 213 | return nil |
| 214 | } |
| 215 | mm, err := gobDecodeObjectAsMap(data) |
| 216 | if err != nil { |
| 217 | return err |
| 218 | } |
| 219 | return unmapTombstoneProperties(mm, t) |
| 220 | } |
| 221 | |
| 222 | // Recipients performs recipient de-duplication on the Tombstone object's To, Bto, CC and BCC properties |
| 223 | func (t *Tombstone) Recipients() ItemCollection { |
| 224 | aud := t.Audience |
| 225 | return ItemCollectionDeduplication(&t.To, &t.CC, &t.Bto, &t.BCC, &aud) |
| 226 | } |
| 227 | |
| 228 | // Clean removes Bto and BCC properties |
| 229 | func (t *Tombstone) Clean() { |
| 230 | _ = OnObject(t, func(o *Object) error { |
| 231 | o.Clean() |
| 232 | return nil |
| 233 | }) |
| 234 | } |
| 235 | |
| 236 | func (t Tombstone) Format(s fmt.State, verb rune) { |
| 237 | switch verb { |
| 238 | case 's', 'v': |
| 239 | _, _ = fmt.Fprintf(s, "%T[%s] { formerType: %q }", t, t.Type, t.FormerType) |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | // ToTombstone |
| 244 | func ToTombstone(it LinkOrIRI) (*Tombstone, error) { |
| 245 | switch i := it.(type) { |
| 246 | case *Tombstone: |
| 247 | return i, nil |
| 248 | case Tombstone: |
| 249 | return &i, nil |
| 250 | default: |
| 251 | return reflectItemToType[Tombstone](it) |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | type withTombstoneFn func(*Tombstone) error |
| 256 | |
| 257 | // OnTombstone calls function fn on it Item if it can be asserted to type *Tombstone |
| 258 | // |
| 259 | // This function should be called if trying to access the Tombstone specific properties |
| 260 | // like "formerType" or "deleted". |
| 261 | // For the other properties OnObject should be used instead. |
| 262 | func OnTombstone(it LinkOrIRI, fn func(*Tombstone) error) error { |
| 263 | if it == nil { |
| 264 | return nil |
| 265 | } |
| 266 | if IsItemCollection(it) { |
| 267 | return callOnItemCollection(it, OnTombstone, fn) |
| 268 | } |
| 269 | ob, err := ToTombstone(it) |
| 270 | if err != nil { |
| 271 | return err |
| 272 | } |
| 273 | return fn(ob) |
| 274 | } |