Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mentions PoC #9279

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft

Mentions PoC #9279

wants to merge 23 commits into from

Conversation

mtomilov
Copy link
Contributor

@mtomilov mtomilov commented Jan 27, 2025

Refs #9281

Adds @mentions support in the API, emails will be addressed separately.
All of create / get / edit / search endpoints now support them.

Testing API

  • Run migrations make db
  • Login as devdata_admin and generate API token from http://localhost:5000/account/developer
  • Create annotation
     curl -X POST --location "http://localhost:5000/api/annotations" \
     -H "Authorization: Bearer <token>" \
     -H "Content-Type: application/json" \
     -d '{
             "uri": "https://www.google.com/",
             "text": "**hello** <span>world</span>"
         }'
    
    Mentions can be already put in the text here and will be returned in the response.
  • Edit annotation
     curl -X PATCH --location "http://localhost:5000/api/annotations/<url safe id>" \
         -H "Authorization: Bearer <token>" \
         -H "Content-Type: application/json" \
         -d '{
                 "text": "Hello <a data-hyp-mention data-userid=\"acct:devdata_admin@localhost\">@devdata_admin</a>, take a look at this\nHello <a data-hyp-mention data-userid=\"acct:devdata_user@localhost\">@devdata_user</a>, take a look at this"
             }'
    
    But I think it's easier to test via edit on already created annotation.
  • Search annotations
    curl -X GET --location "http://localhost:5000/api/search" \ -H "Authorization: Bearer <token>"
    Mentions will be returned here as well

Testing emails

Notifications are sent only on creation for now

  • Create annotation
     curl -X POST --location "http://localhost:5000/api/annotations" \
     -H "Authorization: Bearer <token>" \
     -H "Content-Type: application/json" \
     -d '{
             "uri": "https://www.google.com/",
             "text": "Hello <a data-hyp-mention data-userid=\"acct:devdata_admin@localhost\">@devdata_admin</a>, take a look at this\nHello <a data-hyp-mention data-userid=\"acct:devdata_user@localhost\">@devdata_user</a>, take a look at this"
         }'
    
  • Check h/mail folder for incoming mail

h/services/mention.py Outdated Show resolved Hide resolved

@staticmethod
def _parse_userids(text: str) -> list[str]:
return USERID_PAT.findall(text)
Copy link
Contributor Author

@mtomilov mtomilov Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll probably want to parse userids from text_rendered html.

@mtomilov mtomilov changed the title Mentions POC Mentions PoC Jan 27, 2025
@mtomilov mtomilov requested a review from marcospri January 27, 2025 16:16
h/schemas/annotation.py Outdated Show resolved Hide resolved
h/services/mention.py Show resolved Hide resolved
h/services/mention.py Outdated Show resolved Hide resolved
h/models/mention.py Outdated Show resolved Hide resolved
h/services/mention.py Outdated Show resolved Hide resolved
import re
from typing import Iterable, Sequence

import sqlalchemy as sa
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't be shy about importing anything from sqlalchemy. I mean speling the whole from sqlalchemy import select, ....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, is this the recommended way?

h/services/mention.py Show resolved Hide resolved
h/services/annotation_json.py Show resolved Hide resolved
h/schemas/annotation.py Outdated Show resolved Hide resolved
h/services/annotation_json.py Show resolved Hide resolved
return [notification.mentioned_user.email], subject, text, html


def _get_user_url(user, request):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied from emails/reply_notifcation.py for now

return []

notifications = []
for mention in annotation.mentions:
Copy link
Contributor Author

@mtomilov mtomilov Jan 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need to factor in groups / permissions / limits here as well to avoid sending notifications when not appropriate.

@mtomilov mtomilov requested review from acelaya and marcospri January 29, 2025 15:31
Comment on lines 14 to 17
"userid": self._mention.annotation.userid,
"username": self._mention.user.username,
"display_name": self._mention.user.display_name,
"link": self._mention.user.uri,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can any of these be None? (Other than the display_name)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question
uri can also apparently be false at least in the db, not sure when it makes sense but still.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it actually makes sense. We will not want to link users in the LMS context, for example.

def asdict(self) -> dict[str, Any]:
return {
"userid": self._mention.user.userid,
"username": self._mention.user.username,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In hypothesis/client#6787 (comment), we were discussing that perhaps we don't need to expose the username here, is it can be extracted from the userid.

I don't have a strong opinion though.

Copy link
Contributor Author

@mtomilov mtomilov Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be coming from the mention itself like this. It seems better to just store the info from the get go, especially considering the work Marcos has done on user renames already.
So we'll be storing the username at the time of mention on the mention.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right. This is the username the user had at the time the mention was created, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's missing from the PoC due to some back-and-forth going at the time. Sorry for the confusion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants