Skip to content

Commit

Permalink
save user data when login
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Aug 20, 2024
1 parent 8f5fb70 commit e9a0f16
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 4 deletions.
42 changes: 41 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ regex = "^2024.7.24"
tomli = "^2.0.0"
uvicorn = { version = "^0.30.6", extras = ['standard'] }
uuid6 = "^2024.7.10"
pg8000 = "^1.31.2"

[tool.poetry.group.dev.dependencies]
mypy = "^1.11.1"
Expand Down
11 changes: 10 additions & 1 deletion schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@ alter table patch
alter table patch
ALTER column original_name set default '';

update patch set original_name = '' where original_name is NULL;
update patch
set original_name = ''
where original_name is NULL;

alter table patch
ALTER column original_name set not null;

create table if not exists patch_users
(
user_id int PRIMARY KEY not null,
username varchar(255) not null,
nickname varchar(255) not null
);
48 changes: 48 additions & 0 deletions scripts/fetch_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import html
from urllib.parse import urlparse

import httpx
from pg8000.native import Connection

Check failure on line 5 in scripts/fetch_users.py

View workflow job for this annotation

GitHub Actions / mypy

Skipping analyzing "pg8000.native": module is installed, but missing library stubs or py.typed marker

from config import PG_DSN


u = urlparse(PG_DSN)


def main():

Check failure on line 13 in scripts/fetch_users.py

View workflow job for this annotation

GitHub Actions / mypy

Function is missing a return type annotation
c = httpx.Client()
conn = Connection(
host=u.hostname,
user=u.username,
port=u.port,
password=u.password,
database=u.path.removeprefix("/"),
)

results: list[tuple[int, int]] = conn.run("select from_user_id, wiki_user_id from patch")
s = set()
for u1, u2 in results:
s.add(u1)
s.add(u2)

s.discard(0)

for user in s:
r = c.get(f"https://api.bgm.tv/user/{user}")
data = r.json()
conn.run(
"""
insert into patch_users (user_id, username, nickname) VALUES (:id,:username,:nickname)
on conflict (user_id) do update set
username = excluded.username,
nickname = excluded.nickname
""",
id=data["id"],
username=data["username"],
nickname=html.unescape(data["nickname"]),
)


if __name__ == "__main__":
main()

Check failure on line 48 in scripts/fetch_users.py

View workflow job for this annotation

GitHub Actions / mypy

Call to untyped function "main" in typed context
16 changes: 14 additions & 2 deletions server/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from litestar.types import Empty

from config import BGM_TV_APP_ID, BGM_TV_APP_SECRET, SERVER_BASE_URL
from server.base import Request, User, http_client
from server.base import Request, User, http_client, pg


CALLBACK_URL = f"{SERVER_BASE_URL}/oauth_callback"
Expand Down Expand Up @@ -106,8 +106,8 @@ async def callback(code: str, request: Request) -> Redirect:
if res.status_code >= 300:
raise InternalServerException("api request error")
data = res.json()

user_id = data["user_id"]

access_token = data["access_token"]

res = await http_client.get(
Expand All @@ -119,6 +119,18 @@ async def callback(code: str, request: Request) -> Redirect:

group_id = user["user_group"]

await pg.execute(
"""
insert into patch_users (user_id, username, nickname) VALUES ($1,$2,$3)
on conflict (user_id) do update set
username = excluded.username,
nickname = excluded.nickname
""",
user_id,
user["username"],
user["nickname"],
)

# litestar type this as dict[str, Any], but it maybe Empty
if isinstance(request.session, dict):
back_to = request.session.get("backTo", "/")
Expand Down
5 changes: 5 additions & 0 deletions taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ tasks:
cmds:
- uvicorn server:app --env-file .env

run:
dotenv: [.env]
cmds:
- python {{.CLI_ARGS}}

lock:no-cache:
cmds:
- poetry lock --no-update --no-cache
Expand Down

0 comments on commit e9a0f16

Please sign in to comment.