xn-auth 0.2.49__py3-none-any.whl → 0.2.51__py3-none-any.whl

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.
x_auth/controller.py CHANGED
@@ -2,7 +2,7 @@ from base64 import b64encode
2
2
  from datetime import timedelta
3
3
 
4
4
  from aiogram import Bot
5
- from aiogram.exceptions import TelegramForbiddenError
5
+ from aiogram.exceptions import TelegramForbiddenError, TelegramBadRequest
6
6
  from aiogram.utils.auth_widget import check_signature
7
7
  from aiogram.utils.web_app import WebAppInitData, safe_parse_webapp_init_data, WebAppUser
8
8
  from litestar import Response, post
@@ -38,13 +38,12 @@ class Auth:
38
38
  )
39
39
 
40
40
  async def user_proc(user: WebAppUser) -> Response[XyncUser]:
41
- user_in = await user_model.tg2in(user)
42
- db_user, cr = await user_model.update_or_create(**user_in.df_unq()) # on login: update user in db from tg
41
+ db_user, cr = await user_model.tg_upsert(user) # on login: update user in db from tg
43
42
  if user.allows_write_to_pm is None:
44
43
  try:
45
44
  await Bot(sec).send_chat_action(user.id, "typing")
46
45
  db_user.blocked = False
47
- except TelegramForbiddenError:
46
+ except (TelegramForbiddenError, TelegramBadRequest):
48
47
  db_user.blocked = True
49
48
  else:
50
49
  db_user.blocked = not user.allows_write_to_pm
x_auth/enums.py CHANGED
@@ -13,6 +13,7 @@ class PeerType(IntEnum):
13
13
  supergroup = 4
14
14
  user = 5
15
15
  forum = 6
16
+ direct = 7
16
17
 
17
18
 
18
19
  class RoleScope(IntEnum):
