Skip to content

Commit 9d07ea6

Browse files
committed
feat: auto convert actor/object to id
1 parent 770c19b commit 9d07ea6

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/apmodel/core/activity.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from ..types import Undefined
77
from ..vocab.actor import Actor
88
from .object import Object
9-
from ..context import LDContext
109

1110
if TYPE_CHECKING:
1211
from ..vocab.activity.accept import Accept
@@ -20,7 +19,7 @@ class Activity(Object):
2019
actor: Union[str, "Actor", List[Union[str, "Actor"]], Undefined] = field(
2120
default_factory=Undefined
2221
)
23-
object: Union[Object, Undefined] = field(default_factory=Undefined)
22+
object: Union[str, Object, Undefined] = field(default_factory=Undefined)
2423
target: Union[str, "Actor", List[Union[str, "Actor"]], Undefined] = field(
2524
default_factory=Undefined
2625
)
@@ -37,6 +36,23 @@ def reject(self, id: str, actor: Actor) -> "Reject":
3736
from ..vocab.activity.reject import Reject
3837

3938
return Reject(id=id, object=self, actor=actor)
39+
40+
def to_json(self, keep_object: bool = False): # pyright: ignore[reportIncompatibleMethodOverride]
41+
"""Export activity to JSON
42+
43+
Args:
44+
keep_object (bool, optional): Don't convert to url for target,actor. Defaults to False.
45+
46+
Returns:
47+
_type_: _description_
48+
"""
49+
if not keep_object:
50+
if isinstance(self.actor, Actor):
51+
self.actor = self.actor.id
52+
if isinstance(self.object, Object):
53+
self.object = self.object.id
54+
55+
return super().to_json()
4056

4157

4258

0 commit comments

Comments
 (0)