Skip to content

Add token_count to GET /api/v1/me response #3071

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

Closed
wants to merge 5 commits into from

Conversation

Turbo87
Copy link
Member

@Turbo87 Turbo87 commented Dec 4, 2020

This PR is partly extracted from #1952 to finally resolve issue #19.

The PR adds a meta: { token_count: 42 } object to the GET /api/v1/me response, which can be used to display a banner, flash message, or other indicator on the frontend to signal to the user that he has not created an API token yet. The exact implementation for the frontend will follow in another PR and does not need to be discussed here.

r? @carols10cents since you did the original review on #1952. let me know if you don't currently have time for the review :)

Copy link
Member

@carols10cents carols10cents left a comment

Choose a reason for hiding this comment

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

One request and one question :)

@@ -29,6 +30,11 @@ pub fn me(req: &mut dyn RequestExt) -> EndpointResult {
))
.first(&*conn)?;

let token_count = ApiToken::belonging_to(&user)
.filter(api_tokens::revoked.eq(false))
Copy link
Member

Choose a reason for hiding this comment

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

I think it would make me feel better if one of the tokens in the test was revoked and we asserted that it didn't count towards this total.

Copy link
Member Author

Choose a reason for hiding this comment

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

thinking some more about it, I wonder if this revoked: false query really makes sense for us here. if our primary goal is to teach the user that he needs to create his first token to interact with cargo then, if he has a revoked token already, he apparently doesn't need to be taught this anymore :D


#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct EncodableMeMeta {
pub token_count: i64,
Copy link
Member

Choose a reason for hiding this comment

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

I wonder, what's the criteria for information being in EncodableMe or EncodableMeMeta? Should owned_crates be in EncodableMeMeta, for example?

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks for asking this question. when I was working on this it seemed obvious to me, but now I'm not so sure anymore either. 😅

I'm now wondering if it would be better to instead include the list of token IDs, like in a proper relationship declaration, but that would probably slightly slow down the query and for what we're trying to build it doesn't make a difference.

Maybe my confusion comes from using user for both other (GitHub) users, but also the logged in user account, but with exta information in the latter case.

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought about this a little bit more. I think my reason for putting this in meta was that this is derived data, which corresponds to the number of tokens that will be returned from the GET /api/v1/me/tokens request. It is not actually a property on the user resource, but rather the number of token relationships that this user has.

Should owned_crates be in EncodableMeMeta, for example?

I guess it would be best if this was encoded as a proper relationship instead

@bors
Copy link
Contributor

bors commented Jan 13, 2021

☔ The latest upstream changes (presumably #3173) made this pull request unmergeable. Please resolve the merge conflicts.

@Turbo87 Turbo87 added C-enhancement ✨ Category: Adding new behavior or a change to the way an existing feature works A-backend ⚙️ labels Feb 11, 2021
@Turbo87 Turbo87 closed this Mar 1, 2021
@Turbo87 Turbo87 deleted the token-count branch October 17, 2022 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-backend ⚙️ C-enhancement ✨ Category: Adding new behavior or a change to the way an existing feature works
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants