fedkit 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. fedkit-0.1.0/LICENSE +9 -0
  2. fedkit-0.1.0/PKG-INFO +135 -0
  3. fedkit-0.1.0/README.md +104 -0
  4. fedkit-0.1.0/pyproject.toml +62 -0
  5. fedkit-0.1.0/src/fedkit/__init__.py +1 -0
  6. fedkit-0.1.0/src/fedkit/activities/__init__.py +4 -0
  7. fedkit-0.1.0/src/fedkit/activities/base.py +19 -0
  8. fedkit-0.1.0/src/fedkit/activities/follow.py +98 -0
  9. fedkit-0.1.0/src/fedkit/activities/inbound.py +170 -0
  10. fedkit-0.1.0/src/fedkit/activity.py +267 -0
  11. fedkit-0.1.0/src/fedkit/admin.py +248 -0
  12. fedkit-0.1.0/src/fedkit/apps.py +57 -0
  13. fedkit-0.1.0/src/fedkit/cli.py +26 -0
  14. fedkit-0.1.0/src/fedkit/contentnegotiation.py +194 -0
  15. fedkit-0.1.0/src/fedkit/context.py +156 -0
  16. fedkit-0.1.0/src/fedkit/decorators.py +30 -0
  17. fedkit-0.1.0/src/fedkit/exceptions.py +66 -0
  18. fedkit-0.1.0/src/fedkit/fedkit_typing.py +14 -0
  19. fedkit-0.1.0/src/fedkit/management/__init__.py +1 -0
  20. fedkit-0.1.0/src/fedkit/management/commands/__init__.py +1 -0
  21. fedkit-0.1.0/src/fedkit/management/commands/follow.py +33 -0
  22. fedkit-0.1.0/src/fedkit/managers.py +85 -0
  23. fedkit-0.1.0/src/fedkit/migrations/0001_initial.py +111 -0
  24. fedkit-0.1.0/src/fedkit/migrations/__init__.py +0 -0
  25. fedkit-0.1.0/src/fedkit/models/__init__.py +6 -0
  26. fedkit-0.1.0/src/fedkit/models/action.py +151 -0
  27. fedkit-0.1.0/src/fedkit/models/actor.py +378 -0
  28. fedkit-0.1.0/src/fedkit/models/like.py +35 -0
  29. fedkit-0.1.0/src/fedkit/models/note.py +54 -0
  30. fedkit-0.1.0/src/fedkit/parsers.py +39 -0
  31. fedkit-0.1.0/src/fedkit/registry.py +153 -0
  32. fedkit-0.1.0/src/fedkit/renderers.py +92 -0
  33. fedkit-0.1.0/src/fedkit/schema.py +874 -0
  34. fedkit-0.1.0/src/fedkit/serializers/__init__.py +16 -0
  35. fedkit-0.1.0/src/fedkit/serializers/action.py +56 -0
  36. fedkit-0.1.0/src/fedkit/serializers/activity.py +10 -0
  37. fedkit-0.1.0/src/fedkit/serializers/actor.py +175 -0
  38. fedkit-0.1.0/src/fedkit/serializers/note.py +51 -0
  39. fedkit-0.1.0/src/fedkit/signals.py +133 -0
  40. fedkit-0.1.0/src/fedkit/signature.py +382 -0
  41. fedkit-0.1.0/src/fedkit/tasks/__init__.py +259 -0
  42. fedkit-0.1.0/src/fedkit/tasks/fetch.py +46 -0
  43. fedkit-0.1.0/src/fedkit/templates/fedkit/action.html +13 -0
  44. fedkit-0.1.0/src/fedkit/templates/fedkit/actor.html +435 -0
  45. fedkit-0.1.0/src/fedkit/templates/fedkit/follow_create.html +5 -0
  46. fedkit-0.1.0/src/fedkit/templates/fedkit/follow_delete.html +6 -0
  47. fedkit-0.1.0/src/fedkit/templates/fedkit/follow_detail.html +2 -0
  48. fedkit-0.1.0/src/fedkit/templates/fedkit/follow_list.html +8 -0
  49. fedkit-0.1.0/src/fedkit/templates/fedkit/followers.html +11 -0
  50. fedkit-0.1.0/src/fedkit/templates/fedkit/following.html +7 -0
  51. fedkit-0.1.0/src/fedkit/templates/fedkit/inbox.html +543 -0
  52. fedkit-0.1.0/src/fedkit/templates/fedkit/index.html +54 -0
  53. fedkit-0.1.0/src/fedkit/templates/fedkit/like_create.html +5 -0
  54. fedkit-0.1.0/src/fedkit/templates/fedkit/like_delete.html +6 -0
  55. fedkit-0.1.0/src/fedkit/templates/fedkit/like_detail.html +2 -0
  56. fedkit-0.1.0/src/fedkit/templates/fedkit/like_list.html +8 -0
  57. fedkit-0.1.0/src/fedkit/templates/fedkit/liked.html +93 -0
  58. fedkit-0.1.0/src/fedkit/templates/fedkit/note.html +5 -0
  59. fedkit-0.1.0/src/fedkit/templates/fedkit/outbox.html +151 -0
  60. fedkit-0.1.0/src/fedkit/tests/__init__.py +5 -0
  61. fedkit-0.1.0/src/fedkit/tests/actor.py +92 -0
  62. fedkit-0.1.0/src/fedkit/tests/rename_messages.py +458 -0
  63. fedkit-0.1.0/src/fedkit/tests/test_action.py +43 -0
  64. fedkit-0.1.0/src/fedkit/tests/test_activityobject.py +58 -0
  65. fedkit-0.1.0/src/fedkit/tests/test_actor.py +60 -0
  66. fedkit-0.1.0/src/fedkit/tests/test_fetch.py +73 -0
  67. fedkit-0.1.0/src/fedkit/tests/test_following.py +71 -0
  68. fedkit-0.1.0/src/fedkit/tests/test_inbox.py +138 -0
  69. fedkit-0.1.0/src/fedkit/tests/test_outbox.py +89 -0
  70. fedkit-0.1.0/src/fedkit/tests/test_signature.py +172 -0
  71. fedkit-0.1.0/src/fedkit/tests/test_webfinger.py +49 -0
  72. fedkit-0.1.0/src/fedkit/tests/untest_web.py +59 -0
  73. fedkit-0.1.0/src/fedkit/urls/__init__.py +80 -0
  74. fedkit-0.1.0/src/fedkit/urls/web.py +32 -0
  75. fedkit-0.1.0/src/fedkit/validators.py +104 -0
  76. fedkit-0.1.0/src/fedkit/views/__init__.py +60 -0
  77. fedkit-0.1.0/src/fedkit/views/action.py +37 -0
  78. fedkit-0.1.0/src/fedkit/views/activity.py +47 -0
  79. fedkit-0.1.0/src/fedkit/views/actor.py +54 -0
  80. fedkit-0.1.0/src/fedkit/views/followers.py +79 -0
  81. fedkit-0.1.0/src/fedkit/views/following.py +82 -0
  82. fedkit-0.1.0/src/fedkit/views/globalinbox.py +14 -0
  83. fedkit-0.1.0/src/fedkit/views/inbox.py +158 -0
  84. fedkit-0.1.0/src/fedkit/views/liked.py +46 -0
  85. fedkit-0.1.0/src/fedkit/views/nodeinfo.py +87 -0
  86. fedkit-0.1.0/src/fedkit/views/note.py +39 -0
  87. fedkit-0.1.0/src/fedkit/views/outbox.py +106 -0
  88. fedkit-0.1.0/src/fedkit/views/streaming.py +8 -0
  89. fedkit-0.1.0/src/fedkit/views/timelines.py +13 -0
  90. fedkit-0.1.0/src/fedkit/views/web/__init__.py +18 -0
  91. fedkit-0.1.0/src/fedkit/views/web/following.py +37 -0
  92. fedkit-0.1.0/src/fedkit/views/web/likes.py +132 -0
  93. fedkit-0.1.0/src/fedkit/views/webfinger.py +90 -0
