Skip to content

bovine.activitystreams.activity_factory

bovine.activitystreams.activity_factory

Activity dataclass

A dataclass representing an ActivityStreams Activity.

>>> activity=Activity(
...  type="Like",
...  actor="http://actor.example",
... object="http://some.object.example")
>>> activity.build()
{'@context': 'https://www.w3.org/ns/activitystreams',
    'type': 'Like',
    'actor': 'http://actor.example',
    'object': 'http://some.object.example'}

Parameters:

Name Type Description Default
type str
required
actor str | None
None
followers str | None
None
id str | None
None
published str | None
None
name str | None
None
summary str | None
None
content str | None
None
target str | None
None
object str | None
None
Source code in bovine/bovine/activitystreams/activity_factory.py
@dataclass
class Activity:
    """A dataclass representing an [ActivityStreams Activity](https://www.w3.org/TR/activitystreams-vocabulary/#activity-types).

    ```pycon
    >>> activity=Activity(
    ...  type="Like",
    ...  actor="http://actor.example",
    ... object="http://some.object.example")
    >>> activity.build()
    {'@context': 'https://www.w3.org/ns/activitystreams',
        'type': 'Like',
        'actor': 'http://actor.example',
        'object': 'http://some.object.example'}

    ```

    """

    type: str
    actor: Optional[str] = None
    followers: Optional[str] = None
    id: Optional[str] = None
    published: Optional[str] = None
    to: Set[str] = field(default_factory=set)
    cc: Set[str] = field(default_factory=set)

    name: Optional[str] = None
    summary: Optional[str] = None
    content: Optional[str] = None

    target: Optional[str] = None
    object: Optional[str] = None

    def as_public(self):
        """makes the activity public, i.e. public in to and followers in cc

        ```pycon
        >>> activity = Activity(type="Like",
        ...     actor="http://actor.example",
        ...     followers="http://actor.example/followers")
        >>> activity.as_public().build()
        {'@context': 'https://www.w3.org/ns/activitystreams',
            'type': 'Like',
            'actor': 'http://actor.example',
            'to': ['https://www.w3.org/ns/activitystreams#Public'],
            'cc': ['http://actor.example/followers']}

        ```
        """
        self.to.add("https://www.w3.org/ns/activitystreams#Public")
        if self.followers:
            self.cc.add(self.followers)
        return self

    def as_followers(self):
        """addresses the activity to followers, if they are set

        ```pycon
        >>> activity = Activity(type="Like",
        ...     actor="http://actor.example",
        ...     followers="http://actor.example/followers")
        >>> activity.as_followers().build()
        {'@context': 'https://www.w3.org/ns/activitystreams',
            'type': 'Like',
            'actor': 'http://actor.example',
            'to': ['http://actor.example/followers']}

        ```
        """
        if self.followers:
            self.to.add(self.followers)
        return self

    def as_unlisted(self):
        """makes the activity unlisted, i.e. public in cc and followers in to

        ```pycon
        >>> activity = Activity(type="Like",
        ...     actor="http://actor.example",
        ...     followers="http://actor.example/followers")
        >>> activity.as_unlisted().build()
        {'@context': 'https://www.w3.org/ns/activitystreams',
            'type': 'Like',
            'actor': 'http://actor.example',
            'to': ['http://actor.example/followers'],
            'cc': ['https://www.w3.org/ns/activitystreams#Public']}

        ```
        """
        if self.followers:
            self.to.add(self.followers)
        self.cc.add("https://www.w3.org/ns/activitystreams#Public")
        return self

    def build(self) -> dict:
        """converts the activity into a dict, that can be serialized to JSON"""
        result = {
            "@context": "https://www.w3.org/ns/activitystreams",
            "type": self.type,
            "actor": self.actor,
            "to": list(self.to),
            "cc": list(self.cc - self.to),
        }

        extra_fields = {
            "id": self.id,
            "published": self.published,
            "name": self.name,
            "summary": self.summary,
            "content": self.content,
            "target": self.target,
            "object": self.object,
        }

        if result["to"] is not None and len(result["to"]) == 0:
            del result["to"]
        if result["cc"] is not None and len(result["cc"]) == 0:
            del result["cc"]

        for key, value in extra_fields.items():
            if value:
                result[key] = value

        return result

as_followers

as_followers()

addresses the activity to followers, if they are set

>>> activity = Activity(type="Like",
...     actor="http://actor.example",
...     followers="http://actor.example/followers")
>>> activity.as_followers().build()
{'@context': 'https://www.w3.org/ns/activitystreams',
    'type': 'Like',
    'actor': 'http://actor.example',
    'to': ['http://actor.example/followers']}
Source code in bovine/bovine/activitystreams/activity_factory.py
def as_followers(self):
    """addresses the activity to followers, if they are set

    ```pycon
    >>> activity = Activity(type="Like",
    ...     actor="http://actor.example",
    ...     followers="http://actor.example/followers")
    >>> activity.as_followers().build()
    {'@context': 'https://www.w3.org/ns/activitystreams',
        'type': 'Like',
        'actor': 'http://actor.example',
        'to': ['http://actor.example/followers']}

    ```
    """
    if self.followers:
        self.to.add(self.followers)
    return self

as_public

as_public()

makes the activity public, i.e. public in to and followers in cc

