Skip to content

Commit

Permalink
Include the correct protocol and port in remote IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
Minnozz committed Apr 24, 2024
1 parent 609bc15 commit 4f58b11
Show file tree
Hide file tree
Showing 21 changed files with 46 additions and 58 deletions.
4 changes: 2 additions & 2 deletions bookwyrm/models/author.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pgtrigger

from bookwyrm import activitypub
from bookwyrm.settings import DOMAIN
from bookwyrm.settings import BASE_URL
from bookwyrm.utils.db import format_trigger

from .book import BookDataModel, MergedAuthor
Expand Down Expand Up @@ -70,7 +70,7 @@ def isfdb_link(self):

def get_remote_id(self):
"""editions and works both use "book" instead of model_name"""
return f"https://{DOMAIN}/author/{self.id}"
return f"{BASE_URL}/author/{self.id}"

class Meta:
"""sets up indexes and triggers"""
Expand Down
6 changes: 3 additions & 3 deletions bookwyrm/models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django.utils.translation import gettext_lazy as _
from django.utils.text import slugify

from bookwyrm.settings import DOMAIN
from bookwyrm.settings import BASE_URL
from .fields import RemoteIdField


Expand Down Expand Up @@ -38,7 +38,7 @@ class BookWyrmModel(models.Model):

def get_remote_id(self):
"""generate the url that resolves to the local object, without a slug"""
base_path = f"https://{DOMAIN}"
base_path = BASE_URL
if hasattr(self, "user"):
base_path = f"{base_path}{self.user.local_path}"

Expand All @@ -53,7 +53,7 @@ class Meta:
@property
def local_path(self):
"""how to link to this object in the local app, with a slug"""
local = self.get_remote_id().replace(f"https://{DOMAIN}", "")
local = self.get_remote_id().replace(BASE_URL, "")

