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

Add appropriate getHTML methods for polls

This commit is contained in:
Daniil Gentili 2024-04-28 17:17:34 +02:00
parent 1bb1212d34
commit b307c716aa
5 changed files with 77 additions and 11 deletions

2
docs

@ -1 +1 @@
Subproject commit b4d2327f911fc85c6bdb3170a583240153656f13
Subproject commit ab4b4782f8582ca413bcd7c94eb4900c828fd970

View File

@ -803,6 +803,10 @@
<code><![CDATA[$this->totalVoters]]></code>
<code><![CDATA[$value]]></code>
</MixedAssignment>
<PropertyNotSetInConstructor>
<code><![CDATA[$htmlQuestion]]></code>
<code><![CDATA[$htmlQuestionTelegram]]></code>
</PropertyNotSetInConstructor>
</file>
<file src="src/EventHandler/AbstractPrivateMessage.php">
<PropertyNotSetInConstructor>
@ -1792,6 +1796,12 @@
<code><![CDATA[$API->getIdInternal($rawPinned)]]></code>
</PossiblyNullPropertyAssignmentValue>
</file>
<file src="src/EventHandler/Poll/MultiplePoll.php">
<PropertyNotSetInConstructor>
<code><![CDATA[MultiplePoll]]></code>
<code><![CDATA[MultiplePoll]]></code>
</PropertyNotSetInConstructor>
</file>
<file src="src/EventHandler/Poll/PollAnswer.php">
<MixedArgument>
<code><![CDATA[$rawAnswer['text']['entities']]]></code>
@ -1808,6 +1818,10 @@
<code><![CDATA[$this->text]]></code>
<code><![CDATA[$this->voters]]></code>
</MixedAssignment>
<PropertyNotSetInConstructor>
<code><![CDATA[$html]]></code>
<code><![CDATA[$htmlTelegram]]></code>
</PropertyNotSetInConstructor>
</file>
<file src="src/EventHandler/Poll/QuizPoll.php">
<MixedArgument>
@ -1817,8 +1831,16 @@
<code><![CDATA[$this->solution]]></code>
</MixedAssignment>
<PropertyNotSetInConstructor>
<code><![CDATA[$html]]></code>
<code><![CDATA[$htmlTelegram]]></code>
<code><![CDATA[$htmlSolution]]></code>
<code><![CDATA[$htmlSolutionTelegram]]></code>
<code><![CDATA[QuizPoll]]></code>
<code><![CDATA[QuizPoll]]></code>
</PropertyNotSetInConstructor>
</file>
<file src="src/EventHandler/Poll/SinglePoll.php">
<PropertyNotSetInConstructor>
<code><![CDATA[SinglePoll]]></code>
<code><![CDATA[SinglePoll]]></code>
</PropertyNotSetInConstructor>
</file>
<file src="src/EventHandler/Privacy.php">

View File

@ -21,6 +21,7 @@ use danog\MadelineProto\EventHandler\Poll\MultiplePoll;
use danog\MadelineProto\EventHandler\Poll\PollAnswer;
use danog\MadelineProto\EventHandler\Poll\QuizPoll;
use danog\MadelineProto\EventHandler\Poll\SinglePoll;
use danog\MadelineProto\StrTools;
use JsonSerializable;
use ReflectionClass;
use ReflectionProperty;
@ -99,6 +100,27 @@ abstract class AbstractPoll implements JsonSerializable
return $out;
}
protected readonly string $htmlQuestion;
protected readonly string $htmlQuestionTelegram;
/**
* Get an HTML version of the question.
*
* @psalm-suppress InaccessibleProperty
*
* @param bool $allowTelegramTags Whether to allow telegram-specific tags like tg-spoiler, tg-emoji, mention links and so on...
*/
public function getQuestionHTML(bool $allowTelegramTags = false): string
{
if (!$this->questionEntities) {
return StrTools::htmlEscape($this->question);
}
if ($allowTelegramTags) {
return $this->htmlQuestionTelegram ??= StrTools::entitiesToHtml($this->question, $this->questionEntities, $allowTelegramTags);
}
return $this->htmlQuestion ??= StrTools::entitiesToHtml($this->question, $this->questionEntities, $allowTelegramTags);
}
/** @internal */
public function jsonSerialize(): mixed
{

View File

@ -17,6 +17,7 @@
namespace danog\MadelineProto\EventHandler\Poll;
use danog\MadelineProto\EventHandler\Message\Entities\MessageEntity;
use danog\MadelineProto\StrTools;
use danog\MadelineProto\TL\Types\Bytes;
use JsonSerializable;
use ReflectionClass;
@ -58,6 +59,27 @@ final class PollAnswer implements JsonSerializable
$this->voters = $rawAnswer['voters'] ?? null;
}
protected readonly string $html;
protected readonly string $htmlTelegram;
/**
* Get an HTML version of the answer.
*
* @psalm-suppress InaccessibleProperty
*
* @param bool $allowTelegramTags Whether to allow telegram-specific tags like tg-spoiler, tg-emoji, mention links and so on...
*/
public function getHTML(bool $allowTelegramTags = false): string
{
if (!$this->entities) {
return StrTools::htmlEscape($this->text);
}
if ($allowTelegramTags) {
return $this->htmlTelegram ??= StrTools::entitiesToHtml($this->text, $this->entities, $allowTelegramTags);
}
return $this->html ??= StrTools::entitiesToHtml($this->text, $this->entities, $allowTelegramTags);
}
/** @internal */
public function jsonSerialize(): mixed
{

View File

@ -27,18 +27,18 @@ final class QuizPoll extends AbstractPoll
public readonly ?string $solution;
/** @var list<MessageEntity> Message [entities](https://core.telegram.org/api/entities) for styled text in quiz solution */
public readonly array $entities;
public readonly array $solutionEntities;
/** @internal */
public function __construct(array $rawPoll)
{
parent::__construct($rawPoll);
$this->solution = $rawPoll['results']['solution'] ?? null;
$this->entities = MessageEntity::fromRawEntities($rawPoll['results']['solution_entites'] ?? []);
$this->solutionEntities = MessageEntity::fromRawEntities($rawPoll['results']['solution_entites'] ?? []);
}
protected readonly string $html;
protected readonly string $htmlTelegram;
protected readonly string $htmlSolution;
protected readonly string $htmlSolutionTelegram;
/**
* Get an HTML version of the solution.
@ -47,17 +47,17 @@ final class QuizPoll extends AbstractPoll
*
* @param bool $allowTelegramTags Whether to allow telegram-specific tags like tg-spoiler, tg-emoji, mention links and so on...
*/
public function getHTML(bool $allowTelegramTags = false): ?string
public function getSolutionHTML(bool $allowTelegramTags = false): ?string
{
if ($this->solution === null) {
return null;
}
if (!$this->entities) {
if (!$this->solutionEntities) {
return StrTools::htmlEscape($this->solution);
}
if ($allowTelegramTags) {
return $this->htmlTelegram ??= StrTools::entitiesToHtml($this->solution, $this->entities, $allowTelegramTags);
return $this->htmlSolutionTelegram ??= StrTools::entitiesToHtml($this->solution, $this->solutionEntities, $allowTelegramTags);
}
return $this->html ??= StrTools::entitiesToHtml($this->solution, $this->entities, $allowTelegramTags);
return $this->htmlSolution ??= StrTools::entitiesToHtml($this->solution, $this->solutionEntities, $allowTelegramTags);
}
}