>>> activity = Activity(type="Like",
...     actor="http://actor.example",
...     followers="http://actor.example/followers")
>>> activity.as_public().build()
{'@context': 'https://www.w3.org/ns/activitystreams',
    'type': 'Like',
    'actor': 'http://actor.example',
    'to': ['https://www.w3.org/ns/activitystreams#Public'],
    'cc': ['http://actor.example/followers']}
Source code in bovine/bovine/activitystreams/activity_factory.py
def as_public(self):
    """makes the activity public, i.e. public in to and followers in cc

    ```pycon
    >>> activity = Activity(type="Like",
    ...     actor="http://actor.example",
    ...     followers="http://actor.example/followers")
    >>> activity.as_public().build()
    {'@context': 'https://www.w3.org/ns/activitystreams',
        'type': 'Like',
        'actor': 'http://actor.example',
        'to': ['https://www.w3.org/ns/activitystreams#Public'],
        'cc': ['http://actor.example/followers']}

    ```
    """
    self.to.add("https://www.w3.org/ns/activitystreams#Public")
    if self.followers:
        self.cc.add(self.followers)
    return self

as_unlisted

as_unlisted()

makes the activity unlisted, i.e. public in cc and followers in to

>>> activity = Activity(type="Like",
...     actor="http://actor.example",
...     followers="http://actor.example/followers")
>>> activity.as_unlisted().build()
{'@context': 'https://www.w3.org/ns/activitystreams',
    'type': 'Like',
    'actor': 'http://actor.example',
    'to': ['http://actor.example/followers'],
    'cc': ['https://www.w3.org/ns/activitystreams#Public']}
Source code in bovine/bovine/activitystreams/activity_factory.py
def as_unlisted(self):
    """makes the activity unlisted, i.e. public in cc and followers in to

    ```pycon
    >>> activity = Activity(type="Like",
    ...     actor="http://actor.example",
    ...     followers="http://actor.example/followers")
    >>> activity.as_unlisted().build()
    {'@context': 'https://www.w3.org/ns/activitystreams',
        'type': 'Like',
        'actor': 'http://actor.example',
        'to': ['http://actor.example/followers'],
        'cc': ['https://www.w3.org/ns/activitystreams#Public']}

    ```
    """
    if self.followers:
        self.to.add(self.followers)
    self.cc.add("https://www.w3.org/ns/activitystreams#Public")
    return self

build

build() -> dict

converts the activity into a dict, that can be serialized to JSON

Source code in bovine/bovine/activitystreams/activity_factory.py
def build(self) -> dict:
    """converts the activity into a dict, that can be serialized to JSON"""
    result = {
        "@context": "https://www.w3.org/ns/activitystreams",
        "type": self.type,
        "actor": self.actor,
        "to": list(self.to),
        "cc": list(self.cc - self.to),
    }

    extra_fields = {
        "id": self.id,
        "published": self.published,
        "name": self.name,
        "summary": self.summary,
        "content": self.content,
        "target": self.target,
        "object": self.object,
    }

    if result["to"] is not None and len(result["to"]) == 0:
        del result["to"]
    if result["cc"] is not None and len(result["cc"]) == 0:
        del result["cc"]

    for key, value in extra_fields.items():
        if value:
            result[key] = value

    return result

ActivityFactory dataclass

Basic factory for Activity objects. Can created by BovineClient.activity_factory

>>> activity_factory = ActivityFactory({"id": "http://actor.example"})
>>> activity_factory.like("http://object.example").build()
{'@context': 'https://www.w3.org/ns/activitystreams',
    'type': 'Like',
    'actor': 'http://actor.example',
    'published': '2024-09-26T18:35:42Z',
    'object': 'http://object.example'}

By setting id_generator, one can provide a function that will automatically set the id property:

>>> activity_factory = ActivityFactory({"id": "http://actor.example"},
...     id_generator=lambda: "http://actor.example/id")
>>> activity_factory.like("http://object.example").build()
{'@context': 'https://www.w3.org/ns/activitystreams',
    'type': 'Like',
    'actor': 'http://actor.example',
    'id': 'http://actor.example/id',
    'published': '2024-09-26T18:35:42Z',
    'object': 'http://object.example'}

Parameters:

Name Type Description Default
actor_information dict
required
id_generator Callable[list, str] | None
None
Source code in bovine/bovine/activitystreams/activity_factory.py
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
@dataclass
class ActivityFactory:
    """Basic factory for Activity objects.
    Can created by [BovineClient.activity_factory][bovine.BovineClient.activity_factory]

    ```pycon
    >>> activity_factory = ActivityFactory({"id": "http://actor.example"})
    >>> activity_factory.like("http://object.example").build()
    {'@context': 'https://www.w3.org/ns/activitystreams',
        'type': 'Like',
        'actor': 'http://actor.example',
        'published': '2024-09-26T18:35:42Z',
        'object': 'http://object.example'}

    ```

    By setting id_generator, one can provide a function that will automatically set the id property:

    ```pycon
    >>> activity_factory = ActivityFactory({"id": "http://actor.example"},
    ...     id_generator=lambda: "http://actor.example/id")
    >>> activity_factory.like("http://object.example").build()
    {'@context': 'https://www.w3.org/ns/activitystreams',
        'type': 'Like',
        'actor': 'http://actor.example',
        'id': 'http://actor.example/id',
        'published': '2024-09-26T18:35:42Z',
        'object': 'http://object.example'}

    ```
    """

    actor_information: dict
    id_generator: Callable[[], str] | None = None

    def _defaults_for_object(self, obj, kwargs):
        result = {
            "actor": self.actor_information.get("id", obj.get("attributedTo")),
            "object": obj,
            "cc": recipient(obj.get("cc", [])),
            "to": recipient(obj.get("to", [])),
            "published": bovine.utils.now_isoformat(),
            "followers": self.actor_information.get("followers"),
            **kwargs,
        }
        if self.id_generator:
            result["id"] = self.id_generator()
        return result

    def create(self, obj, **kwargs):
        """Activity of type Create from Object

        ```pycon
        >>> activity_factory = ActivityFactory({"id": "http://actor.example"})
        >>> obj = {"type": "Note", "content": "hello world!", "to": "http://you.example"}
        >>> activity_factory.create(obj).build()
        {'@context': 'https://www.w3.org/ns/activitystreams',
            'type': 'Create',
            'actor': 'http://actor.example',
            'to': ['http://you.example'],
            'published': '2024-09-26T18:35:42Z',
            'object': {'type': 'Note',
                'content': 'hello world!',
                'to': 'http://you.example'}}

        ```
        """
        return Activity(
            type="Create",
            **self._defaults_for_object(obj, kwargs),
        )

    def update(self, obj, **kwargs):
        """Activity of type Update from Object"""
        return Activity(
            type="Update",
            **self._defaults_for_object(obj, kwargs),
        )

    def _base_defaults(self, kwargs):
        result = {
            "actor": self.actor_information["id"],
            "published": bovine.utils.now_isoformat(),
            **kwargs,
        }
        if self.actor_information.get("followers") and not result.get("followers"):
            result["followers"] = self.actor_information.get("followers")

        if self.id_generator:
            result["id"] = self.id_generator()

        return result

    def _defaults(self, target, kwargs):
        return {"object": target, **self._base_defaults(kwargs)}

    def like(self, target, **kwargs):
        """Like for target

        ```pycon
        >>> activity_factory = ActivityFactory({"id": "http://actor.example"})
        >>> activity_factory.like("http://object.example").build()
        {'@context': 'https://www.w3.org/ns/activitystreams',
            'type': 'Like',
            'actor': 'http://actor.example',
            'published': '2024-09-26T18:35:42Z',
            'object': 'http://object.example'}

        ```


        """
        return Activity(type="Like", **self._defaults(target, kwargs))

    def delete(self, target, **kwargs):
        """Delete for target

        ```pycon
        >>> activity_factory = ActivityFactory({"id": "http://actor.example"})
        >>> activity_factory.delete("http://bad.example", to={"http://you.example"}).build()
        {'@context': 'https://www.w3.org/ns/activitystreams',
            'type': 'Delete',
            'actor': 'http://actor.example',
            'to': ['http://you.example'],
            'published': '2024-09-26T18:35:42Z',
            'object': 'http://bad.example'}

        ```
        """
        return Activity(type="Delete", **self._defaults(target, kwargs))

    def accept(self, activity, include_activity=False, **kwargs):
        """Accept for object

        ```pycon
        >>> follow = ActivityFactory({"id":
        ...     "http://you.example"}).follow("http://actor.example",
        ...     id="http://actor.example/follow_id").build()
        >>> follow
        {'@context': 'https://www.w3.org/ns/activitystreams',
            'type': 'Follow',
            'actor': 'http://you.example',
            'to': ['http://actor.example'],
            'id': 'http://actor.example/follow_id',
            'published': '2024-09-26T18:35:42Z',
            'object': 'http://actor.example'}

        >>> activity_factory = ActivityFactory({"id": "http://actor.example"})
        >>> activity_factory.accept(follow).build()
        {'@context': 'https://www.w3.org/ns/activitystreams',
            'type': 'Accept',
            'actor': 'http://actor.example',
            'to': ['http://you.example'],
            'published': '2024-09-26T18:35:42Z',
            'object': 'http://actor.example/follow_id'}

        ```
        """

        if isinstance(activity, str):
            return Activity(type="Accept", **self._defaults(activity, kwargs))

        obj = id_for_object(activity)
        if obj is None or include_activity:
            obj = activity

        return Activity(
            type="Accept",
            **self._defaults(obj, kwargs),
            to={id_for_object(activity.get("actor"))},
        )

    def reject(self, activity, include_activity=False, **kwargs):
        """Reject for object

        ```pycon
        >>> follow = ActivityFactory({"id":
        ...     "http://you.example"}).follow("http://actor.example",
        ...     id="http://actor.example/follow_id").build()
        >>> follow
        {'@context': 'https://www.w3.org/ns/activitystreams',
            'type': 'Follow',
            'actor': 'http://you.example',
            'to': ['http://actor.example'],
            'id': 'http://actor.example/follow_id',
            'published': '2024-09-26T18:35:42Z',
            'object': 'http://actor.example'}

        >>> activity_factory = ActivityFactory({"id": "http://actor.example"})
        >>> activity_factory.reject(follow).build()
        {'@context': 'https://www.w3.org/ns/activitystreams',
            'type': 'Reject',
            'actor': 'http://actor.example',
            'to': ['http://you.example'],
            'published': '2024-09-26T18:35:42Z',
            'object': 'http://actor.example/follow_id'}

        ```
        """

        if isinstance(activity, str):
            return Activity(type="Reject", **self._defaults(activity, kwargs))

        obj = id_for_object(activity)
        if obj is None or include_activity:
            obj = activity

        return Activity(
            type="Reject",
            **self._defaults(obj, kwargs),
            to={id_for_object(activity.get("actor"))},
        )

    def announce(self, obj, **kwargs):
        """Announce for object

        ```pycon
        >>> activity_factory = ActivityFactory({"id": "http://actor.example",
        ...     "followers": "http://actor.example/followers"})
        >>> activity_factory.announce("http://object.example").as_public().build()
        {'@context': 'https://www.w3.org/ns/activitystreams',
            'type': 'Announce',
            'actor': 'http://actor.example',
            'to': ['https://www.w3.org/ns/activitystreams#Public'],
            'cc': ['http://actor.example/followers'],
            'published': '2024-09-26T18:35:42Z',
            'object': 'http://object.example'}

        ```
        """
        return Activity(
            type="Announce",
            **self._defaults(obj, kwargs),
        )

    def follow(self, obj: str | dict, **kwargs):
        """Follow for object

        ```pycon
        >>> activity_factory = ActivityFactory({"id": "http://actor.example"})
        >>> activity_factory.follow("http://you.example").build()
        {'@context': 'https://www.w3.org/ns/activitystreams',
            'type': 'Follow',
            'actor': 'http://actor.example',
            'to': ['http://you.example'],
            'published': '2024-09-26T18:35:42Z',
            'object': 'http://you.example'}

        ```

        If the object is an actor, its id is used

        ```pycon
        >>> activity_factory = ActivityFactory({"id": "http://actor.example"})
        >>> actor = {"type": "Person", "id": "http://you.example"}
        >>> activity_factory.follow(actor).build()
        {'@context': 'https://www.w3.org/ns/activitystreams',
            'type': 'Follow',
            'actor': 'http://actor.example',
            'to': ['http://you.example'],
            'published': '2024-09-26T18:35:42Z',
            'object': 'http://you.example'}

        ```


        :param obj: Object to be followed
        :param **kwargs: Passed to [Activity][bovine.activitystreams.activity_factory.Activity]'s constructor

        """

        obj = id_for_object(obj)

        return Activity(
            type="Follow",
            **self._defaults(obj, kwargs),
            to={obj},
        )

    def undo(self, activity, include_activity=False, **kwargs):
        """Undo for activity

        ```pycon
        >>> activity_factory = ActivityFactory({"id": "http://actor.example"})
        >>> follow = activity_factory.follow("http://you.example",
        ...     id="http://actor.example/follow_id").build()
        >>> activity_factory.undo(follow).build()
        {'@context': 'https://www.w3.org/ns/activitystreams',
            'type': 'Undo',
            'actor': 'http://actor.example',
            'to': ['http://you.example'],
            'published': '2024-09-26T18:35:42Z',
            'object': 'http://actor.example/follow_id'}

        ```

        If the activity doesn't have an id, it is fully included.


        ```pycon
        >>> activity_factory = ActivityFactory({"id": "http://actor.example",
        ...     "followers": "http://actor.example/followers"})
        >>> announce = activity_factory.announce("http://object.example").as_public().build()
        >>> activity_factory.undo(announce).build()
        {'@context': 'https://www.w3.org/ns/activitystreams',
            'type': 'Undo',
            'actor': 'http://actor.example',
            'to': ['https://www.w3.org/ns/activitystreams#Public'],
            'cc': ['http://actor.example/followers'],
            'published': '2024-09-26T18:35:42Z',
            'object': {'@context': 'https://www.w3.org/ns/activitystreams',
                'type': 'Announce',
                'actor': 'http://actor.example',
                'to': ['https://www.w3.org/ns/activitystreams#Public'],
                'cc': ['http://actor.example/followers'],
                'published': '2024-09-26T18:35:42Z',
                'object': 'http://object.example'}}


        ```
        """

        if isinstance(activity, str):
            return Activity(type="Undo", **self._defaults(activity, kwargs))

        obj = id_for_object(activity)
        if obj is None or include_activity:
            obj = activity

        return Activity(
            type="Undo",
            **self._defaults(obj, kwargs),
            to={id_for_object(x) for x in activity.get("to", [])},
            cc={id_for_object(x) for x in activity.get("cc", [])},
        )

    def custom(self, **kwargs):
        """Allows creating a custom activity

        ```pycon
        >>> activity_factory = ActivityFactory({"id": "http://actor.example"})
        >>> activity_factory.custom(type="AnimalSound", content="moo").build()
        {'@context': 'https://www.w3.org/ns/activitystreams',
            'type': 'AnimalSound',
            'actor': 'http://actor.example',
            'published': '2024-09-26T18:35:42Z',
            'content': 'moo'}

        ```

        Or ready to send to your followers

        ```pycon
        >>> activity_factory = ActivityFactory({"id": "http://actor.example",
        ...     "followers": "http://actor.example/followers"})
        >>> activity_factory.custom(type="AnimalSound", content="moo").as_public().build()
        {'@context': 'https://www.w3.org/ns/activitystreams',
            'type': 'AnimalSound',
            'actor': 'http://actor.example',
            'to': ['https://www.w3.org/ns/activitystreams#Public'],
            'cc': ['http://actor.example/followers'],
            'published': '2024-09-26T18:35:42Z',
            'content': 'moo'}

        ```


        """

        return Activity(**self._base_defaults(kwargs))

    def block(self, actor: str | dict, **kwargs):
        """Block for actor

        ```pycon
        >>> activity_factory = ActivityFactory({"id": "http://actor.example"})
        >>> activity_factory.block("http://you.example").build()
        {'@context': 'https://www.w3.org/ns/activitystreams',
            'type': 'Block',
            'actor': 'http://actor.example',
            'to': ['http://you.example'],
            'published': '2024-09-26T18:35:42Z',
            'object': 'http://you.example'}

        ```

        If the object is an actor, its id is used

        ```pycon
        >>> activity_factory = ActivityFactory({"id": "http://actor.example"})
        >>> actor = {"type": "Person", "id": "http://you.example"}
        >>> activity_factory.block(actor).build()
        {'@context': 'https://www.w3.org/ns/activitystreams',
            'type': 'Block',
            'actor': 'http://actor.example',
            'to': ['http://you.example'],
            'published': '2024-09-26T18:35:42Z',
            'object': 'http://you.example'}

        ```

        """

        actor = id_for_object(actor)

        return Activity(
            type="Block",
            **self._defaults(actor, kwargs),
            to={actor},
        )

accept

accept(activity, include_activity=False, **kwargs)

Accept for object

>>> follow = ActivityFactory({"id":
...     "http://you.example"}).follow("http://actor.example",
...     id="http://actor.example/follow_id").build()
>>> follow
{'@context': 'https://www.w3.org/ns/activitystreams',
    'type': 'Follow',
    'actor': 'http://you.example',
    'to': ['http://actor.example'],
    'id': 'http://actor.example/follow_id',
    'published': '2024-09-26T18:35:42Z',
    'object': 'http://actor.example'}

>>> activity_factory = ActivityFactory({"id": "http://actor.example"})
>>> activity_factory.accept(follow).build()
{'@context': 'https://www.w3.org/ns/activitystreams',
    'type': 'Accept',
    'actor': 'http://actor.example',
    'to': ['http://you.example'],
    'published': '2024-09-26T18:35:42Z',
    'object': 'http://actor.example/follow_id'}
Source code in bovine/bovine/activitystreams/activity_factory.py
def accept(self, activity, include_activity=False, **kwargs):
    """Accept for object

    ```pycon
    >>> follow = ActivityFactory({"id":
    ...     "http://you.example"}).follow("http://actor.example",
    ...     id="http://actor.example/follow_id").build()
    >>> follow
    {'@context': 'https://www.w3.org/ns/activitystreams',
        'type': 'Follow',
        'actor': 'http://you.example',
        'to': ['http://actor.example'],
        'id': 'http://actor.example/follow_id',
        'published': '2024-09-26T18:35:42Z',
        'object': 'http://actor.example'}

    >>> activity_factory = ActivityFactory({"id": "http://actor.example"})
    >>> activity_factory.accept(follow).build()
    {'@context': 'https://www.w3.org/ns/activitystreams',
        'type': 'Accept',
        'actor': 'http://actor.example',
        'to': ['http://you.example'],
        'published': '2024-09-26T18:35:42Z',
        'object': 'http://actor.example/follow_id'}

    ```
    """

    if isinstance(activity, str):
        return Activity(type="Accept", **self._defaults(activity, kwargs))

    obj = id_for_object(activity)
    if obj is None or include_activity:
        obj = activity

    return Activity(
        type="Accept",
        **self._defaults(obj, kwargs),
        to={id_for_object(activity.get("actor"))},
    )

