Skip to content

Commit

Permalink
feat(libactivitypub): add Delete activity
Browse files Browse the repository at this point in the history
- `activity` introduces a new class `Delete` that represents a "Delete"
  activity.
  • Loading branch information
kikuomax committed Sep 11, 2023
1 parent 1551c15 commit b3e13cb
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions lib/libactivitypub/src/libactivitypub/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def parse_object(obj: Dict[str, Any]) -> 'Activity':
return Announce.parse_object(obj)
if obj_type == 'Create':
return Create.parse_object(obj)
if obj_type == 'Delete':
return Delete.parse_object(obj)
if obj_type == 'Follow':
return Follow.parse_object(obj)
if obj_type == 'Like':
Expand Down Expand Up @@ -286,6 +288,52 @@ def resolve_objects(self, object_store: ObjectStore):
resolve_object(self._underlying['object'], object_store)


class Delete(Activity):
"""Wraps a "Delete" activity.
"""
def __init__(self, underlying: Dict[str, Any]):
"""Wraps a given "Follow" activity object.
:raises ValueError: if ``underlying`` does not represent an activity.
:raises TypeError: if ``underlying`` does not represent a valid object,
or if ``underlying`` does not have ``object``.
"""
super().__init__(underlying)
if 'object' not in underlying:
raise TypeError('invalid Delete activity: object is missing')

@staticmethod
def parse_object(obj: Dict[str, Any]) -> 'Delete':
"""Parses a given "Delete" activity object.
:raises ValueError: if ``obj`` does not represent an activity.
:raises TypeError: if ``obj`` does not represent a "Delete" activity
object.
"""
if obj.get('type') != 'Delete':
raise TypeError(f'type must be "Delete": {obj.get("type")}')
return Delete(obj)

@property
def object_id(self) -> str:
"""ID of the (to be) deleted object.
"""
return Reference(self._underlying['object']).id

def visit(self, visitor: 'ActivityVisitor'):
visitor.visit_delete(self)

def resolve_objects(self, object_store: ObjectStore):
"""Resolves the referenced object.
Resolves ``object``.
"""
super().resolve_objects(object_store)
resolve_object(self._underlying['object'], object_store)


class Follow(Activity):
"""Wraps a "Follow" activity.
"""
Expand Down Expand Up @@ -514,6 +562,11 @@ def visit_create(self, create: Create):
"""
LOGGER.debug('ignoring "Create": %s', create._underlying) # pylint: disable=protected-access

def visit_delete(self, delete: Delete):
"""Processes a "Delete" activity.
"""
LOGGER.debug('ignoring "Delete": %s', delete._underlying) # pylint: disable=protected-access

def visit_follow(self, follow: Follow):
"""Processes a "Follow" activity.
"""
Expand Down

0 comments on commit b3e13cb

Please sign in to comment.