1.0.25: Add `disable_security` param to forms, galleries, lists and query galleries. Clean up code a lil bit

pull/1/head
hikari.ftg 2022-04-08 12:26:42 +00:00
parent 6dacf19387
commit f76db91648
7 changed files with 156 additions and 107 deletions

View File

@ -86,7 +86,8 @@ class Events(InlineUnit):
or "photo" in res
or "gif" in res
or "video" in res
or "file" in res and "mime_type" in res
or "file" in res
and "mime_type" in res
)
and "title" in res
for res in result
@ -111,7 +112,9 @@ class Events(InlineUnit):
thumb_url=res.get("thumb", None),
thumb_width=128,
thumb_height=128,
reply_markup=self._generate_markup(res.get("reply_markup", None)),
reply_markup=self._generate_markup(
res.get("reply_markup", None)
),
)
]
elif "photo" in res:
@ -124,7 +127,9 @@ class Events(InlineUnit):
parse_mode="HTML",
thumb_url=res.get("thumb", res["photo"]),
photo_url=res["photo"],
reply_markup=self._generate_markup(res.get("reply_markup", None)),
reply_markup=self._generate_markup(
res.get("reply_markup", None)
),
)
]
elif "gif" in res:
@ -136,7 +141,9 @@ class Events(InlineUnit):
parse_mode="HTML",
thumb_url=res.get("thumb", res["gif"]),
gif_url=res["gif"],
reply_markup=self._generate_markup(res.get("reply_markup", None)),
reply_markup=self._generate_markup(
res.get("reply_markup", None)
),
)
]
elif "video" in res:
@ -150,7 +157,9 @@ class Events(InlineUnit):
thumb_url=res.get("thumb", res["video"]),
video_url=res["video"],
mime_type="video/mp4",
reply_markup=self._generate_markup(res.get("reply_markup", None)),
reply_markup=self._generate_markup(
res.get("reply_markup", None)
),
)
]
elif "file" in res:
@ -164,7 +173,9 @@ class Events(InlineUnit):
thumb_url=res.get("thumb", res["file"]),
document_url=res["file"],
mime_type=res["mime_type"],
reply_markup=self._generate_markup(res.get("reply_markup", None)),
reply_markup=self._generate_markup(
res.get("reply_markup", None)
),
)
]
@ -224,18 +235,23 @@ class Events(InlineUnit):
for button in utils.array_sum(form.get("buttons", [])):
if button.get("_callback_data", None) == query.data:
if (
form.get("force_me", False) and query.from_user.id == self._me
) or (
await self._check_inline_sec_by_mask(
mask=form.get(
"perms_map",
lambda: self._client.dispatcher.security._default,
)(),
user=query.from_user.id,
message=form["message"],
form.get("disable_security", False)
or (
form.get("force_me", False)
and query.from_user.id == self._me
)
or (
await self._check_inline_sec_by_mask(
mask=form.get(
"perms_map",
lambda: self._client.dispatcher.security._default,
)(),
user=query.from_user.id,
message=form["message"],
)
if "message" in form
else False
)
if "message" in form
else False
):
pass
elif (
@ -286,19 +302,23 @@ class Events(InlineUnit):
if query.data in self._custom_map:
if (
self._custom_map[query.data].get("force_me", False)
and query.from_user.id == self._me
) or (
await self._check_inline_sec_by_mask(
mask=self._custom_map[query.data].get(
"perms_map",
lambda: self._client.dispatcher.security._default,
)(),
user=query.from_user.id,
message=self._custom_map[query.data]["message"],
self._custom_map[query.data].get("disable_security", False)
or (
self._custom_map[query.data].get("force_me", False)
and query.from_user.id == self._me
)
or (
await self._check_inline_sec_by_mask(
mask=self._custom_map[query.data].get(
"perms_map",
lambda: self._client.dispatcher.security._default,
)(),
user=query.from_user.id,
message=self._custom_map[query.data]["message"],
)
if "message" in self._custom_map[query.data]
else False
)
if "message" in self._custom_map[query.data]
else False
):
pass
elif (

View File

@ -31,36 +31,41 @@ class Form(InlineUnit):
message: Union[Message, int],
reply_markup: Union[List[List[dict]], List[dict], dict] = None,
*,
force_me: bool = True,
force_me: bool = False,
always_allow: Union[List[list], None] = None,
ttl: Union[int, bool] = False,
on_unload: Union[FunctionType, None] = None,
manual_security: bool = False,
disable_security: bool = False,
) -> Union[str, bool]:
"""Creates inline form with callback
"""
Creates inline form with callback
Args:
text
Content of inline form. HTML markdown supported
message
Where to send inline. Can be either `Message` or `int`
reply_markup
List of buttons to insert in markup. List of dicts with
keys: text, callback
force_me
Either this form buttons must be pressed only by owner scope or no
always_allow
Users, that are allowed to press buttons in addition to previous rules
ttl
Time, when the form is going to be unloaded. Unload means, that the form
buttons with inline queries and callback queries will become unusable, but
buttons with type url will still work as usual. Pay attention, that ttl can't
be bigger, than default one (1 day) and must be either `int` or `False`
on_unload
Callback, called when form is unloaded and/or closed. You can clean up trash
or perform another needed action
manual_security
By default, Hikka will try to inherit inline buttons security from the caller (command)
If you want to avoid this, pass `manual_security=True`
text
Content of inline form. HTML markdown supported
message
Where to send inline. Can be either `Message` or `int`
reply_markup
List of buttons to insert in markup. List of dicts with
keys: text, callback
force_me
Either this form buttons must be pressed only by owner scope or no
always_allow
Users, that are allowed to press buttons in addition to previous rules
ttl
Time, when the form is going to be unloaded. Unload means, that the form
buttons with inline queries and callback queries will become unusable, but
buttons with type url will still work as usual. Pay attention, that ttl can't
be bigger, than default one (1 day) and must be either `int` or `False`
on_unload
Callback, called when form is unloaded and/or closed. You can clean up trash
or perform another needed action
manual_security
By default, Hikka will try to inherit inline buttons security from the caller (command)
If you want to avoid this, pass `manual_security=True`
disable_security
By default, Hikka will try to inherit inline buttons security from the caller (command)
If you want to disable all security checks on this form in particular, pass `disable_security=True`
"""
if reply_markup is None:
@ -77,6 +82,10 @@ class Form(InlineUnit):
logger.error("Invalid type for `manual_security`")
return False
if not isinstance(disable_security, bool):
logger.error("Invalid type for `disable_security`")
return False
if not isinstance(message, (Message, int)):
logger.error("Invalid type for `message`")
return False
@ -150,6 +159,7 @@ class Form(InlineUnit):
**({"perms_map": perms_map} if perms_map else {}),
**({"message": message} if isinstance(message, Message) else {}),
**({"force_me": force_me} if force_me else {}),
**({"disable_security": disable_security} if disable_security else {}),
**({"ttl": round(time.time()) + ttl} if ttl else {}),
**({"always_allow": always_allow} if always_allow else {}),
}

View File

@ -50,37 +50,42 @@ class Gallery(InlineUnit):
preload: Union[bool, int] = False,
gif: bool = False,
manual_security: bool = False,
disable_security: bool = False,
_reattempt: bool = False,
) -> Union[bool, str]:
"""
Processes inline gallery
Args:
caption
Caption for photo, or callable, returning caption
Caption for photo, or callable, returning caption
message
Where to send inline. Can be either `Message` or `int`
Where to send inline. Can be either `Message` or `int`
next_handler
Callback function, which must return url for next photo or list with photo urls
Callback function, which must return url for next photo or list with photo urls
force_me
Either this gallery buttons must be pressed only by owner scope or no
Either this gallery buttons must be pressed only by owner scope or no
always_allow
Users, that are allowed to press buttons in addition to previous rules
Users, that are allowed to press buttons in addition to previous rules
ttl
Time, when the gallery is going to be unloaded. Unload means, that the gallery
will become unusable. Pay attention, that ttl can't
be bigger, than default one (1 day) and must be either `int` or `False`
Time, when the gallery is going to be unloaded. Unload means, that the gallery
will become unusable. Pay attention, that ttl can't
be bigger, than default one (1 day) and must be either `int` or `False`
on_unload
Callback, called when gallery is unloaded and/or closed. You can clean up trash
or perform another needed action
Callback, called when gallery is unloaded and/or closed. You can clean up trash
or perform another needed action
preload
Either to preload gallery photos beforehand or no. If yes - specify threshold to
be loaded. Toggle this attribute, if your callback is too slow to load photos
in real time
Either to preload gallery photos beforehand or no. If yes - specify threshold to
be loaded. Toggle this attribute, if your callback is too slow to load photos
in real time
gif
Whether the gallery will be filled with gifs. If you omit this argument and specify
gifs in `next_handler`, they will be interpreted as plain images (not GIFs!)
Whether the gallery will be filled with gifs. If you omit this argument and specify
gifs in `next_handler`, they will be interpreted as plain images (not GIFs!)
manual_security
By default, Hikka will try to inherit inline buttons security from the caller (command)
If you want to avoid this, pass `manual_security=True`
By default, Hikka will try to inherit inline buttons security from the caller (command)
If you want to avoid this, pass `manual_security=True`
disable_security
By default, Hikka will try to inherit inline buttons security from the caller (command)
If you want to disable all security checks on this gallery in particular, pass `disable_security=True`
"""
if not isinstance(caption, str) and not callable(caption):
@ -91,6 +96,10 @@ class Gallery(InlineUnit):
logger.error("Invalid type for `manual_security`")
return False
if not isinstance(disable_security, bool):
logger.error("Invalid type for `disable_security`")
return False
if not isinstance(message, (Message, int)):
logger.error("Invalid type for `message`")
return False
@ -166,6 +175,7 @@ class Gallery(InlineUnit):
"current_index": 0,
**({"ttl": round(time.time()) + ttl} if ttl else {}),
**({"force_me": force_me} if force_me else {}),
**({"disable_security": disable_security} if disable_security else {}),
**({"on_unload": on_unload} if callable(on_unload) else {}),
**({"preload": preload} if preload else {}),
**({"gif": gif} if gif else {}),
@ -175,21 +185,14 @@ class Gallery(InlineUnit):
}
default_map = {
**(
{"always_allow": self._galleries[gallery_uid]["always_allow"]}
if "always_allow" in self._galleries[gallery_uid]
else {}
),
**(
{"force_me": self._galleries[gallery_uid]["force_me"]}
if "force_me" in self._galleries[gallery_uid]
else {}
),
**(
{"ttl": self._galleries[gallery_uid]["ttl"]}
if "ttl" in self._galleries[gallery_uid]
else {}
),
**({"always_allow": always_allow} if always_allow else {}),
**({"force_me": force_me} if force_me else {}),
**({"disable_security": disable_security} if disable_security else {}),
**({"perms_map": perms_map} if perms_map else {}),
**({"message": message} if isinstance(message, Message) else {}),
}

View File

@ -35,33 +35,42 @@ class List(InlineUnit):
ttl: Union[int, bool] = False,
on_unload: Union[FunctionType, None] = None,
manual_security: bool = False,
disable_security: bool = False,
) -> Union[bool, str]:
"""
Processes inline lists
Args:
message
Where to send list. Can be either `Message` or `int`
Where to send list. Can be either `Message` or `int`
strings
List of strings, which should become inline list
List of strings, which should become inline list
force_me
Either this list buttons must be pressed only by owner scope or no
Either this list buttons must be pressed only by owner scope or no
always_allow
Users, that are allowed to press buttons in addition to previous rules
Users, that are allowed to press buttons in addition to previous rules
ttl
Time, when the list is going to be unloaded. Unload means, that the list
will become unusable. Pay attention, that ttl can't
be bigger, than default one (1 day) and must be either `int` or `False`
Time, when the list is going to be unloaded. Unload means, that the list
will become unusable. Pay attention, that ttl can't
be bigger, than default one (1 day) and must be either `int` or `False`
on_unload
Callback, called when list is unloaded and/or closed. You can clean up trash
or perform another needed action
Callback, called when list is unloaded and/or closed. You can clean up trash
or perform another needed action
manual_security
By default, Hikka will try to inherit inline buttons security from the caller (command)
If you want to avoid this, pass `manual_security=True`
By default, Hikka will try to inherit inline buttons security from the caller (command)
If you want to avoid this, pass `manual_security=True`
disable_security
By default, Hikka will try to inherit inline buttons security from the caller (command)
If you want to disable all security checks on this list in particular, pass `disable_security=True`
"""
if not isinstance(manual_security, bool):
logger.error("Invalid type for `manual_security`")
return False
if not isinstance(disable_security, bool):
logger.error("Invalid type for `disable_security`")
return False
if not isinstance(message, (Message, int)):
logger.error("Invalid type for `message`")
return False
@ -110,6 +119,7 @@ class List(InlineUnit):
"strings": strings,
**({"ttl": round(time.time()) + ttl} if ttl else {}),
**({"force_me": force_me} if force_me else {}),
**({"disable_security": disable_security} if disable_security else {}),
**({"on_unload": on_unload} if callable(on_unload) else {}),
**({"always_allow": always_allow} if always_allow else {}),
**({"perms_map": perms_map} if perms_map else {}),
@ -117,21 +127,14 @@ class List(InlineUnit):
}
default_map = {
**(
{"always_allow": self._lists[list_uid]["always_allow"]}
if "always_allow" in self._lists[list_uid]
else {}
),
**(
{"force_me": self._lists[list_uid]["force_me"]}
if "force_me" in self._lists[list_uid]
else {}
),
**(
{"ttl": self._lists[list_uid]["ttl"]}
if "ttl" in self._lists[list_uid]
else {}
),
**({"always_allow": always_allow} if always_allow else {}),
**({"force_me": force_me} if force_me else {}),
**({"disable_security": disable_security} if disable_security else {}),
**({"perms_map": perms_map} if perms_map else {}),
**({"message": message} if isinstance(message, Message) else {}),
}

View File

@ -21,13 +21,16 @@ class QueryGallery(InlineUnit):
query: InlineQuery,
items: List[dict],
*,
force_me: bool = True,
force_me: bool = False,
disable_security: bool = False,
always_allow: Union[list, None] = None,
) -> None:
"""
query
Processes inline query galleries
Args:
query
`InlineQuery` which should be answered with inline gallery
items
items
Array of dicts with inline results.
Each dict *must* has a:
- `title` - The title of the result
@ -35,15 +38,22 @@ class QueryGallery(InlineUnit):
- `next_handler` - Inline gallery handler. Callback or awaitable
Each dict *could* has a:
- `caption` - Caption of photo. Defaults to `""`
force_me
force_me
Either this gallery buttons must be pressed only by owner scope or no
always_allow
always_allow
Users, that are allowed to press buttons in addition to previous rules
disable_security
By default, Hikka will try to check security of gallery
If you want to disable all security checks on this gallery in particular, pass `disable_security=True`
"""
if not isinstance(force_me, bool):
logger.error("Invalid type for `force_me`")
return False
if not isinstance(disable_security, bool):
logger.error("Invalid type for `disable_security`")
return False
if always_allow and not isinstance(always_allow, list):
logger.error("Invalid type for `always_allow`")
return False
@ -103,6 +113,7 @@ class QueryGallery(InlineUnit):
"ttl": round(time.time()) + 120,
**({"always_allow": always_allow} if always_allow else {}),
**({"force_me": force_me} if force_me else {}),
**({"disable_security": disable_security} if disable_security else {}),
**({"caption": i["caption"]} if "caption" in i else {}),
}

View File

@ -62,6 +62,8 @@ class InlineStuffMod(loader.Module):
message=m,
next_handler=self.inline._custom_map[id_]["handler"],
caption=self.inline._custom_map[id_].get("caption", ""),
force_me=self.inline._custom_map[id_].get("force_me", False),
disable_security=self.inline._custom_map[id_].get("disable_security", False),
)
async def _check_bot(self, username: str) -> bool:

View File

@ -1 +1 @@
__version__ = (1, 0, 24)
__version__ = (1, 0, 25)