announce

announce(obj, **kwargs)

Announce for object

>>> activity_factory = ActivityFactory({"id": "http://actor.example",
...     "followers": "http://actor.example/followers"})
>>> activity_factory.announce("http://object.example").as_public().build()
{'@context': 'https://www.w3.org/ns/activitystreams',
    'type': 'Announce',
    'actor': 'http://actor.example',
    'to': ['https://www.w3.org/ns/activitystreams#Public'],
    'cc': ['http://actor.example/followers'],
    'published': '2024-09-26T18:35:42Z',
    'object': 'http://object.example'}
Source code in bovine/bovine/activitystreams/activity_factory.py
def announce(self, obj, **kwargs):
    """Announce for object

    ```pycon
    >>> activity_factory = ActivityFactory({"id": "http://actor.example",
    ...     "followers": "http://actor.example/followers"})
    >>> activity_factory.announce("http://object.example").as_public().build()
    {'@context': 'https://www.w3.org/ns/activitystreams',
        'type': 'Announce',
        'actor': 'http://actor.example',
        'to': ['https://www.w3.org/ns/activitystreams#Public'],
        'cc': ['http://actor.example/followers'],
        'published': '2024-09-26T18:35:42Z',
        'object': 'http://object.example'}

    ```
    """
    return Activity(
        type="Announce",
        **self._defaults(obj, kwargs),
    )

block

block(actor: str | dict, **kwargs)

Block for actor

>>> activity_factory = ActivityFactory({"id": "http://actor.example"})
>>> activity_factory.block("http://you.example").build()
{'@context': 'https://www.w3.org/ns/activitystreams',
    'type': 'Block',
    'actor': 'http://actor.example',
    'to': ['http://you.example'],
    'published': '2024-09-26T18:35:42Z',
    'object': 'http://you.example'}

