Browse Source

Implement inbox Update for Person; Add summary support

Dashie der otter 4 weeks ago
parent
commit
63797db922
3 changed files with 42 additions and 2 deletions
  1. 15
    1
      activitypub/backend.py
  2. 26
    0
      models.py
  3. 1
    1
      templates/macros.jinja2

+ 15
- 1
activitypub/backend.py View File

@@ -1,7 +1,7 @@
1 1
 from little_boxes import activitypub as ap
2 2
 from flask import current_app
3 3
 import requests
4
-from models import db, Activity, create_remote_actor, Actor
4
+from models import db, Activity, create_remote_actor, Actor, update_remote_actor
5 5
 from urllib.parse import urlparse
6 6
 from .vars import Box
7 7
 from version import VERSION
@@ -209,3 +209,17 @@ class Reel2BitsBackend(ap.Backend):
209 209
     def fetch_iri(self, iri: str) -> ap.ObjectType:
210 210
         current_app.logger.debug(f"asked to fetch {iri}")
211 211
         return self._fetch_iri(iri)
212
+
213
+    def inbox_update(self, as_actor: ap.Person, update: ap.Update) -> None:
214
+        obj = update.get_object()
215
+        current_app.logger.debug(f"inbox_update {obj.ACTIVITY_TYPE} {obj!r} as {as_actor!r}")
216
+
217
+        db_actor = Actor.query.filter(Actor.url == as_actor.id).first()
218
+        if not db_actor:
219
+            current_app.logger.error(f"cannot find actor {as_actor!r}")
220
+            return
221
+
222
+        if obj.ACTIVITY_TYPE == ap.ActivityType.PERSON:
223
+            update_remote_actor(db_actor.id, obj)
224
+        else:
225
+            raise NotImplementedError

+ 26
- 0
models.py View File

@@ -542,6 +542,7 @@ def create_remote_actor(activity_actor: ap.BaseActivity):
542 542
     domain = urlparse(activity_actor.url)
543 543
     actor.domain = domain.netloc
544 544
     actor.type = "Person"
545
+    # FIXME: test for .name, it won't exist if not set (at least for mastodon)
545 546
     actor.name = activity_actor.preferredUsername  # mastodon don't have .name
546 547
     actor.manually_approves_followers = False
547 548
     actor.url = activity_actor.id  # FIXME: or .id ??? [cf backend.py:52-53]
@@ -549,5 +550,30 @@ def create_remote_actor(activity_actor: ap.BaseActivity):
549 550
     actor.inbox_url = activity_actor.inbox
550 551
     actor.outbox_url = activity_actor.outbot
551 552
     actor.public_key = activity_actor.get_key().pubkey_pem
553
+    actor.summary = activity_actor.summary
552 554
 
553 555
     return actor
556
+
557
+
558
+def update_remote_actor(actor_id: int, activity_actor: ap.BaseActivity) -> None:
559
+    """
560
+    :param actor_id: an Actor db ID
561
+    :param activity_actor: a Little Boxes Actor object
562
+    :return: nothing
563
+    """
564
+    actor = Actor.query.filter(Actor.id == actor_id).first()
565
+    current_app.logger.debug(f"asked to update Actor {actor_id}: {activity_actor!r}")
566
+
567
+    actor.preferred_username = activity_actor.preferredUsername
568
+    domain = urlparse(activity_actor.url)
569
+    actor.domain = domain.netloc
570
+    actor.name = activity_actor.name
571
+    actor.manually_approves_followers = False
572
+    actor.url = activity_actor.id  # FIXME: or .id ??? [cf backend.py:52-53]
573
+    actor.shared_inbox_url = activity_actor._data.get("endpoints", {}).get("sharedInbox")
574
+    actor.inbox_url = activity_actor.inbox
575
+    actor.outbox_url = activity_actor.outbot
576
+    actor.public_key = activity_actor.get_key().pubkey_pem
577
+    actor.summary = activity_actor.summary
578
+
579
+    db.session.commit()

+ 1
- 1
templates/macros.jinja2 View File

@@ -7,7 +7,7 @@
7 7
             {% else %}
8 8
                 <h6 class="card-title"><a href="{{ url }}">{{ name }}</a></h6>
9 9
             {% endif %}
10
-            <p class="card-text">{{ summary or "" }}</p>
10
+            <p class="card-text">{{ summary|safe or "" }}</p>
11 11
         </div>
12 12
         {% if current_user.is_authenticated %}
13 13
             {% if follow != None %}

Loading…
Cancel
Save