pull/1/head
Thomas Sileo 2018-06-13 09:38:12 +02:00
rodzic cfe7944261
commit c842c282e5
4 zmienionych plików z 155 dodań i 25 usunięć

Wyświetl plik

@ -5,8 +5,8 @@ install:
- pip install -r requirements.txt
- pip install -r dev-requirements.txt
script:
# - mypy --ignore-missing-imports .
# - flake8 .
# - mypy --ignore-missing-imports little_boxes
- flake8 .
- black --check
- python -m pytest -vv --cov=little_boxes
- codecov

Wyświetl plik

@ -9,8 +9,8 @@ from enum import Enum
from .errors import BadActivityError
from .errors import UnexpectedActivityTypeError
from .errors import NotFromOutboxError
from .errors import ActivityNotFoundError
from .urlutils import check_url
# from .errors import ActivityNotFoundError
# from .urlutils import check_url
from .utils import parse_collection
from typing import List
@ -20,8 +20,6 @@ from typing import Any
from typing import Union
from typing import Type
import requests
logger = logging.getLogger(__name__)
@ -323,10 +321,12 @@ class BaseBackend(object):
def outbox_update(self, activity: "Update") -> None:
pass
def inbox_create(self, activity: "Create") -> None:
@track_call
def inbox_create(self, as_actor: "Person", activity: "Create") -> None:
pass
def outbox_create(self, activity: "Create") -> None:
@track_call
def outbox_create(self, as_actor: "Person", activity: "Create") -> None:
pass
@ -357,7 +357,7 @@ class BaseActivity(object, metaclass=_ActivityMeta):
True
) # Most of the object requires an actor, so this flag in on by default
def __init__(self, **kwargs) -> None:
def __init__(self, **kwargs) -> None: # noqa: C901
if kwargs.get("type") and kwargs.pop("type") != self.ACTIVITY_TYPE.value:
raise UnexpectedActivityTypeError(
f"Expect the type to be {self.ACTIVITY_TYPE.value!r}"
@ -380,6 +380,9 @@ class BaseActivity(object, metaclass=_ActivityMeta):
kwargs.pop("actor")
actor = self._validate_person(actor)
self._data["actor"] = actor
elif self.ACTIVITY_TYPE == ActivityType.NOTE:
if 'attributedTo' not in kwargs:
raise BadActivityError(f"Note is missing attributedTo")
else:
raise BadActivityError("missing actor")
@ -561,7 +564,11 @@ class BaseActivity(object, metaclass=_ActivityMeta):
raise NotImplementedError
def _post_to_outbox(
self, obj_id: str, activity: ObjectType, recipients: List[str]
self,
as_actor: "Person",
obj_id: str,
activity: ObjectType,
recipients: List[str],
) -> None:
raise NotImplementedError
@ -630,7 +637,7 @@ class BaseActivity(object, metaclass=_ActivityMeta):
activity = clean_activity(self.to_dict())
try:
self._post_to_outbox(obj_id, activity, recipients)
self._post_to_outbox(self.get_actor(), obj_id, activity, recipients)
logger.debug(f"called post to outbox hook")
except NotImplementedError:
logger.debug("post to outbox hook not implemented")
@ -644,7 +651,7 @@ class BaseActivity(object, metaclass=_ActivityMeta):
def _recipients(self) -> List[str]:
return []
def recipients(self) -> List[str]:
def recipients(self) -> List[str]: # noqa: C901
recipients = self._recipients()
actor_id = self.get_actor().id
@ -759,7 +766,11 @@ class Follow(BaseActivity):
BACKEND.new_follower(self.get_object(), self)
def _post_to_outbox(
self, obj_id: str, activity: ObjectType, recipients: List[str]
self,
as_actor: "Person",
obj_id: str,
activity: ObjectType,
recipients: List[str],
) -> None:
# XXX The new_following event will be triggered by Accept
pass
@ -837,7 +848,11 @@ class Undo(BaseActivity):
raise NotFromOutboxError(f"object {self!r} is not owned by this instance")
def _post_to_outbox(
self, obj_id: str, activity: ObjectType, recipients: List[str]
self,
as_actor: "Person",
obj_id: str,
activity: ObjectType,
recipients: List[str],
) -> None:
logger.debug("processing undo to outbox")
logger.debug("self={}".format(self))
@ -872,7 +887,13 @@ class Like(BaseActivity):
# ABC
self.inbox_undo_like(self)
def _post_to_outbox(self, obj_id: str, activity: ObjectType, recipients: List[str]):
def _post_to_outbox(
self,
as_actor: "Person",
obj_id: str,
activity: ObjectType,
recipients: List[str],
):
# ABC
self.outbox_like(self)
@ -920,7 +941,11 @@ class Announce(BaseActivity):
self.inbox_undo_annnounce(self)
def _post_to_outbox(
self, obj_id: str, activity: ObjectType, recipients: List[str]
self,
as_actor: "Person",
obj_id: str,
activity: ObjectType,
recipients: List[str],
) -> None:
# ABC
self.outbox_announce(self)
@ -971,7 +996,11 @@ class Delete(BaseActivity):
)
def _post_to_outbox(
self, obj_id: str, activity: ObjectType, recipients: List[str]
self,
as_actor: "Person",
obj_id: str,
activity: ObjectType,
recipients: List[str],
) -> None:
# ABC
self.outbox_delete(self)
@ -1000,7 +1029,11 @@ class Update(BaseActivity):
raise NotFromOutboxError(f"object {self!r} is not owned by this instance")
def _post_to_outbox(
self, obj_id: str, activity: ObjectType, recipients: List[str]
self,
as_actor: "Person",
obj_id: str,
activity: ObjectType,
recipients: List[str],
) -> None:
# ABC
self.outbox_update(self)
@ -1042,14 +1075,16 @@ class Create(BaseActivity):
return recipients
def _process_from_inbox(self, as_actor: "Person") -> None:
# ABC
self.inbox_create(self)
BACKEND.inbox_create(as_actor, self)
def _post_to_outbox(
self, obj_id: str, activity: ObjectType, recipients: List[str]
self,
as_actor: "Person",
obj_id: str,
activity: ObjectType,
recipients: List[str],
) -> None:
# ABC
self.outbox_create(self)
BACKEND.outbox_create(self.get_actor(), self)
class Tombstone(BaseActivity):
@ -1124,7 +1159,7 @@ class Note(BaseActivity):
class Box(object):
def __init__(self, actor: Person):
def __init__(self, actor: Person) -> None:
self.actor = actor
@ -1135,6 +1170,9 @@ class Outbox(Box):
f"{activity.get_actor()!r} cannot post into {self.actor!r} outbox"
)
if activity.ACTIVITY_TYPE == ActivityType.NOTE:
activity = activity.build_create()
activity.post_to_outbox()
def get(self, activity_iri: str) -> BaseActivity:

