From f46eff65fd998504a3dee5b8dfd02009f3c0e420 Mon Sep 17 00:00:00 2001 From: jedi Date: Thu, 27 Aug 2026 00:37:32 +0200 Subject: [PATCH 01/18] stash --- docs/development.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/development.md b/docs/development.md index b4e1dcd..9001215 100644 --- a/docs/development.md +++ b/docs/development.md @@ -108,4 +108,11 @@ The two backend instances are set up to use the domains `a.localhost` and `b.loc server is used to direct the frontend to the correct backend instance. The frontend is configured to act as if it was served from the domain `a.localhost`. Access the frontend at `http://localhost:8080/`, backend at `http://localhost:8080/api/`, api docs -at `http://localhost:8080/docs/` and the wiki at `http://localhost:8080/wiki/`. \ No newline at end of file +at `http://localhost:8080/docs/` and the wiki at `http://localhost:8080/wiki/`. + +The dev proxies terminate TLS with `frontend/.local/localhost.crt`, signed by the dev-only CA at +`frontend/.local/RootCA.crt` (regenerate both with `frontend/.local/make_ca.sh` and +`make_localhost.sh` if they've expired). Import `RootCA.crt` into your OS/browser trust store once; +without that, requests the frontend makes itself (federation calls, the DoH lookup) will fail with +opaque network errors even though clicking through the browser's own cert warning for the page you +navigated to by hand looked fine. \ No newline at end of file From d56784eb8d4ee2dbb01b4668e3d45bc2d23d1f64 Mon Sep 17 00:00:00 2001 From: jedi Date: Thu, 27 Aug 2026 01:34:34 +0200 Subject: [PATCH 02/18] stash --- backend/files/media_urls.py | 4 +- backend/files/tests.py | 32 +++- backend/toolshed/tests/test_files.py | 20 ++- backend/toolshed/tests/test_inventory.py | 205 +++++++++++++++++------ backend/toolshed/tests/test_locations.py | 113 ++++++++----- frontend/src/short-id.js | 16 ++ frontend/src/tests/short-id.js | 36 +++- frontend/src/views/StorageLocation.vue | 2 +- 8 files changed, 335 insertions(+), 93 deletions(-) diff --git a/backend/files/media_urls.py b/backend/files/media_urls.py index fe8bdc7..b8c90f8 100644 --- a/backend/files/media_urls.py +++ b/backend/files/media_urls.py @@ -25,9 +25,11 @@ THUMBNAIL_SIZES = (32, 64, 256) def _accessible_files(request): # Shared by media_urls and thumbnail_urls: a file is visible if the requester is - # friends-or-self with whatever references it (item, profile picture), or it's their own staged photo. + # friends-or-self with whatever references it (item, profile picture), a member of the group + # that owns the item it's attached to, or it's their own staged photo. return File.objects.filter( Q(connected_items__owner__in=request.user.friends_or_self()) | + Q(connected_items__owner_group__in=request.user.member_of_groups.all()) | Q(profile_picture_users__in=request.user.friends_or_self()) | Q(staged_by_workflows__owner__in=request.user.user.all()) ).distinct() diff --git a/backend/files/tests.py b/backend/files/tests.py index 8c160e9..bf2c498 100644 --- a/backend/files/tests.py +++ b/backend/files/tests.py @@ -7,7 +7,8 @@ from django.core.files.base import ContentFile from django.core.files.storage import DefaultStorage, default_storage from django.db import IntegrityError, transaction from django.test import Client, override_settings -from authentication.tests import SignatureAuthClient, ToolshedTestCase, UserTestMixin +from authentication.tests import SignatureAuthClient, ToolshedTestCase, UserTestMixin, GroupTestMixin +from toolshed.models import InventoryItem from toolshed.tests import InventoryTestMixin from nacl.hash import sha256 from nacl.encoding import HexEncoder @@ -216,6 +217,35 @@ class MediaUrlTestCase(FilesTestMixin, UserTestMixin, InventoryTestMixin, Toolsh self.assertEqual(reply.status_code, 404) +class GroupOwnedMediaUrlTestCase(FilesTestMixin, UserTestMixin, GroupTestMixin, ToolshedTestCase): + """_accessible_files() only checked connected_items__owner (personal items) before, never + connected_items__owner_group - a group-owned item's own files were unreachable via /media/ or + /thumbnails/ for every member, including ones who could see and edit the item itself.""" + + def setUp(self): + super().setUp() + self.prepare_files() + self.prepare_users() + self.prepare_groups() + self.f['group1'].members.add(self.f['local_user2'].public_identity) + self.f['group_item'] = InventoryItem.create_for_owner( + owner_group=self.f['group1'], owned_quantity=1, name='group-drill') + self.f['group_item'].files.add(self.f['test_file1']) + + @override_settings(SERVE_X_ACCEL_REDIRECT=True) + def test_group_member_can_view_group_item_file(self): + reply = client.get( + f"/media/{self.f['hash1'][:2]}/{self.f['hash1'][2:4]}/{self.f['hash1'][4:6]}/{self.f['hash1'][6:]}", + self.f['local_user2']) + self.assertEqual(reply.status_code, 200) + + def test_non_member_cannot_view_group_item_file(self): + reply = client.get( + f"/media/{self.f['hash1'][:2]}/{self.f['hash1'][2:4]}/{self.f['hash1'][4:6]}/{self.f['hash1'][6:]}", + self.f['ext_user1']) + self.assertEqual(reply.status_code, 404) + + class ThumbnailUrlTestCase(FilesTestMixin, UserTestMixin, InventoryTestMixin, ToolshedTestCase): def setUp(self): super().setUp() diff --git a/backend/toolshed/tests/test_files.py b/backend/toolshed/tests/test_files.py index a6877e5..1aa1356 100644 --- a/backend/toolshed/tests/test_files.py +++ b/backend/toolshed/tests/test_files.py @@ -1,4 +1,5 @@ from django.test import Client +from authentication.models import Group from authentication.tests import SignatureAuthClient, UserTestMixin, GroupTestMixin, ToolshedTestCase from files.tests import FilesTestMixin from toolshed.models import File, InventoryItem @@ -140,7 +141,7 @@ class FileApiTestCase(UserTestMixin, FilesTestMixin, InventoryTestMixin, Toolshe self.assertEqual(self.f['item1'].files.count(), 2) def test_get_inventory(self): - reply = client.get('/api/inventory_items/', self.f['local_user1']) + reply = client.get('/api/inventory_items/{}/'.format(self.f['local_user1']), self.f['local_user1']) self.assertEqual(reply.status_code, 200) self.assertEqual(len(reply.json()), 2) self.assertEqual(reply.json()[0]['name'], 'test1') @@ -213,3 +214,20 @@ class GroupOwnedFileApiTestCase(UserTestMixin, GroupTestMixin, FilesTestMixin, T self.f['ext_user1']) self.assertEqual(response.status_code, 404) self.assertEqual(self.f['group_item'].files.count(), 1) + + def test_item_files_when_id_collides_across_two_groups(self): + group2 = Group.objects.create(name='group2', domain=self.f['example_com'].name) + group2.members.add(self.f['local_user2'].public_identity) + group2_item = InventoryItem.create_for_owner( + owner_group=group2, owned_quantity=1, name='group2-drill', availability_policy='private') + self.assertEqual(self.f['group_item'].id, group2_item.id) + + response = client.get(f"/api/item_files/{self.f['group_item'].id}/", self.f['local_user2']) + + self.assertEqual(response.status_code, 200) + + def test_group_item_files_listed_in_all_files(self): + response = client.get('/api/files/', self.f['local_user2']) + self.assertEqual(response.status_code, 200) + self.assertEqual(len(response.json()), 1) + self.assertEqual(response.json()[0]['id'], self.f['test_file1'].id) diff --git a/backend/toolshed/tests/test_inventory.py b/backend/toolshed/tests/test_inventory.py index 8a1b1a5..79ba9d9 100644 --- a/backend/toolshed/tests/test_inventory.py +++ b/backend/toolshed/tests/test_inventory.py @@ -1,7 +1,7 @@ from authentication.models import Group from authentication.tests import SignatureAuthClient, UserTestMixin, GroupTestMixin, ToolshedTestCase from files.tests import FilesTestMixin -from toolshed.models import InventoryItem, Category +from toolshed.models import InventoryItem, Category, StorageLocation from toolshed.tests import InventoryTestMixin, CategoryTestMixin, TagTestMixin, PropertyTestMixin, LocationTestMixin client = SignatureAuthClient() @@ -16,9 +16,10 @@ class InventoryApiTestCase(UserTestMixin, InventoryTestMixin, ToolshedTestCase): self.prepare_tags() self.prepare_properties() self.prepare_inventory() + self.own_handle = str(self.f['local_user1']) def test_get_inventory(self): - reply = client.get('/api/inventory_items/', self.f['local_user1']) + reply = client.get('/api/inventory_items/{}/'.format(self.own_handle), self.f['local_user1']) self.assertEqual(reply.status_code, 200) self.assertEqual(len(reply.json()), 2) self.assertEqual(reply.json()[0]['name'], 'test1') @@ -38,7 +39,7 @@ class InventoryApiTestCase(UserTestMixin, InventoryTestMixin, ToolshedTestCase): self.assertEqual(reply.json()[1]['availability_policy'], 'friends') def test_post_new_item(self): - reply = client.post('/api/inventory_items/', self.f['local_user1'], { + reply = client.post('/api/inventory_items/{}/'.format(self.own_handle), self.f['local_user1'], { 'availability_policy': 'rent', 'category': 'cat2', 'name': 'test3', @@ -61,7 +62,7 @@ class InventoryApiTestCase(UserTestMixin, InventoryTestMixin, ToolshedTestCase): self.assertEqual([p.value for p in item.itemproperty_set.all()], ['value3', 'value4']) def test_post_new_item2(self): - reply = client.post('/api/inventory_items/', self.f['local_user1'], { + reply = client.post('/api/inventory_items/{}/'.format(self.own_handle), self.f['local_user1'], { 'availability_policy': 'share', 'name': 'test3', 'description': 'test', @@ -80,7 +81,7 @@ class InventoryApiTestCase(UserTestMixin, InventoryTestMixin, ToolshedTestCase): self.assertEqual([p for p in item.properties.all()], []) def test_post_new_item_empty(self): - reply = client.post('/api/inventory_items/', self.f['local_user1'], { + reply = client.post('/api/inventory_items/{}/'.format(self.own_handle), self.f['local_user1'], { 'availability_policy': 'rent', 'owned_quantity': 1, 'image': '', @@ -89,7 +90,7 @@ class InventoryApiTestCase(UserTestMixin, InventoryTestMixin, ToolshedTestCase): self.assertEqual(InventoryItem.objects.count(), 2) def test_post_new_item3(self): - reply = client.post('/api/inventory_items/', self.f['local_user1'], { + reply = client.post('/api/inventory_items/{}/'.format(self.own_handle), self.f['local_user1'], { 'availability_policy': 'private', 'name': 'test3', 'description': 'test', @@ -109,7 +110,7 @@ class InventoryApiTestCase(UserTestMixin, InventoryTestMixin, ToolshedTestCase): self.assertEqual([p for p in item.properties.all()], []) def test_put_item(self): - reply = client.put('/api/inventory_items/1/', self.f['local_user1'], { + reply = client.put('/api/inventory_items/{}/1/'.format(self.own_handle), self.f['local_user1'], { 'availability_policy': 'sell', 'name': 'test4', 'description': 'new description', @@ -132,7 +133,7 @@ class InventoryApiTestCase(UserTestMixin, InventoryTestMixin, ToolshedTestCase): self.assertEqual([p.value for p in item.itemproperty_set.all()], ['value5', 'value6', 'value7']) def test_patch_item(self): - reply = client.patch('/api/inventory_items/1/', self.f['local_user1'], { + reply = client.patch('/api/inventory_items/{}/1/'.format(self.own_handle), self.f['local_user1'], { 'description': 'new description2', 'category': 'cat1', 'owned_quantity': 100, @@ -152,7 +153,7 @@ class InventoryApiTestCase(UserTestMixin, InventoryTestMixin, ToolshedTestCase): self.assertEqual([p.value for p in item.itemproperty_set.all()], ['value8']) def test_patch_item2(self): - reply = client.patch('/api/inventory_items/1/', self.f['local_user1'], { + reply = client.patch('/api/inventory_items/{}/1/'.format(self.own_handle), self.f['local_user1'], { 'description': 'new description2', 'category': None, 'owned_quantity': 100, @@ -171,7 +172,7 @@ class InventoryApiTestCase(UserTestMixin, InventoryTestMixin, ToolshedTestCase): self.assertEqual([p for p in item.properties.all()], []) def test_delete_item(self): - reply = client.delete('/api/inventory_items/1/', self.f['local_user1']) + reply = client.delete('/api/inventory_items/{}/1/'.format(self.own_handle), self.f['local_user1']) self.assertEqual(reply.status_code, 204) self.assertEqual(InventoryItem.objects.count(), 1) self.assertEqual(InventoryItem.objects.get(id=2).name, 'test2') @@ -228,16 +229,19 @@ class InventoryApiTestCase(UserTestMixin, InventoryTestMixin, ToolshedTestCase): self.assertEqual(reply.json()['name'], 'test1') def test_get_shared_item_not_friend(self): + # Non-visible resolves the same way a non-member's group access does now (see + # GroupOwnedInventoryApiTestCase.test_non_member_cannot_see_or_edit) - 404, not a + # separate 403 special case for "not a friend". reply = client.get('/api/inventory_items/testuser1@example.com/' + str(self.f['item1'].id) + '/', self.f['ext_user1']) - self.assertEqual(reply.status_code, 403) + self.assertEqual(reply.status_code, 404) def test_get_shared_item_private(self): private_item = InventoryItem.create_for_owner( owner=self.f['local_user1'], owned_quantity=1, name='secret', availability_policy='private') reply = client.get('/api/inventory_items/testuser1@example.com/' + str(private_item.id) + '/', self.f['local_user2']) - self.assertEqual(reply.status_code, 403) + self.assertEqual(reply.status_code, 404) reply = client.get('/api/inventory_items/testuser1@example.com/' + str(private_item.id) + '/', self.f['local_user1']) self.assertEqual(reply.status_code, 200) @@ -256,6 +260,98 @@ class InventoryApiTestCase(UserTestMixin, InventoryTestMixin, ToolshedTestCase): self.assertEqual(reply.status_code, 400) +class ResolveShortIdApiTestCase(UserTestMixin, InventoryTestMixin, GroupTestMixin, LocationTestMixin, + ToolshedTestCase): + def setUp(self): + super().setUp() + self.prepare_users() + self.prepare_categories() + self.prepare_tags() + self.prepare_properties() + self.prepare_inventory() + self.prepare_groups() + self.prepare_locations() + self.owner_id = self.f['local_user1'].public_identity.pk + + def test_resolve_item_as_owner(self): + reply = client.get(f'/api/resolve_short_id/item/{self.owner_id}/{self.f["item1"].id}/', + self.f['local_user1']) + self.assertEqual(reply.status_code, 200) + self.assertEqual(reply.json(), {'handle': 'testuser1@example.com', 'id': self.f['item1'].id}) + + def test_resolve_item_as_friend(self): + reply = client.get(f'/api/resolve_short_id/item/{self.owner_id}/{self.f["item1"].id}/', + self.f['local_user2']) + self.assertEqual(reply.status_code, 200) + + def test_resolve_item_not_friend(self): + reply = client.get(f'/api/resolve_short_id/item/{self.owner_id}/{self.f["item1"].id}/', + self.f['ext_user1']) + self.assertEqual(reply.status_code, 403) + + def test_resolve_item_private_not_owner(self): + private_item = InventoryItem.create_for_owner( + owner=self.f['local_user1'], owned_quantity=1, name='secret', availability_policy='private') + reply = client.get(f'/api/resolve_short_id/item/{self.owner_id}/{private_item.id}/', + self.f['local_user2']) + self.assertEqual(reply.status_code, 403) + reply = client.get(f'/api/resolve_short_id/item/{self.owner_id}/{private_item.id}/', + self.f['local_user1']) + self.assertEqual(reply.status_code, 200) + + def test_resolve_item_unknown_owner(self): + reply = client.get('/api/resolve_short_id/item/999999/1/', self.f['local_user2']) + self.assertEqual(reply.status_code, 404) + + def test_resolve_item_unknown_local_id(self): + reply = client.get(f'/api/resolve_short_id/item/{self.owner_id}/999999/', self.f['local_user2']) + self.assertEqual(reply.status_code, 404) + + def test_resolve_unsupported_kind(self): + # workflow/category/file/group aren't wired up yet - see + # docs/handles-and-shortids.md#domain-qualified-short-id's note on the remaining gap. + reply = client.get(f'/api/resolve_short_id/workflow/{self.owner_id}/1/', self.f['local_user2']) + self.assertEqual(reply.status_code, 400) + + def test_resolve_storage_location_as_owner(self): + reply = client.get(f'/api/resolve_short_id/storage_location/{self.owner_id}/{self.f["loc1"].id}/', + self.f['local_user1']) + self.assertEqual(reply.status_code, 200) + self.assertEqual(reply.json(), {'handle': 'testuser1@example.com', 'id': self.f['loc1'].id}) + + def test_resolve_storage_location_unknown_local_id(self): + reply = client.get(f'/api/resolve_short_id/storage_location/{self.owner_id}/999999/', + self.f['local_user1']) + self.assertEqual(reply.status_code, 404) + + def test_resolve_group_item_as_member(self): + item = InventoryItem.create_for_owner( + owner_group=self.f['group1'], owned_quantity=1, name='drill', availability_policy='private') + reply = client.get(f'/api/resolve_short_id/group_item/{self.f["group1"].pk}/{item.id}/', + self.f['local_user1']) + self.assertEqual(reply.status_code, 200) + self.assertEqual(reply.json(), {'handle': str(self.f['group1']), 'id': item.id}) + + def test_resolve_group_item_non_member_denied(self): + item = InventoryItem.create_for_owner( + owner_group=self.f['group1'], owned_quantity=1, name='drill', availability_policy='private') + reply = client.get(f'/api/resolve_short_id/group_item/{self.f["group1"].pk}/{item.id}/', + self.f['local_user2']) + self.assertEqual(reply.status_code, 403) + + def test_resolve_group_storage_location_as_member(self): + location = StorageLocation.create_for_owner(owner_group=self.f['group1'], name='shelf') + reply = client.get( + f'/api/resolve_short_id/group_storage_location/{self.f["group1"].pk}/{location.id}/', + self.f['local_user1']) + self.assertEqual(reply.status_code, 200) + self.assertEqual(reply.json(), {'handle': str(self.f['group1']), 'id': location.id}) + + def test_resolve_unknown_group(self): + reply = client.get('/api/resolve_short_id/group_item/999999/1/', self.f['local_user1']) + self.assertEqual(reply.status_code, 404) + + class TestInventoryItemWithFileApiTestCase(UserTestMixin, FilesTestMixin, InventoryTestMixin, ToolshedTestCase): def setUp(self): super().setUp() @@ -265,9 +361,10 @@ class TestInventoryItemWithFileApiTestCase(UserTestMixin, FilesTestMixin, Invent self.prepare_properties() self.prepare_files() self.prepare_inventory() + self.own_handle = str(self.f['local_user1']) def test_post_item_with_file_id(self): - reply = client.post('/api/inventory_items/', self.f['local_user1'], { + reply = client.post('/api/inventory_items/{}/'.format(self.own_handle), self.f['local_user1'], { 'name': 'test4', 'description': 'test', 'category': 'cat1', @@ -290,7 +387,7 @@ class TestInventoryItemWithFileApiTestCase(UserTestMixin, FilesTestMixin, Invent self.assertEqual([f for f in item.files.all()], [self.f['test_file1']]) def test_post_item_with_encoded_file(self): - reply = client.post('/api/inventory_items/', self.f['local_user1'], { + reply = client.post('/api/inventory_items/{}/'.format(self.own_handle), self.f['local_user1'], { 'name': 'test4', 'description': 'test', 'category': 'cat1', @@ -313,7 +410,7 @@ class TestInventoryItemWithFileApiTestCase(UserTestMixin, FilesTestMixin, Invent self.assertEqual([f for f in item.files.all()], [self.f['test_file3']]) def test_post_item_with_file_id_fail(self): - reply = client.post('/api/inventory_items/', self.f['local_user1'], { + reply = client.post('/api/inventory_items/{}/'.format(self.own_handle), self.f['local_user1'], { 'name': 'test4', 'description': 'test', 'category': 'cat1', @@ -325,7 +422,7 @@ class TestInventoryItemWithFileApiTestCase(UserTestMixin, FilesTestMixin, Invent self.assertEqual(reply.status_code, 400) def test_post_item_with_encoded_file_fail(self): - reply = client.post('/api/inventory_items/', self.f['local_user1'], { + reply = client.post('/api/inventory_items/{}/'.format(self.own_handle), self.f['local_user1'], { 'name': 'test4', 'description': 'test', 'category': 'cat1', @@ -349,11 +446,11 @@ class GroupOwnedInventoryApiTestCase(UserTestMixin, GroupTestMixin, CategoryTest self.prepare_files() self.prepare_locations() self.f['group1'].members.add(self.f['local_user2'].public_identity) + self.group_handle = '+' + str(self.f['group1'])[1:] def create_group_item(self, name='drill'): - return client.post('/api/inventory_items/', self.f['local_user1'], { + return client.post('/api/inventory_items/{}/'.format(self.group_handle), self.f['local_user1'], { 'name': name, 'owned_quantity': 1, 'availability_policy': 'private', - 'owner_group': str(self.f['group1'])[1:], }) def test_create_group_owned_item(self): @@ -366,16 +463,15 @@ class GroupOwnedInventoryApiTestCase(UserTestMixin, GroupTestMixin, CategoryTest self.assertIsNone(reply.json()['owner']) def test_create_group_owned_item_non_member_denied(self): - reply = client.post('/api/inventory_items/', self.f['ext_user1'], { + reply = client.post('/api/inventory_items/{}/'.format(self.group_handle), self.f['ext_user1'], { 'name': 'drill', 'owned_quantity': 1, 'availability_policy': 'private', - 'owner_group': str(self.f['group1'])[1:], }) self.assertEqual(reply.status_code, 403) self.assertEqual(InventoryItem.objects.count(), 0) def test_other_member_can_edit(self): item_id = self.create_group_item().json()['id'] - reply = client.patch('/api/inventory_items/{}/'.format(item_id), self.f['local_user2'], { + reply = client.patch('/api/inventory_items/{}/{}/'.format(self.group_handle, item_id), self.f['local_user2'], { 'name': 'drill-renamed' }) self.assertEqual(reply.status_code, 200) @@ -383,18 +479,33 @@ class GroupOwnedInventoryApiTestCase(UserTestMixin, GroupTestMixin, CategoryTest def test_other_member_can_delete(self): item_id = self.create_group_item().json()['id'] - reply = client.delete('/api/inventory_items/{}/'.format(item_id), self.f['local_user2']) + reply = client.delete('/api/inventory_items/{}/{}/'.format(self.group_handle, item_id), self.f['local_user2']) self.assertEqual(reply.status_code, 204) self.assertEqual(InventoryItem.objects.filter(id=item_id).count(), 0) + def test_delete_group_item_when_id_collides_with_own_personal_item(self): + own_handle = str(self.f['local_user2']) + personal_reply = client.post('/api/inventory_items/{}/'.format(own_handle), self.f['local_user2'], { + 'name': 'personal-drill', 'owned_quantity': 1, 'availability_policy': 'private', + }) + self.assertEqual(personal_reply.json()['id'], 1) + item_id = self.create_group_item().json()['id'] + self.assertEqual(item_id, 1) + + reply = client.delete('/api/inventory_items/{}/{}/'.format(self.group_handle, item_id), self.f['local_user2']) + + self.assertEqual(reply.status_code, 204) + self.assertEqual(InventoryItem.objects.filter(name='drill').count(), 0) + self.assertEqual(InventoryItem.objects.filter(name='personal-drill').count(), 1) + def test_remote_member_without_local_account_can_edit(self): # A remote member (KnownIdentity, no ToolshedUser row) must still act on group-owned # items - not unauthorized just because .user.exists() is False. self.f['group1'].members.add(self.f['ext_user1'].public_identity) item_id = self.create_group_item().json()['id'] - reply = client.get('/api/inventory_items/{}/'.format(item_id), self.f['ext_user1']) + reply = client.get('/api/inventory_items/{}/{}/'.format(self.group_handle, item_id), self.f['ext_user1']) self.assertEqual(reply.status_code, 200) - reply = client.patch('/api/inventory_items/{}/'.format(item_id), self.f['ext_user1'], { + reply = client.patch('/api/inventory_items/{}/{}/'.format(self.group_handle, item_id), self.f['ext_user1'], { 'name': 'drill-renamed-by-remote-member' }) self.assertEqual(reply.status_code, 200) @@ -402,32 +513,32 @@ class GroupOwnedInventoryApiTestCase(UserTestMixin, GroupTestMixin, CategoryTest def test_non_member_cannot_see_or_edit(self): item_id = self.create_group_item().json()['id'] - reply = client.get('/api/inventory_items/{}/'.format(item_id), self.f['ext_user1']) + reply = client.get('/api/inventory_items/{}/{}/'.format(self.group_handle, item_id), self.f['ext_user1']) self.assertEqual(reply.status_code, 404) def test_group_items_excluded_from_personal_list(self): self.create_group_item() - reply = client.get('/api/inventory_items/', self.f['local_user1']) + reply = client.get('/api/inventory_items/{}/'.format(str(self.f['local_user1'])), self.f['local_user1']) self.assertEqual(reply.status_code, 200) self.assertEqual(len(reply.json()), 0) - def test_group_items_listed_by_group_query_param(self): + def test_group_items_listed_by_owner_handle(self): self.create_group_item() - reply = client.get('/api/inventory_items/?group={}'.format(str(self.f['group1'])[1:]), self.f['local_user2']) + reply = client.get('/api/inventory_items/{}/'.format(self.group_handle), self.f['local_user2']) self.assertEqual(reply.status_code, 200) self.assertEqual(len(reply.json()), 1) self.assertEqual(reply.json()[0]['name'], 'drill') - def test_group_items_not_listed_for_non_member_query_param(self): + def test_group_items_not_listed_for_non_member(self): self.create_group_item() - reply = client.get('/api/inventory_items/?group={}'.format(str(self.f['group1'])[1:]), self.f['ext_user1']) + reply = client.get('/api/inventory_items/{}/'.format(self.group_handle), self.f['ext_user1']) self.assertEqual(reply.status_code, 200) self.assertEqual(len(reply.json()), 0) def test_create_group_owned_item_with_full_fields(self): # Parity with InventoryApiTestCase.test_post_new_item: tags/properties/category attach # to a group-owned item the same way as a personal one. - reply = client.post('/api/inventory_items/', self.f['local_user1'], { + reply = client.post('/api/inventory_items/{}/'.format(self.group_handle), self.f['local_user1'], { 'availability_policy': 'rent', 'category': 'cat2', 'name': 'drill', @@ -435,7 +546,6 @@ class GroupOwnedInventoryApiTestCase(UserTestMixin, GroupTestMixin, CategoryTest 'owned_quantity': 3, 'tags': ['tag1', 'tag2'], 'properties': [{'name': 'prop1', 'value': 'value1'}, {'name': 'prop2', 'value': 'value2'}], - 'owner_group': str(self.f['group1'])[1:], }) self.assertEqual(reply.status_code, 201) item = InventoryItem.objects.get(name='drill') @@ -450,22 +560,21 @@ class GroupOwnedInventoryApiTestCase(UserTestMixin, GroupTestMixin, CategoryTest def test_create_group_owned_item_empty_fails(self): # Parity with InventoryApiTestCase.test_post_new_item_empty: clean()'s name-or-files validation still applies. - reply = client.post('/api/inventory_items/', self.f['local_user1'], { - 'availability_policy': 'private', 'owned_quantity': 1, 'owner_group': str(self.f['group1'])[1:], + reply = client.post('/api/inventory_items/{}/'.format(self.group_handle), self.f['local_user1'], { + 'availability_policy': 'private', 'owned_quantity': 1, }) self.assertEqual(reply.status_code, 400) self.assertEqual(InventoryItem.objects.count(), 0) def test_create_group_owned_item_nonexistent_group(self): - reply = client.post('/api/inventory_items/', self.f['local_user1'], { + reply = client.post('/api/inventory_items/+nonexistent@example.com/', self.f['local_user1'], { 'name': 'drill', 'owned_quantity': 1, 'availability_policy': 'private', - 'owner_group': 'nonexistent@example.com', }) self.assertEqual(reply.status_code, 404) self.assertEqual(InventoryItem.objects.count(), 0) - def test_group_items_listed_for_nonexistent_group_query_param(self): - reply = client.get('/api/inventory_items/?group=nonexistent@example.com', self.f['local_user1']) + def test_group_items_listed_for_nonexistent_group(self): + reply = client.get('/api/inventory_items/+nonexistent@example.com/', self.f['local_user1']) self.assertEqual(reply.status_code, 200) self.assertEqual(len(reply.json()), 0) @@ -473,7 +582,7 @@ class GroupOwnedInventoryApiTestCase(UserTestMixin, GroupTestMixin, CategoryTest # Parity with InventoryApiTestCase.test_put_item, but as a PUT by a different member than # the creator, to exercise the _is_authorized branch in perform_update for PUT too. item_id = self.create_group_item().json()['id'] - reply = client.put('/api/inventory_items/{}/'.format(item_id), self.f['local_user2'], { + reply = client.put('/api/inventory_items/{}/{}/'.format(self.group_handle, item_id), self.f['local_user2'], { 'availability_policy': 'sell', 'name': 'drill-4000', 'description': 'new description', @@ -493,13 +602,12 @@ class GroupOwnedInventoryApiTestCase(UserTestMixin, GroupTestMixin, CategoryTest def test_patch_group_item_clears_fields(self): # Parity with InventoryApiTestCase.test_patch_item2 - clearing category/tags/properties. - reply = client.post('/api/inventory_items/', self.f['local_user1'], { + reply = client.post('/api/inventory_items/{}/'.format(self.group_handle), self.f['local_user1'], { 'name': 'drill', 'owned_quantity': 1, 'availability_policy': 'private', 'category': 'cat1', 'tags': ['tag1'], - 'owner_group': str(self.f['group1'])[1:], }) item_id = reply.json()['id'] - reply = client.patch('/api/inventory_items/{}/'.format(item_id), self.f['local_user2'], { + reply = client.patch('/api/inventory_items/{}/{}/'.format(self.group_handle, item_id), self.f['local_user2'], { 'category': None, 'tags': [], 'properties': [] }) self.assertEqual(reply.status_code, 200) @@ -508,9 +616,9 @@ class GroupOwnedInventoryApiTestCase(UserTestMixin, GroupTestMixin, CategoryTest self.assertEqual([t for t in item.tags.all()], []) def test_group_item_storage_location(self): - reply = client.post('/api/inventory_items/', self.f['local_user1'], { + reply = client.post('/api/inventory_items/{}/'.format(self.group_handle), self.f['local_user1'], { 'name': 'drill', 'owned_quantity': 1, 'availability_policy': 'private', - 'storage_location': self.f['loc1'].id, 'owner_group': str(self.f['group1'])[1:], + 'storage_location': self.f['loc1'].id, }) self.assertEqual(reply.status_code, 201) item = InventoryItem.objects.get(name='drill') @@ -518,19 +626,18 @@ class GroupOwnedInventoryApiTestCase(UserTestMixin, GroupTestMixin, CategoryTest def test_post_group_item_with_file_id(self): # Parity with TestInventoryItemWithFileApiTestCase.test_post_item_with_file_id. - reply = client.post('/api/inventory_items/', self.f['local_user1'], { + reply = client.post('/api/inventory_items/{}/'.format(self.group_handle), self.f['local_user1'], { 'name': 'drill', 'owned_quantity': 1, 'availability_policy': 'private', - 'files': [self.f['test_file1'].id], 'owner_group': str(self.f['group1'])[1:], + 'files': [self.f['test_file1'].id], }) self.assertEqual(reply.status_code, 201) item = InventoryItem.objects.get(name='drill') self.assertEqual([f for f in item.files.all()], [self.f['test_file1']]) def test_post_group_item_with_encoded_file(self): - reply = client.post('/api/inventory_items/', self.f['local_user1'], { + reply = client.post('/api/inventory_items/{}/'.format(self.group_handle), self.f['local_user1'], { 'name': 'drill', 'owned_quantity': 1, 'availability_policy': 'private', 'files': [{'data': self.f['encoded_content3'], 'mime_type': 'text/plain'}], - 'owner_group': str(self.f['group1'])[1:], }) self.assertEqual(reply.status_code, 201) item = InventoryItem.objects.get(name='drill') @@ -578,4 +685,4 @@ class InventoryItemIdAllocationTestCase(UserTestMixin, ToolshedTestCase): personal_item = InventoryItem.create_for_owner(owner=self.f['local_user1'], name='personal') group_item = InventoryItem.create_for_owner(owner_group=group, name='group-owned') self.assertEqual(personal_item.id, 1) - self.assertEqual(group_item.id, 1) \ No newline at end of file + self.assertEqual(group_item.id, 1) diff --git a/backend/toolshed/tests/test_locations.py b/backend/toolshed/tests/test_locations.py index 6850db4..1dc9163 100644 --- a/backend/toolshed/tests/test_locations.py +++ b/backend/toolshed/tests/test_locations.py @@ -16,6 +16,7 @@ class LocationApiTestCase(UserTestMixin, InventoryTestMixin, LocationTestMixin, self.prepare_properties() self.prepare_locations() self.prepare_inventory() + self.own_handle = str(self.f['local_user1']) def test_locations(self): self.assertEqual("loc1", str(self.f['loc1'])) @@ -30,7 +31,7 @@ class LocationApiTestCase(UserTestMixin, InventoryTestMixin, LocationTestMixin, self.assertEqual(self.f['loc1'], self.f['loc4'].parent) def test_get_inventory(self): - reply = client.get('/api/inventory_items/', self.f['local_user1']) + reply = client.get('/api/inventory_items/{}/'.format(self.own_handle), self.f['local_user1']) self.assertEqual(reply.status_code, 200) self.assertEqual(len(reply.json()), 2) self.assertEqual(reply.json()[0]['name'], 'test1') @@ -50,7 +51,7 @@ class LocationApiTestCase(UserTestMixin, InventoryTestMixin, LocationTestMixin, self.assertEqual(reply.json()[1]['availability_policy'], 'friends') def test_get_inventory_item(self): - reply = client.get('/api/storage_locations/', self.f['local_user1']) + reply = client.get('/api/storage_locations/{}/'.format(self.own_handle), self.f['local_user1']) self.assertEqual(reply.status_code, 200) self.assertEqual(len(reply.json()), 4) self.assertEqual(reply.json()[0]['name'], 'loc1') @@ -71,7 +72,7 @@ class LocationApiTestCase(UserTestMixin, InventoryTestMixin, LocationTestMixin, self.assertEqual(reply.json()[3]['path'], 'loc1/loc4') def test_post_new_location(self): - reply = client.post('/api/storage_locations/', self.f['local_user1'], { + reply = client.post('/api/storage_locations/{}/'.format(self.own_handle), self.f['local_user1'], { 'name': 'loc5', 'description': 'a new location', }) @@ -84,7 +85,7 @@ class LocationApiTestCase(UserTestMixin, InventoryTestMixin, LocationTestMixin, self.assertEqual(reply.json()['path'], 'loc5') def test_post_new_nested_location(self): - reply = client.post('/api/storage_locations/', self.f['local_user1'], { + reply = client.post('/api/storage_locations/{}/'.format(self.own_handle), self.f['local_user1'], { 'name': 'loc5', 'parent': self.f['loc3'].id, }) @@ -94,10 +95,11 @@ class LocationApiTestCase(UserTestMixin, InventoryTestMixin, LocationTestMixin, self.assertEqual(reply.json()['path'], 'loc1/loc3/loc5') def test_patch_location(self): - reply = client.patch('/api/storage_locations/' + str(self.f['loc2'].id) + '/', self.f['local_user1'], { - 'name': 'loc2-renamed', - 'parent': self.f['loc1'].id, - }) + reply = client.patch( + '/api/storage_locations/{}/{}/'.format(self.own_handle, self.f['loc2'].id), self.f['local_user1'], { + 'name': 'loc2-renamed', + 'parent': self.f['loc1'].id, + }) self.assertEqual(reply.status_code, 200) location = StorageLocation.objects.get(id=self.f['loc2'].id) self.assertEqual(location.name, 'loc2-renamed') @@ -105,7 +107,8 @@ class LocationApiTestCase(UserTestMixin, InventoryTestMixin, LocationTestMixin, self.assertEqual(reply.json()['path'], 'loc1/loc2-renamed') def test_delete_location(self): - reply = client.delete('/api/storage_locations/' + str(self.f['loc4'].id) + '/', self.f['local_user1']) + reply = client.delete( + '/api/storage_locations/{}/{}/'.format(self.own_handle, self.f['loc4'].id), self.f['local_user1']) self.assertEqual(reply.status_code, 204) self.assertEqual(StorageLocation.objects.count(), 3) self.assertEqual(StorageLocation.objects.filter(id=self.f['loc4'].id).count(), 0) @@ -113,20 +116,25 @@ class LocationApiTestCase(UserTestMixin, InventoryTestMixin, LocationTestMixin, def test_delete_location_with_items_sets_null(self): item = InventoryItem.create_for_owner( owner=self.f['local_user1'], name='located_item', storage_location=self.f['loc3']) - reply = client.delete('/api/storage_locations/' + str(self.f['loc3'].id) + '/', self.f['local_user1']) + reply = client.delete( + '/api/storage_locations/{}/{}/'.format(self.own_handle, self.f['loc3'].id), self.f['local_user1']) self.assertEqual(reply.status_code, 204) item.refresh_from_db() self.assertIsNone(item.storage_location) self.assertEqual(InventoryItem.objects.filter(id=item.id).count(), 1) def test_locations_are_owner_scoped(self): - reply = client.get('/api/storage_locations/', self.f['local_user2']) + reply = client.get('/api/storage_locations/{}/'.format(str(self.f['local_user2'])), self.f['local_user2']) self.assertEqual(reply.status_code, 200) self.assertEqual(len(reply.json()), 0) def test_cannot_delete_other_users_location(self): - reply = client.delete('/api/storage_locations/' + str(self.f['loc1'].id) + '/', self.f['local_user2']) - self.assertEqual(reply.status_code, 404) + # local_user2 is a friend of local_user1 (see prepare_inventory), so local_user1's own + # handle resolves and the location is visible - friends can read but never write, so this + # is a 403 (found, not authorized), not a 404. + reply = client.delete( + '/api/storage_locations/{}/{}/'.format(self.own_handle, self.f['loc1'].id), self.f['local_user2']) + self.assertEqual(reply.status_code, 403) self.assertEqual(StorageLocation.objects.filter(id=self.f['loc1'].id).count(), 1) @@ -136,10 +144,13 @@ class GroupOwnedLocationApiTestCase(UserTestMixin, GroupTestMixin, ToolshedTestC self.prepare_users() self.prepare_groups() self.f['group1'].members.add(self.f['local_user2'].public_identity) + # The '+' prefix is the '#'->'+' escape resolve_owner_handle expects for a group handle in + # the owner_handle URL path parameter - the same escape encodeHandleForUrl uses. + self.group_handle = '+' + str(self.f['group1'])[1:] def create_group_location(self, name='shelf'): - return client.post('/api/storage_locations/', self.f['local_user1'], { - 'name': name, 'owner_group': str(self.f['group1'])[1:], + return client.post('/api/storage_locations/{}/'.format(self.group_handle), self.f['local_user1'], { + 'name': name, }) def test_create_group_owned_location(self): @@ -152,67 +163,91 @@ class GroupOwnedLocationApiTestCase(UserTestMixin, GroupTestMixin, ToolshedTestC self.assertIsNone(reply.json()['owner']) def test_create_group_owned_location_non_member_denied(self): - reply = client.post('/api/storage_locations/', self.f['ext_user1'], { - 'name': 'shelf', 'owner_group': str(self.f['group1'])[1:], + reply = client.post('/api/storage_locations/{}/'.format(self.group_handle), self.f['ext_user1'], { + 'name': 'shelf', }) self.assertEqual(reply.status_code, 403) self.assertEqual(StorageLocation.objects.count(), 0) def test_other_member_can_edit(self): location_id = self.create_group_location().json()['id'] - reply = client.patch('/api/storage_locations/{}/'.format(location_id), self.f['local_user2'], { - 'name': 'shelf-renamed' - }) + reply = client.patch( + '/api/storage_locations/{}/{}/'.format(self.group_handle, location_id), self.f['local_user2'], { + 'name': 'shelf-renamed' + }) self.assertEqual(reply.status_code, 200) self.assertEqual(StorageLocation.objects.get(id=location_id).name, 'shelf-renamed') def test_other_member_can_delete(self): location_id = self.create_group_location().json()['id'] - reply = client.delete('/api/storage_locations/{}/'.format(location_id), self.f['local_user2']) + reply = client.delete( + '/api/storage_locations/{}/{}/'.format(self.group_handle, location_id), self.f['local_user2']) self.assertEqual(reply.status_code, 204) self.assertEqual(StorageLocation.objects.filter(id=location_id).count(), 0) + def test_delete_group_location_when_id_collides_with_own_personal_location(self): + # Mirrors InventoryItemViewSet's own regression test - id is only unique within its own + # owner/owner_group scope, so a member's personal location and their group's location can + # land on the same id. Every route being scoped by an explicit owner_handle - never a + # bare, unscoped id - means this can no longer raise MultipleObjectsReturned. + own_handle = str(self.f['local_user2']) + personal_reply = client.post('/api/storage_locations/{}/'.format(own_handle), self.f['local_user2'], + {'name': 'personal-shelf'}) + self.assertEqual(personal_reply.json()['id'], 1) + location_id = self.create_group_location().json()['id'] + self.assertEqual(location_id, 1) + + reply = client.delete( + '/api/storage_locations/{}/{}/'.format(self.group_handle, location_id), self.f['local_user2']) + + self.assertEqual(reply.status_code, 204) + self.assertEqual(StorageLocation.objects.filter(name='shelf').count(), 0) + self.assertEqual(StorageLocation.objects.filter(name='personal-shelf').count(), 1) + def test_remote_member_without_local_account_can_edit(self): # A remote member (KnownIdentity, no ToolshedUser row) must still act on group-owned # locations - not unauthorized just because .user.exists() is False. self.f['group1'].members.add(self.f['ext_user1'].public_identity) location_id = self.create_group_location().json()['id'] - reply = client.get('/api/storage_locations/{}/'.format(location_id), self.f['ext_user1']) + reply = client.get( + '/api/storage_locations/{}/{}/'.format(self.group_handle, location_id), self.f['ext_user1']) self.assertEqual(reply.status_code, 200) - reply = client.patch('/api/storage_locations/{}/'.format(location_id), self.f['ext_user1'], { - 'name': 'shelf-renamed-by-remote-member' - }) + reply = client.patch( + '/api/storage_locations/{}/{}/'.format(self.group_handle, location_id), self.f['ext_user1'], { + 'name': 'shelf-renamed-by-remote-member' + }) self.assertEqual(reply.status_code, 200) self.assertEqual(StorageLocation.objects.get(id=location_id).name, 'shelf-renamed-by-remote-member') def test_non_member_cannot_see_or_edit(self): location_id = self.create_group_location().json()['id'] - reply = client.get('/api/storage_locations/{}/'.format(location_id), self.f['ext_user1']) + reply = client.get( + '/api/storage_locations/{}/{}/'.format(self.group_handle, location_id), self.f['ext_user1']) self.assertEqual(reply.status_code, 404) def test_group_locations_excluded_from_personal_list(self): self.create_group_location() - reply = client.get('/api/storage_locations/', self.f['local_user1']) + reply = client.get('/api/storage_locations/{}/'.format(str(self.f['local_user1'])), self.f['local_user1']) self.assertEqual(reply.status_code, 200) self.assertEqual(len(reply.json()), 0) - def test_group_locations_listed_by_group_query_param(self): + def test_group_locations_listed_by_owner_handle(self): self.create_group_location() - reply = client.get('/api/storage_locations/?group={}'.format(str(self.f['group1'])[1:]), self.f['local_user2']) + reply = client.get('/api/storage_locations/{}/'.format(self.group_handle), self.f['local_user2']) self.assertEqual(reply.status_code, 200) self.assertEqual(len(reply.json()), 1) self.assertEqual(reply.json()[0]['name'], 'shelf') - def test_group_locations_not_listed_for_non_member_query_param(self): + def test_group_locations_not_listed_for_non_member(self): self.create_group_location() - reply = client.get('/api/storage_locations/?group={}'.format(str(self.f['group1'])[1:]), self.f['ext_user1']) + reply = client.get('/api/storage_locations/{}/'.format(self.group_handle), self.f['ext_user1']) self.assertEqual(reply.status_code, 200) self.assertEqual(len(reply.json()), 0) def test_group_location_can_be_parent_for_group_member(self): parent_id = self.create_group_location('shelf').json()['id'] - reply = client.post('/api/storage_locations/', self.f['local_user2'], { - 'name': 'bin', 'owner_group': str(self.f['group1'])[1:], 'parent': parent_id, + reply = client.post('/api/storage_locations/{}/'.format(self.group_handle), self.f['local_user2'], { + 'name': 'bin', 'parent': parent_id, }) self.assertEqual(reply.status_code, 201) self.assertEqual(reply.json()['path'], 'shelf/bin') @@ -224,15 +259,15 @@ class GroupOwnedLocationApiTestCase(UserTestMixin, GroupTestMixin, ToolshedTestC # InventoryItem.storage_location already had before group ownership existed here; not # something this feature narrows. personal = StorageLocation.create_for_owner(name='mine', owner=self.f['local_user1']) - reply = client.post('/api/storage_locations/', self.f['local_user1'], { - 'name': 'bin', 'owner_group': str(self.f['group1'])[1:], 'parent': personal.id, + reply = client.post('/api/storage_locations/{}/'.format(self.group_handle), self.f['local_user1'], { + 'name': 'bin', 'parent': personal.id, }) self.assertEqual(reply.status_code, 201) def test_group_location_not_valid_parent_for_non_member(self): parent_id = self.create_group_location('shelf').json()['id'] - reply = client.post('/api/storage_locations/', self.f['ext_user1'], { - 'name': 'bin', 'parent': parent_id, - }) + reply = client.post( + '/api/storage_locations/{}/'.format(str(self.f['ext_user1'])), self.f['ext_user1'], { + 'name': 'bin', 'parent': parent_id, + }) self.assertEqual(reply.status_code, 400) - diff --git a/frontend/src/short-id.js b/frontend/src/short-id.js index 1f79672..9fcbff8 100644 --- a/frontend/src/short-id.js +++ b/frontend/src/short-id.js @@ -204,6 +204,22 @@ export function deserializeShortId(ints) { return schema.interpret(fieldValues) } +export function encodeDomainQualifiedShortId(domain, ints) { + return domain + ':' + encodeShortId(ints) +} + +export function decodeDomainQualifiedShortId(text) { + const i = text.indexOf(':~') + if (i === -1) { + throw new Error("not a domain-qualified short id: expected ':~'") + } + return {domain: text.slice(0, i), token: text.slice(i + 1), ints: decodeShortId(text.slice(i + 1))} +} + +export function isDomainQualifiedShortId(text) { + return typeof text === 'string' && /^[^\s~]+:~/.test(text) +} + export function serializeShortId({kind, ...fieldValues}) { const entry = Object.entries(SCHEMAS).find(([, s]) => s.name === kind) if (!entry) { diff --git a/frontend/src/tests/short-id.js b/frontend/src/tests/short-id.js index 8e92fb5..291358f 100644 --- a/frontend/src/tests/short-id.js +++ b/frontend/src/tests/short-id.js @@ -1,4 +1,7 @@ -import {encodeShortId, decodeShortId, deserializeShortId, serializeShortId} from '../short-id.js' +import { + encodeShortId, decodeShortId, deserializeShortId, serializeShortId, + encodeDomainQualifiedShortId, decodeDomainQualifiedShortId, isDomainQualifiedShortId +} from '../short-id.js' test('encodes the worked example from docs/handles-and-shortids.md', () => { const token = encodeShortId([0, 7, 42]) @@ -108,3 +111,34 @@ test('rejects a missing field', () => { test('rejects a negative field value', () => { expect(() => encodeShortId([0, -1, 42])).toThrow() }) + +test('encodes a Domain-Qualified Short ID as ":"', () => { + expect(encodeDomainQualifiedShortId('toolsheddomain.tld', [0, 7, 42])).toBe('toolsheddomain.tld:~DyU') +}) + +test('decodes a Domain-Qualified Short ID back into its domain and ints', () => { + const decoded = decodeDomainQualifiedShortId('toolsheddomain.tld:~DyU') + expect(decoded.domain).toBe('toolsheddomain.tld') + expect(decoded.token).toBe('~DyU') + expect(decoded.ints).toEqual([0, 7, 42]) +}) + +test('round-trips a Domain-Qualified Short ID through encode/decode', () => { + const named = {kind: 'storage_location', owner_identity_id: 3, storage_location_id: 1000} + const encoded = encodeDomainQualifiedShortId('example.com', serializeShortId(named)) + const {domain, ints} = decodeDomainQualifiedShortId(encoded) + expect(domain).toBe('example.com') + expect(deserializeShortId(ints)).toEqual(named) +}) + +test('rejects a Domain-Qualified Short ID missing the "~" after the domain', () => { + expect(() => decodeDomainQualifiedShortId('toolsheddomain.tldDyU')).toThrow() + expect(() => decodeDomainQualifiedShortId('~DyU')).toThrow() +}) + +test('isDomainQualifiedShortId tells a ":~token" apart from a bare token', () => { + expect(isDomainQualifiedShortId('toolsheddomain.tld:~DyU')).toBe(true) + expect(isDomainQualifiedShortId('~DyU')).toBe(false) + expect(isDomainQualifiedShortId('alice@example.com:i42')).toBe(false) + expect(isDomainQualifiedShortId('not a short id at all')).toBe(false) +}) diff --git a/frontend/src/views/StorageLocation.vue b/frontend/src/views/StorageLocation.vue index 579788d..0c2c2af 100644 --- a/frontend/src/views/StorageLocation.vue +++ b/frontend/src/views/StorageLocation.vue @@ -73,7 +73,7 @@
Edit From 6ee5ae38b10ace42830846c5f679e103e5d56837 Mon Sep 17 00:00:00 2001 From: jedi Date: Thu, 27 Aug 2026 01:39:03 +0200 Subject: [PATCH 03/18] stash --- backend/toolshed/api/files.py | 24 ++-- docs/glossary.md | 60 +++++--- docs/handles-and-shortids.md | 154 ++++++++++++++------- frontend/src/assets/fonts/pixel/LICENSE.md | 9 -- frontend/src/label-layouts.js | 19 ++- frontend/src/views/Print.vue | 18 +-- frontend/src/views/Scan.vue | 77 ++++++----- frontend/src/views/ShortId.vue | 38 +++-- 8 files changed, 246 insertions(+), 153 deletions(-) delete mode 100644 frontend/src/assets/fonts/pixel/LICENSE.md diff --git a/backend/toolshed/api/files.py b/backend/toolshed/api/files.py index 03b51d9..b8b57a8 100644 --- a/backend/toolshed/api/files.py +++ b/backend/toolshed/api/files.py @@ -11,29 +11,29 @@ from toolshed.models import InventoryItem, WorkflowInstance def _get_authorized_item(identity, item_id): - """Look up an item by its owner-scoped id and confirm identity may act on it - either as its - personal owner (requires a local ToolshedUser account) or as a current member of its owning - group (works for a remote member too, since group membership is identity-level, see - docs/design-in-progress/groups-mvp.md). id is only unique within one owner/group's own items, - so the lookup itself must be scoped rather than a bare global get. Returns None if not found - or not authorized, the same shape InventoryItem.DoesNotExist handling around it already - expects.""" + """Owner-or-group-scoped item lookup; returns None if identity may not act on it.""" if identity.user.exists(): try: return InventoryItem.objects.get(owner=identity.user.get(), id=item_id) except InventoryItem.DoesNotExist: pass - try: - return InventoryItem.objects.get(owner_group__in=identity.member_of_groups.all(), id=item_id) - except InventoryItem.DoesNotExist: - return None + # Checked one group at a time, not owner_group__in=, since id is only unique within one group's own items and a combined query could raise MultipleObjectsReturned on a collision. + for group in identity.member_of_groups.all(): + item = InventoryItem.objects.filter(owner_group=group, id=item_id).first() + if item: + return item + return None @api_view(['GET']) @permission_classes([IsAuthenticated]) @authentication_classes([SignatureAuthenticationLocal]) def list_all_files(request, format=None): # /files/ - files = File.objects.select_related().filter(connected_items__owner=request.user).distinct() + # request.user is a ToolshedUser here; reach group membership via public_identity. + files = File.objects.select_related().filter( + Q(connected_items__owner=request.user) | + Q(connected_items__owner_group__in=request.user.public_identity.member_of_groups.all()) + ).distinct() return Response(FileSerializer(files, many=True).data) diff --git a/docs/glossary.md b/docs/glossary.md index 2a3cc62..a186a98 100644 --- a/docs/glossary.md +++ b/docs/glossary.md @@ -47,7 +47,8 @@ and talks to it directly, regardless of where the frontend itself was loaded fro A name that's unique within its own scope and carries, as part of itself, enough information to say where it's authoritative, without needing a central registry to look it up. The general term covering [user handles](#user-handle), [group handles](#group-handle) (proposed), -[classification handles](#classification-handle), and [item handles](#item-handle) (proposed). +[classification handles](#classification-handle), [User-Qualified IDs](#user-qualified-id), and +[Domain-Qualified Short IDs](#domain-qualified-short-id). *See: [federation.md](federation.md#unique-handles)* **Strict resolution** (Implemented) @@ -109,7 +110,8 @@ The asymmetric keypair backing exactly one [user handle](#user-handle); together keypair backing it are what's called an [identity](#identity). The private key signs requests made as that user and never leaves their control; the public key is handed out to establish trust in the handle (at registration, or via a [friend](#friend-friendship) exchange) and is used to verify -signatures. Only user handles carry a keypair, not groups, classification handles, or item handles. +signatures. Only user handles carry a keypair, not groups, classification handles, or +User-Qualified IDs. *See: [federation.md](federation.md#cryptography)* **Membership list** (Proposed) @@ -157,8 +159,8 @@ a property alias specifically, also states a unit conversion (even if it's "iden **Classification handle** (Implemented) Umbrella term for a [tag](#tag-property-category), [property](#tag-property-category), or [category](#tag-property-category) handle, of the form `origin#type:name`, e.g. -`git:base#property:length`. Distinct from a [user handle](#user-handle) or [item -handle](#item-handle): it names a reusable classification concept, not an actor or an owned thing. +`git:base#property:length`. Distinct from a [user handle](#user-handle) or [User-Qualified +ID](#user-qualified-id): it names a reusable classification concept, not an actor or an owned thing. *See: [federation.md](federation.md#tags-properties-and-categories)* **Definition fingerprint** (Partially implemented) @@ -201,29 +203,47 @@ strings today. ## Items & Physical Labels -**Item Handle** (Proposed) -The compact identifier for a specific item that's meaningful outside its owner's own account: -`user@domain.tld:id`, the owner's [user handle](#user-handle) plus a [local id](#local-id). Used -where it's already clear from context that it's a Toolshed item, e.g. inside the app, in exports, -in logs, so it doesn't need to spell that out or be openable on its own. Contrast with [Item -URL](#item-url), the self-contained form for when no such context can be assumed. -*See: [items-labels.md](design-in-progress/items-labels.md#open-design-questions)* +**Domain-Qualified Short ID** (Implemented) +A [domain](#domain) paired with a [short id](handles-and-shortids.md#short-ids) token, e.g. +`toolsheddomain.tld:~DyU`. A bare short id token is opaque and scoped to whichever backend minted +it, nothing in the token itself says which backend's id numbering to read it against, so it only +means something inside the app instance that's currently talking to that backend. Prefixing it with +its domain supplies the missing piece, the same way any other handle's domain half does, so the +pair keeps resolving correctly even after being copied out of the instance it was minted on. Unlike +a [User-Qualified ID](#user-qualified-id) or [Item URL](#item-url), it isn't limited to owned kinds +(any kind in the short id registry can be domain-qualified) and isn't meant to be openable with zero +context, no scheme, no path, opaque bit-packed payload, it belongs inside the app or between things +that already speak its short-id format, not on a physical label. +*See: [handles-and-shortids.md](handles-and-shortids.md#domain-qualified-short-id)* -**Item Label** (Proposed) +**Item Label** (Implemented) A physical, scannable encoding (QR code, barcode, or similar) of an item's [Item URL](#item-url), meant to be printed and stuck on the physical object it refers to. *See: [items-labels.md](design-in-progress/items-labels.md#goals)* -**Item URL** (Proposed) -The self-contained URL form of an [Item Handle](#item-handle), for use with no context at all, e.g. -an [Item Label](#item-label): `https:///i/user@domain.tld/id`. Has to open directly -to the right frontend and land on the right item on its own, since the reader can't be assumed to -already know what it is or which server it belongs to. Any frontend can serve this URL, the host -named in it isn't part of the item's identity, only the handle in its path is. +**Item URL** (Implemented) +The self-contained URL form of an item's [User-Qualified ID](#user-qualified-id), for use with no +context at all, e.g. an [Item Label](#item-label): `https:///i/user@domain.tld/id`. +Has to open directly to the right frontend and land on the right item on its own, since the reader +can't be assumed to already know what it is or which server it belongs to. Any frontend can serve +this URL, the host named in it isn't part of the item's identity, only the handle in its path is. *See: [items-labels.md](design-in-progress/items-labels.md#open-design-questions)* **Local id** (Implemented) An item's identifier as it exists today: unique only within its owner's own inventory, not -meaningful outside that owner's account. The starting point both the [Item -Handle](#item-handle) and [Item URL](#item-url) build on. +meaningful outside that owner's account. The starting point both a [User-Qualified +ID](#user-qualified-id) and [Item URL](#item-url) build on. *See: [federation.md](federation.md#items), [items-labels.md](design-in-progress/items-labels.md#problem)* + +**User-Qualified ID** (Implemented) +The compact identifier for a specific owned thing (an item, a storage location, ...) that's +meaningful outside its owner's own account: `:`, e.g. +`alice@example.com:i42`, the owner's [user handle](#user-handle) (or [group +handle](#group-handle) for a group-owned thing) plus a one-letter kind tag (`i` for item, `s` for +storage location) and a [local id](#local-id). Used where it's already clear from context that it's +Toolshed data, e.g. inside the app, in exports, in logs, so it doesn't need to spell that out or be +openable on its own. Generalizes what used to be a bespoke, item-only "Item Handle" concept: the +kind letter is what an item-specific `user@domain.tld:id` shape was missing, nothing in that string +said it was specifically an item. Contrast with [Item URL](#item-url), the self-contained form for +when no such context can be assumed. +*See: [handles-and-shortids.md](handles-and-shortids.md#user-qualified-id)* diff --git a/docs/handles-and-shortids.md b/docs/handles-and-shortids.md index 324f8c0..db73635 100644 --- a/docs/handles-and-shortids.md +++ b/docs/handles-and-shortids.md @@ -4,8 +4,8 @@ This is the syntax-level reference for two related but separate naming schemes. "Unique Handles" section covers *why* Toolshed hands out handles at all and what each kind (user, group, tag/property/category) means conceptually; this document covers the parsing rules those handles have to follow once they're written down or embedded somewhere - legal characters and -escaping. It also covers short ids end to end: a separate, newer scheme for packing small integer -id chains into a compact token, implemented in `frontend/src/short-id.js`. +escaping. It also covers short ids end to end: a separate scheme for packing small integer id +chains into a compact token. ## Handle syntax @@ -15,14 +15,14 @@ A username ends up embedded, unescaped, in several composite formats beyond its can't contain any character that already means something else in one of those: `@` (the user/domain separator in a user handle), `#` (the group-handle prefix, and the origin/type separator in a classification handle, see federation.md's Tags, Properties, and Categories -section), `:` (the id delimiter in a proposed Item Handle, the type/name delimiter in a -classification handle, and the delimiter in a signed request's `Authorization` header), `+` -(reserved as the URL-embedding escape for `#`, see below), `~` (the short-id token prefix, see -Short IDs below, and a proposed collision-disambiguation suffix delimiter on a tag's origin), and -`/` (the path-segment delimiter every handle and id ultimately sits next to once embedded in a -URL). This has to be enforced by an explicit validator rather than left to Django's default -`UnicodeUsernameValidator` (`^[\w.@+-]+\Z`), which currently permits both `@` and `+` (its own -regex doesn't happen to allow `#`, `:`, or `/`, but that's incidental, not a designed restriction). +section), `:` (the kind/id delimiter in a User-Qualified ID (see below), the type/name delimiter in +a classification handle, the domain/token delimiter in a Domain-Qualified Short ID (see below), and +the delimiter in a signed request's `Authorization` header), `+` (reserved as the URL-embedding +escape for `#`, see below), `~` (the short-id token prefix, see Short IDs below, and a +collision-disambiguation suffix delimiter on a tag's origin), and `/` (the path-segment delimiter +every handle and id ultimately sits next to once embedded in a URL). This has to be enforced by an +explicit validator rather than left to a framework default, which doesn't draw the line in the same +place. ### Embedding a `#`-bearing handle in a URL @@ -44,17 +44,45 @@ otherwise forbidden in every field a handle is built from (see Reserved characte group name or tag name could itself contain a literal `+`, it would be indistinguishable from an escaped `#` once decoded. -Implemented in `frontend/src/handle-url.js` (`encodeHandleForUrl`/`decodeHandleFromUrl`). +### User-Qualified ID + +Owned things (an item, a storage location, ...) can be referred to outside their owner's own +account: anything whose id is only unique within one owner's own numbering needs an owner-qualified +form to resolve globally. + +A **User-Qualified ID** is an owner handle (a user handle, or a group handle for a group-owned +thing) with a kind letter and a local id appended, separated by a single `:`: +`:`, e.g. `alice@example.com:i42`. This is the same `origin#type:name` +pattern classification handles already use, built on `:` instead of `#` so it avoids the URL +fragment-escaping problem (see Embedding a `#`-bearing handle in a URL above) — a User-Qualified ID +isn't meant to sit directly in a URL path segment. + +Its payload is a plain decimal local id with nothing self-describing baked in, unlike a Short ID's +bit-packed payload, so the owner is spelled out as a full handle rather than just a domain: +`example.com:i42` would be ambiguous between every user on that domain with local item id `42`; +`alice@example.com:i42` isn't. An owner handle already carries its domain, so a User-Qualified ID is +cross-domain-resolvable as written, with no separate domain-qualified wrapper needed. + +The kind letter is a small, closed registry. A group handle already looks visibly different from a +user handle (`#` prefix), so unlike the short id kind registry below, this one doesn't need separate +letters for a user-owned vs. group-owned kind — the owner half already says which it is. + +| letter | kind | notes | +|---|---|---| +| `i` | item | covers both a user-owned item and a group-owned item | +| `s` | storage location | covers both a user-owned and a group-owned storage location | + +`workflow` has no letter assigned; the registry can grow without breaking anything already printed. +Kinds with no owner (`category`, `group`, `file`) don't fit this scheme: `category` and `group` each +already have their own dedicated handle form. `file` has neither an owner to qualify by nor a +dedicated handle of its own; a Domain-Qualified Short ID (below) is the fallback for it. ## Short IDs A general encoding for turning a small, fixed-shape list of integers into a compact, URL-safe token, with no server-side lookup table involved: the code *is* the data, nothing is stored -server-side to make it resolvable. Originally proposed to answer items-labels.md's open "what does -the handle/URL actually look like" question, but the encoding itself isn't item-specific; anything -currently addressed by a short chain of small integers is a candidate. Implemented and tested in -`frontend/src/short-id.js`; try it live at `/~` (`frontend/src/views/ShortId.vue`), which -decodes whatever token is in the URL and also lists worked examples for every registered kind. +server-side to make it resolvable. Not item-specific; anything currently addressed by a short chain +of small integers is a candidate. ### Shape: a kind tag, then a fixed list of integers @@ -67,29 +95,21 @@ range" - so kind 3 is encoded as escape + chunked-int `0`, kind 4 as escape + `1 costs nothing for a kind that already fits in the direct range, and keeps the tag itself extensible forever without ever having to widen it out from under codes that were already printed. A narrow tag only pays off if kind usage is actually skewed the way id values are (a few kinds dominate), -which is why the registry below is ordered by expected frequency, cheapest (most-used) kind first: +which is why the registry below is ordered by expected frequency, cheapest (most-used) kind first. +Kind ids are scoped **per arity**, not globally: the same id can (and does) name a different kind +depending on how many fields follow it, since the arity is always known before the kind id needs +disambiguating: -| kind | name | fields | notes | -|---|---|---|---| -| 0 | `item` | `owner_identity_id`, `item_local_id` | dominant case - the primary physical-label use case | -| 1 | `storage_location` | `owner_identity_id`, `storage_location_id` | also label-printed | -| 2 | `category` | `category_id` | label-adjacent (tagging); global, no owner | -| 3 | `workflow` | `owner_identity_id`, `workflow_id` | shared in-app, not printed - first to pay the escape's cost | -| 4 | `group` | `group_id` | shared even less often; global, no owner | -| 5 | `file` | `file_id` | least often shared standalone; global, deduplicated by content hash | - -`owner_identity_id` is `KnownIdentity.pk` (`backend/authentication/models.py`), not -`ToolshedUser.pk`. Every local account already has exactly one stable `KnownIdentity` row -(`ToolshedUser.public_identity`, created once at registration and never recreated), and every -friend this backend knows about - local or remote - is represented by that same table, unique on -`(username, domain)`. So one small integer already stands in for "this owner, as known by this -backend" for both cases, with no separate local-vs-remote branching needed, and it's the same row -federation.md's Cryptography section already treats as the trust anchor for a handle's public key. -It appears on `item`, `storage_location`, and `workflow` because their backing models -(`InventoryItem`, `StorageLocation`, `WorkflowInstance`) all FK `ToolshedUser` directly; `category`, -`group`, and `file` skip it because their models are global/unscoped (`Group` has an unowned -`members` M2M, `File` is deduplicated globally by content hash), so a bare row id is already -everything needed to look them up. +| kind id | arity | name | fields | notes | +|---|---|---|---|---| +| 0 | 2 | `item` | `owner_identity_id`, `item_local_id` | dominant case - a personally-owned item, the primary physical-label use case | +| 0 | 1 | `category` | `category_id` | label-adjacent (tagging); global, no owner - shares id 0 with `item` since the two never need the same arity | +| 1 | 2 | `group_item` | `owner_group_id`, `item_local_id` | same use case as `item`, but for a group-owned item | +| 1 | 1 | `group` | `group_id` | shared even less often; global, no owner | +| 2 | 2 | `storage_location` | `owner_identity_id`, `storage_location_id` | also label-printed | +| 2 | 1 | `file` | `file_id` | least often shared standalone; global, deduplicated by content hash | +| 3 | 2 | `workflow` | `owner_identity_id`, `workflow_id` | shared in-app, not printed - needs the escape range, since every other slot in the direct range (0-2) is already double-booked across the two arities in use | +| 4 | 2 | `group_storage_location` | `owner_group_id`, `storage_location_id` | same use case as `storage_location`, but for a group-owned location (see `group_item` above for the same owner/owner_group split); the direct range is fully double-booked, so this is the second kind that needs the escape range | A short id is inherently scoped to the backend that minted it (an "owner" field is a row that only exists in, and only means anything to, that one backend's database), not a portable replacement for @@ -119,13 +139,12 @@ Standard base64 assumes byte-aligned (8-bit) input, grouping 3 bytes into 4 outp padding to a byte boundary before encoding. Since there's no byte layer here to begin with, the bit-packed stream is instead packed directly into 6-bit groups and mapped straight onto the URL-safe base64 alphabet (RFC 4648 §5: `-` and `_` in place of `+` and `/`), with the final -character's unused low bits padded with zeros. That padding is safe by construction: the decoder -always knows exactly how many integers a given kind calls for, and a chunk's continuation bit is -`1 = more follows`, so a run of zero-padding at the very end can never be misread as "one more -chunk" - it decodes as a terminated chunk, at which point every field the schema called for has -already been produced and decoding simply stops. No `=` padding characters are needed either; those -exist in classic base64 purely to communicate trailing-byte padding, and there is no byte layer -here to need that. +character's unused low bits padded with zeros. That padding is safe by construction: it's always +0-5 zero bits (just enough to reach the next multiple of 6), and the decoder stops reading fields +the moment a chunk decodes to 0 with nothing left after it - by the last-field-nonzero rule above, +that can only be the padding, never a genuine field, so it's discarded rather than counted. No `=` +padding characters are needed either; those exist in classic base64 purely to communicate +trailing-byte padding, and there is no byte layer here to need that. ### The leading `~` @@ -140,6 +159,35 @@ short id can be handed to any part of the stack without first checking which enc there. +### Domain-Qualified Short ID + +A short id token is deliberately opaque and scoped to whichever backend minted it: handed a bare +`~DyU`, nothing in the token itself says which backend's numbering it should be read against. +That's fine as long as the token stays inside a context that already knows the answer (the app the +user is currently looking at), but not once a token needs to travel outside that context, e.g. +pasted into a message to a friend on a different domain, or logged somewhere not tied to one +backend. + +The fix is the same one every other cross-domain handle in this project already uses: pair the +opaque part with the domain that's authoritative for it. A **Domain-Qualified Short ID** is a short +id token prefixed with a domain and a literal `:`, e.g. `toolsheddomain.tld:~DyU`. The domain half +is exactly a handle's domain half (see federation.md's Unique Handles section): not a location, +just a statement of which backend to resolve the token against. Decoding still means handing the +`~token` half to that backend's decoder, same as ever; it's just no longer ambiguous which +backend's decoder to hand it to. + +This is deliberately not the same thing as a User-Qualified ID (`owner-handle:i42`) or an Item URL. +It isn't a URL and isn't meant to be openable by something that doesn't already know what a +Toolshed short id is: there's no scheme, no path, and the payload after `:~` is bit-packed base64, +opaque to a human. It belongs to the same "context already makes clear it's Toolshed data" class as +a compact User-Qualified ID, meant for use inside the app (or between things that already speak the +short-id format), not for a physical label or a link shared outside it. What it adds over a bare +`~token` is that it no longer depends on "whichever backend I currently happen to be talking to" — +the domain travels with it, so it keeps resolving to the same entity once copied elsewhere. Unlike +a User-Qualified ID, it isn't limited to owned kinds: every kind in the short id registry can be +domain-qualified the same way, since the domain only ever names which backend's token namespace +applies, not which kind the token decodes to or whether that kind has an owner. + ### Worked examples Encoding `kind = item` (0), `owner_identity_id = 7`, `item_local_id = 42`: @@ -152,17 +200,19 @@ Total: 17 meaningful bits, padded to the next multiple of 6 (18) with one zero b base64 characters: **`~DyU`**. The same worked-out form for one example of every registered kind - `Bits` is the same -space-separated segmentation (kind tag, escape offset if present, each field, then padding) the -Examples table on `/~` (`frontend/src/views/ShortId.vue`) shows for every registered kind: +space-separated segmentation: kind tag, escape offset if present, each field, then padding. Kinds +are grouped by shared id below to make the per-arity reuse visible: | Kind | Fields | Serialized | Bits | Token | |---|---|---|---|---| | `item` | `owner_identity_id: 7`, `item_local_id: 42` | `[0, 7, 42]` | `00 00111 1001001010 0` | `~DyU` | -| `storage_location` | `owner_identity_id: 3`, `storage_location_id: 1000` | `[1, 3, 1000]` | `01 00011 100111111001000 00` | `~Rz8g` | -| `category` | `category_id: 5` | `[2, 5]` | `10 00101 00000` | `~ig` | -| `workflow` | `owner_identity_id: 2`, `workflow_id: 9` | `[3, 2, 9]` | `11 00000 00010 01001 0` | `~wCS` | -| `group` | `group_id: 11` | `[4, 11]` | `11 00001 01011` | `~wr` | -| `file` | `file_id: 123` | `[5, 123]` | `11 00010 1011101011 0` | `~xXW` | +| `category` | `category_id: 5` | `[0, 5]` | `00 00101 00000` | `~Cg` | +| `group_item` | `owner_group_id: 5`, `item_local_id: 42` | `[1, 5, 42]` | `01 00101 1001001010 0` | `~SyU` | +| `group` | `group_id: 11` | `[1, 11]` | `01 01011 00000` | `~Vg` | +| `storage_location` | `owner_identity_id: 3`, `storage_location_id: 1000` | `[2, 3, 1000]` | `10 00011 100111111001000 00` | `~hz8g` | +| `file` | `file_id: 123` | `[2, 123]` | `10 1011101011` | `~rr` | +| `workflow` | `owner_identity_id: 2`, `workflow_id: 9` | `[3, 2, 9]` | `11 00000 00010 01001 0` | `~wiS` | +| `group_storage_location` | `owner_group_id: 5`, `storage_location_id: 1000` | `[4, 5, 1000]` | `11 00001 00101 100111111001000 000` | `~wln5A` | `workflow`, `group`, and `file` are kinds 3-5, so their `Bits` column shows the escape tag (`11`) followed by its own offset segment payload fields. \ No newline at end of file diff --git a/frontend/src/assets/fonts/pixel/LICENSE.md b/frontend/src/assets/fonts/pixel/LICENSE.md deleted file mode 100644 index c8e62bf..0000000 --- a/frontend/src/assets/fonts/pixel/LICENSE.md +++ /dev/null @@ -1,9 +0,0 @@ -# Pixel fonts used for small label text (see ../../../scss/_pixel-fonts.scss) - -- **Tom Thumb** (`TomThumb.ttf`) - by Brian Swetland, TTF conversion by gheja - (https://github.com/gheja/tom-thumb-ttf). Licensed CC0 or CC-BY 3.0 (original: - https://robey.lag.net/2010/01/23/tiny-monospace-font.html). -- **PICO-8** (`PICO-8.ttf`) - reproduction by Jacob Pierce - (https://github.com/jacobpierce/pico-8-font). MIT License, Copyright (c) 2016 Jacob Pierce. -- **Silkscreen** (`Silkscreen-Regular.woff2`) - by Jason Kottke, served via Google Fonts - (https://fonts.google.com/specimen/Silkscreen). SIL Open Font License 1.1. diff --git a/frontend/src/label-layouts.js b/frontend/src/label-layouts.js index 7d8fed1..a0d7381 100644 --- a/frontend/src/label-layouts.js +++ b/frontend/src/label-layouts.js @@ -149,10 +149,16 @@ export const LABEL_TEMPLATES = [ }, { id: "item-handle", name: "Item handle", - description: "The compact owner@domain:id handle - meaningful in-app, not scannable on its own.", + description: "The compact owner@domain:i User-Qualified ID - meaningful in-app, not scannable on its own.", required_vars: ["itemHandle"], tags: ["internal"], layout: [{type: "text", content: c => c.itemHandle}] }, + { + id: "location-handle", name: "Storage location handle", + description: "The compact owner@domain:s User-Qualified ID - meaningful in-app, not scannable on its own.", + required_vars: ["locationHandle"], tags: ["internal"], + layout: [{type: "text", content: c => c.locationHandle}] + }, { id: "item-url", name: "Item URL", description: "The full item URL as text, with no code - for copying rather than scanning.", @@ -252,11 +258,16 @@ export const DERIVED_VARS = { inputs: ["webdomain", "userHandle", "itemId"], calc: (f) => `${f.webdomain}/i/${encodeHandleForUrl(f.userHandle)}/${f.itemId}`, }, - // Compact "owner handle + id" form (see docs/design-in-progress/items-labels.md) - meaningful - // only where context already makes clear it's a Toolshed item, unlike itemUrl. + // Compact User-Qualified ID (see docs/handles-and-shortids.md#user-qualified-id) - owner + // handle + a one-letter kind tag + local id, meaningful only where context already makes + // clear it's Toolshed data, unlike itemUrl. `i` = item, `s` = storage location. itemHandle: { inputs: ["userHandle", "itemId"], - calc: (f) => `${f.userHandle}:${f.itemId}`, + calc: (f) => `${f.userHandle}:i${f.itemId}`, + }, + locationHandle: { + inputs: ["userHandle", "locationId"], + calc: (f) => `${f.userHandle}:s${f.locationId}`, }, }; diff --git a/frontend/src/views/Print.vue b/frontend/src/views/Print.vue index 4b1ffab..07afb53 100644 --- a/frontend/src/views/Print.vue +++ b/frontend/src/views/Print.vue @@ -234,7 +234,7 @@ diff --git a/frontend/src/views/Inventory.vue b/frontend/src/views/Inventory.vue index 15b8727..18f6023 100644 --- a/frontend/src/views/Inventory.vue +++ b/frontend/src/views/Inventory.vue @@ -2,12 +2,26 @@
-

Inventory Own & Friends"

+

Inventory

+
+
+ + +
+
-
{{ user }}'s Inventory
+
{{ selectedOwner }}'s Inventory
@@ -25,7 +39,7 @@ - + {{ item.name }} @@ -34,10 +48,10 @@ {{ item.owned_quantity }} - + - + @@ -52,7 +66,7 @@
-
@@ -70,10 +84,10 @@ {{ item.owned_quantity }}
- - Edit
- - Add + + Add
@@ -115,6 +129,7 @@ export default { data() { return { layout: "grid", + selectedOwner: null, } }, components: { @@ -123,15 +138,53 @@ export default { ...BIcons }, computed: { - ...mapGetters(["inventory_items", "loaded_items", "identityIdByHandle", "groupIdByHandle"]), - ...mapState(["user", "storage_locations"]), + ...mapGetters(["inventory_items", "groupInventoryItems", "loaded_items", "identityIdByHandle", "groupIdByHandle"]), + ...mapState(["user", "storage_locations", "groups", "groupMemberships", "friends"]), + // Groups hosted here plus groups only known via a GroupMembership pointer - see + // Groups.vue's allGroups and InventoryNew.vue's ownerGroups for the same merge/dedupe. + ownerGroups() { + const hostedHandles = new Set(this.groups.map(group => group.handle)) + const foreign = this.groupMemberships.filter(m => !hostedHandles.has(m.handle)) + return [...this.groups, ...foreign].sort((a, b) => a.handle.localeCompare(b.handle)) + }, + isGroupSelected() { + return !!this.selectedOwner && this.selectedOwner.startsWith('#') + }, + // Own items and group items can be created/edited/deleted here; a friend's items are + // shown for browsing only - the backend rejects writes for anyone but the owner or a + // fellow group member (see inventory.py's perform_create/update/destroy). + canEdit() { + return this.selectedOwner === this.user || this.isGroupSelected + }, + items() { + // item_map is keyed by owner handle regardless of whether that owner is a group or a + // friend, so the same getter serves both - see store.js's groupInventoryItems. + return this.selectedOwner === this.user ? this.inventory_items : this.groupInventoryItems(this.selectedOwner) + }, + addItemRoute() { + return this.selectedOwner === this.user + ? '/inventory/new' + : `/inventory/new?group=${encodeHandleForUrl(this.selectedOwner)}` + } }, methods: { - ...mapActions(["fetchInventoryItems", "deleteInventoryItem", "fetchStorageLocations", "fetchIdMap"]), - // Always the viewer's own items (fetchInventoryItems has no group filter); the owner - // handle is always included so /inventory/:handle/:id has exactly one shape. + ...mapActions(["fetchInventoryItems", "fetchGroupInventoryItems", "fetchFriendInventoryItems", + "deleteInventoryItem", "fetchStorageLocations", "fetchIdMap", "fetchGroups", + "fetchGroupMemberships", "fetchFriends"]), + fetchItemsForOwner() { + if (this.selectedOwner === this.user) return this.fetchInventoryItems() + if (this.isGroupSelected) return this.fetchGroupInventoryItems({groupHandle: this.selectedOwner}) + return this.fetchFriendInventoryItems({friendHandle: this.selectedOwner}) + }, + tryDeleteItem(item) { + this.deleteInventoryItem(item).then(() => { + this.fetchItemsForOwner() + }) + }, + // The owner handle is derived from the item itself, not the current selection, so a + // link stays correct even if the dropdown selection changes underneath it. itemRoute(item) { - return `/inventory/${encodeHandleForUrl(this.user)}/${item.id}` + return `/inventory/${encodeHandleForUrl(item.owner_group || item.owner)}/${item.id}` }, only_images(files) { return files.filter(file => file.mime_type.startsWith("image/")); @@ -156,10 +209,22 @@ export default { return {path: '/print', query: {kind: 'item', userHandle, item: item.id}} }, }, + watch: { + selectedOwner() { + this.fetchItemsForOwner() + } + }, + created() { + // Set before the first render so addItemRoute/items never see selectedOwner=null + // while user is already populated (which would misroute to the group branch). + this.selectedOwner = this.user + }, async mounted() { - await this.fetchInventoryItems() await this.fetchStorageLocations() await this.fetchIdMap() + await this.fetchGroups() + await this.fetchGroupMemberships() + await this.fetchFriends() } } diff --git a/frontend/src/views/StorageLocation.vue b/frontend/src/views/StorageLocation.vue index 0c2c2af..2e9869e 100644 --- a/frontend/src/views/StorageLocation.vue +++ b/frontend/src/views/StorageLocation.vue @@ -3,11 +3,22 @@

Storage Locations

+
+
+ + +
+
-
{{ user }}'s Storage Locations
+
{{ selectedOwner }}'s Storage Locations
@@ -25,7 +36,7 @@ - + {{ location.name }} @@ -40,7 +51,7 @@ - + @@ -55,7 +66,7 @@
-
@@ -73,7 +84,7 @@
Edit @@ -95,8 +106,8 @@
- - Add + + Add
@@ -116,6 +127,7 @@ export default { data() { return { layout: "grid", + selectedOwner: null, } }, components: { @@ -123,14 +135,37 @@ export default { ...BIcons }, computed: { - ...mapGetters(["identityIdByHandle", "groupIdByHandle"]), - ...mapState(["user", "storage_locations"]), + ...mapGetters(["identityIdByHandle", "groupIdByHandle", "groupStorageLocations"]), + ...mapState(["user", "storage_locations", "groups", "groupMemberships"]), + // Groups hosted here plus groups only known via a GroupMembership pointer - see + // Groups.vue's allGroups and Inventory.vue's ownerGroups for the same merge/dedupe. + ownerGroups() { + const hostedHandles = new Set(this.groups.map(group => group.handle)) + const foreign = this.groupMemberships.filter(m => !hostedHandles.has(m.handle)) + return [...this.groups, ...foreign].sort((a, b) => a.handle.localeCompare(b.handle)) + }, + locations() { + return this.selectedOwner === this.user ? this.storage_locations : this.groupStorageLocations(this.selectedOwner) + }, + addLocationRoute() { + return this.selectedOwner === this.user + ? '/storage-locations/new' + : `/storage-locations/new?group=${encodeHandleForUrl(this.selectedOwner)}` + } }, methods: { - ...mapActions(["fetchStorageLocations", "deleteStorageLocation", "fetchIdMap"]), - // This list is always the caller's own personal locations (fetchStorageLocations has no - // group content), so the owner_group branches below are currently unreachable here - kept - // for parity with Inventory.vue's shortIdLink/printLinkFor in case that ever changes. + ...mapActions(["fetchStorageLocations", "fetchGroupStorageLocations", "deleteStorageLocation", + "fetchIdMap", "fetchGroups", "fetchGroupMemberships"]), + fetchLocationsForOwner() { + return this.selectedOwner === this.user + ? this.fetchStorageLocations() + : this.fetchGroupStorageLocations({groupHandle: this.selectedOwner}) + }, + tryDeleteLocation(location) { + this.deleteStorageLocation(location).then(() => { + this.fetchLocationsForOwner() + }) + }, locationRoute(location) { return `/storage-locations/${encodeHandleForUrl(location.owner_group || location.owner)}/${location.id}` }, @@ -153,9 +188,20 @@ export default { return {path: '/print', query: {kind: 'storage-location', userHandle, location: location.id}} }, }, + watch: { + selectedOwner() { + this.fetchLocationsForOwner() + } + }, + created() { + // Set before the first render so addLocationRoute/locations never see selectedOwner=null + // while user is already populated (which would misroute to the group branch). + this.selectedOwner = this.user + }, async mounted() { - await this.fetchStorageLocations() await this.fetchIdMap() + await this.fetchGroups() + await this.fetchGroupMemberships() } } From 7276750c6672c5cd9aff88668a026e7095959ed7 Mon Sep 17 00:00:00 2001 From: jedi Date: Thu, 27 Aug 2026 20:41:19 +0200 Subject: [PATCH 10/18] stash --- docs/design-in-progress/api-classification.md | 77 +++++ docs/design-in-progress/groups-mvp.md | 24 +- docs/design-in-progress/image-caching.md | 2 +- docs/design-in-progress/items-labels.md | 24 +- docs/development.md | 2 +- docs/glossary-todo.md | 40 +-- docs/glossary.md | 32 ++- docs/implementation.md | 271 ++++++++++++++++++ frontend/src/router.js | 33 ++- frontend/src/views/Inventory.vue | 94 +++--- frontend/src/views/InventoryNew.vue | 11 +- frontend/src/views/StorageLocation.vue | 82 +++--- frontend/src/views/StorageLocationDetail.vue | 6 +- frontend/src/views/StorageLocationEdit.vue | 6 +- frontend/src/views/StorageLocationNew.vue | 17 +- 15 files changed, 552 insertions(+), 169 deletions(-) create mode 100644 docs/design-in-progress/api-classification.md create mode 100644 docs/implementation.md diff --git a/docs/design-in-progress/api-classification.md b/docs/design-in-progress/api-classification.md new file mode 100644 index 0000000..24792b2 --- /dev/null +++ b/docs/design-in-progress/api-classification.md @@ -0,0 +1,77 @@ +# API Endpoint Versioning Scope (Design in Progress) + +Status: decision deferred. The `/api/` prefix has already been changed to `/api/v1/` for every +route under that prefix (inventory items, storage locations, search, friends, friend requests, +groups, group invites, id mapping, tags/properties/categories/availability policies/info, +files/item files/staged files, and account export/import/delete). Whether that blanket approach is +the right scope, or whether versioning should be narrower, is the open question below. + +## Problem + +A versioned URL prefix matters most where two independently-deployed servers need to agree on a +wire contract: federation. An endpoint that's only ever called by a server's own frontend against +its own backend is deployed in lockstep with that backend, so it has no independent version to +track. Applying a uniform prefix everywhere is simpler, but it also prefixes a large number of +routes that never cross a domain boundary, and skips one non-`/api/` route +(`/auth/user//`, used to fetch a friend's profile/avatar) and the file/thumbnail serving +routes (`/media/...`), both of which do cross a domain boundary and arguably belong in the +versioned surface more than some of the routes currently in it. + +One exception either way: the version/capability discovery route (`GET /api/version/`) has to stay +unprefixed no matter which option is chosen — it's how a caller finds out which versions a server +speaks in the first place, so it can't itself live behind a version segment. + +## Options + +**Option A: version every real endpoint uniformly.** +Every route under `/api/`, `/auth/`, `/admin/`, and `/media/` gets the `/api/v1/` prefix (except +`/api/version/`), regardless of whether it's ever called across a domain boundary. Simple to state +and apply; doesn't require maintaining a classification of which routes are federation-facing. + +**Option B: version only routes that actually cross a domain boundary.** +Only routes a server's own frontend calls against a *different* server (directly, or that another +server calls against this one) get the `/api/v1/` prefix. Everything else keeps its existing, +unprefixed path. Smaller versioned surface, but requires keeping the classification below current +as routes change, and means a single URL path can't ever serve both a same-domain and +cross-domain purpose without the whole path being versioned. + +## Current classification + +Verified against actual call sites (which routes are dispatched against a friend's/foreign +server rather than the caller's own home server), not just against which routes are technically +reachable by a non-local caller — several routes accept a broader signature-based authentication +than they actually need, and one route family enforces "local caller only" itself even though its +authentication layer would allow a remote identity through. + +**Crosses a domain boundary today:** +- `POST /friendrequests/` (also called against the recipient's own server, not just the sender's) +- `GET/POST /groupinvites/` (invite delivery is posted to the invitee's own server) +- `POST /group_invites/accept/` (posted to the group's own home server) +- `GET /groups//`, `DELETE /groups//members//`, `POST /groups//invites/` +- `* /inventory_items//...`, `* /storage_locations//...` (group-owned or + friend-owned items/locations) +- `GET /search/` (fans out to every known server) +- `GET /resolve_short_id////` +- `GET /auth/user//` (friend profile/avatar lookup — not currently under `/api/`) +- `GET /media/...`, `GET /thumbnail//...` (image bytes fetched from the owning server — + not currently under `/api/`) + +**Never observed leaving the caller's own home server:** +- `GET/POST /friends/`, `DELETE /friends//`, `DELETE /friendrequests//` +- `GET /groups/` (the collection route, as opposed to `/groups//`) +- `DELETE /groupinvites//`, `POST /groupinvites//accept/`, `GET /groupmemberships/` +- `GET /idmap/` (distinct from `/resolve_short_id/...`, which does cross domains) +- `* /workflows/...` +- `GET /tags/`, `GET /properties/`, `GET /categories/`, `GET /availability_policies/`, `GET /info/`, + `GET /domains/` (also not currently called from the frontend at all) +- `POST /import/`, `GET /export/`, `DELETE /account_data/`, `DELETE /account/` — these accept the + same broad authentication as the federation-facing routes, but reject any non-local caller + themselves +- `GET /files/`, `* /item_files/...`, `* /staged_files/...` +- `GET/PATCH /auth/user/`, `POST /auth/register/`, `POST /auth/token/`, `GET /auth/preferences/`, + `GET/PUT /auth/self/preferences/`, `DELETE /auth/self/preferences//`, `* /auth/users/...` + (admin management) +- everything under `/admin/` (domain/category/property/tag administration) + +**Exception regardless of which option is chosen:** +- `GET /api/version/` diff --git a/docs/design-in-progress/groups-mvp.md b/docs/design-in-progress/groups-mvp.md index 5fcaefe..5b683d6 100644 --- a/docs/design-in-progress/groups-mvp.md +++ b/docs/design-in-progress/groups-mvp.md @@ -50,16 +50,20 @@ separate "discover groups you're not in" browsing for MVP — you land in a grou it, the same way you become friends with someone by request/accept, not by browsing a directory of all users. -Known limitation: this list only ever queries the member's own home backend, so it only shows -groups actually hosted there (groups you created, or joined on your own domain). Membership itself -works regardless of which backend hosts the group — a remote member can still be invited, accept, -and fully edit/delete the group's items (see "Owning items as a group" below) — but a group hosted -on someone else's backend won't show up in your own "My Groups" list, because unlike friendship -(which both sides record), group membership is only ever recorded on the group's own home backend, -and there's no index anywhere of "which other backends has this identity been added to." Making a -remote membership discoverable would need a small personal pointer index (written by the client at -join time) plus a handle-based group lookup on the group's own backend; deferred as a fast-follow -alongside group-friending. +Listing groups (the table itself) only ever queries the member's own home backend — it's this +backend's own view of "groups I host you in" — but every row links to the same group detail page +regardless of where the group is actually hosted. Mirroring friendship (which both sides record), +the member's own home backend also remembers the bare fact of a remote membership, as a pointer +alongside the group's own home backend's real membership record, and merges the two into one table +keyed by handle (a group present in both keeps its home-hosted row, which already carries a member +count). + +A group's full detail resolves from its handle the same way a friend's foreign items already do: +the client resolves the target group's own domain from its handle and talks to that backend +directly, rather than assuming home. `GroupDetail.vue` needs no "is this group local or foreign" +branch anywhere, including for creating a new item/storage location — the owner selector binds +directly to the group's handle, and a group reference is addressed by handle everywhere it appears, +never by a bare internal id. ### Group detail page diff --git a/docs/design-in-progress/image-caching.md b/docs/design-in-progress/image-caching.md index d2469c7..4616557 100644 --- a/docs/design-in-progress/image-caching.md +++ b/docs/design-in-progress/image-caching.md @@ -129,7 +129,7 @@ long-lived, network-interposing piece of code — exactly the kind of thing a su or an XSS-planted `registration.update()` would target. If the page's message handler blindly signs whatever URL the request names, a compromised SW stops being "something that can read images this identity can already see" and becomes "something that can get a validly-signed request for *any* -endpoint" — e.g. `POST /api/inventory/items/5/delete` or `POST /api/friends/accept` — and then just +endpoint" — e.g. `POST /api/v1/inventory/items/5/delete` or `POST /api/v1/friends/accept` — and then just replay it directly against the real backend. That's a full account-takeover primitive smuggled in through what was supposed to be an image-caching optimization, and it's strictly worse than not having the bridge at all. diff --git a/docs/design-in-progress/items-labels.md b/docs/design-in-progress/items-labels.md index 6a636fb..14287f2 100644 --- a/docs/design-in-progress/items-labels.md +++ b/docs/design-in-progress/items-labels.md @@ -1,8 +1,11 @@ # Item Handles & Physical Labels (Design in Progress) -Status: not implemented. This document collects the problem, goals, and open design questions for -giving inventory items stable identifiers and physical (scannable) labels. Nothing here is -settled. +Status: partially implemented. This document collects the problem, goals, and open design +questions for giving inventory items stable identifiers and physical (scannable) labels. The core +handle/URL shape question below is settled, generalized to cover any owned kind, not just items — +see [User-Qualified ID](../handles-and-shortids.md#user-qualified-id) and +[Item URL](../glossary.md#item-url). The remaining open questions (unguessability, label retirement +on deletion, route reconciliation, lending/borrowing) are not. ## Problem @@ -59,11 +62,16 @@ unguessability question below). Two distinct formats are needed, because "an item handle" is used in two different situations: - *A compact handle, for use where context already makes clear it's a Toolshed item.* Inside the - app, in exports, in logs, anywhere the reader already knows they're looking at Toolshed data, - the handle doesn't need to spell that out or be openable on its own. This can be as short as - `user@domain.tld:id`, the item's owner handle with `:id` appended, mirroring how a tag/category - handle already appends `:name` after its origin (see federation.md's Unique Handles section). - No new delimiter concept, just the same pattern applied to items. + app, in exports, in logs, anywhere the reader already knows they're looking at Toolshed data, the + handle doesn't need to spell that out or be openable on its own. An item's compact handle is the + item-kind instance of the general [User-Qualified + ID](../handles-and-shortids.md#user-qualified-id) scheme: the item's owner handle, the `i` kind + letter, and the local id, e.g. `alice@example.com:i42`. The same scheme covers any owned kind (a + storage location, for instance); item handles are just the motivating case. An alternative in the + same compact-handle family pairs the owner's domain with a [short + id](../handles-and-shortids.md#short-ids) instead, e.g. `domain.tld:~DyU` (see + [Domain-Qualified Short ID](../handles-and-shortids.md#domain-qualified-short-id)) — shorter, at + the cost of needing the app's short id decoder to make sense of it at all. - *A self-contained URL, for use with no context at all.* A physical label, a link shared outside the app, has to work without the reader already knowing what it is or which server it belongs diff --git a/docs/development.md b/docs/development.md index 9001215..9126ec6 100644 --- a/docs/development.md +++ b/docs/development.md @@ -107,7 +107,7 @@ This will start an instance of the frontend and wiki, a limited DoH (DNS over HT The two backend instances are set up to use the domains `a.localhost` and `b.localhost`, the local DoH server is used to direct the frontend to the correct backend instance. The frontend is configured to act as if it was served from the domain `a.localhost`. -Access the frontend at `http://localhost:8080/`, backend at `http://localhost:8080/api/`, api docs +Access the frontend at `http://localhost:8080/`, backend at `http://localhost:8080/api/v1/`, api docs at `http://localhost:8080/docs/` and the wiki at `http://localhost:8080/wiki/`. The dev proxies terminate TLS with `frontend/.local/localhost.crt`, signed by the dev-only CA at diff --git a/docs/glossary-todo.md b/docs/glossary-todo.md index 1b91583..3c7b62a 100644 --- a/docs/glossary-todo.md +++ b/docs/glossary-todo.md @@ -33,16 +33,6 @@ inconsistencies are listed, not every correct usage that was checked and cleared ## Handle / User handle -- ~~`backend/authentication/models.py` (`class KnownIdentity`...), `signature_auth.py` - (`author_identity`...), `frontend/src/identity.js` (`serializeIdentityRecord`...)~~ — no longer a - finding: the glossary now has an explicit **Identity** entry (handle + keypair, held together as - the unit a backend trusts), and this is exactly what these already name. No renaming needed here; - if anything, these are the parts of the codebase the new Identity entry should point to as - reference implementations. -- `backend/toolshed/serializers.py:49-57` (`FriendSerializer`) — API field is literally named - `"username"` but its value is a full handle (`username + '@' + domain`). Already - self-acknowledged in a comment at `frontend/src/store.js:404`. **Highest-value single fix** — - it's a live API contract, not just an internal name. - `frontend/src/store.js` — several action params destructured as `{username}` that actually carry a full handle: `lookupServer` (313), `getFriendServers` (359), `fetchFriendProfile` (401-405), `login` (276-282). @@ -66,23 +56,6 @@ it; Actor and Targeted sharing are unimplemented with no competing name anywhere "neighbor" vocabulary — refers to unreachable backend *domains* during discovery, not to friendship, despite reading like a synonym at a glance. -## Group / Group handle / Membership list - -- `backend/backend/settings.py:36` — `django.contrib.auth` ships a built-in `Group` model, shown - as "Groups" in the Django admin. Not unregistered anywhere. Will collide by name with the - proposed actor-type Group once that's implemented — worth a decision now (unregister the - built-in admin Group, or otherwise disambiguate) before the real feature lands. -- `issues.md` (issue #3, "Group Concept", ~lines 62-156) — a standalone proposal that conflicts - with the already-settled `docs/design-in-progress/groups.md` design on three points at once: - - `Group.public_key`/`private_key` fields (contradicts "does a group need its own keypair? No"). - - `GroupMembership` backed by a signed `membership_certificate` rather than a plain membership - list (contradicts the glossary's Membership list entry). - - Bare `Group.handle` strings with no `#` prefix, e.g. `"makerspace-nord"`, and a - `GroupInvitationIncoming.group_handle` field/API surface (`POST /api/groups/` etc., ~lines - 65-66, 117-121, 150-156) that never uses the `#groupname@domain` shape. - This is a design-conflict issue, not a wording tweak — `issues.md` should be reconciled with (or - explicitly marked superseded by) `groups.md` before anyone implements from it. - ## Keypair / Private key / Public key - Wire-format drift on the one field that actually crosses the network: `befriender_key` is used @@ -100,8 +73,10 @@ it; Actor and Targeted sharing are unimplemented with no competing name anywhere - `backend/configure.py:130` ("Identifier set {} already imported, skipping") and the model `ImportedIdentifierSets` (`backend/hostadmin/models.py:13-19`) — call an imported origin dataset an "identifier set". -- `issues.md:206-210` — Instance Admin TODO list: "identifier-sets" for **Origin** and bare - "identifiers" for **Classification handle**, both alternate terms not matching glossary names. +- `issues.md:200-202` — Instance Admin TODO list: "identifier-sets" for **Origin** (matches + `ImportedIdentifierSets`/`configure.py`: one identifier set per `shared_data/*.json` file, named + `git:`, i.e. exactly an Origin) and bare "identifiers" for **Classifier** (not Classification + handle — a classification handle is the `origin#type:name` pointer, not the entity it names). ## Alias @@ -118,13 +93,6 @@ it; Actor and Targeted sharing are unimplemented with no competing name anywhere column-auto-mapping heuristic treats `"type"` as a synonym for Category: `lowerColumn.includes('category') || lowerColumn.includes('type')`. -## Item Handle - -- `frontend/src/views/Search.vue:30,52,110` — a field literally named `handle` is computed here - (`e.owner==this.user ? e.id : "shared/"+e.owner+"/"+e.id`), but it's a router-path fragment, not - an Item Handle: no domain-qualified `user@domain.tld:id` shape, and `e.owner` is a bare username. - Whoever implements the real Item Handle later is likely to collide with this existing variable. - ## Item Label - `frontend/src/components/workflow/workflows/FotoFirstBulkImportWorkflow.vue:567-572,822` and diff --git a/docs/glossary.md b/docs/glossary.md index a186a98..90c3643 100644 --- a/docs/glossary.md +++ b/docs/glossary.md @@ -80,15 +80,17 @@ is first trusted for that handle. Currently only exists between users; groups ha groups befriending groups, are proposed extensions of the same mechanism, not a new one. *See: [federation.md](federation.md#cryptography), [groups.md](design-in-progress/groups.md#should-a-group-be-able-to-grant-read-access-to-non-members-group-friends)* -**Group** (Proposed) +**Group** (Implemented) A second kind of [actor](#actor), modeling collective ownership (a club, workshop, or company) rather than any one person owning something. All members hold equal edit rights over what the group owns; membership itself is the privilege, there's no separate owner/member distinction within a group. Backed by a [membership list](#membership-list) rather than a [keypair](#keypair-private-key-public-key), -and identified by a [group handle](#group-handle). +and identified by a [group handle](#group-handle). Groups can own items and storage locations today +(`owner_group` on both); a group having its own friends, or befriending another group, is not +implemented, so a group is not yet a full [actor](#actor) in every sense of that entry. *See: [groups.md](design-in-progress/groups.md#what-a-group-is)* -**Group handle** (Proposed) +**Group handle** (Implemented) A [group](#group)'s handle: a name and [domain](#domain) written like a [user handle](#user-handle) but prefixed with `#`, e.g. `#groupname@toolsheddomain.tld`. The prefix keeps groups and users in disjoint namespaces on the same domain (no squatting collision between a user and a group wanting @@ -114,11 +116,16 @@ signatures. Only user handles carry a keypair, not groups, classification handle User-Qualified IDs. *See: [federation.md](federation.md#cryptography)* -**Membership list** (Proposed) -The record of which [user handles](#user-handle) currently belong to a [group](#group), maintained -by whichever backend is authoritative for the group's handle. What backs a group's identity in -place of a keypair: a request "as the group" is a normal signed request from a current member, plus -a check against this list, not a request signed by some shared group key. +**Membership list** (Implemented) +The authoritative record of which [user handles](#user-handle) currently belong to a +[group](#group), maintained by whichever backend is authoritative for the group's handle +(`Group.members` in `authentication/models.py`). What backs a group's identity in place of a +keypair: a request "as the group" is a normal signed request from a current member, plus a check +against this list, not a request signed by some shared group key. Distinct from +`authentication.models.GroupMembership`, a second, member-side record kept on a *member's own* home +backend mirroring the bare fact of belonging (analogous to how a friendship is independently +recorded on both sides) — that pointer is not itself authoritative, it exists so a member's own +backend can list groups it believes the member belongs to without querying every other backend. *See: [groups.md](design-in-progress/groups.md#does-a-group-need-its-own-keypair)* **Signature / Signing** (Implemented) @@ -195,10 +202,11 @@ server or a domain, it can equally be a shared reference dataset (like the files *See: [federation.md](federation.md#tags-properties-and-categories)* **Tag / Property / Category** (Implemented) -The three kinds of classification entity an item can reference, each identified by a -[classification handle](#classification-handle). A property additionally carries unit metadata -(`unit_symbol`/`unit_name`), though property *values* on an item are plain, undeclared-type -strings today. +The three kinds of classification entity an item can reference, collectively called a +**classifier**, each identified by a [classification handle](#classification-handle). A property +additionally carries unit metadata (`unit_symbol`/`unit_name`), though property *values* on an item +are plain, undeclared-type strings today. A single [origin](#origin) (e.g. one +`backend/shared_data/*.json` file) typically defines many classifiers at once. *See: [federation.md](federation.md#tags-properties-and-categories), [tags.md](design-in-progress/tags.md)* ## Items & Physical Labels diff --git a/docs/implementation.md b/docs/implementation.md new file mode 100644 index 0000000..848f7aa --- /dev/null +++ b/docs/implementation.md @@ -0,0 +1,271 @@ +# Implementation Notes + +Design rationale and non-obvious invariants that used to live as long inline comments, moved here so the code stays skimmable while the reasoning stays discoverable. Code comments reference these sections by anchor. + +## Content-Addressed Files & Caching + +### FileCache Design Rationale +`fileCache` (`frontend/src/fileCache.js`) is a shared, session-lifetime cache for the bytes behind `AuthenticatedImage`'s `src` (a `/media/...` hash-addressed path; see `backend/files/media_urls.py`). Content there is immutable and hash-addressed (SHA-256), so a cache hit never needs revalidation — the same `src` string can only ever resolve to the same bytes, for any owner or requester. + +It is deliberately NOT Vuex state: it holds `Blob`/object-URL data that nothing needs reactive access to (components only bind the resulting object-URL string, which they hold in their own local state), so a plain module-level `Map` avoids Vue's reactivity overhead entirely and sidesteps proxying `Blob` instances for no benefit. + +### SHA-256 File Hashing Must Match The Backend +Before uploading, the frontend computes a SHA-256 hash of a file's raw bytes via the Web Crypto API (`crypto.subtle.digest('SHA-256', ...)`). This hash must match the one the backend computes over the same bytes (`backend/files/models.py`, `hashlib.sha256`), so the hash computed client-side can later be used to identify the same `File` row server-side without a mismatch. This same computation appears in `CameraFileSource.vue`, `DragDropFileSource.vue`, `FsFileSource.vue`, and `WebcamFileSource.vue`. + +### Stale Orphan Cleanup At The Canonical Hash Path +`FileManager.create()` (`backend/files/models.py`) derives each file's upload path entirely from its content hash (`hash_upload`), and `hash` is unique in the database. That means if no `File` row currently owns a given hash, anything already sitting at that hash's computed storage path is necessarily a stale orphan — for example, left behind by a bug in cleanup code that deleted a `File` row without removing its stored bytes, or by a crashed upload. + +Before saving a new `File` row, `create()` proactively deletes anything already at the expected path rather than letting Django's storage layer invent an alternate filename to avoid the apparent "collision". A suffixed name would silently break every part of the app that derives a file's URL purely from its hash (media serving, thumbnail generation, item/avatar attachment), and no future caller could ever discover the file again. `backend/files/tests.py`'s `test_file_upload_reclaims_stale_orphan_at_canonical_path` is a regression test for this exact scenario. + +### RGBA Flattening Avoids Revealing Black Under Transparent Pixels +When generating thumbnails (`backend/files/media_urls.py`, `thumbnail_urls`), the source image is first converted to RGBA and composited onto an opaque white background before being flattened to RGB and saved as JPEG. This matters because some source modes (grayscale+alpha, palette-with-transparency, RGBA) store meaningless color or luminance data underneath fully transparent pixels, which is often zeroed out (i.e. black). Converting straight to RGB without compositing reveals that zeroed data as solid black instead of showing nothing, producing a thumbnail that looks broken even though the visible, opaque content is fine. + +`backend/files/tests.py`'s `test_thumbnail_flattens_transparency_instead_of_going_black` is a regression test built around an `'LA'` (grayscale + alpha) source image whose transparent half has zeroed-out luminance underneath, reproducing exactly this failure mode. + +## Label Rendering Engine + +### QR Encoder Loading +`anyd-qr.js`'s own `loadAnyDCode()` memoizes the wasm instantiation itself, so calling it more than once (each of `Print.vue` and `LabelLayoutPreview.vue` does, on mount) is free — `label.js`'s module-level `anyd` variable just mirrors its resolved value so `buildRenderTree` can use it synchronously. Until it resolves, a QR-family leaf (any `QR_LEAF_TYPES` entry) throws (see `encodeQr`) the same way an oversized value already does — callers already have to handle `layoutContent` throwing, so this reuses that path rather than adding a second failure mode. + +### QR Leaf Type Mapping +`QR_LEAF_TYPES` maps each of `label-layouts.js`'s `LABEL_TEMPLATES` leaf types that draw a code to the `anyd-qr.js` symbology/error-correction level (and, for rMQR, size strategy) it renders as (see anyd's `EncodeOptions` — `ecc`/`size` — and its per-symbology `EcLevel` enums, `wasm.rs`'s `qr_ec`/`micro_ec`/`rmqr_ec`/`rmqr_size`). Which combination a given label uses is baked into its layout tree (see `label-layouts.js`'s `"qr"`-prefixed templates), rather than a single choice applied to every code leaf alike, so there's no longer a global selector for any of them (see `Print.vue`). + +A plain symbology id (no suffix) always means anyd's own defaults — ecc `"M"`, rMQR size `"balanced"` — every other value gets a `"-"` suffix naming it: +- **ecc**: the same letter anyd itself uses (`qr_ec`/`micro_ec`'s L/M/Q/H), except micro-qr's `"Detection"` (`MicroEcLevel::Detection`, an M1-only error-*detection*-but-not-correction mode with no plain single-letter grade of its own). Coverage isn't uniform across symbologies (see `qr_ec`/`micro_ec`/`rmqr_ec`) — full QR takes all four grades, Micro QR swaps `"L"` (QR's actual lowest) for `"Detection"` (lower still, but M1-only) and has no `"H"` at all, and rMQR only ever supports `"M"` or `"H"`. +- **size** (rMQR only, see `rmqr_size`/`SizeStrategy`): `"min"`/`"max"` prefer the shortest (flattest, widest) or tallest (narrowest) symbol that fits the text, over the default `"balanced"` (smallest total module area) — which shape to prefer depends on which of the tape's two axes (across vs. along the feed) is more constrained. + +rMQR's matrix isn't square (see `encodeQr`'s width/height), unlike qr/micro-qr, which always are. + +### QR Module Matrix Shim +`encodeQr` wraps anyd's row-major `Uint8Array` matrix in a BitMatrix-alike shim (`{width, height, get(row, col)}`), matching the shape `drawQrLeaf`/`snapQrToCrispSize` expect — they predate this and were written against the old `"qrcode"` package's own `modules.size`/`get()` shape. anyd's matrix already excludes the quiet zone from width/height (see its `ModuleMatrix` type), same as the old library's BitMatrix. width/height are kept separate rather than a single `size` (the old library's own shape, always square) since rMQR symbols are rectangular. + +### Layout Tree Structure +A layout is a tree built from two shapes, alternating orientation by nesting depth: + +- An array is a "split" node: its children sit side by side (a *row*) at even depth (the root, depth 0, is always a row), or stacked (a *column*) at odd depth. To turn a row into a column, wrap it in an extra one-element array — that array is one depth deeper, so its lone child (the original row) is now read at odd depth. +- An object is a leaf: `{type, content}` where `type` is one of `QR_LEAF_TYPES`' keys draws a QR/Micro QR/rMQR code at that id's symbology/error-correction level (see QR Leaf Type Mapping above), `{type: "text", content}` draws a text block — either way `content` is a function from the resolved field values to the string (or, for `"text"`, an array of strings — one per line) to render. `{type: "empty", "min-width": "2mm"}` / `{type: "empty", "min-height": "2mm"}` is a spacer with no ink of its own — the *only* way padding/gaps enter a layout, since nothing here draws a border, margin or gap on its own. An "empty" leaf's dimension always names the axis its enclosing split flows along: `"min-width"` inside a row, `"min-height"` inside a column. + +See `label-layouts.js`'s `LABEL_TEMPLATES` for concrete trees. + +### Pixel Font Selection +Below 33px, a general-purpose sans-serif gets blurry/illegible, so `drawTextLeaf` switches to one of `fontTierFor`'s bitmap-style fonts instead (see `../assets/fonts/pixel/LICENSE.md`) — Silkscreen/Chava/Pixelon/Terminus across increasingly larger size bands, snapping to whichever fixed/hinted size each reads best at rather than whatever `fontPx` literally asks for. Unlike the top (Inter) tier, these are rendered through `pixel-font.js`'s `drawPixelText` rather than canvas `fillText` — see Freetype Production Rendering below for why. At 33px and up, `fontTierFor` names Inter explicitly (see `../scss/_label-fonts.scss`) rather than falling back to the CSS generic `sans-serif` keyword — that keyword resolves to a different real font per browser/OS, which would make the same label print differently depending on where it was rendered from. + +Every tier was chosen only after rendering through `prototypes/freetype-wasm/ladder.html` — FreeType's own monochrome rasterizer at real canvas pixel sizes — and inspecting the actual pixels; checking that `fillText` was merely *called* doesn't confirm anything legible got drawn, and neither does a DPI-adjusted size that was never the number actually handed to the rasterizer. Two earlier candidates were tried and dropped: Tom Thumb's declared ascent/descent (0 / ~fontPx, backwards from a normal font) turned out not to be a centering quirk — its actual visible ink was only ~1/3.2 of its own nominal font-size — and PICO-8 rendered cleanly but has no lowercase glyphs at all (silently draws lowercase input as uppercase), ruling it out for real label content (item handles, URLs) that isn't reliably all-caps; see `frontend/src/assets/fonts/pixel/LICENSE.md`. Chava carries the same uppercase-only limitation but was kept anyway, restricted to a narrow size band, since PICO-8's dealbreaker didn't get applied consistently — see that same LICENSE.md. + +Each tier's `size` (the actual px handed to the rasterizer) replaces the old Tom Thumb-only `scale` correction, generalized: `fontPx` (the layout-derived logical size, compared against each branch's threshold) still drives box centering/stacking regardless of what `size` a tier resolves to. Those thresholds and `MIN_TEXT_PX` are both compared against that same logical, unscaled `fontPx`, deliberately not adjusted for the tape's dpi: a browser's font rasterizer only ever sees a raw pixel count, with no notion of "physical size" at all, so that's what determines whether a glyph's fine detail survives — confirmed by real-Chromium testing, where a raw 4.35px render was a solid blob regardless of what a dpi-scaled version of that number would have implied. + +A tier's optional `tracking` retunes glyph spacing (`drawPixelText` adds it to every glyph's advance) without ever touching the rendered glyph size. + +### Freetype Production Rendering +Canvas 2D's `fillText` is always anti-aliased — there is no monochrome/hinted-bitmap text mode in the API, however carefully a glyph's size/position are snapped to the pixel grid — and `weblabel.js`'s `bitmap.js` hard-thresholds the whole canvas at 50% luminance before it ever reaches a printer (a pin either fires or it doesn't). Anti-aliased glyph edges surviving into that threshold read as stray or missing pixels, not a clean bitmap-font shape. `pixel-font.js`'s `drawPixelText` sidesteps this for every `fontTierFor` tier below Inter by rendering through the same FreeType-wasm rasterizer `ladder.html` uses to *pick* those fonts in the first place (`FT_LOAD_TARGET_MONO`, true 1-bit output) and blitting the result at integer pixel offsets — `measureLine`'s pass computes a line's real ink extent (advance width, max ascent/descent actually reached by *these* glyphs) the same way `drawTextLeaf`'s old `ctx.measureText(...).actualBoundingBox*` did, so centering math is unchanged; only where the pixels themselves come from is different. Inter (the one non-`pixel` tier) still uses `fillText` — anti-aliasing is expected/fine at that size, the same reasoning `weblabel.js`'s own text renderer relies on ("what stops small text turning to mush is not the threshold but the size"). + +`preloadPixelFontRenderer()` (re-exported from `label.js`, defined in `pixel-font.js`) loads `freetype.wasm` and fetches every bitmap tier's font bytes up front, the same pattern as `preloadQrEncoder`/`anyd` (see QR Encoder Loading) — `drawTree`/`drawTextLeaf` stay fully synchronous, so both `Print.vue` and `LabelLayoutPreview.vue` await it (alongside `preloadQrEncoder`) before their first `redraw()`, and `drawPixelText` throws the same "still loading" shape of error `encodeQr` does if a draw is ever attempted before it resolves. The wasm module holds exactly one font face loaded at a time (`ft_load_font` replaces whatever was there); `ensureFace` skips the reload when consecutive leaves already share a family. + +### Affine Width Height Relations +Every node's width/height relate to each other affinely — `width = A*height + B` for a node read in row context, `height = A*width + B` in column context — because a leaf is either scale-free (a text block, whose aspect ratio holds at any size: `A = aspect` or `1/aspect`, `B = 0`) or a fixed physical size (an "empty" spacer, or a QR code once its crisp pixel size is known — see Crisp QR Sizing: `A = 0`, `B` = the size in px). Splits combine their children's relations by addition (a row's total width is the sum of each child's width for the shared height, and symmetrically for a column), which stays affine, so the same two numbers describe a whole subtree no matter how deeply it nests. + +`ownAxis` is true if the split directly containing `node` is a row, false if a column — for a leaf, that's what "empty" measures itself against; for a split, its own axis (and thus how it combines its children) is always the opposite, per the alternating-depth rule. `wantWidth` is true to ask for `{A, B}` such that `width = A*height + B`, false for `height = A*width + B`; requesting the direction a split doesn't naturally combine in just inverts its own relation. + +### Fixed Size Relation Edge Case +In `relation()`, when every child is a fixed size (`a === 0`) in the combining direction — e.g. a row that's just one crisp QR leaf, with no scale-free (text) sibling to invert against — inverting "width = b" for an `a` of 0 would divide by zero: a constant width genuinely doesn't determine a height, since nothing here actually scales with it. Instead, ask each child directly for its own size in the wanted direction (every one of them must be similarly fixed, since only a fixed leaf ever contributes `a === 0`), and take the largest — the shared dimension has to fit whichever child needs the most room, with any child that ends up with room to spare centered within it (see `drawQrLeaf`). + +### Crisp Qr Sizing +A QR code needs an integer number of pixels per module to render crisply rather than blurring at a fractional scale, so its true size is whatever that rounds down to — almost never the scale-free box its aspect ratio alone would suggest. `snapQrToCrispSize`, called once every QR-family leaf has a provisional (scale-free) box from a first `layoutTree` pass, pins each one's real `box.width`/`box.height` as `crispWidth`/`crispHeight`, so `relation()` starts treating it as a fixed size, the same as an "empty" leaf, instead of one that scales with whatever height/width it's offered. A second `relation()`/`layoutTree()` pass (see Label Content Layout) then resizes everything else around that real footprint, so nothing downstream reserves — and leaves unfilled — room for a squarer/differently-shaped code than what actually gets drawn. Kept as two independent dimensions rather than one `crispSize` (as when every code here was a square QR) since an rMQR symbol isn't square — see `encodeQr`. + +### Render Tree Construction +`buildRenderTree` turns a resolved content tree (see `templateContent` — leaf objects carry a `value` rather than a `content` function) into one ready for layout: a QR-family leaf gets its actual encoded modules (see `encodeQr`, keyed off the leaf's own type via `QR_LEAF_TYPES`) and an aspect ratio taken from their real width/height — 1 (square) for qr/micro-qr, but not for rmqr, whose symbols are rectangular — a text leaf gets its measured natural aspect ratio, and an "empty" leaf passes through untouched. Multi-line text (`value` is an array) measures as one leaf, not one per line — splitting it into a column of independently-sized leaves would let each line grow to its own full width, ending up at a different font size than its neighbors, which is legible but not what "one text field" should look like. + +### Canvas Font Loading +Applies only to Inter, the one `fontTierFor` tier still drawn with `fillText` (see Freetype Production Rendering above — every other tier goes through `pixel-font.js` instead, which fetches its own font bytes directly and has no `@font-face`/`document.fonts` involvement at all). A `@font-face` family already in use elsewhere on the page loads in time for `drawTextLeaf`'s use, but canvas text silently falls back to the next font in the stack (there isn't one here, so the browser default) if drawn before its first-ever load finishes — unlike DOM text, a canvas `fillText` never waits or repaints on its own once the real font arrives. Kicking off `document.fonts.load()` there means only that very first draw at a given size risks the fallback; every redraw after it (`Print.vue`'s live preview redraws on every keystroke) picks up the real font. + +### Label Content Layout +`layoutContent` builds, sizes and validates the tree for a fixed `fixedSize` (the tape's cross-web `printAreaPx`, or the fallback preview's reference height) — the one dimension every layout scales from, plus `pxPerMm` to turn "empty" leaves' physical sizes into pixels. `fixedSize` and the tree's content fully determine its overall size along the other, growing axis (the one that runs along the tape as it feeds); `maxLength`, when finite (a fixed-length/die-cut tape), rejects content that doesn't fit rather than shrinking it. + +`orientation` picks which axis `fixedSize` binds to: `"along"` (the default) fixes the tree's height — the tape's cross-web width — and grows its width along the feed direction, same as a plain read top-to-bottom design. `"across"` fixes the tree's width instead and grows its height, so the design is built turned 90deg from how it'd read `"along"` — `drawLabel`/`drawFallbackLabel` are what actually rotate the drawing back into the physical raster's fixed orientation; nothing here needs to know about that rotation, since `relation()`/`layoutTree()` already solve the tree in either direction symmetrically. + +Sizing runs twice: a first pass treats every QR-family leaf as the scale-free box its real width/height ratio suggests, purely to find out how much room each one would actually be offered; from that, `snapQrToCrispSize` pins each one's real (smaller, crisp-pixel) size. The second pass then resolves the whole tree again with that real size fixed in, so every sibling and the overall size reflect what's actually drawn rather than the idealized box no code ever quite fills. + +### Tape Fed Label Drawing +`drawLabel` is the tape-fed layout — it draws a fully resolved content tree (see `templateContent`) at the tape's real pixel dimensions. `orientation` is `"along"` (the default) to lay the design out reading along the tape's feed direction, or `"across"` to turn it 90deg so it reads across the tape instead — either way the physical raster this returns is still exactly `printedLength x tape.printAreaPx` (that's fixed by the tape/print head, not a choice this makes); `"across"` just draws the (now width-fixed, see Label Content Layout) tree through a rotated canvas transform so it lands correctly in that same raster, rather than transposing every box the tree itself computed. See `DEBUG_LEAF_BORDERS` to outline every leaf's box. + +### Across Orientation Rotation +When `drawLabel` draws with `orientation === "across"`, the tree was solved width-fixed (see Label Content Layout) — its width already exactly fills `tape.printAreaPx`, so only its (growing) height needs the same along-the-feed centering `origin` got earlier; `translate`+`rotate` then carries that tree-local `(x, y)` box straight into the physical (`printedLength x printAreaPx`) raster, a quarter turn at a time. + +### Fallback Label Preview +`drawFallbackLabel` is the no-webusb preview/PNG — same layout tree and renderer as `drawLabel`, just scaled from a fixed reference height instead of a real tape's, and with no `maxLength` (there's no physical tape to run out of, so the canvas just grows to fit) and no printer feed margin, since there's no real print head here to keep clear of. + +## Label Templates, Preview & Pixel Fonts + +### Template Layout Tree +A template's `layout` is a tree as described in label.js, with leaves whose `type` is one of label.js's `QR_LEAF_TYPES` keys or `"text"`, and whose `content` is a function from the resolved field values (see label.js's `buildLabelFields`) to what they render. `null`/`undefined` returned from that function means the field isn't available yet (see `templateIsAvailable`). A template is only selectable once every leaf's `content` resolves to a value. + +### Generated QR-Only Template Matrix +`QR_ONLY_TEMPLATES` is the full "just the code" matrix: every `{symbology, error-correction level, [rMQR] size strategy}` combination label.js's `QR_LEAF_TYPES` supports, one template each. A plain `id` (no suffix) is always anyd's own defaults: ecc `"M"` and, for rMQR, size `"balanced"`. Coverage isn't uniform (see `QR_LEAF_TYPES`): full QR gets all four ecc grades L/M/Q/H; Micro QR swaps `"L"` (QR's actual lowest) for the even-lower, M1-only, detection-only `"Detection"`, and has no `"H"` at all; rMQR only ever supports ecc `"M"` or `"H"`, each crossed with all three size strategies (balanced/min/max). The matrix is generated (rather than hand-writing every near-duplicate entry) so that a symbology/level/size combination it's missing is one new row here, not a new block to keep in sync with its neighbors. `id` doubles as the layout's leaf `type`, since that's exactly what `QR_LEAF_TYPES` is keyed by. + +### Derived Vars Shape And Ordering +A derived var (`DERIVED_VARS`) is a format string calculated from other vars rather than typed directly - it doesn't get its own input, just a read-only, live-recalculated display next to the ones that do (see Print.vue and `withDerivedVars`). Its `inputs` name every var (base or, in principle, another derived one - e.g. `itemUrl`/`itemHandle`, which both read the derived `userHandle`) that `calc` reads. `inputs` is declared up front rather than inferred from `calc`'s body so `BASE_VARS` can include a var like `"webdomain"` that only feeds a calculation and that no template ever references directly. Declaration order matters here: `withDerivedVars` runs these in a single pass, so a derived var must be declared after every other derived var it depends on. + +### Location Id Known Var +`BASE_VARS` is computed purely from `KNOWN_VARS` (every template's `required_vars`, see `templateIsAvailable`) plus whatever `DERIVED_VARS` reads - there's no separate registry of "fields a prefill might supply". `label.js`'s `"storage-location"` `LABEL_FIELD_BUILDERS` entry has always produced a `locationId`, and Print.vue's `shortId()` method has always checked `f.locationId` to resolve a `storage_location` short link, but until the `"location-id"`/`"owner-id-text-location"` templates existed, no template's `required_vars` ever named `locationId` - so it was never in `BASE_VARS`, Print.vue's `fields()` computed (which only copies `BASE_VARS` keys out of `varValues`) silently dropped it, and `shortId()` could never see it. That meant `shortId`/`shortUrl` never resolved for a storage location, so none of the short-link templates - including `"short-url-qr"`, whose own description says it's "for this item or storage location" - were ever actually available for one, despite the plumbing existing end to end. Any future prefill field meant to reach `fields()`/`DERIVED_VARS` needs the same anchor: at least one template naming it in `required_vars`. + +## Print View + +### Tape-Full Print Margin +The tape-fed preview draws two representations of the tape: `.tape-full` (in the template, wrapping `.label-preview`/the canvas) spans the tape's entire physical width (`tape.mediaWidthMm`), while the canvas inside it is only `printAreaPx` wide. A label printer's print head can't mark all the way to a tape's outer edges, so the true printable area is narrower than the tape itself, by an amount that isn't a fixed or predictable fraction of the tape width. `.tape-full` centers the canvas within itself (equal margin on both sides) and applies a faint background tint to that margin, so it reads as real, if unprintable, tape rather than empty page space. Because of this gap, the vertical ruler (`verticalRulerTicks`) is deliberately built from `tape.mediaWidthMm`, not from `printAreaPx`/`dpi` — ticking off only the printable width would run past the edge of the visible tape. The ruler's container is sized from `mediaWidthMm` too (see the template's inline `height`), so ticks can never run past what's actually drawn. + +### Calculated Short Link Fields +`SHORT_URL_VAR` ("shortUrl") and `SHORT_ID_VAR` ("shortId") are Print.vue-local additions on top of label-layouts.js's own `DERIVED_VARS`. Resolving either needs the current `identityIdByHandle` map (populated by store.js's `fetchIdMap`) to turn a handle into the numeric `owner_identity_id` that short-id.js encodes — that dependency on Vuex state means neither can be a pure fields-to-value calculation like the rest of `DERIVED_VARS`, so both are computed here instead (see the `shortId` method and the `fields` computed). They're excluded from `baseVars`, unlike every other entry `KNOWN_VARS` normally picks up from a template's `required_vars`, because neither is ever typed directly into the form. `SHORT_URL_VAR` is filtered out again in the `baseVars`/`derivedVars` computed even though the "Short link (QR code)" template's `required_vars` puts it in `BASE_VARS` — it isn't actually a label-layouts.js `DERIVED_VARS` entry, it's calculated here. `SHORT_ID_VAR` isn't referenced by any template's `required_vars` today, but is filtered the same way in case one ever is. `SHORT_ID_VAR` itself is the bare short-id.js token (e.g. "~AbCd12") with no domain or leading "/" — for a label that wants just the compact code rather than a full scannable URL, printed only by "internal"-tagged templates meant for this same app/instance. `SHORT_URL_VAR` is not simply `webdomain + "/" + shortId` the way it looks: see ShortId Resolution below for why it's domain-qualified instead. + +### Libweblabel Served Unbundled +`libweblabel.js` is served verbatim from `public/vendor/` rather than bundled by Vite. Its own emscripten glue resolves its `.wasm` sibling relative to *its own* `import.meta.url` at runtime, so both files need to keep sitting together, unhashed, at a stable URL rather than a Vite-fingerprinted asset path. + +### Concurrent Wasm Loading +`preloadQrEncoder()` and `preloadPixelFontRenderer()` (see Freetype Production Rendering) are both kicked off in `mounted()` without an immediate `await`, so they load concurrently with each other and with `MultiPrinterBlob.load()` instead of serializing three independent wasm fetches. Every `redraw()`/`redrawFallback()` call still waits on both (via `fontsReady`, itself `Promise.all([qrReady, preloadPixelFontRenderer()])`) before drawing, since a qr/mqr/rmqr leaf throws (see label.js's `encodeQr`) until the encoder has finished loading, and a bitmap-tier text leaf throws the same way (see `drawPixelText`) until the font renderer has. + +### Ruler Tier Selection +`RULER_TIERS` controls how far apart plain ticks and labeled/major ticks sit, both getting coarser the longer the ruler runs — tightly spaced ticks (and their labels) get too cramped to read or render once there are enough of them. The list is ordered smallest threshold first; `rulerTier` picks the last entry whose `aboveMm` the ruler's own length clears, so a finer or coarser tier is added there rather than growing a pile of separate constants. Every tier's `majorEveryMm` is a multiple of its own `tickMm`, so major ticks always land on a tick that's actually drawn. `rulerTier` itself is a single shared value keyed off whichever axis (`horizontalTotalMm`/`verticalTotalMm`) is physically longer, so a long label's ruler never ends up coarser or finer than the tape-width ruler right next to it just because the other axis happens to be shorter — both rulers coarsen together once *either* axis needs it. + +### Fields Computed: Dropping Blank Values +The `fields` computed builds the named content fields templates draw from: the form's own base vars, plus every `DERIVED_VARS` format string calculated live from those, so typing a `userHandle`/`itemId` (by hand or via prefill) recalculates `itemUrl`/`itemHandle` the same way either way. A blank or uncalculated value is dropped entirely rather than passed through as an empty string, so it reads as *absent* to `templateIsAvailable`/`templateContent` the same way a prefill that never supplied it would — that's what LabelLayoutPreview.vue greys a template's thumbnail out on. + +### ShortId Resolution +The `shortId` method builds the same shortened link Inventory.vue's and StorageLocation.vue's own `shortIdLink` build for one of their rows, but returns the bare token (see short-id.js's `encodeShortId`) rather than a router target or full URL — `shortenedRoute`'s leading "/" is stripped since this is plain display/label content, not something this view itself navigates to. Which short-id.js kind applies (`item` vs `storage_location`) depends on which id field the current prefill's `LABEL_FIELD_BUILDERS` populated (see label.js) — `itemId` vs `locationId` — rather than trusting the prefill's own `kind` directly, so hand-typing a `userHandle`+`itemId` with no prefill at all still resolves the same way. It falls back to no value, the same as an unresolved `DERIVED_VARS` entry, until `identityIdByHandle` has loaded (see `mounted`'s `fetchIdMap`) or if the handle isn't in it. + +The `fields` computed doesn't just reattach *a* domain to build `shortUrl` from this same token, it reattaches the `homeDomain` computed specifically — the printing user's own home domain (`state.user`'s domain half), not `derived.domain` (the printed thing's own owner handle domain, which for a group-owned print is the group's domain, not necessarily the printer's). This matters because `owner_identity_id`/`owner_group_id` are resolved via `identityIdByHandle`/`groupIdByHandle`, both sourced from `state.idmap`, which always loads from the printing user's own home server (`getHomeServers`) regardless of whose item is being printed — so the domain a Domain-Qualified Short ID needs to carry is always the printer's home domain, the one whose KnownIdentity/Group numbering the encoded ints are actually scoped to (see docs/handles-and-shortids.md#domain-qualified-short-id). Getting this wrong (e.g. using `derived.domain` for a group print) would silently mint a token that resolves to the wrong backend, or to nothing, once scanned by someone else. + +### Domain-Qualified Short ID Resolution +A Domain-Qualified Short ID (`:~`) resolving to something other than "the app I'm currently in" needs a real network round-trip, since the token's ints are only meaningful to whichever backend minted them (see docs/handles-and-shortids.md#domain-qualified-short-id). `router.js`'s `domainQualifiedRoute(domain, ints)` is the single chokepoint every entry surface (the `/:short_id` route's `beforeEnter` guard, `ShortId.vue`'s `resolveIfQualified`, `Scan.vue`'s `describeForeignShortId`) funnels through: if `domain` matches the caller's own home domain (`store.state.user`'s domain, read via `isLoggedIn` first to force its lazy hydration — see `getHomeServers`'s own comment on the same ordering issue) it resolves locally via the existing idmap-based `expandedRoute`, exactly like a bare token; otherwise it dispatches `resolveShortId` (`store.js`), which SRV-discovers `domain`'s backend (`getFriendServers`) and calls its new `GET /api/v1/resolve_short_id////` (`backend/toolshed/api/inventory.py`). That endpoint answers "what does this owner/local id pair mean" against its own KnownIdentity/Group numbering, applying the exact same friend-or-self/membership and `availability_policy` checks `InventoryItemViewSet.get_queryset()` already does (see Owner-Handle Scoped Routes below) — it only differs in being keyed by numeric owner id instead of a handle string, since the caller doesn't have a handle yet, that's exactly what it returns (`{handle, id}`). Only the four owned short-id kinds (`item`/`group_item`/`storage_location`/`group_storage_location`) are wired up; `group`/`category`/`file`/`workflow` return 400 and the frontend falls through to `ShortId.vue`'s debug view. + +### Fit-Zoom Preview Scaling +`fitZoom` fits the preview to its card without ever needing a horizontal scrollbar for a label this small: it magnifies short labels up to `MAX_ZOOM` rather than showing them at native (tiny) size, and never lets the preview grow past `MAX_PREVIEW_HEIGHT_PX` tall however long or wide the label itself runs. It returns the zoom actually used, so callers that care (`redraw`'s ruler bookkeeping) don't have to re-derive it. When magnifying, the zoom is rounded *down* to a whole number rather than rounded or ceiled: `image-rendering: pixelated` only looks crisp when every source pixel maps to the same number of screen pixels, and at a fractional zoom (the common case, since the raw zoom is just whatever ratio the tape/card happen to produce) some source pixels get rounded up to one extra screen pixel and others don't, unevenly warping fine, already-pixel-perfect detail like a crisp QR module or a tiny bitmap font glyph. Flooring keeps the same "never bigger than available space" guarantee the raw zoom already had. Shrinking (zoom < 1) has no equivalent whole-factor snap to make, since downsampling always blends source pixels, so it's left as-is. + +### Mm Ruler Layout +The tape-fed preview's mm ruler is a horizontal track above the canvas and a vertical one to its left, both ticked in real physical millimeters rather than preview pixels, since what they're measuring is the actual label. + +### Preview Track No-Scroll Invariant +`.preview-track` holds the horizontal ruler and the canvas, and is deliberately never scrollable (no `overflow-x: auto`): `fitZoom`'s zoom always satisfies `canvas.width * zoom <= available`, so the canvas can never actually be wider than this has room for, and a scrollbar here would let the ruler and canvas drift apart (or just look broken) for no reason. `min-width: 0` only lets this flex item shrink to the card's real available width — it doesn't enable scrolling. + +### Label-Preview Override In Preview Track +`.preview-track .label-preview` overrides the standalone `.label-preview` rule: nested here, it must neither scroll nor center its canvas. `overflow-x: visible` (never `auto`) rules out a second, inner scrollbar, and `text-align: left` keeps the canvas flush with the ruler's zero tick instead of drifting to the middle of whatever spare width the card has. `padding: 0` makes the canvas's own edges exactly this box's edges too, so the ruler's ticks line up with those same edges — any padding here would leave the ticks and the actual canvas misaligned. + +## Routing, Handles & Short IDs + +### Escaping Hash in Handles for URL Path Segments +`router.js`'s `encodeHandleForUrl`/`decodeHandleFromUrl` embed/extract a handle in a URL path segment. `#` starts a URI's fragment component, so a group handle (`#name@domain`) or classification handle (`origin#type:name`) can't appear unescaped in a path segment. `+` stands in for `#` there instead of the usual `%23` - safe to reverse unambiguously because every field a handle is built from is already required to exclude `+` (see docs/federation.md's "Reserved characters" and its "Embedding a `#`-bearing handle in a URL" section). The canonical handle itself never changes; this only affects how one gets embedded in, or read back out of, a URL path segment. + +### Owner-Filtered Overview Routes Use Path Segments, Not Query Strings +`/inventory/:owner?` and `/storage-location/:owner?` (`Inventory.vue`/`StorageLocation.vue`'s `owner` prop) filter the overview to one owner - the caller, a group, or a friend - using the same handle shape/escaping as `/groups/:handle` (see Escaping Hash in Handles above). `/inventory/new/:group?` and `/storage-locations/new/:group?` (`InventoryNew.vue`/`StorageLocationNew.vue`'s `group` prop) prefill the owner picker the same way when arriving from a filtered overview. All four are optional trailing path params (`:name?`), the same style as `/workflows/:id/:phase?` - deliberately not a query string: vue-router's query parser treats a bare `+` as an encoded space (the x-www-form-urlencoded convention), so the `#`->`+` swap that works fine in a path segment would silently corrupt a group handle passed as `?owner=...` instead. + +These coexist safely with their more specific neighbors (`/inventory/new/:group?` next to `/inventory/:owner?` and `/inventory/:handle/:id`; `/storage-location/:owner?` next to the plural `/storage-locations/...` routes) because vue-router's matcher always ranks a route's more literal/static segments above a dynamic one at the same depth, regardless of declaration order - e.g. `/inventory/new` resolves to the static `new` segment, never `:owner` on `/inventory/:owner?`. + +`ownerOverviewRoute`/`ownerLocationOverviewRoute` (`router.js`) only ever pick between the caller and their own group, never a friend, since a friend's items/locations are read-only (see Perform Update Rejects Non-Owned Items Explicitly below). + +### Item Short-Link Redirect Route +The self-contained label/short-link entry point (see `label.js`'s `LABEL_CONTENT_BUILDERS` and docs/design-in-progress/items-labels.md). `:handle` is already URL-escaped the same way `/inventory/:handle/:id` expects it, so this is just a shorter alias for that route, with no owner-is-the-viewer special case: `InventoryItemViewSet.get_queryset()` (see Owner-Handle Scoped Routes below) already treats "it's the viewer's own item" as one case of "the viewer may see this owner's item," not a separate path. + +### `beforeEnter` Guard vs `redirect` for `/:short_id` +This route uses a `beforeEnter` guard, not `redirect`: `redirect` is called synchronously and its return value is used as-is (never awaited), and it also *must* resolve to a valid location on every match (an unresolvable one throws, see vue-router's `handleRedirectRecord`) - it can't itself wait on `fetchIdMap` (see `NEEDS_IDMAP`) for the `item`/`group_item` kinds whose owner handle isn't resolvable from the token alone. A guard can return `null`/undefined to mean "proceed to the component instead," which is exactly what's needed here: when `expandedRoute` can't resolve yet (or ever - an unrecognized kind), stay on this same URL and mount `ShortId.vue` in place, which has full component-lifecycle async support and takes it from there - fetch idmap, retry, redirect once resolved, or keep showing the decode view. Vue Router guards support returning a Promise, not just a value, which is what makes this the right chokepoint for a Domain-Qualified Short ID too (see Domain-Qualified Short ID Resolution above): the guard is `async`, and for a foreign domain it `await`s `domainQualifiedRoute` (a real network round-trip) before deciding whether to redirect or fall through to `ShortId.vue`, the exact same "resolve or fall through" shape as the synchronous idmap case. + +## Federation, Identity, Friends & Groups + +### Group Invite And Accept Self-Certifying Verification +`verify_incoming_group_invite(request, raw_request_body, handle_field, key_field)` is a self-certifying verifier for the two legs of the group invite/accept dance that land on a backend which doesn't have the caller cached as a `KnownIdentity` yet (see `docs/design-in-progress/groups-mvp.md`): the inviter delivering an invite to the invitee's own backend (`handle_field='inviter'`, `key_field='inviter_key'`), and the invitee accepting on the group's home backend (`handle_field='invitee'`, `key_field='invitee_key'`). It mirrors `verify_incoming_friend_request` exactly, just with configurable field names so the same logic serves both legs. + +### Group Membership Is Recorded On Both Sides, Like Friendship +Accepting a group invite used to only ever write membership on the group's own home backend (`Group.members`, via `acceptGroupInvite`); the invitee's own home backend just deleted its now-obsolete `GroupInviteIncoming` and forgot the invite ever happened. That meant a member's own backend had no way to answer "which groups is this identity in" for a group hosted elsewhere. `recordGroupMembership` (`POST /api/v1/groupinvites//accept/`, local-auth) closes that gap: it's the second leg of the accept dance, called by the client right after the group-home-backend accept succeeds, and it converts the `GroupInviteIncoming` into a durable `GroupMembership` pointer (just `user`, `group_name`, `group_domain` — no roster, since this backend isn't authoritative for the group) instead of discarding it. `GET /api/v1/groupmemberships/` (also local-auth) lists a user's own pointers; `Groups.vue`'s "Other groups you're a member of" section is this list, filtered against the locally-hosted `groups` to avoid double-listing. This is deliberately the same shape as the friend-accept flow's second POST (see below): each side's own home backend independently records the relationship in its own local table, so neither side depends on the other's backend being reachable later to know it happened. + +### Owner-Handle Scoped Routes +`InventoryItemViewSet`/`StorageLocationViewSet` (`toolshed/api/inventory.py`) address every route — list/create/retrieve/update/destroy — by an owner handle URL parameter, resolved by `resolve_owner_handle(handle)` into `(owner_user, owner_group)`. There's deliberately no bare, handle-less route: `id` is only unique within its own owner/owner_group scope, so a route spanning more than one such scope could match two different objects sharing a local id, raising `MultipleObjectsReturned`. + +Detail routes (retrieve/update/destroy) still address the object by its owner-scoped `id`, not the internal row id. The router names the URL capture group `pk`, so `lookup_url_kwarg` stays `'pk'` — only `lookup_field` changes, to `'id'` — this is not a mismatch to "fix"; renaming `lookup_url_kwarg` to match would break the URL conf, which still names the capture group `pk`. + +### Perform Update Rejects Non-Owned Items Explicitly +Since `get_queryset()` (see Owner-Handle Scoped Routes above) can now return a friend's items/locations (read-only) on the same route, an object found there isn't necessarily writable. `perform_update` on both `InventoryItemViewSet` and `StorageLocationViewSet` checks `_is_authorized` and raises `PermissionDenied` explicitly, rather than silently no-op, which returning without saving would otherwise do. + +### Friend Request and Accept Protocol Flow +The friend request/accept flow spans two backends (A hosts `x@A`, B hosts `y@B`): +1. `x@A` sends a friend request to `y@B`: + 1. `x@A`'s client POSTs to `A/api/v1/friendrequests/` with `{from: x@A, to: y@B}`. + 2. A's backend creates a `FriendRequestOutgoing`, containing `x@A`'s identity and `y@B`'s name. + 3. `x@A`'s client POSTs to `B/api/v1/friendrequests/` with `{from: x@A, to: y@B, public_key: x@A's public key}`. + 4. B's backend creates a `FriendRequestIncoming`, containing `y@B`'s and `x@A`'s identities. +2. `y@B` accepts the friend request: + 1. `y@B`'s client POSTs to `A/api/v1/friendrequests/` with `{from: x@A, to: y@B, public_key: y@B's public key}`. + 2. A's backend matches the data to the `FriendRequestOutgoing` object, deletes both, and creates a `Friend` object containing `x@A`'s and `y@B`'s identities. + 3. `y@B`'s client POSTs to `B/api/v1/friends/` containing the id of the `FriendRequestIncoming` object. + 4. B's backend creates its own `Friend` object, using the identities from the `FriendRequestIncoming` object. + +### Fetch Item By Handle: Group vs Personal Handles +`fetchItemByHandle` in `store.js` branches on whether the handle is a group handle (leading `#`) or a personal/friend handle. A personal/friend handle goes through `fetchForeignItem`, which GETs the item directly by owner handle (see Owner-Handle Scoped Routes above). A group handle has no equivalent single-item GET route, so it resolves via `fetchGroup` (domain-routed by the handle, see `GroupDetail` in `toolshed/api/group.py`, addressed by handle directly) followed by the already-correct, already-authenticated group listing (`fetchGroupInventoryItems`), picking the matching item out of that list — which only ever contains this one group's own items, so an id collision with anything else can't happen. + +## Workflows Architecture + +### Workflow Catalog +`frontend/src/workflows.js` is the single source of truth for every workflow type known to the frontend: what it's called, what category/description/icons it has, how many steps it has and what they're called, what its initial payload looks like, and which Vue component renders it. + +Each workflow has a fully co-located component + metadata as a static `meta` option on the component (`Component.meta`, right next to `name`/`props`/etc.) in `@/components/workflow/workflows/*.vue` - `workflows.js` simply imports those components and reads `.meta` off of them to build the catalog. + +This replaces a previous design of a parallel `BaseWorkflow` class hierarchy (metadata) plus a separate per-step `ComponentRegistry.js` (components) - both concerns now live in one flat array with each component responsible for its own metadata and UI implementation. + +### Workflow Payload Is An Opaque String +The backend stores `WorkflowInstance.payload` as an opaque string - it never parses or understands it as JSON. The frontend is fully responsible for serializing it before sending and deserializing it after receiving. + +### Workflow `meta` Co-location +Workflow metadata (title, category, description, icons, step definitions, initial payload) is co-located with its implementation as a static `meta` property on the workflow's Vue component, so there is a single source of truth per workflow type. It's consumed by `@/workflows.js` (via `Component.meta`) to assemble the catalog used by the `Workflows` and `WorkflowDetail` views. + +### Bulk Item Import Workflow +`BulkItemImportWorkflow.vue` implements every step of the `import-items` workflow in a single component. Steps 1 (File Upload) and 6 (Import Items) have fully custom UI; the remaining steps (2-5, 7) currently fall back to a generic "in progress" placeholder driven by this workflow's own step metadata, and can be fleshed out later without touching any other file. + +### Staged Photos Are The Durable State +In `FotoFirstBulkImportWorkflow.vue`, `photos`' durable state is the `WorkflowInstance.staged_files` relation itself, kept in sync directly by `stageFile()`/`unstageFile()` rather than by writing to `payload`. On load, `photos` is seeded from the `workflowInstance` prop, not from `payload`. Entries restored this way have no local bytes yet (this session never uploaded them), so `dataUrl` stays null and the gallery falls back to fetching a thumbnail by hash via `AuthenticatedImage`. Symmetrically, when a photo is newly staged, it's persisted server-side right away (keyed to the workflow instance) so it survives a reload or a switch to another device - nothing about photos needs to go into `payload` too. + +### Thumbnail Lookup By Hash +`thumbnailPathForHash()` in `FotoFirstBulkImportWorkflow.vue` builds a derived storage path mirroring `hash_upload()` (`files/models.py`), matching how `FileSerializer.name` already builds file URLs elsewhere in the app (e.g. `AuthenticatedImage`'s `src` for item files). `files/media_urls.py`'s `thumbnail_urls` generates and disk-caches a resized JPEG at that path on first request, since a gallery card only needs a small image, not the full-size original. + +Once a photo finishes uploading, the gallery switches it to the server-fetched thumbnail by re-looking it up by hash rather than mutating the closed-over `photo` object from the upload closure - that reference predates the `this.photos.push()` call, so it's the raw object, not the reactive proxy Vue tracks, and writing to it wouldn't trigger a re-render. + +### Foto First Bulk Import Workflow +`FotoFirstBulkImportWorkflow2.vue` implements every step of the `foto-first-bulk-import` workflow (photo capture, image processing, item detail entry, import completion) in a single component. Keeping the whole workflow in one file avoids splitting closely related state (photos, processed images, completed items) across many small step components and their prop/emit boundaries. + +### Staged Files Are Identified By Hash Alone +A staged file's SHA-256 hash is enough to identify it, since the client computes content hashes the same way the backend does and can fetch bytes from a hash-derived storage path. This underpins several places in the workflow file-staging API: + +- `WorkflowInstanceSerializer.staged_files` (`backend/toolshed/serializers.py`) exposes only hashes, not a fuller `FileSerializer` representation like `InventoryItemSerializer.files` uses - for a file staged by *this* session there's nothing more to say, and for one staged elsewhere (another device/tab), the hash is what lets this session recognize and fetch it. +- `get_staged_files()` (`backend/toolshed/api/files.py`) returns the same bare hash list, useful mainly for discovering what another session/device already staged on a workflow. +- `post_item_file()`'s `file_hash` branch (`backend/toolshed/api/files.py`) lets a caller attach a file already staged on one of their own workflows by content hash, instead of re-uploading bytes already stored server-side. Workflows are always personally owned, so this only applies to a caller with a local account. + +## Offline Data Export & Import + +### File Naming Convention In Exports +Files attached to a user's inventory items are yielded by `inventory_files()` as `(arcname, data)` pairs, deduplicated by content hash and placed under a `files/` subfolder in the export zip, e.g. `files/`. The extension is guessed from each `File`'s `mime_type` so attachments and images remain viewable once extracted, rather than sitting as extension-less blobs. This same `files/` convention is used for the profile picture and is what `available_files` (passed into `import_profile()` and `import_inventory()`) is keyed by. + +### Profile Import Semantics +`import_profile()` is a fault-tolerant importer for `profile.json`. Only `first_name`, `last_name`, `email`, and `profile_picture` are applied to the account; `profile_picture` is only set when its value is a `files/...` path present in `available_files` (see File Naming Convention above). `username` and `domain` are ignored even if present in the payload, since they identify the account itself and cannot be changed by an import. + +### Account & Data Deletion +`delete_user_data()` permanently deletes everything that the data export covers, while keeping the account itself intact: inventory items (hard delete, bypassing soft-delete), storage locations, account preferences, the friends relation on the user's public identity, the profile picture, and any `File` blobs that become orphaned as a result. A `File` is only deleted by `_delete_orphaned_files()` once nothing else references it - no `InventoryItem`, no `ToolshedUser` (as a profile picture), and no `WorkflowInstance` (as a staged file) - since files are deduplicated by content hash and may be shared with other items or users. The `ToolshedUser` account (and its underlying `KnownIdentity`) is deliberately *not* deleted by `delete_user_data()`; see `delete_account()` in `toolshed/api/offlinedata.py` for that. + +`delete_user_account()` builds on this: it calls `delete_user_data()` first and then deletes the `ToolshedUser` row itself, closing the local account. The `KnownIdentity` is still kept so that remote friends/history relating to this identity remain intact for other users - only the local account is closed. Both functions return a summary dict describing what was removed; `delete_user_account()`'s summary additionally sets `account: True`. + +### Location Import Ordering And Savepoints +`import_locations()` fault-tolerantly imports `locations.csv` into `StorageLocation` rows. Rows are read by header label; a row missing the required `name` column is skipped, and optional columns (`description`, `category`) are simply omitted if absent. Rows are processed in path-depth order (shallowest first) so that a child location's parent has already been created by the time it's needed. Each row runs inside its own transaction savepoint (`transaction.atomic()`), so a DB-level failure on one row (e.g. a constraint violation) can't poison the surrounding transaction and silently break every subsequent row - it only skips that one row. + +### Handle Resolution Semantics +A fully qualified handle (e.g. `git:base#tag:drill`) identifies one *specific* entity from one *specific* origin. `_resolve_handle()` therefore only ever resolves such a handle to an *existing* model instance and never creates one: silently creating a new local entity named after the raw handle string would be incorrect, since that's not the entity the handle actually points to. It raises `_HandleNotFound` (defined for exactly this purpose) when the handle's entity type doesn't match the expected one, or when no such object exists locally - which lets the caller (`import_inventory()`) skip just that row and report a helpful error instead of guessing. + +### Properties CSV Encoding +An inventory item's properties are encoded into the `properties` CSV cell as a comma-separated `handle=value` list by `_encode_properties_cell()`, and decoded back into `(Property, value)` tuples by `_parse_properties_cell()`. Because both the list separator (`,`) and the key/value separator (`=`) could otherwise appear inside a value, `_quote_value_if_needed()` wraps any value containing a comma or a quote character in CSV-style double quotes (doubling embedded quotes), and `_split_quoted_comma_list()` is the matching reader-side routine: it splits on commas while honouring quoted substrings so a quoted value's own commas aren't mistaken for separators, unescapes doubled quotes (`""`) back to a single literal quote, and strips only the single space that follows each `", "` separator (as written by the encoder) rather than doing a blanket `.strip()` - so genuine leading/trailing whitespace inside a quoted value survives the round trip. + +### Inventory Import Semantics +`import_inventory()` fault-tolerantly imports `inventory.csv` into `InventoryItem` rows owned by the target user. The `files` column's paths are looked up in `available_files` (see File Naming Convention above); a reference to a file that wasn't successfully extracted from the zip is simply ignored rather than failing the row. If a row references a fully qualified tag/property/category handle that doesn't resolve locally (see Handle Resolution Semantics above), the whole item is skipped - rather than creating a bogus local entity - and a message is appended to the returned `errors` list. As with `import_locations()`, each row runs inside its own transaction savepoint so one row's DB-level failure can't break the rest of the import. + +## Camera Scanning & Inventory UI + +### Video Stream Attach And Resize Sync +`setupVideoStream` in `Scan.vue` attaches a `MediaStream` to the `