name = None
if hasattr(self, "name_field"):
Expand Down
4 changes: 2 additions & 2 deletions bookwyrm/models/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from bookwyrm.isbn.isbn import hyphenator_singleton as hyphenator
from bookwyrm.preview_images import generate_edition_preview_image_task
from bookwyrm.settings import (
DOMAIN,
BASE_URL,
DEFAULT_LANGUAGE,
LANGUAGE_ARTICLES,
ENABLE_PREVIEW_IMAGES,
Expand Down Expand Up @@ -327,7 +327,7 @@ def save(self, *args: Any, **kwargs: Any) -> None:

def get_remote_id(self):
"""editions and works both use "book" instead of model_name"""
return f"https://{DOMAIN}/book/{self.id}"
return f"{BASE_URL}/book/{self.id}"

def guess_sort_title(self):
"""Get a best-guess sort title for the current book"""
Expand Down
4 changes: 2 additions & 2 deletions bookwyrm/models/group.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
""" do book related things with other users """
from django.db import models, IntegrityError, transaction
from django.db.models import Q
from bookwyrm.settings import DOMAIN
from bookwyrm.settings import BASE_URL
from .base_model import BookWyrmModel
from . import fields
from .relationship import UserBlocks
Expand All @@ -17,7 +17,7 @@ class Group(BookWyrmModel):

def get_remote_id(self):
"""don't want the user to be in there in this case"""
return f"https://{DOMAIN}/group/{self.id}"
return f"{BASE_URL}/group/{self.id}"

@classmethod
def followers_filter(cls, queryset, viewer):
Expand Down
4 changes: 2 additions & 2 deletions bookwyrm/models/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.utils import timezone

from bookwyrm import activitypub
from bookwyrm.settings import DOMAIN
from bookwyrm.settings import BASE_URL

from .activitypub_mixin import CollectionItemMixin, OrderedCollectionMixin
from .base_model import BookWyrmModel
Expand Down Expand Up @@ -50,7 +50,7 @@ class List(OrderedCollectionMixin, BookWyrmModel):

def get_remote_id(self):
"""don't want the user to be in there in this case"""
return f"https://{DOMAIN}/list/{self.id}"
return f"{BASE_URL}/list/{self.id}"

@property
def collection_queryset(self):
Expand Down
4 changes: 2 additions & 2 deletions bookwyrm/models/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.db import models
from django.utils.translation import gettext_lazy as _

from bookwyrm.settings import DOMAIN
from bookwyrm.settings import BASE_URL
from .base_model import BookWyrmModel


Expand Down Expand Up @@ -46,7 +46,7 @@ def raise_not_editable(self, viewer):
raise PermissionDenied()

def get_remote_id(self):
return f"https://{DOMAIN}/settings/reports/{self.id}"
return f"{BASE_URL}/settings/reports/{self.id}"

def comment(self, user, note):
"""comment on a report"""
Expand Down
4 changes: 2 additions & 2 deletions bookwyrm/models/shelf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.utils import timezone

from bookwyrm import activitypub
from bookwyrm.settings import DOMAIN
from bookwyrm.settings import BASE_URL
from bookwyrm.tasks import BROADCAST
from .activitypub_mixin import CollectionItemMixin, OrderedCollectionMixin
from .base_model import BookWyrmModel
Expand Down Expand Up @@ -71,7 +71,7 @@ def get_remote_id(self):
@property
def local_path(self):
"""No slugs"""
return self.get_remote_id().replace(f"https://{DOMAIN}", "")
return self.get_remote_id().replace(BASE_URL, "")

def raise_not_deletable(self, viewer):
"""don't let anyone delete a default shelf"""
Expand Down
13 changes: 3 additions & 10 deletions bookwyrm/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from bookwyrm.models.shelf import Shelf
from bookwyrm.models.status import Status
from bookwyrm.preview_images import generate_user_preview_image_task
from bookwyrm.settings import BASE_URL, DOMAIN, ENABLE_PREVIEW_IMAGES, USE_HTTPS, LANGUAGES
from bookwyrm.settings import BASE_URL, ENABLE_PREVIEW_IMAGES, LANGUAGES
from bookwyrm.signatures import create_key_pair
from bookwyrm.tasks import app, MISC
from bookwyrm.utils import regex
Expand All @@ -42,12 +42,6 @@ def get_feed_filter_choices():
return [f[0] for f in FeedFilterChoices]


def site_link():
"""helper for generating links to the site"""
protocol = "https" if USE_HTTPS else "http"
return f"{protocol}://{DOMAIN}"


# pylint: disable=too-many-public-methods
class User(OrderedCollectionPageMixin, AbstractUser):
"""a user who wants to read books"""
Expand Down Expand Up @@ -368,11 +362,10 @@ def save(self, *args, **kwargs):

with transaction.atomic():
# populate fields for local users
link = site_link()
self.remote_id = f"{link}/user/{self.localname}"
self.remote_id = f"{BASE_URL}/user/{self.localname}"
self.followers_url = f"{self.remote_id}/followers"
self.inbox = f"{self.remote_id}/inbox"
self.shared_inbox = f"{link}/inbox"
self.shared_inbox = f"{BASE_URL}/inbox"
self.outbox = f"{self.remote_id}/outbox"

# an id needs to be set before we can proceed with related models
Expand Down
8 changes: 3 additions & 5 deletions bookwyrm/tests/connectors/test_abstract_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from bookwyrm import models
from bookwyrm.connectors import abstract_connector, ConnectorException
from bookwyrm.connectors.abstract_connector import Mapping, get_data
from bookwyrm.settings import DOMAIN
from bookwyrm.settings import BASE_URL


class AbstractConnector(TestCase):
Expand Down Expand Up @@ -86,7 +86,7 @@ def test_abstract_connector_init(self):
def test_get_or_create_book_existing(self):
"""find an existing book by remote/origin id"""
self.assertEqual(models.Book.objects.count(), 1)
self.assertEqual(self.book.remote_id, f"https://{DOMAIN}/book/{self.book.id}")
self.assertEqual(self.book.remote_id, f"{BASE_URL}/book/{self.book.id}")
self.assertEqual(self.book.origin_id, "https://example.com/book/1234")

# dedupe by origin id
Expand All @@ -95,9 +95,7 @@ def test_get_or_create_book_existing(self):
self.assertEqual(result, self.book)

# dedupe by remote id
result = self.connector.get_or_create_book(
f"https://{DOMAIN}/book/{self.book.id}"
)
result = self.connector.get_or_create_book(f"{BASE_URL}/book/{self.book.id}")

self.assertEqual(models.Book.objects.count(), 1)
self.assertEqual(result, self.book)
Expand Down
6 changes: 4 additions & 2 deletions bookwyrm/tests/data/ap_user_move.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
"bookwyrmUser": true,
"manuallyApprovesFollowers": false,
"discoverable": false,
"alsoKnownAs": ["https://your.domain.here/user/rat"],
"alsoKnownAs": [
"https://your.domain.here:4242/user/rat"
],
"devices": "https://friend.camp/users/tripofmice/collections/devices",
"tag": [],
"icon": {
"type": "Image",
"mediaType": "image/png",
"url": "https://example.com/images/avatars/AL-2-crop-50.png"
}
}
}
Binary file modified bookwyrm/tests/data/bookwyrm_account_export.tar.gz
Binary file not shown.
6 changes: 3 additions & 3 deletions bookwyrm/tests/data/user_import.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
"attributedTo": "https://www.example.com//user/rat",
"content": "<p>I like it</p>",
"to": [
"https://your.domain.here/user/rat/followers"
"https://your.domain.here:4242/user/rat/followers"
],
"cc": [],
"replies": {
Expand Down Expand Up @@ -395,7 +395,7 @@
"https://local.lists/9999"
],
"follows": [
"https://your.domain.here/user/rat"
"https://your.domain.here:4242/user/rat"
],
"blocks": ["https://your.domain.here/user/badger"]
"blocks": ["https://your.domain.here:4242/user/badger"]
}
8 changes: 4 additions & 4 deletions bookwyrm/tests/models/test_base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from bookwyrm import models
from bookwyrm.models import base_model
from bookwyrm.settings import DOMAIN
from bookwyrm.settings import BASE_URL