If the object is an actor, its id is used

>>> activity_factory = ActivityFactory({"id": "http://actor.example"})
>>> actor = {"type": "Person", "id": "http://you.example"}
>>> activity_factory.block(actor).build()
{'@context': 'https://www.w3.org/ns/activitystreams',
    'type': 'Block',
    'actor': 'http://actor.example',
    'to': ['http://you.example'],
    'published': '2024-09-26T18:35:42Z',
    'object': 'http://you.example'}
Source code in bovine/bovine/activitystreams/activity_factory.py
def block(self, actor: str | dict, **kwargs):
    """Block for actor

    ```pycon
    >>> activity_factory = ActivityFactory({"id": "http://actor.example"})
    >>> activity_factory.block("http://you.example").build()
    {'@context': 'https://www.w3.org/ns/activitystreams',
        'type': 'Block',
        'actor': 'http://actor.example',
        'to': ['http://you.example'],
        'published': '2024-09-26T18:35:42Z',
        'object': 'http://you.example'}

    ```

    If the object is an actor, its id is used

    ```pycon
    >>> activity_factory = ActivityFactory({"id": "http://actor.example"})
    >>> actor = {"type": "Person", "id": "http://you.example"}
    >>> activity_factory.block(actor).build()
    {'@context': 'https://www.w3.org/ns/activitystreams',
        'type': 'Block',
        'actor': 'http://actor.example',
        'to': ['http://you.example'],
        'published': '2024-09-26T18:35:42Z',
        'object': 'http://you.example'}

    ```

    """

    actor = id_for_object(actor)

    return Activity(
        type="Block",
        **self._defaults(actor, kwargs),
        to={actor},
    )

create

create(obj, **kwargs)

Activity of type Create from Object

>>> activity_factory = ActivityFactory({"id": "http://actor.example"})
>>> obj = {"type": "Note", "content": "hello world!", "to": "http://you.example"}
>>> activity_factory.create(obj).build()
{'@context': 'https://www.w3.org/ns/activitystreams',
    'type': 'Create',
    'actor': 'http://actor.example',
    'to': ['http://you.example'],
    'published': '2024-09-26T18:35:42Z',
    'object': {'type': 'Note',
        'content': 'hello world!',
        'to': 'http://you.example'}}
Source code in bovine/bovine/activitystreams/activity_factory.py
def create(self, obj, **kwargs):
    """Activity of type Create from Object

    ```pycon
    >>> activity_factory = ActivityFactory({"id": "http://actor.example"})
    >>> obj = {"type": "Note", "content": "hello world!", "to": "http://you.example"}
    >>> activity_factory.create(obj).build()
    {'@context': 'https://www.w3.org/ns/activitystreams',
        'type': 'Create',
        'actor': 'http://actor.example',
        'to': ['http://you.example'],
        'published': '2024-09-26T18:35:42Z',
        'object': {'type': 'Note',
            'content': 'hello world!',
            'to': 'http://you.example'}}

    ```
    """
    return Activity(
        type="Create",
        **self._defaults_for_object(obj, kwargs),
    )

custom

custom(**kwargs)

Allows creating a custom activity

>>> activity_factory = ActivityFactory({"id": "http://actor.example"})
>>> activity_factory.custom(type="AnimalSound", content="moo").build()
{'@context': 'https://www.w3.org/ns/activitystreams',
    'type': 'AnimalSound',
    'actor': 'http://actor.example',
    'published': '2024-09-26T18:35:42Z',
    'content': 'moo'}

Or ready to send to your followers

>>> activity_factory = ActivityFactory({"id": "http://actor.example",
...     "followers": "http://actor.example/followers"})
>>> activity_factory.custom(type="AnimalSound", content="moo").as_public().build()
{'@context': 'https://www.w3.org/ns/activitystreams',
    'type': 'AnimalSound',
    'actor': 'http://actor.example',
    'to': ['https://www.w3.org/ns/activitystreams#Public'],
    'cc': ['http://actor.example/followers'],
    'published': '2024-09-26T18:35:42Z',
    'content': 'moo'}
Source code in bovine/bovine/activitystreams/activity_factory.py
def custom(self, **kwargs):
    """Allows creating a custom activity

    ```pycon
    >>> activity_factory = ActivityFactory({"id": "http://actor.example"})
    >>> activity_factory.custom(type="AnimalSound", content="moo").build()
    {'@context': 'https://www.w3.org/ns/activitystreams',
        'type': 'AnimalSound',
        'actor': 'http://actor.example',
        'published': '2024-09-26T18:35:42Z',
        'content': 'moo'}

    ```

    Or ready to send to your followers

    ```pycon
    >>> activity_factory = ActivityFactory({"id": "http://actor.example",
    ...     "followers": "http://actor.example/followers"})
    >>> activity_factory.custom(type="AnimalSound", content="moo").as_public().build()
    {'@context': 'https://www.w3.org/ns/activitystreams',
        'type': 'AnimalSound',
        'actor': 'http://actor.example',
        'to': ['https://www.w3.org/ns/activitystreams#Public'],
        'cc': ['http://actor.example/followers'],
        'published': '2024-09-26T18:35:42Z',
        'content': 'moo'}

    ```


    """

    return Activity(**self._base_defaults(kwargs))

