Skip to content

Commit

Permalink
Add appropriate getHTML methods for polls
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Apr 28, 2024
1 parent 1bb1212 commit b307c71
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 11 deletions.
26 changes: 24 additions & 2 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand All @@ -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>
Expand All @@ -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">
Expand Down
22 changes: 22 additions & 0 deletions src/EventHandler/AbstractPoll.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand Down Expand Up @@ -99,6 +100,27 @@ private static function getPollAnswers(array $answers, array $result): array
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
{
Expand Down
22 changes: 22 additions & 0 deletions src/EventHandler/Poll/PollAnswer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,6 +59,27 @@ public function __construct(array $rawAnswer)
$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
{
Expand Down
16 changes: 8 additions & 8 deletions src/EventHandler/Poll/QuizPoll.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -47,17 +47,17 @@ public function __construct(array $rawPoll)
*
* @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);
}
}

0 comments on commit b307c71

Please sign in to comment.