Repository: bookwyrm-social/bookwyrm

Issue:
Issues with federating book links
**Describe the bug**
This may be 2 bugs but I'm not sure:

**1)** Admitted links that come from another instance show up in my pending link domains but are not connected to a book.
![image](https://user-images.githubusercontent.com/66644064/152956488-dab12631-5174-4aae-a368-6ccd3ad8d201.png)

**2)** The other way round, when I import a book from wyrms to b.s, it doesn't have the links (this may be due to them not being approved for b.s though):
![image](https://user-images.githubusercontent.com/66644064/152956983-152d65c1-4e0e-428b-8dd5-dc1df4c900f5.png)




Discussion:
After a little investigation I think the issue is that file links are being created as non-file links (the base model), which don't have associated books

Resolving patch:
diff --git a/bookwyrm/activitypub/base_activity.py b/bookwyrm/activitypub/base_activity.py
--- a/bookwyrm/activitypub/base_activity.py
+++ b/bookwyrm/activitypub/base_activity.py
@@ -227,7 +227,7 @@ def set_related_field(
     model_field = getattr(model, related_field_name)
     if hasattr(model_field, "activitypub_field"):
         setattr(activity, getattr(model_field, "activitypub_field"), instance.remote_id)
-    item = activity.to_model()
+    item = activity.to_model(model=model)
 
     # if the related field isn't serialized (attachments on Status), then
     # we have to set it post-creation
@@ -298,6 +298,7 @@ class Link(ActivityObject):
     mediaType: str = None
     id: str = None
     attributedTo: str = None
+    availability: str = None
     type: str = "Link"
 
     def serialize(self, **kwargs):


Test patch:
diff --git a/bookwyrm/tests/models/test_book_model.py b/bookwyrm/tests/models/test_book_model.py
--- a/bookwyrm/tests/models/test_book_model.py
+++ b/bookwyrm/tests/models/test_book_model.py
@@ -1,7 +1,6 @@
 """ testing models """
 from dateutil.parser import parse
 
-from imagekit.models import ImageSpecField
 from django.test import TestCase
 from django.utils import timezone
 
diff --git a/bookwyrm/tests/views/books/test_links.py b/bookwyrm/tests/views/books/test_links.py
--- a/bookwyrm/tests/views/books/test_links.py
+++ b/bookwyrm/tests/views/books/test_links.py
@@ -80,6 +80,15 @@ def test_add_link_post(self, *_):
         activity = json.loads(mock.call_args[1]["args"][1])
         self.assertEqual(activity["type"], "Update")
         self.assertEqual(activity["object"]["type"], "Edition")
+        self.assertIsInstance(activity["object"]["fileLinks"], list)
+        self.assertEqual(
+            activity["object"]["fileLinks"][0]["href"], "https://www.example.com"
+        )
+        self.assertEqual(activity["object"]["fileLinks"][0]["mediaType"], "HTML")
+        self.assertEqual(
+            activity["object"]["fileLinks"][0]["attributedTo"],
+            self.local_user.remote_id,
+        )
 
         link = models.FileLink.objects.get()
         self.assertEqual(link.name, "www.example.com")
diff --git a/bookwyrm/tests/views/inbox/test_inbox_update.py b/bookwyrm/tests/views/inbox/test_inbox_update.py
--- a/bookwyrm/tests/views/inbox/test_inbox_update.py
+++ b/bookwyrm/tests/views/inbox/test_inbox_update.py
@@ -6,7 +6,6 @@
 from django.test import TestCase
 
 from bookwyrm import models, views
-from bookwyrm.activitypub.base_activity import set_related_field
 
 
 # pylint: disable=too-many-public-methods