1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-30 06:18:58 +01:00

Simplify code escaping

This commit is contained in:
Daniil Gentili 2023-11-20 21:48:21 +01:00
parent 853fa4167d
commit 1dcc9af659
10 changed files with 93 additions and 22 deletions

View File

@ -448,7 +448,7 @@ Want to add your own open-source project to this list? [Click here!](https://doc
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#extractmessageupdate-array-updates-array" name="extractMessageUpdate">Extract an update message constructor from an Updates constructor: extractMessageUpdate</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#extractbotapifile-array-info-array" name="extractBotAPIFile">Extract file info from bot API message: extractBotAPIFile</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.getCustomEmojiDocuments.html" name="messages.getCustomEmojiDocuments">Fetch custom emoji stickers »: messages.getCustomEmojiDocuments</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/chatlists.getChatlistUpdates.html" name="chatlists.getChatlistUpdates">Fetch new chats associated with an imported chat folder deep link ». Must be invoked at most every chatlistupdateperiod seconds (as per the related client configuration parameter »): chatlists.getChatlistUpdates</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/chatlists.getChatlistUpdates.html" name="chatlists.getChatlistUpdates">Fetch new chats associated with an imported chat folder deep link ». Must be invoked at most every chatlist_update_period seconds (as per the related client configuration parameter »): chatlists.getChatlistUpdates</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.getSavedRingtones.html" name="account.getSavedRingtones">Fetch saved notification sounds: account.getSavedRingtones</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.getMessageEditData.html" name="messages.getMessageEditData">Find out if a media message's caption can be edited: messages.getMessageEditData</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.finishTakeoutSession.html" name="account.finishTakeoutSession">Finish account takeout session: account.finishTakeoutSession</a>
@ -615,7 +615,7 @@ Want to add your own open-source project to this list? [Click here!](https://doc
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.getUnreadReactions.html" name="messages.getUnreadReactions">Get unread reactions to messages you sent: messages.getUnreadReactions</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#getwebwarnings-string" name="getWebWarnings">Get various warnings to show to the user in the web UI: getWebWarnings</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.getWebAuthorizations.html" name="account.getWebAuthorizations">Get web login widget authorizations: account.getWebAuthorizations</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.getMessageReadParticipants.html" name="messages.getMessageReadParticipants">Get which users read a specific message: only available for groups and supergroups with less than chatreadmarksizethreshold members, read receipts will be stored for chatreadmarkexpireperiod seconds after the message was sent, see client configuration for more info »: messages.getMessageReadParticipants</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.getMessageReadParticipants.html" name="messages.getMessageReadParticipants">Get which users read a specific message: only available for groups and supergroups with less than chat_read_mark_size_threshold members, read receipts will be stored for chat_read_mark_expire_period seconds after the message was sent, see client configuration for more info »: messages.getMessageReadParticipants</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#getsecretmessage-int-chatid-int-randomid-danog-madelineproto-eventhandler-message-secretmessage" name="getSecretMessage">Gets a secret chat message: getSecretMessage</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.getNotifySettings.html" name="account.getNotifySettings">Gets current notification settings for a given user/group, from all users/all groups: account.getNotifySettings</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.getFeaturedEmojiStickers.html" name="messages.getFeaturedEmojiStickers">Gets featured custom emoji stickersets: messages.getFeaturedEmojiStickers</a>

2
docs

@ -1 +1 @@
Subproject commit 768b8692b4c31ebb2fa485a554d351b3b5edcac0
Subproject commit 8e5b001495ee886ff2a9f7b252ac7d41f1b013cb

View File

@ -0,0 +1,19 @@
<?php declare(strict_types=1);
namespace danog\MadelineProto\EventHandler\Message\Entities;
/**
* Message entity representing a [user mention](https://core.telegram.org/api/mentions) created by the user, not returned by the API.
*/
final class InputMentionName extends MessageEntity
{
/** @var int|string Identifier of the user that was mentioned */
public readonly int|string $userId;
/** @internal */
protected function __construct(array $rawEntities)
{
parent::__construct($rawEntities);
$this->userId = $rawEntities['user_id'];
}
}

View File

@ -41,7 +41,8 @@ abstract class MessageEntity implements JsonSerializable
'messageEntityCode' => new Code($entity),
'messageEntityPre' => new Pre($entity),
'messageEntityTextUrl' => new TextUrl($entity),
'messageEntityMentionName','inputMessageEntityMentionName' => new MentionName($entity),
'messageEntityMentionName' => new MentionName($entity),
'inputMessageEntityMentionName' => new InputMentionName($entity),
'messageEntityPhone' => new Phone($entity),
'messageEntityCashtag' => new Cashtag($entity),
'messageEntityUnderline' => new Underline($entity),

View File

@ -1321,6 +1321,15 @@ abstract class InternalDoc
{
$this->wrapper->getAPI()->logout();
}
/**
* Escape string for markdown code section.
*
* @param string $what String to escape
*/
public static function markdownCodeEscape(string $what): string
{
return \danog\MadelineProto\StrTools::markdownCodeEscape($what);
}
/**
* Escape string for markdown codeblock.
*

View File

@ -9,7 +9,7 @@ final class Blacklist
'account.getPasswordSettings' => 'You cannot use this method directly; use $MadelineProto->update2fa($params), instead (see https://docs.madelineproto.xyz for more info)',
'messages.receivedQueue' => 'You cannot use this method directly',
'messages.getDhConfig' => 'You cannot use this method directly, instead use $MadelineProto->getDhConfig();',
'auth.bindTempAuthKey' => 'You cannot use this method directly, instead modify the PFS and default_temp_auth_key_expires_in settings, see https://docs.madelineproto.xyz/docs/SETTINGS.html for more info',
'auth.bindTempAuthKey' => 'You cannot use this method directly, instead modify the PFS settings, see https://docs.madelineproto.xyz/docs/SETTINGS.html for more info',
'auth.exportAuthorization' => 'You cannot use this method directly, use $MadelineProto->exportAuthorization() instead, see https://docs.madelineproto.xyz/docs/LOGIN.html',
'auth.importAuthorization' => 'You cannot use this method directly, use $MadelineProto->importAuthorization($authorization) instead, see https://docs.madelineproto.xyz/docs/LOGIN.html',
'auth.logOut' => 'You cannot use this method directly, use the logout method instead (see https://docs.madelineproto.xyz for more info)',

View File

@ -25,6 +25,7 @@ use danog\MadelineProto\EventHandler\Message\Entities\Bold;
use danog\MadelineProto\EventHandler\Message\Entities\Code;
use danog\MadelineProto\EventHandler\Message\Entities\CustomEmoji;
use danog\MadelineProto\EventHandler\Message\Entities\Email;
use danog\MadelineProto\EventHandler\Message\Entities\InputMentionName;
use danog\MadelineProto\EventHandler\Message\Entities\Italic;
use danog\MadelineProto\EventHandler\Message\Entities\Mention;
use danog\MadelineProto\EventHandler\Message\Entities\MentionName;
@ -161,6 +162,7 @@ abstract class StrTools extends Extension
$entity instanceof Spoiler => $allowTelegramTags ? '<tg-spoiler>' : '',
$entity instanceof CustomEmoji => $allowTelegramTags ? '<tg-emoji emoji-id="'.$entity->documentId.'">' : '',
$entity instanceof MentionName => $allowTelegramTags ? '<a href="tg://user?id='.$entity->userId.'">' : '',
$entity instanceof InputMentionName => $allowTelegramTags ? '<a href="tg://user?id='.$entity->userId.'">' : '',
default => '',
};
$offset += $length;
@ -176,6 +178,7 @@ abstract class StrTools extends Extension
$entity instanceof Spoiler => $allowTelegramTags ? '</tg-spoiler>' : '',
$entity instanceof CustomEmoji => $allowTelegramTags ? "</tg-emoji>" : '',
$entity instanceof MentionName => $allowTelegramTags ? '</a>' : '',
$entity instanceof InputMentionName => $allowTelegramTags ? '</a>' : '',
default => '',
} . ($insertions[$offset] ?? '');
}
@ -274,6 +277,15 @@ abstract class StrTools extends Extension
{
return str_replace('```', '\\```', $what);
}
/**
* Escape string for markdown code section.
*
* @param string $what String to escape
*/
public static function markdownCodeEscape(string $what): string
{
return str_replace('`', '\\`', $what);
}
/**
* Escape string for URL.
*

View File

@ -232,6 +232,10 @@ trait BotAPI
unset($data['_']);
$data['type'] = 'phone_number';
return $data;
case 'messageEntityBlockquote':
unset($data['_']);
$data['type'] = 'block_quote';
return $data;
case 'messageEntityMention':
unset($data['_']);
$data['type'] = 'mention';

View File

@ -98,24 +98,30 @@ final class MarkdownEntities extends Entities
}
$offset++;
$char = "](";
} elseif ($char === '`' && $next === '`' && ($markdown[$offset+1] ?? '') === '`') {
} elseif ($char === '`') {
$message .= $piece;
$messageLen += StrTools::mbStrlen($piece);
$offset += 2;
$langLen = strcspn($markdown, "\n ", $offset);
$language = substr($markdown, $offset, $langLen);
$offset += $langLen;
if ($markdown[$offset] === "\n") {
$offset++;
$token = '`';
$language = null;
if ($next === '`' && ($markdown[$offset+1] ?? '') === '`') {
$token = '```';
$offset += 2;
$langLen = strcspn($markdown, "\n ", $offset);
$language = substr($markdown, $offset, $langLen);
$offset += $langLen;
if ($markdown[$offset] === "\n") {
$offset++;
}
}
$piece = '';
$posClose = $offset;
while (($posClose = strpos($markdown, '```', $posClose)) !== false) {
while (($posClose = strpos($markdown, $token, $posClose)) !== false) {
if ($markdown[$posClose-1] === '\\') {
$piece .= substr($markdown, $offset, ($posClose-$offset)-1)."```";
$posClose += 3;
$piece .= substr($markdown, $offset, ($posClose-$offset)-1).$token;
$posClose += \strlen($token);
$offset = $posClose;
continue;
}
@ -144,14 +150,17 @@ final class MarkdownEntities extends Entities
}
if ($pieceLen > 0) {
$entities []= [
'_' => 'messageEntityPre',
'_' => match ($token) {
'```' => 'messageEntityPre',
'`' => 'messageEntityCode',
},
'language' => $language,
'offset' => $start,
'length' => $pieceLen,
];
}
$offset = $posClose+3;
$offset = $posClose+\strlen($token);
continue;
}

View File

@ -218,6 +218,11 @@ class EntitiesTest extends MadelineTestCase
'length' => 9,
'type' => 'underline',
],
[
'offset' => 49,
'length' => 10,
'type' => 'block_quote',
],
[
'offset' => 60,
'length' => 18,
@ -244,7 +249,7 @@ class EntitiesTest extends MadelineTestCase
'type' => 'spoiler',
],
],
'<b>test</b><br><i>test</i> <code>test</code> <pre language="html">test</pre> <a href="https://example.com/">test</a> <s>strikethrough</s> <u>underline</u> blockquote <a href="https://google.com">https://google.com</a> <a href="mailto:daniil@daniil.it">daniil@daniil.it</a> <a href="phone:+39398172758722">+39398172758722</a> <a href="https://t.me/daniilgentili">@daniilgentili</a> <tg-spoiler>spoiler</tg-spoiler> &lt;b&gt;not_bold&lt;/b&gt;',
'<b>test</b><br><i>test</i> <code>test</code> <pre language="html">test</pre> <a href="https://example.com/">test</a> <s>strikethrough</s> <u>underline</u> <blockquote>blockquote</blockquote> <a href="https://google.com">https://google.com</a> <a href="mailto:daniil@daniil.it">daniil@daniil.it</a> <a href="phone:+39398172758722">+39398172758722</a> <a href="https://t.me/daniilgentili">@daniilgentili</a> <tg-spoiler>spoiler</tg-spoiler> &lt;b&gt;not_bold&lt;/b&gt;',
],
[
'markdown',
@ -346,14 +351,26 @@ class EntitiesTest extends MadelineTestCase
],
[
'markdown',
"```\n".StrTools::markdownCodeblockEscape('\\ ```').'```',
'\\ ```',
"```\na_b\n".StrTools::markdownCodeblockEscape('\\ ```').'```',
"a_b\n\\ ```",
[
[
'offset' => 0,
'length' => 9,
'type' => 'pre',
'language' => '',
],
],
],
[
'markdown',
'`a_b '.StrTools::markdownCodeEscape('`').'`',
'a_b `',
[
[
'offset' => 0,
'length' => 5,
'type' => 'pre',
'language' => '',
'type' => 'code',
],
],
],