# pylint: disable=attribute-defined-outside-init
Expand Down Expand Up @@ -44,14 +44,14 @@ def test_remote_id(self):
"""these should be generated"""
self.test_model.id = 1 # pylint: disable=invalid-name
expected = self.test_model.get_remote_id()
self.assertEqual(expected, f"https://{DOMAIN}/bookwyrmtestmodel/1")
self.assertEqual(expected, f"{BASE_URL}/bookwyrmtestmodel/1")

def test_remote_id_with_user(self):
"""format of remote id when there's a user object"""
self.test_model.user = self.local_user
self.test_model.id = 1
expected = self.test_model.get_remote_id()
self.assertEqual(expected, f"https://{DOMAIN}/user/mouse/bookwyrmtestmodel/1")
self.assertEqual(expected, f"{BASE_URL}/user/mouse/bookwyrmtestmodel/1")

def test_set_remote_id(self):
"""this function sets remote ids after creation"""
Expand All @@ -60,7 +60,7 @@ def test_set_remote_id(self):
instance = models.Work.objects.create(title="work title")
instance.remote_id = None
base_model.set_remote_id(None, instance, True)
self.assertEqual(instance.remote_id, f"https://{DOMAIN}/book/{instance.id}")
self.assertEqual(instance.remote_id, f"{BASE_URL}/book/{instance.id}")

# shouldn't set remote_id if it's not created
instance.remote_id = None
Expand Down
2 changes: 1 addition & 1 deletion bookwyrm/tests/models/test_book_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def setUpTestData(cls):

def test_remote_id(self):
"""fanciness with remote/origin ids"""
remote_id = f"https://{settings.DOMAIN}/book/{self.work.id}"
remote_id = f"{settings.BASE_URL}/book/{self.work.id}"
self.assertEqual(self.work.get_remote_id(), remote_id)
self.assertEqual(self.work.remote_id, remote_id)

Expand Down
2 changes: 1 addition & 1 deletion bookwyrm/tests/models/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def setUpTestData(cls):
def test_remote_id(self, *_):
"""shelves use custom remote ids"""
book_list = models.List.objects.create(name="Test List", user=self.local_user)
expected_id = f"https://{settings.DOMAIN}/list/{book_list.id}"
expected_id = f"{settings.BASE_URL}/list/{book_list.id}"
self.assertEqual(book_list.get_remote_id(), expected_id)

def test_to_activity(self, *_):
Expand Down
2 changes: 1 addition & 1 deletion bookwyrm/tests/models/test_shelf_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_remote_id(self, *_):
shelf = models.Shelf.objects.create(
name="Test Shelf", identifier="test-shelf", user=self.local_user
)
expected_id = f"https://{settings.DOMAIN}/user/mouse/books/test-shelf"
expected_id = f"{settings.BASE_URL}/user/mouse/books/test-shelf"
self.assertEqual(shelf.get_remote_id(), expected_id)