fedkit-0.1.0/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 deltabps
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
fedkit-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,135 @@
1
+ Metadata-Version: 2.3
2
+ Name: fedkit
3
+ Version: 0.1.0
4
+ Summary: A reusable app to implement fediverse functionality.
5
+ Author: Andreas Neumeier
6
+ Author-email: Andreas Neumeier <andreas@neumeier.org>
7
+ License: MIT License
8
+
9
+ Copyright (c) 2025 deltabps
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16
+ Classifier: Framework :: Django
17
+ Classifier: Programming Language :: Python :: 3
18
+ Requires-Dist: celery[redis]
19
+ Requires-Dist: django>=5.2
20
+ Requires-Dist: djangorestframework
21
+ Requires-Dist: pillow
22
+ Requires-Dist: pyld
23
+ Requires-Dist: requests
24
+ Requires-Python: >=3.11
25
+ Project-URL: Bug Tracker, https://codeberg.org/deltabps/fedkit/issues
26
+ Project-URL: Changelog, https://codeberg.org/deltabps/fedkit/issues
27
+ Project-URL: Documentation, https://docs.pramari.de
28
+ Project-URL: Homepage, https://pramari.de/@fedkit
29
+ Project-URL: Repository, https://codeberg.org/deltabps/fedkit
30
+ Description-Content-Type: text/markdown
31
+
32
+ # fedkit
33
+
34
+ # fedkit: ActivityPub & ActivityStreams for Python & Django
35
+
36
+ **fedkit** is a Python library built to enable easy integration of the **ActivityStreams** and **ActivityPub** federation protocols into Python-built websites, with a particular focus on the **Django** framework.
37
+
38
+ It allows your application to participate in the Fediverse, enabling seamless communication with other federated services like Mastodon, Pleroma, and PeerTube.
39
+
40
+ -----
41
+
42
+ ## ✨ (Planned) Features
43
+
44
+ - [ ] **ActivityStreams Object Handling:** Simple and declarative Python classes for creating, validating, and serializing **ActivityStreams** objects.
45
+ - [ ] **ActivityPub Protocol Support:** Tools for handling **Inboxes**, **Outboxes**, and remote actor discovery (WebFinger).
46
+ - [ ] **Django Integration:** Built-in utilities, database models, and view helpers to quickly add federation capabilities to your existing Django project.
47
+ * [ ] **Decoupled Design:** Core federation logic can be used in any Python web framework (Flask, FastAPI, etc.).
48
+
49
+ -----
50
+
51
+ ## 🚀 Installation
52
+
53
+ You can install `fedkit` directly using pip:
54
+
55
+ ```bash
56
+ pip install fedkit
57
+ ```
58
+
59
+ ### Django Setup
60
+
61
+ 1. **Add to `INSTALLED_APPS`**:
62
+ To integrate `fedkit` into your Django project, add it to your `settings.py`:
63
+
64
+ ```python
65
+ # settings.py
66
+
67
+ INSTALLED_APPS = [
68
+ # ... your other apps
69
+ 'fedkit',
70
+ # ...
71
+ ]
72
+ ```
73
+
74
+ 2. **Include URLs**:
75
+ Add the `fedkit` URLs to your project's `urls.py` to handle standard ActivityPub routes (like actor inboxes/outboxes):
76
+
77
+ ```python
78
+ # urls.py
79
+
80
+ from django.urls import path, include
81
+
82
+ urlpatterns = [
83
+ # ... your other URLs
84
+ path('@', include('fedkit.urls')),
85
+ ]
86
+ ```
87
+
88
+ 3. **Run Migrations**:
89
+ Apply the necessary database migrations:
90
+
91
+ ```bash
92
+ python manage.py migrate
93
+ ```
94
+
95
+ -----
96
+
97
+ ## ⚙️ Basic Usage
98
+
99
+ ### 1\. Defining a Federating Model
100
+
101
+ You can extend the built-in `fedkit` Actor and Object models to make your application's entities addressable in the Fediverse.
102
+
103
+ ```python
104
+
105
+ ```
106
+
107
+ ### 2\. Creating an Activity
108
+
109
+ Use the dedicated utilities to create and sign an Activity, then deliver it to an Outbox.
110
+
111
+ ```python
112
+
113
+ ```
114
+
115
+ -----
116
+
117
+ ## 📖 Documentation & Examples
118
+
119
+ For detailed instructions on configuring federation settings, customizing views, handling **Inboxes**, and implementing full server-to-server communication, please refer to the official documentation (coming soon).
120
+
121
+ -----
122
+
123
+ ## 🤝 Contributing
124
+
125
+ **fedkit** is an open-source project and contributions are highly welcome\!
126
+
127
+ If you'd like to contribute, please check out the repository on Codeberg, fork the project, and submit a pull request with your suggested changes. You can also report issues or suggest features via the issue tracker.
128
+
129
+ -----
130
+
131
+ ## ⚖️ License
132
+
133
+ This project is licensed under the **[LICENSE\_HERE]** License.
134
+
135
+ Please see the `LICENSE` file in the repository for more details.
fedkit-0.1.0/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # fedkit
2
+
3
+ # fedkit: ActivityPub & ActivityStreams for Python & Django
4
+
5
+ **fedkit** is a Python library built to enable easy integration of the **ActivityStreams** and **ActivityPub** federation protocols into Python-built websites, with a particular focus on the **Django** framework.
6
+
7
+ It allows your application to participate in the Fediverse, enabling seamless communication with other federated services like Mastodon, Pleroma, and PeerTube.
8
+
9
+ -----
10
+
11
+ ## ✨ (Planned) Features
12
+
13
+ - [ ] **ActivityStreams Object Handling:** Simple and declarative Python classes for creating, validating, and serializing **ActivityStreams** objects.
14
+ - [ ] **ActivityPub Protocol Support:** Tools for handling **Inboxes**, **Outboxes**, and remote actor discovery (WebFinger).
15
+ - [ ] **Django Integration:** Built-in utilities, database models, and view helpers to quickly add federation capabilities to your existing Django project.
16
+ * [ ] **Decoupled Design:** Core federation logic can be used in any Python web framework (Flask, FastAPI, etc.).
17
+
18
+ -----
19
+
20
+ ## 🚀 Installation
21
+
22
+ You can install `fedkit` directly using pip:
23
+
24
+ ```bash
25
+ pip install fedkit
26
+ ```
27
+
28
+ ### Django Setup
29
+
30
+ 1. **Add to `INSTALLED_APPS`**:
31
+ To integrate `fedkit` into your Django project, add it to your `settings.py`:
32
+
33
+ ```python
34
+ # settings.py
35
+
36
+ INSTALLED_APPS = [
37
+ # ... your other apps
38
+ 'fedkit',
39
+ # ...
40
+ ]
41
+ ```
42
+
43
+ 2. **Include URLs**:
44
+ Add the `fedkit` URLs to your project's `urls.py` to handle standard ActivityPub routes (like actor inboxes/outboxes):
45
+
46
+ ```python
47
+ # urls.py
48
+
49
+ from django.urls import path, include
50
+
51
+ urlpatterns = [
52
+ # ... your other URLs
53
+ path('@', include('fedkit.urls')),
54
+ ]
55
+ ```
56
+
57
+ 3. **Run Migrations**:
58
+ Apply the necessary database migrations:
59
+
60
+ ```bash
61
+ python manage.py migrate
62
+ ```
63
+
64
+ -----
65
+
66
+ ## ⚙️ Basic Usage
67
+
68
+ ### 1\. Defining a Federating Model
69
+
70
+ You can extend the built-in `fedkit` Actor and Object models to make your application's entities addressable in the Fediverse.
71
+
72
+ ```python
73
+
74
+ ```
75
+
76
+ ### 2\. Creating an Activity
77
+
78
+ Use the dedicated utilities to create and sign an Activity, then deliver it to an Outbox.
79
+
80
+ ```python
81
+
82
+ ```
83
+
84
+ -----
85
+
86
+ ## 📖 Documentation & Examples
87
+
88
+ For detailed instructions on configuring federation settings, customizing views, handling **Inboxes**, and implementing full server-to-server communication, please refer to the official documentation (coming soon).
89
+
90
+ -----
91
+
92
+ ## 🤝 Contributing
93
+
94
+ **fedkit** is an open-source project and contributions are highly welcome\!
95
+
96
+ If you'd like to contribute, please check out the repository on Codeberg, fork the project, and submit a pull request with your suggested changes. You can also report issues or suggest features via the issue tracker.
97
+
98
+ -----
99
+
100
+ ## ⚖️ License
101
+
102
+ This project is licensed under the **[LICENSE\_HERE]** License.
103
+
104
+ Please see the `LICENSE` file in the repository for more details.
@@ -0,0 +1,62 @@
1
+ [project]
2
+ name = "fedkit"
3
+ version = "0.1.0"
4
+ description = "A reusable app to implement fediverse functionality."
5
+ readme = "README.md"
6
+ license = { file = "LICENSE" }
7
+ authors = [{ name = "Andreas Neumeier", email = "andreas@neumeier.org" }]
8
+ requires-python = ">=3.11"
9
+ classifiers = ["Framework :: Django", "Programming Language :: Python :: 3"]
10
+ dependencies = [
11
+ "celery[redis]",
12
+ "Django>=5.2",
13
+ "djangorestframework",
14
+ "Pillow",
15
+ "pyld",
16
+ "requests",
17
+ ]
18
+
19
+
20
+ [project.urls]
21
+ Homepage = "https://pramari.de/@fedkit"
22
+ Documentation = "https://docs.pramari.de"
23
+ Repository = "https://codeberg.org/deltabps/fedkit"
24
+ "Bug Tracker" = "https://codeberg.org/deltabps/fedkit/issues"
25
+ Changelog = "https://codeberg.org/deltabps/fedkit/issues"
26
+
27
+ [project.scripts]
28
+ fedkit = "fedkit.cli:main"
29
+
30
+ [build-system]
31
+ requires = ["uv_build >= 0.9.5, <0.10.0"]
32
+ build-backend = "uv_build"
33
+
34
+ [tool.pytest.ini_options]
35
+ DJANGO_SETTINGS_MODULE = "tests.settings"
36
+ pythonpath = [
37
+ "src"
38
+ ]
39
+ addopts = [
40
+ "--import-mode=importlib",
41
+ ]
42
+
43
+ [tool.ruff]
44
+ line-length = 100
45
+ indent-width = 4
46
+
47
+ [tool.ruff.format]
48
+ quote-style = "double"
49
+ indent-style = "space"
50
+ line-ending = "auto"
51
+
52
+ [tool.ruff.lint]
53
+ select = [
54
+ "F", # pyflakes
55
+ "E", # pycodestyle
56
+ "I", # isort
57
+ "RUF", # ruff-specific rules
58
+ ]
59
+ fixable = ["ALL"]
60
+
61
+ [tool.ruff.lint.pydocstyle]
62
+ convention = "google"
@@ -0,0 +1 @@
1
+ __version__ = "0.1.0"
@@ -0,0 +1,4 @@
1
+ from .follow import Accept, Follow
2
+ from .inbound import create, delete, echorequest, like, undo
3
+
4
+ __all__ = ["Accept", "Follow", "create", "delete", "echorequest", "like", "undo"]
@@ -0,0 +1,19 @@
1
+ from fedkit.activity import ActivityObject
2
+ from fedkit.models import Actor
3
+ from fedkit.signature import Signature
4
+
5
+
6
+ class BaseActivity(object):
7
+ def __init__(
8
+ self, target: Actor, activity: ActivityObject, signature: Signature
9
+ ) -> None:
10
+ self.actor = target
11
+ self.activity = activity
12
+ self.signature = signature
13
+
14
+ def __str__(self):
15
+ return f"Activity ID: {self.activity_id}, Name: {self.name}, Description: {self.description or 'No description provided'}"
16
+
17
+ def respond(self) -> (dict, bool):
18
+ """Method to respond to the activity."""
19
+ raise NotImplementedError("This method should be overridden by subclasses.")
@@ -0,0 +1,98 @@
1
+ import logging
2
+
3
+ from django.conf import settings
4
+ from rest_framework import status
5
+
6
+ from fedkit.models import Actor
7
+ from fedkit.signals import action
8
+ from fedkit.tasks import fetchRemoteActor
9
+
10
+ from .base import BaseActivity
11
+
12
+ logger = logging.getLogger(__name__)
13
+
14
+
15
+ class Accept(BaseActivity):
16
+ """
17
+ Received an Accept.
18
+
19
+ Remember the accept-id in the database.
20
+ So we can later delete the follow request.
21
+
22
+ :param target: The target of the activity
23
+ :param activity: The :py:class:fedkit.activity.Activityobject`
24
+ """
25
+
26
+ def respond(self):
27
+ """
28
+ Accept the Follow activity.
29
+ Remember the accept-id in the database.
30
+ So we can later delete the follow request.
31
+ """
32
+ from fedkit.models import Follow
33
+
34
+ assert self.target is self.activity.actor
35
+
36
+ follow = Follow.objects.get(actor=self.activity.actor)
37
+ follow.accepted = self.activity.id # remember the accept-id
38
+ follow.save()
39
+
40
+ """
41
+ .. todo::
42
+ Check the actual message that needs to be returned.
43
+ """
44
+
45
+ return {"status": "accepted."}, status.HTTP_200_OK
46
+
47
+
48
+ class Follow(BaseActivity):
49
+ def respond(self):
50
+ """
51
+ .. example:: Follow Activity {
52
+ '@context': 'https://www.w3.org/ns/activitystreams',
53
+ 'id': 'https://neumeier.org/o/ca357ba56dc24554bfb7646a1db2c67f',
54
+ 'type': 'Follow',
55
+ 'actor': 'https://neumeier.org',
56
+ 'object': 'https://pramari.de/accounts/andreas/'
57
+ }
58
+ """
59
+
60
+ logger.error(
61
+ f"Activity: {self.activity.actor} wants to follow {self.activity.object}"
62
+ )
63
+
64
+ assert self.activity.actor is not None
65
+ assert self.actor is self.activity.actor
66
+
67
+ # Step 1:
68
+ # Create the actor profile in the database
69
+ # and establish the follow relationship
70
+ remoteActor = fetchRemoteActor(self.activity.actor)
71
+ remoteActorObject = Actor.objects.get(id=remoteActor.get("id"))
72
+ localActorObject = Actor.objects.get(id=self.activity.object)
73
+ remoteActorObject.follows.add(localActorObject)
74
+
75
+ # Step 2:
76
+ # Confirm the follow request to message.actor
77
+ from fedkit.tasks.activitypub import acceptFollow
78
+
79
+ """
80
+ .. todo::
81
+ - Check if the signature is valid
82
+ - Store the follow request in the database
83
+ - Defer the task to the background
84
+ """
85
+ action_id = action.send(
86
+ sender=localActorObject, verb="Accept", action_object=remoteActorObject
87
+ )
88
+
89
+ if settings.DEBUG:
90
+ acceptFollow(remoteActor.get("inbox"), self.activity, action_id[0][1].id)
91
+ else:
92
+ acceptFollow.delay(
93
+ remoteActor.get("inbox"), self.activity, action_id[0][1].id
94
+ )
95
+
96
+ return {
97
+ "status": f"OK: {self.activity.actor} followed {self.activity.object}"
98
+ }, status.HTTP_200_OK
@@ -0,0 +1,170 @@
1
+ import logging
2
+
3
+ from django.http import JsonResponse
4
+ from webapp.signals import action
5
+
6
+ from fedkit.activity import ActivityObject
7
+ from fedkit.models import Actor
8
+
9
+ from .follow import Follow
10
+
11
+ logger = logging.getLogger(__name__)
12
+
13
+ __all__ = ["Follow", "action_decorator", "create", "like", "undo"]
14
+
15
+
16
+ def action_decorator(f):
17
+ """
18
+ Decorator for inbound activity handlers.
19
+
20
+ Not clear anymore what this does.
21
+ """
22
+
23
+ def wrapper(target, activity: ActivityObject, *args, **kwargs):
24
+ try:
25
+ localactor = Actor.objects.get(id=activity.actor)
26
+ except Actor.DoesNotExist:
27
+ logger.error(f"Actor not found: '{activity.actor}'")
28
+ return JsonResponse(
29
+ {"error": "Actor f'{activity.actor}' not found"}, status=404
30
+ )
31
+
32
+ try:
33
+ action_object = Actor.objects.get(id=activity.object)
34
+ except Actor.DoesNotExist:
35
+ logger.error(f"{activity.type}: Object not found: '{activity.object}'")
36
+ action_object = None
37
+
38
+ action.send(
39
+ sender=localactor,
40
+ verb=activity.type,
41
+ action_object=action_object,
42
+ target=target,
43
+ )
44
+
45
+ return f(target, activity, *args, **kwargs)
46
+
47
+ return wrapper
48
+
49
+
50
+ @action_decorator
51
+ def create(target: Actor, activity: ActivityObject) -> JsonResponse:
52
+ """
53
+ Create a new `:model:Note`.
54
+
55
+ Type: Note
56
+ {
57
+ 'id': 'https://23.social/users/andreasofthings/statuses/112728133944821188',
58
+ 'type': 'Note',
59
+ 'summary': None,
60
+ 'inReplyTo': None,
61
+ 'published': '2024-07-04T12:06:57Z',
62
+ 'url': 'https://23.social/@andreasofthings/112728133944821188',
63
+ 'attributedTo': 'https://23.social/users/andreasofthings',
64
+ 'to': ['https://www.w3.org/ns/activitystreams#Public'],
65
+ 'cc': ['https://23.social/users/andreasofthings/followers'],
66
+ 'sensitive': False,
67
+ 'atomUri': 'https://23.social/users/andreasofthings/statuses/112728133944821188',
68
+ 'inReplyToAtomUri': None,
69
+ 'conversation': 'tag:23.social,2024-07-04:objectId=4444254:objectType=Conversation',
70
+ 'content': '<p>I implemented http signatures (both sign and verify) for the fediverse.</p><p>In python.</p><p>I feel like I made fire.</p>',
71
+ 'contentMap': {'en': '<p>I implemented http signatures (both sign and verify) for the fediverse.</p><p>In python.</p><p>I feel like I made fire.</p>'},
72
+ 'attachment': [],
73
+ 'tag': [],
74
+ 'replies': {
75
+ 'id': 'https://23.social/users/andreasofthings/statuses/112728133944821188/replies',
76
+ 'type': 'Collection',
77
+ 'first': {
78
+ 'type': 'CollectionPage',
79
+ 'next': 'https://23.social/users/andreasofthings/statuses/112728133944821188/replies?only_other_accounts=true&page=true',
80
+ 'partOf': 'https://23.social/users/andreasofthings/statuses/112728133944821188/replies',
81
+ 'items': []
82
+ }
83
+ }
84
+ } # noqa: E501
85
+ """
86
+
87
+ logger.error(f"Create Object: {activity.object}")
88
+
89
+ note = activity.object
90
+ assert note is not None
91
+ assert isinstance(note, dict)
92
+
93
+ if note.get("type") == "Note":
94
+ from webapp.models import Note
95
+
96
+ localNote = Note.objects.create( # noqa: F841
97
+ remoteID=note.get("id"),
98
+ content=note.get("content"),
99
+ published=note.get("published"),
100
+ )
101
+
102
+ return JsonResponse(
103
+ {
104
+ "status": f"success: {activity.actor} {activity.type} {activity.object}"
105
+ }
106
+ )
107
+
108
+
109
+ @action_decorator
110
+ def delete(target: Actor, activity: ActivityObject) -> JsonResponse:
111
+ """
112
+ Delete an activity.
113
+ """
114
+
115
+ return JsonResponse({"status": "cannot delete"})
116
+
117
+
118
+ def echorequest(activity: ActivityObject, signature: str) -> JsonResponse:
119
+ """
120
+ Echo a request.
121
+ """
122
+
123
+ return JsonResponse({"status": "cannot echo"})
124
+
125
+
126
+ @action_decorator
127
+ def like(target: Actor, activity: ActivityObject) -> JsonResponse:
128
+ """
129
+ Like an activity.
130
+ """
131
+
132
+ return JsonResponse({"status": "cannot like"})
133
+
134
+
135
+ @action_decorator
136
+ def undo(target: Actor, activity: ActivityObject):
137
+ """
138
+ Undo an activity.
139
+
140
+ Object: (example)
141
+ {
142
+ 'id': 'https://23.social/b271295c-7a1b-4da8-ae58-927fea32bb60',
143
+ 'type': 'Follow',
144
+ 'actor': 'https://23.social/users/andreasofthings',
145
+ 'object': 'https://pramari.de/@andreas'
146
+ }
147
+ """
148
+ logger.error(f"Activity Object: {activity}")
149
+
150
+ if not activity.id:
151
+ return JsonResponse({"status": "missing id"})
152
+
153
+ if not activity.object:
154
+ return JsonResponse({"status": "missing object"})
155
+
156
+ if (
157
+ not activity.actor and activity.object.get("type").lower() != "follow"
158
+ ):
159
+ return JsonResponse({"status": "invalid object/unsupported activity"})
160
+
161
+ from webapp.models.activitypub.actor import Follow
162
+
163
+ try:
164
+ follow = Follow.objects.get(accepted=activity.object.get("id"))
165
+ except Follow.DoesNotExist:
166
+ return JsonResponse({"status": "follow not found"})
167
+ follow.delete()
168
+ logger.error(f"{activity.actor} has undone {activity.object}")
169
+
170
+ return JsonResponse({"status": "undone"})