delete

delete(target, **kwargs)

Delete for target

>>> activity_factory = ActivityFactory({"id": "http://actor.example"})
>>> activity_factory.delete("http://bad.example", to={"http://you.example"}).build()
{'@context': 'https://www.w3.org/ns/activitystreams',
    'type': 'Delete',
    'actor': 'http://actor.example',
    'to': ['http://you.example'],
    'published': '2024-09-26T18:35:42Z',
    'object': 'http://bad.example'}
Source code in bovine/bovine/activitystreams/activity_factory.py
def delete(self, target, **kwargs):
    """Delete for target

    ```pycon
    >>> activity_factory = ActivityFactory({"id": "http://actor.example"})
    >>> activity_factory.delete("http://bad.example", to={"http://you.example"}).build()
    {'@context': 'https://www.w3.org/ns/activitystreams',
        'type': 'Delete',
        'actor': 'http://actor.example',
        'to': ['http://you.example'],
        'published': '2024-09-26T18:35:42Z',
        'object': 'http://bad.example'}

    ```
    """
    return Activity(type="Delete", **self._defaults(target, kwargs))

follow

follow(obj: str | dict, **kwargs)

Follow for object

>>> activity_factory = ActivityFactory({"id": "http://actor.example"})
>>> activity_factory.follow("http://you.example").build()
{'@context': 'https://www.w3.org/ns/activitystreams',
    'type': 'Follow',
    'actor': 'http://actor.example',
    'to': ['http://you.example'],
    'published': '2024-09-26T18:35:42Z',
    'object': 'http://you.example'}

If the object is an actor, its id is used

>>> activity_factory = ActivityFactory({"id": "http://actor.example"})
>>> actor = {"type": "Person", "id": "http://you.example"}
>>> activity_factory.follow(actor).build()
{'@context': 'https://www.w3.org/ns/activitystreams',
    'type': 'Follow',
    'actor': 'http://actor.example',
    'to': ['http://you.example'],
    'published': '2024-09-26T18:35:42Z',
    'object': 'http://you.example'}

Parameters:

Name Type Description Default
obj str | dict

Object to be followed

required
**kwargs

Passed to Activity’s constructor

{}
Source code in bovine/bovine/activitystreams/activity_factory.py
def follow(self, obj: str | dict, **kwargs):
    """Follow for object

    ```pycon
    >>> activity_factory = ActivityFactory({"id": "http://actor.example"})
    >>> activity_factory.follow("http://you.example").build()
    {'@context': 'https://www.w3.org/ns/activitystreams',
        'type': 'Follow',
        'actor': 'http://actor.example',
        'to': ['http://you.example'],
        'published': '2024-09-26T18:35:42Z',
        'object': 'http://you.example'}

    ```

    If the object is an actor, its id is used

    ```pycon
    >>> activity_factory = ActivityFactory({"id": "http://actor.example"})
    >>> actor = {"type": "Person", "id": "http://you.example"}
    >>> activity_factory.follow(actor).build()
    {'@context': 'https://www.w3.org/ns/activitystreams',
        'type': 'Follow',
        'actor': 'http://actor.example',
        'to': ['http://you.example'],
        'published': '2024-09-26T18:35:42Z',
        'object': 'http://you.example'}

    ```


    :param obj: Object to be followed
    :param **kwargs: Passed to [Activity][bovine.activitystreams.activity_factory.Activity]'s constructor

    """

    obj = id_for_object(obj)

    return Activity(
        type="Follow",
        **self._defaults(obj, kwargs),
        to={obj},
    )

like

like(target, **kwargs)

Like for target

>>> activity_factory = ActivityFactory({"id": "http://actor.example"})
>>> activity_factory.like("http://object.example").build()
{'@context': 'https://www.w3.org/ns/activitystreams',
    'type': 'Like',
    'actor': 'http://actor.example',
    'published': '2024-09-26T18:35:42Z',
    'object': 'http://object.example'}
Source code in bovine/bovine/activitystreams/activity_factory.py
def like(self, target, **kwargs):
    """Like for target

    ```pycon
    >>> activity_factory = ActivityFactory({"id": "http://actor.example"})
    >>> activity_factory.like("http://object.example").build()
    {'@context': 'https://www.w3.org/ns/activitystreams',
        'type': 'Like',
        'actor': 'http://actor.example',
        'published': '2024-09-26T18:35:42Z',
        'object': 'http://object.example'}

    ```


    """
    return Activity(type="Like", **self._defaults(target, kwargs))

reject

reject(activity, include_activity=False, **kwargs)

Reject for object

>>> follow = ActivityFactory({"id":
...     "http://you.example"}).follow("http://actor.example",
...     id="http://actor.example/follow_id").build()
>>> follow
{'@context': 'https://www.w3.org/ns/activitystreams',
    'type': 'Follow',
    'actor': 'http://you.example',
    'to': ['http://actor.example'],
    'id': 'http://actor.example/follow_id',
    'published': '2024-09-26T18:35:42Z',
    'object': 'http://actor.example'}

>>> activity_factory = ActivityFactory({"id": "http://actor.example"})
>>> activity_factory.reject(follow).build()
{'@context': 'https://www.w3.org/ns/activitystreams',
    'type': 'Reject',
    'actor': 'http://actor.example',
    'to': ['http://you.example'],
    'published': '2024-09-26T18:35:42Z',
    'object': 'http://actor.example/follow_id'}
