xn-auth 0.2.47__tar.gz → 0.2.48__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xn-auth
3
- Version: 0.2.47
3
+ Version: 0.2.48
4
4
  Summary: Auth adapter for XN-Api framework
5
5
  Author-email: Artemiev <mixartemev@gmail.com>
6
6
  License: MIT
@@ -1,6 +1,8 @@
1
1
  from base64 import b64encode
2
2
  from datetime import timedelta
3
3
 
4
+ from aiogram import Bot
5
+ from aiogram.exceptions import TelegramForbiddenError
4
6
  from aiogram.utils.auth_widget import check_signature
5
7
  from aiogram.utils.web_app import WebAppInitData, safe_parse_webapp_init_data, WebAppUser
6
8
  from litestar import Response, post
@@ -18,7 +20,7 @@ async def retrieve_user_handler(token: Tok, _cn: ASGIConnection) -> AuthUser:
18
20
 
19
21
 
20
22
  async def revoked_token_handler(token: Tok, _cn: ASGIConnection) -> bool:
21
- return token.extras["blocked"]
23
+ return False # token.extras["blocked"]
22
24
 
23
25
 
24
26
  class Auth:
@@ -38,11 +40,25 @@ class Auth:
38
40
  async def user_proc(user: WebAppUser) -> Response[XyncUser]:
39
41
  user_in = await user_model.tg2in(user)
40
42
  db_user, cr = await user_model.update_or_create(**user_in.df_unq()) # on login: update user in db from tg
43
+ if user.allows_write_to_pm is None:
44
+ try:
45
+ await Bot(sec).send_chat_action(user.id, "typing")
46
+ db_user.blocked = False
47
+ except TelegramForbiddenError:
48
+ db_user.blocked = True
49
+ else:
50
+ db_user.blocked = not user.allows_write_to_pm
51
+ await db_user.save()
41
52
  res = self.jwt.login(
42
53
  identifier=str(db_user.id),
43
54
  token_extras={"role": db_user.role, "blocked": db_user.blocked},
44
55
  response_body=XyncUser.model_validate(
45
- {**user.model_dump(), "xid": db_user.id, "pub": db_user.pub and b64encode(db_user.pub)}
56
+ {
57
+ **user.model_dump(),
58
+ "xid": db_user.id,
59
+ "pub": db_user.pub and b64encode(db_user.pub),
60
+ "allows_write_to_pm": user.allows_write_to_pm or not db_user.blocked,
61
+ }
46
62
  ),
47
63
  )
48
64
  res.cookies[0].httponly = False
@@ -51,7 +51,7 @@ class User(Model):
51
51
  first_name: str | None = CharField(63)
52
52
  pic: str | None = CharField(127, null=True)
53
53
  last_name: str | None = CharField(31, null=True)
54
- blocked: bool = BooleanField(default=False)
54
+ blocked: bool = BooleanField(null=True)
55
55
  lang: Lang | None = IntEnumField(Lang, default=Lang.ru, null=True)
56
56
  role: Role = IntEnumField(Role, default=Role.READER)
57
57
  prv = UniqBinaryField(unique=True, null=True) # len=32
@@ -68,7 +68,9 @@ class User(Model):
68
68
  "last_name": u.last_name,
69
69
  "username_id": un.id,
70
70
  "lang": u.language_code and Lang[u.language_code],
71
- "pic": hasattr(u, "photo_url") and u.photo_url.replace("https://t.me/i/userpic/320/", "")[:-4],
71
+ "pic": hasattr(u, "photo_url")
72
+ and u.photo_url
73
+ and u.photo_url.replace("https://t.me/i/userpic/320/", "")[:-4],
72
74
  }
73
75
  )
74
76
  if blocked is not None:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xn-auth
3
- Version: 0.2.47
3
+ Version: 0.2.48
4
4
  Summary: Auth adapter for XN-Api framework
5
5
  Author-email: Artemiev <mixartemev@gmail.com>
6
6
  License: MIT
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes