Skip to content

Commit

Permalink
Make comments an option
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérôme Deuchnord authored and Deuchnord committed Aug 18, 2023
1 parent 3dbadef commit 7f5261d
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 7 deletions.
6 changes: 6 additions & 0 deletions config.dist.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,9 @@ tag_format = "camelCase"
# A list of groups you want to send the message to, additionally to the followers.
# This is required to get the messages discoverable by some social applications like Lemmy.
groups = []

# Set this to true to enable the comments feature.
# When disabled (default behavior):
# - the comments are silently rejected (social applications still receive an Accepted response to prevent them sending the responses again and again)
# - the API and JS widget are not available
accept_responses = false
7 changes: 4 additions & 3 deletions f2ap/activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def handle_inbox(

check_message_signature(config, actor, headers, inbox)

return actor, handle_inbox_message(db, inbox, on_following_accepted)
return actor, handle_inbox_message(db, inbox, on_following_accepted, config.message.accept_responses)


def get_actor_from_inbox(db: Database, inbox: dict) -> dict:
Expand Down Expand Up @@ -201,7 +201,7 @@ def check_message_signature(


def handle_inbox_message(
db: Database, inbox: dict, on_following_accepted: Callable
db: Database, inbox: dict, on_following_accepted: Callable, accept_responses: bool
) -> Optional[dict]:
if (
inbox.get("type") == "Accept"
Expand All @@ -219,7 +219,7 @@ def handle_inbox_message(
db.delete_follower(inbox.get("actor"))
return

if inbox.get("type") == "Create" and inbox.get("object", {}).get("type") == "Note":
if accept_responses and inbox.get("type") == "Create" and inbox.get("object", {}).get("type") == "Note":
# Save comments to a note
note = inbox.get("object")
in_reply_to = note.get("inReplyTo")
Expand Down Expand Up @@ -251,6 +251,7 @@ def handle_inbox_message(
return

# Tombstone might be a Note, try to delete it.
# Note: this is always done, even when accept_responses is False, just in case it has been disabled lately.
db.delete_comment(o.get("id"))

return
Expand Down
3 changes: 2 additions & 1 deletion f2ap/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def followers_link(self) -> str:

class Message:
def __init__(
self, format: str, tag_format: str = "camelCase", groups: [str] = None
self, format: str, tag_format: str = "camelCase", groups: [str] = None, accept_responses: bool = False
):
valid_tag_formats = self.get_tags_formatters().keys()

Expand All @@ -147,6 +147,7 @@ def __init__(

self.tag_format = tag_format
self.groups = groups if groups is not None else []
self.accept_responses = accept_responses

@staticmethod
def get_tags_formatters() -> {str: Callable}:
Expand Down
20 changes: 19 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ black = "^22.10.0"
pycln = "^2.1.2"
pytest = "^7.2.0"
pytest-cov = "^4.0.0"
pytest-mock = "^3.10.0"
coveralls = "^3.3.1"
httpx = "^0.23.3"

Expand Down
6 changes: 5 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from os import path, remove
from uuid import uuid4

from f2ap.config import Configuration
from f2ap.data import Database
Expand All @@ -7,7 +8,10 @@
PATH_DIRNAME = path.dirname(__file__)


def get_fake_db(db_path: str, delete_first: bool = False) -> Database:
def get_fake_db(db_path: str = None, delete_first: bool = False) -> Database:
if db_path is None:
db_path = f"/files/database.{uuid4().hex}.db"

realpath = f"{PATH_DIRNAME}/{db_path}"
if delete_first and path.exists(realpath):
remove(realpath)
Expand Down
29 changes: 28 additions & 1 deletion tests/activitypub.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from uuid import uuid4
from pytest_mock import MockerFixture

from . import get_fake_db

from f2ap import activitypub
from f2ap.enum import Visibility
from f2ap.model import W3C_ACTIVITYSTREAMS_PUBLIC
from f2ap.model import W3C_ACTIVITYSTREAMS_PUBLIC, Note


def test_get_note_visibility_public_note():
Expand Down Expand Up @@ -58,3 +63,25 @@ def test_get_note_visibility_mentioned_only_note():
"cc": ["https://example.net/users/anotherUser"],
}
)


def test_inbox_can_handle_comments(mocker: MockerFixture):
db = get_fake_db()
comment_uuid = uuid4()
mocker.patch.object(db, "insert_comment", return_value=comment_uuid)

activitypub.handle_inbox()

replying_to = Note(
uuid=comment_uuid,
id=42,
name=
)
url = n
published_on = n
author_url = n
content = n
visibility = n
tags = []

db.insert_comment.assert_called_with(replying_to, url, published_on, author_url, content, visibility, tags)

0 comments on commit 7f5261d

Please sign in to comment.