Skip to content

Smart syncing of Users#1165

Merged
MarkKoz merged 23 commits into
python-discord:masterfrom
RohanJnr:smart_syncing_users
Oct 8, 2020
Merged

Smart syncing of Users#1165
MarkKoz merged 23 commits into
python-discord:masterfrom
RohanJnr:smart_syncing_users

Conversation

@RohanJnr
Copy link
Copy Markdown
Contributor

@RohanJnr RohanJnr commented Sep 21, 2020

Closes #1119

Implementation

  • Refactor Code to support paginated response.
  • Bulk Update Users whose properties have changed.
  • Update Users via PATCH method by only sending user ID and modified user details.

Details

Only merge this PR after the Site PR is merged.

…tion

Added method to recursively GET users if paginated and another method to parse URL and return endpoint and query parameters.
@RohanJnr
Copy link
Copy Markdown
Contributor Author

This PR will be ready for review when the related site PR has been merged.
python-discord/site#378

instead of creating and updating a single user at a time, a list of dicts will be sent for bulk update and creation.
@RohanJnr RohanJnr marked this pull request as ready for review September 25, 2020 10:07
@RohanJnr RohanJnr requested a review from a team as a code owner September 25, 2020 10:07
@RohanJnr RohanJnr requested review from ikuyarihS and jb3 and removed request for a team September 25, 2020 10:07
@ghost ghost added the needs 2 approvals label Sep 25, 2020
Copy link
Copy Markdown
Contributor

@ks129 ks129 left a comment

Choose a reason for hiding this comment

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

Currently some code style/small changes, but there is one big issue: Type hinting variables that have very clear type is pointless.

Comment thread bot/exts/backend/sync/_syncers.py Outdated
return users

@staticmethod
def get_endpoint(url: str) -> tuple:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it's better to use typing.Tuple for this.

Comment thread bot/exts/backend/sync/_syncers.py Outdated
for user in diff.updated:
await self.bot.api_client.put(f'bot/users/{user.id}', json=user._asdict())
if diff.created:
created: list = [user._asdict() for user in diff.created]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This have already [] and this indicates that this is list. I think type hint in this variable is unnecessary.

Comment thread bot/exts/backend/sync/_syncers.py Outdated
created: list = [user._asdict() for user in diff.created]
await self.bot.api_client.post("bot/users", json=created)
if diff.updated:
updated: list = [self.patch_dict(user) for user in diff.updated]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same as L395

if diff.created:
created: list = [user._asdict() for user in diff.created]
await self.bot.api_client.post("bot/users", json=created)
if diff.updated:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like you removed log of users updating. I think it's better to restore it.

Comment thread bot/exts/backend/sync/_syncers.py Outdated
@staticmethod
def patch_dict(user: _User) -> dict:
"""Convert namedtuple to dict by omitting None values."""
user_dict: dict = {}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

{} already indicates that this is dict, no need for type hint.

Comment thread bot/exts/backend/sync/_syncers.py Outdated
return endpoint, params

@staticmethod
def patch_dict(user: _User) -> dict:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Use typing.Dictand provide key-value too.

@ghost ghost added s: waiting for author Waiting for author to address a review or respond to a comment and removed needs 2 approvals labels Sep 30, 2020
@ghost ghost added needs 2 approvals and removed s: waiting for author Waiting for author to address a review or respond to a comment labels Oct 1, 2020
@MarkKoz MarkKoz added p: 1 - high High Priority t: feature New feature or request a: backend Related to internal functionality and utilities (error_handler, logging, security, utils and core) labels Oct 1, 2020
@MrHemlock MrHemlock marked this pull request as draft October 2, 2020 16:06
@ghost ghost added s: waiting for author Waiting for author to address a review or respond to a comment and removed needs 2 approvals labels Oct 2, 2020
@ghost ghost added needs 2 approvals and removed s: waiting for author Waiting for author to address a review or respond to a comment labels Oct 4, 2020
@RohanJnr RohanJnr requested a review from MarkKoz October 7, 2020 18:02
Copy link
Copy Markdown
Contributor

@MarkKoz MarkKoz left a comment

Choose a reason for hiding this comment

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

This is almost there.

Comment thread bot/exts/backend/sync/_syncers.py Outdated
Comment thread bot/exts/backend/sync/_syncers.py Outdated
Comment thread bot/exts/backend/sync/_syncers.py Outdated
@ghost ghost added s: waiting for author Waiting for author to address a review or respond to a comment and removed needs 2 approvals labels Oct 7, 2020
Copy link
Copy Markdown
Contributor

@MarkKoz MarkKoz left a comment

Choose a reason for hiding this comment

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

Almost forgot: the code in my comment omitted the comments only for the sake of example. I think the comments that existed in _get_diff were useful and you should keep them in one form or another. They may not make 100% sense any more due to changes, so you may need to revise them a bit.

@ghost ghost added needs 2 approvals and removed s: waiting for author Waiting for author to address a review or respond to a comment labels Oct 8, 2020
@RohanJnr RohanJnr requested a review from MarkKoz October 8, 2020 09:37
Copy link
Copy Markdown
Contributor

@ks129 ks129 left a comment

Choose a reason for hiding this comment

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

Looks good for me

@ghost ghost removed the needs 1 approval label Oct 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: backend Related to internal functionality and utilities (error_handler, logging, security, utils and core) p: 1 - high High Priority t: feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Smart syncing of users to the site

3 participants