Source code in bovine/bovine/activitystreams/activity_factory.py
def reject(self, activity, include_activity=False, **kwargs):
    """Reject for object

    ```pycon
    >>> follow = ActivityFactory({"id":
    ...     "http://you.example"}).follow("http://actor.example",
    ...     id="http://actor.example/follow_id").build()
    >>> follow
    {'@context': 'https://www.w3.org/ns/activitystreams',
        'type': 'Follow',
        'actor': 'http://you.example',
        'to': ['http://actor.example'],
        'id': 'http://actor.example/follow_id',
        'published': '2024-09-26T18:35:42Z',
        'object': 'http://actor.example'}

    >>> activity_factory = ActivityFactory({"id": "http://actor.example"})
    >>> activity_factory.reject(follow).build()
    {'@context': 'https://www.w3.org/ns/activitystreams',
        'type': 'Reject',
        'actor': 'http://actor.example',
        'to': ['http://you.example'],
        'published': '2024-09-26T18:35:42Z',
        'object': 'http://actor.example/follow_id'}

    ```
    """

    if isinstance(activity, str):
        return Activity(type="Reject", **self._defaults(activity, kwargs))

    obj = id_for_object(activity)
    if obj is None or include_activity:
        obj = activity

    return Activity(
        type="Reject",
        **self._defaults(obj, kwargs),
        to={id_for_object(activity.get("actor"))},
    )

undo

undo(activity, include_activity=False, **kwargs)

Undo for activity

>>> activity_factory = ActivityFactory({"id": "http://actor.example"})
>>> follow = activity_factory.follow("http://you.example",
...     id="http://actor.example/follow_id").build()
>>> activity_factory.undo(follow).build()
{'@context': 'https://www.w3.org/ns/activitystreams',
    'type': 'Undo',
    'actor': 'http://actor.example',
    'to': ['http://you.example'],
    'published': '2024-09-26T18:35:42Z',
    'object': 'http://actor.example/follow_id'}

If the activity doesn’t have an id, it is fully included.

>>> activity_factory = ActivityFactory({"id": "http://actor.example",
...     "followers": "http://actor.example/followers"})
>>> announce = activity_factory.announce("http://object.example").as_public().build()
>>> activity_factory.undo(announce).build()
{'@context': 'https://www.w3.org/ns/activitystreams',
    'type': 'Undo',
    'actor': 'http://actor.example',
    'to': ['https://www.w3.org/ns/activitystreams#Public'],
    'cc': ['http://actor.example/followers'],
    'published': '2024-09-26T18:35:42Z',
    'object': {'@context': 'https://www.w3.org/ns/activitystreams',
        'type': 'Announce',
        'actor': 'http://actor.example',
        'to': ['https://www.w3.org/ns/activitystreams#Public'],
        'cc': ['http://actor.example/followers'],
        'published': '2024-09-26T18:35:42Z',
        'object': 'http://object.example'}}
Source code in bovine/bovine/activitystreams/activity_factory.py
def undo(self, activity, include_activity=False, **kwargs):
    """Undo for activity

    ```pycon
    >>> activity_factory = ActivityFactory({"id": "http://actor.example"})
    >>> follow = activity_factory.follow("http://you.example",
    ...     id="http://actor.example/follow_id").build()
    >>> activity_factory.undo(follow).build()
    {'@context': 'https://www.w3.org/ns/activitystreams',
        'type': 'Undo',
        'actor': 'http://actor.example',
        'to': ['http://you.example'],
        'published': '2024-09-26T18:35:42Z',
        'object': 'http://actor.example/follow_id'}

    ```

    If the activity doesn't have an id, it is fully included.


    ```pycon
    >>> activity_factory = ActivityFactory({"id": "http://actor.example",
    ...     "followers": "http://actor.example/followers"})
    >>> announce = activity_factory.announce("http://object.example").as_public().build()
    >>> activity_factory.undo(announce).build()
    {'@context': 'https://www.w3.org/ns/activitystreams',
        'type': 'Undo',
        'actor': 'http://actor.example',
        'to': ['https://www.w3.org/ns/activitystreams#Public'],
        'cc': ['http://actor.example/followers'],
        'published': '2024-09-26T18:35:42Z',
        'object': {'@context': 'https://www.w3.org/ns/activitystreams',
            'type': 'Announce',
            'actor': 'http://actor.example',
            'to': ['https://www.w3.org/ns/activitystreams#Public'],
            'cc': ['http://actor.example/followers'],
            'published': '2024-09-26T18:35:42Z',
            'object': 'http://object.example'}}


    ```
    """

    if isinstance(activity, str):
        return Activity(type="Undo", **self._defaults(activity, kwargs))

    obj = id_for_object(activity)
    if obj is None or include_activity:
        obj = activity

    return Activity(
        type="Undo",
        **self._defaults(obj, kwargs),
        to={id_for_object(x) for x in activity.get("to", [])},
        cc={id_for_object(x) for x in activity.get("cc", [])},
    )

update

update(obj, **kwargs)

Activity of type Update from Object

Source code in bovine/bovine/activitystreams/activity_factory.py
def update(self, obj, **kwargs):
    """Activity of type Update from Object"""
    return Activity(
        type="Update",
        **self._defaults_for_object(obj, kwargs),
    )