Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for detecting announce in Mastodon. Added unlike and unannounce support #396

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/feditest/nodedrivers/fallback/fediverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,27 @@ def like_object(self, actor_acct_uri: str, object_uri: str) -> None:
+ ' and hit return when done.')


# Python 3.12 @override
def unlike_object(self, actor_acct_uri: str, object_uri: str) -> None:
prompt_user(
f'On FediverseNode "{ self.hostname }", make actor "{ actor_acct_uri }" unlike the object at "{ object_uri }"'
+ ' and hit return when done.')


# Python 3.12 @override
def announce_object(self, actor_acct_uri: str, object_uri: str) -> None:
prompt_user(
f'On FediverseNode "{ self.hostname }", make actor "{ actor_acct_uri }" announce/reblog/boost the object at "{ object_uri }"'
+ ' and hit return when done.')


# Python 3.12 @override
def unannounce_object(self, actor_acct_uri: str, object_uri: str) -> None:
prompt_user(
f'On FediverseNode "{ self.hostname }", make actor "{ actor_acct_uri }" unannounce/undo reblog/undo boost the object at "{ object_uri }"'
+ ' and hit return when done.')


# Python 3.12 @override
def actor_has_received_object(self, actor_acct_uri: str, object_uri: str) -> str | None:
answer = prompt_user(
Expand Down
41 changes: 39 additions & 2 deletions src/feditest/nodedrivers/mastodon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,14 @@ def like_object(self, object_uri: str) -> None:
raise ValueError(f'Cannot find Note on { self }: "{ object_uri }"')


def unlike_object(self, object_uri: str) -> None:
if note := self._find_note_dict_by_uri(object_uri):
note_id = note['id']
response = self.http_post(f'/api/v1/statuses/{ note_id }/unfavourite')
return response
raise ValueError(f'Cannot find Note on { self }: "{ object_uri }"')


def announce_object(self, object_uri: str) -> None:
if note := self._find_note_dict_by_uri(object_uri):
note_id = note['id']
Expand All @@ -329,15 +337,30 @@ def announce_object(self, object_uri: str) -> None:
raise ValueError(f'Cannot find Note on { self }: "{ object_uri }"')


def unannounce_object(self, object_uri: str) -> None:
if note := self._find_note_dict_by_uri(object_uri):
note_id = note['id']
response = self.http_post(f'/api/v1/statuses/{ note_id }/unreblog')
return response
raise ValueError(f'Cannot find Note on { self }: "{ object_uri }"')


def actor_has_received_object(self, object_uri: str) -> dict[str, Any]:
# Depending on how the Note is addressed and follow status, Mastodon puts it into the Home timeline or only
# into notifications.
# Check for it in the home timeline.
elements = self.http_get('/api/v1/timelines/home')
# Home timeline first case: a post was created by an account we follow
response = find_first_in_array(elements, lambda s: s['uri'] == object_uri)
if not response:
# Home timeline second case: an announce/boost was created by an account we follow -- need to look for the original URI
if reblog_response := find_first_in_array(elements, lambda s: 'reblog' in s and s['reblog']['uri'] == object_uri) :
response = reblog_response['reblog']
if not response:
# Check for it in notifications: mentions arrive here
elements = self.http_get('/api/v1/notifications')
notifications_response = find_first_in_array(elements, lambda s: s['status']['uri'] == object_uri)
if notifications_response:
# s['status'] exists for some things in notifications, but not others (such as "follow")
if notifications_response := find_first_in_array(elements, lambda s: 'status' in s and s['status']['uri'] == object_uri) :
response = notifications_response['status']
return response

Expand Down Expand Up @@ -683,13 +706,27 @@ def like_object(self, actor_acct_uri: str, object_uri: str) -> None:
self._run_poor_mans_cron()


# Python 3.12 @override
def unlike_object(self, actor_acct_uri: str, object_uri: str) -> None:
mastodon_client = self._get_mastodon_client_by_actor_acct_uri(actor_acct_uri)
mastodon_client.unlike_object(object_uri)
self._run_poor_mans_cron()


# Python 3.12 @override
def announce_object(self, actor_acct_uri: str, object_uri: str) -> None:
mastodon_client = self._get_mastodon_client_by_actor_acct_uri(actor_acct_uri)
mastodon_client.announce_object(object_uri)
self._run_poor_mans_cron()


# Python 3.12 @override
def unannounce_object(self, actor_acct_uri: str, object_uri: str) -> None:
mastodon_client = self._get_mastodon_client_by_actor_acct_uri(actor_acct_uri)
mastodon_client.unannounce_object(object_uri)
self._run_poor_mans_cron()


# Python 3.12 @override
def actor_has_received_object(self, actor_acct_uri: str, object_uri: str) -> str | None:
mastodon_client = self._get_mastodon_client_by_actor_acct_uri(actor_acct_uri)
Expand Down
14 changes: 14 additions & 0 deletions src/feditest/protocols/fediverse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,27 @@ def like_object(self, actor_acct_uri: str, object_uri: str) -> None:
raise NotImplementedByNodeError(self, FediverseNode.like_object)


def unlike_object(self, actor_acct_uri: str, object_uri: str) -> None:
"""
Unlike an object (like a note) that was previously liked
"""
raise NotImplementedByNodeError(self, FediverseNode.unlike_object)


def announce_object(self, actor_acct_uri: str, object_uri: str) -> None:
"""
Announce an object (boost, reblog).
"""
raise NotImplementedByNodeError(self, FediverseNode.announce_object)


def unannounce_object(self, actor_acct_uri: str, object_uri: str) -> None:
"""
Undo a previous announce of an object (boost, reblog).
"""
raise NotImplementedByNodeError(self, FediverseNode.unannounce_object)


def actor_has_received_object(self, actor_acct_uri: str, object_uri: str) -> str | None:
"""
If the object at object_uri has arrived with the Actor at actor_acct_uri, return the content
Expand Down
Loading