x_auth/models.py CHANGED
@@ -27,10 +27,9 @@ from tortoise.fields import (
27
27
  from tortoise.fields.data import CharEnumFieldInstance
28
28
 
29
29
  from x_auth import types
30
- from x_model.field import DatetimeSecField, UInt8Field, UniqBinaryField, UInt1Field, UInt2Field
30
+ from x_model.field import DatetimeSecField, UInt8Field, UInt1Field, UInt2Field
31
31
  from x_model.models import Model, TsTrait
32
32
  from tortoise import Model as TortModel
33
- from x_model.types import BaseUpd
34
33
 
35
34
  from x_auth.enums import Lang, Role, PeerType
36
35
 
@@ -54,41 +53,38 @@ class User(Model):
54
53
  blocked: bool = BooleanField(null=True)
55
54
  lang: Lang | None = IntEnumField(Lang, default=Lang.ru, null=True)
56
55
  role: Role = IntEnumField(Role, default=Role.READER)
57
- prv = UniqBinaryField(unique=True, null=True) # len=32
58
- pub = UniqBinaryField(unique=True, null=True) # len=32
59
56
 
60
57
  app: BackwardOneToOneRelation["App"]
61
58
 
62
59
  @classmethod
63
- async def tg2in(cls, u: PyroUser | AioUser | WebAppUser, blocked: bool = None) -> BaseUpd:
60
+ async def tg2in(cls, u: PyroUser | AioUser | WebAppUser, blocked: bool = None) -> dict:
64
61
  un, _ = await cls._meta.fields_map["username"].related_model.update_or_create({"username": u.username}, id=u.id)
65
- user = cls.validate(
66
- {
67
- "first_name": u.first_name,
68
- "last_name": u.last_name,
69
- "username_id": un.id,
70
- "lang": u.language_code and Lang[u.language_code],
71
- "pic": hasattr(u, "photo_url")
72
- and u.photo_url
73
- and u.photo_url.replace("https://t.me/i/userpic/320/", "")[:-4],
74
- }
62
+ pic = hasattr(u, "photo_url") and u.photo_url and u.photo_url.replace("https://t.me/i/userpic/320/", "")[:-4]
63
+ pic = (
64
+ pic
65
+ or isinstance(u, AioUser)
66
+ and (photos := (await u.bot.get_user_profile_photos(u.id)).photos)
67
+ and photos[0][-1].file_id
68
+ or None
75
69
  )
70
+ user_dict = {
71
+ "first_name": u.first_name,
72
+ "last_name": u.last_name,
73
+ "lang": u.language_code and Lang[u.language_code],
74
+ "pic": pic,
75
+ }
76
76
  if blocked is not None:
77
- user.blocked = blocked
78
- return user
77
+ user_dict["blocked"] = blocked
78
+ return user_dict
79
79
 
80
80
  @classmethod
81
81
  async def is_blocked(cls, sid: str) -> bool:
82
82
  return (await cls[int(sid)]).blocked
83
83
 
84
84
  @classmethod
85
- async def tg_upsert(cls, u: PyroUser | AioUser, blocked: bool = None) -> tuple["User", bool]:
86
- user_in: cls.in_type() = await cls.tg2in(u, blocked)
87
- prms = user_in.df_unq()
88
- return await cls.update_or_create(**prms)
89
-
90
- # class Meta:
91
- # abstract = True
85
+ async def tg_upsert(cls, u: PyroUser | AioUser | WebAppUser, blocked: bool = None) -> tuple["User", bool]:
86
+ user_in: dict = await cls.tg2in(u, blocked)
87
+ return await cls.update_or_create(user_in, username_id=u.id)
92
88
 
93
89
 
94
90
  class Country(Model):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xn-auth
3
- Version: 0.2.49
3
+ Version: 0.2.51
4
4
  Summary: Auth adapter for XN-Api framework
5
5
  Author-email: Artemiev <mixartemev@gmail.com>
6
6
  License: MIT
@@ -0,0 +1,10 @@
1
+ x_auth/controller.py,sha256=rV_nc6BU9B_5TeqPaDGYtqfvWVaIEnrLuknzcOM5vDQ,3701
2
+ x_auth/enums.py,sha256=pciVrb92S9YQFMhHe6pKNYwcvtevwfMXIiG1WxAEa-Q,761
3
+ x_auth/exceptions.py,sha256=2B4okJxhPyNqTJXlSTfblJUQJ60bLGXdgJIu6ue7S6w,162
4
+ x_auth/middleware.py,sha256=JfssQomDv0J_69GfS2a_2_uyRzs26zSY6IW1Vk7m8K0,2900
5
+ x_auth/models.py,sha256=V3lvrUVtS5viyqO_-Y7u2zQep5bRjPOpWmc5kweMrN0,9211
6
+ x_auth/types.py,sha256=xVi1A8CrGfdfS2HbaxX-X3lEJWH_1TvTk-u8TeqKzQg,1514
7
+ xn_auth-0.2.51.dist-info/METADATA,sha256=74gUBa5rPA2t5rnVIcvca5ITE5mxGgwx_m22BLf7Z_Y,823
8
+ xn_auth-0.2.51.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
9
+ xn_auth-0.2.51.dist-info/top_level.txt,sha256=ydMDkzxgQPtW-E_MNDfUAroAFZvWSqU-x_kZSA7NSFo,7
10
+ xn_auth-0.2.51.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- x_auth/controller.py,sha256=2UUS-v5DcKmXplVeAfoxgfDB9fP8E47HnU49egK-9rc,3731
2
- x_auth/enums.py,sha256=Z4-pJVWNNrjl2_RZqo2ZwuQR5WR46-A76VWmd7k7-4E,746
3
- x_auth/exceptions.py,sha256=2B4okJxhPyNqTJXlSTfblJUQJ60bLGXdgJIu6ue7S6w,162
4
- x_auth/middleware.py,sha256=JfssQomDv0J_69GfS2a_2_uyRzs26zSY6IW1Vk7m8K0,2900
5
- x_auth/models.py,sha256=MVd-iFG3CX6-5EVIedd8KBb3fNW_FuwHvKs7_SCwHl8,9312
6
- x_auth/types.py,sha256=xVi1A8CrGfdfS2HbaxX-X3lEJWH_1TvTk-u8TeqKzQg,1514
7
- xn_auth-0.2.49.dist-info/METADATA,sha256=DLHIFbcunfFTkvj6yHfVWteElYFQ7-VHGC8BXh0lnSc,823
8
- xn_auth-0.2.49.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
9
- xn_auth-0.2.49.dist-info/top_level.txt,sha256=ydMDkzxgQPtW-E_MNDfUAroAFZvWSqU-x_kZSA7NSFo,7
10
- xn_auth-0.2.49.dist-info/RECORD,,