Wyświetl plik

@ -27,7 +27,10 @@ class Error(Exception):
return rv
def __repr__(self) -> str:
return f"{self.__class__.__qualname__}({self.message!r}, payload={self.payload!r}, status_code={self.status_code})"
return (
f"{self.__class__.__qualname__}({self.message!r}, "
f"payload={self.payload!r}, status_code={self.status_code})"
)
class ActorBlockedError(Error):

Wyświetl plik

@ -164,3 +164,92 @@ def test_little_boxes_follow_unfollow():
assert back.followers(me) == []
assert back.following(me) == []
def test_little_boxes_follow_and_new_note_public_only():
back, f = test_little_boxes_follow()
me = back.get_user("tom")
other = back.get_user("tom2")
outbox = ap.Outbox(me)
note = ap.Note(to=[ap.AS_PUBLIC], cc=[], attributedTo=me.id, content="Hello")
outbox.post(note)
back.assert_called_methods(
me,
(
"an Create activity is published",
"outbox_new",
lambda as_actor: _assert_eq(as_actor.id, me.id),
lambda activity: _assert_eq(activity.get_object().id, note.id),
),
(
'"outbox_create" hook is called',
"outbox_create",
lambda as_actor: _assert_eq(as_actor.id, me.id),
lambda create: _assert_eq(create.get_object().id, note.id),
),
)
back.assert_called_methods(other)
def test_little_boxes_follow_and_new_note_to_single_actor():
back, f = test_little_boxes_follow()
me = back.get_user("tom")
other = back.get_user("tom2")
outbox = ap.Outbox(me)
note = ap.Note(
to=[ap.AS_PUBLIC], cc=[other.id], attributedTo=me.id, content="Hello"
)
outbox.post(note)
back.assert_called_methods(
me,
(
"an Create activity is published",
"outbox_new",
lambda as_actor: _assert_eq(as_actor.id, me.id),
lambda activity: _assert_eq(activity.get_object().id, note.id),
),
(
'"outbox_create" hook is called',
"outbox_create",
lambda as_actor: _assert_eq(as_actor.id, me.id),
lambda create: _assert_eq(create.get_object().id, note.id),
),
(
"the Undo activity is posted to the followee",
"post_to_remote_inbox",
lambda as_actor: _assert_eq(as_actor.id, me.id),
lambda payload: None,
lambda recipient: _assert_eq(recipient, other.inbox),
),
)
back.assert_called_methods(
other,
(
"receiving the Undo, ensure we check the actor is not blocked",
"outbox_is_blocked",
lambda as_actor: _assert_eq(as_actor.id, other.id),
lambda remote_actor: _assert_eq(remote_actor, me.id),
),
(
"receiving the Create activity",
"inbox_new",
lambda as_actor: _assert_eq(as_actor.id, other.id),
lambda activity: _assert_eq(activity.get_object().id, note.id),
),
(
'"inbox_create" hook is called',
"inbox_create",
lambda as_actor: _assert_eq(as_actor.id, other.id),
lambda create: _assert_eq(create.get_object().id, note.id),
),
)