From f6ffbe8cc75db49f1d0e3a5e49f397f4cbefb817 Mon Sep 17 00:00:00 2001 From: Misha Tomilov Date: Fri, 7 Feb 2025 13:25:10 +0100 Subject: [PATCH] Update mention json payload --- docs/_extra/api-reference/schemas/annotation.yaml | 4 ++-- h/presenters/mention_json.py | 6 +++++- tests/unit/h/presenters/mention_json_test.py | 10 ++++++++++ tests/unit/h/services/mention_test.py | 1 + 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/docs/_extra/api-reference/schemas/annotation.yaml b/docs/_extra/api-reference/schemas/annotation.yaml index 9e0735934cf..e6d9897148a 100644 --- a/docs/_extra/api-reference/schemas/annotation.yaml +++ b/docs/_extra/api-reference/schemas/annotation.yaml @@ -209,9 +209,9 @@ Annotation: pattern: "acct:^[A-Za-z0-9._]{3,30}@.*$" description: user account ID in the format `"acct:@"` example: "acct:felicity_nunsun@hypothes.is" - username: + original_userid: type: string - description: The username of the user at the time of the mention + description: The original account ID mentioned in the annotation text display_name: type: string description: The display name of the user diff --git a/h/presenters/mention_json.py b/h/presenters/mention_json.py index ebd61188f41..b0098f37f53 100644 --- a/h/presenters/mention_json.py +++ b/h/presenters/mention_json.py @@ -1,6 +1,7 @@ from typing import Any from h.models import Mention +from h.util.user import format_userid class MentionJSONPresenter: @@ -12,7 +13,10 @@ def __init__(self, mention: Mention): def asdict(self) -> dict[str, Any]: return { "userid": self._mention.user.userid, - "username": self._mention.username, + "original_userid": format_userid( + self._mention.username, self._mention.user.authority + ), + "username": self._mention.user.username, "display_name": self._mention.user.display_name, "link": self._mention.user.uri, } diff --git a/tests/unit/h/presenters/mention_json_test.py b/tests/unit/h/presenters/mention_json_test.py index 23cf9db902f..b5251c9f914 100644 --- a/tests/unit/h/presenters/mention_json_test.py +++ b/tests/unit/h/presenters/mention_json_test.py @@ -2,6 +2,7 @@ from h.models import Mention from h.presenters.mention_json import MentionJSONPresenter +from h.util.user import format_userid class TestMentionJSONPresenter: @@ -12,11 +13,20 @@ def test_as_dict(self, user, annotation): assert data == { "userid": user.userid, + "original_userid": user.userid, "username": user.username, "display_name": user.display_name, "link": user.uri, } + def test_as_dict_with_different_username(self, user, annotation): + new_username = "new_username" + mention = Mention(annotation=annotation, user=user, username=new_username) + + data = MentionJSONPresenter(mention).asdict() + + assert data["original_userid"] == format_userid(new_username, user.authority) + @pytest.fixture def user(self, factories): return factories.User.build() diff --git a/tests/unit/h/services/mention_test.py b/tests/unit/h/services/mention_test.py index 5ca404353e5..480dc9935c9 100644 --- a/tests/unit/h/services/mention_test.py +++ b/tests/unit/h/services/mention_test.py @@ -104,6 +104,7 @@ def test_it(self, pyramid_request, user_service, MentionService): session=pyramid_request.db, user_service=user_service, ) + assert service == MentionService.return_value @pytest.fixture