def test_to_activity(self, *_):
Expand Down
4 changes: 2 additions & 2 deletions bookwyrm/tests/models/test_status_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def setUp(self):
def test_status_generated_fields(self, *_):
"""setting remote id"""
status = models.Status.objects.create(content="bleh", user=self.local_user)
expected_id = f"https://{settings.DOMAIN}/user/mouse/status/{status.id}"
expected_id = f"{settings.BASE_URL}/user/mouse/status/{status.id}"
self.assertEqual(status.remote_id, expected_id)
self.assertEqual(status.privacy, "public")

Expand Down Expand Up @@ -151,7 +151,7 @@ def test_status_with_hashtag_to_activity(self, *_):
self.assertEqual(activity["tag"][0]["type"], "Hashtag")
self.assertEqual(activity["tag"][0]["name"], "#content")
self.assertEqual(
activity["tag"][0]["href"], f"https://{settings.DOMAIN}/hashtag/{tag.id}"
activity["tag"][0]["href"], f"{settings.BASE_URL}/hashtag/{tag.id}"
)

def test_status_with_mention_to_activity(self, *_):
Expand Down
13 changes: 5 additions & 8 deletions bookwyrm/tests/models/test_user_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@

from bookwyrm import models
from bookwyrm.management.commands import initdb
from bookwyrm.settings import USE_HTTPS, DOMAIN
from bookwyrm.settings import DOMAIN, BASE_URL


# pylint: disable=missing-class-docstring
# pylint: disable=missing-function-docstring
class User(TestCase):

protocol = "https://" if USE_HTTPS else "http://"

@classmethod
def setUpTestData(cls):
with (
Expand Down Expand Up @@ -49,11 +46,11 @@ def setUpTestData(cls):

def test_computed_fields(self):
"""username instead of id here"""
expected_id = f"{self.protocol}{DOMAIN}/user/mouse"
expected_id = f"{BASE_URL}/user/mouse"
self.assertEqual(self.user.remote_id, expected_id)
self.assertEqual(self.user.username, f"mouse@{DOMAIN}")
self.assertEqual(self.user.localname, "mouse")
self.assertEqual(self.user.shared_inbox, f"{self.protocol}{DOMAIN}/inbox")
self.assertEqual(self.user.shared_inbox, f"{BASE_URL}/inbox")
self.assertEqual(self.user.inbox, f"{expected_id}/inbox")
self.assertEqual(self.user.outbox, f"{expected_id}/outbox")
self.assertEqual(self.user.followers_url, f"{expected_id}/followers")
Expand Down Expand Up @@ -130,7 +127,7 @@ def test_save_auth_group(self):
patch("bookwyrm.lists_stream.populate_lists_task.delay"),
):
user = models.User.objects.create_user(
f"test2{DOMAIN}",
"test2",
"test2@bookwyrm.test",
localname="test2",
**user_attrs,
Expand All @@ -145,7 +142,7 @@ def test_save_auth_group(self):
patch("bookwyrm.lists_stream.populate_lists_task.delay"),
):
user = models.User.objects.create_user(
f"test1{DOMAIN}",
"test1",
"test1@bookwyrm.test",
localname="test1",
**user_attrs,
Expand Down
2 changes: 0 additions & 2 deletions bookwyrm/tests/views/preferences/test_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ def setUpTestData(cls):
patch("bookwyrm.lists_stream.populate_lists_task.delay"),
patch("bookwyrm.suggested_users.rerank_user_task.delay"),
):

cls.local_user = models.User.objects.create_user(
"rat",
"rat@rat.com",
"ratword",
local=True,
discoverable=True,
localname="rat",
remote_id="https://your.domain.here/user/rat",
)

with (
Expand Down
4 changes: 2 additions & 2 deletions bookwyrm/tests/views/test_isbn.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from bookwyrm import models, views
from bookwyrm.tests.validate_html import validate_html
from bookwyrm.settings import DOMAIN
from bookwyrm.settings import BASE_URL


class IsbnViews(TestCase):
Expand Down Expand Up @@ -55,7 +55,7 @@ def test_isbn_json_response(self):
data = json.loads(response.content)
self.assertEqual(len(data), 1)
self.assertEqual(data[0]["title"], "Test Book")
self.assertEqual(data[0]["key"], f"https://{DOMAIN}/book/{self.book.id}")
self.assertEqual(data[0]["key"], f"{BASE_URL}/book/{self.book.id}")

def test_isbn_html_response(self):
"""searches local data only and returns book data in json format"""
Expand Down

0 comments on commit 4f58b11

Please sign in to comment.