1
0
mirror of https://github.com/danog/MadelineProto.git synced 2025-01-22 15:31:12 +01:00

Add support for secret chats in SimpleEventHandler!

- Add SecretChat service message classes.
- Separate PrivateMessage classes.
- Note that chatId and senderId marked as TODO to be added soon ...
- cs-fix
This commit is contained in:
Mahdi 2023-09-06 23:01:07 +03:30
parent cbe87bca8d
commit 252f820011
13 changed files with 383 additions and 27 deletions

View File

@ -36,8 +36,10 @@ abstract class AbstractMessage extends Update implements SimpleFilters
public readonly int $id;
/** Whether the message is outgoing */
public readonly bool $out;
//TODO add support for secret chats
/** ID of the chat where the message was sent */
public readonly int $chatId;
//TODO add support for secret chats
/** ID of the sender of the message */
public readonly int $senderId;
/** ID of the message to which this message is replying */
@ -78,7 +80,7 @@ abstract class AbstractMessage extends Update implements SimpleFilters
$this->date = $rawMessage['date'];
$this->mentioned = $rawMessage['mentioned'];
$this->silent = $rawMessage['silent'];
$this->ttlPeriod = $rawMessage['ttl_period'] ?? null;
$this->ttlPeriod = $rawMessage['ttl_period'] ?? $rawMessage['ttl'] ?? null;
if (isset($rawMessage['reply_to']) && $rawMessage['reply_to']['_'] === 'messageReplyHeader') {
$replyTo = $rawMessage['reply_to'];

View File

@ -0,0 +1,44 @@
<?php declare(strict_types=1);
/**
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Mahdi <mahdi.talaee1379@gmail.com>
* @copyright 2016-2023 Mahdi <mahdi.talaee1379@gmail.com>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto\EventHandler;
use danog\MadelineProto\EventHandler\Message\Service\DialogScreenshotTaken;
use danog\MadelineProto\MTProto;
abstract class AbstractPrivateMessage extends Message
{
/** @internal */
public function __construct(MTProto $API, array $rawMessage, array $info)
{
parent::__construct($API, $rawMessage, $info);
}
/**
* Notify the other user in a private chat that a screenshot of the chat was taken.
*
*/
public function screenShot(): DialogScreenshotTaken
{
$result = $this->getClient()->methodCallAsyncRead(
'messages.sendScreenshotNotification',
[
'peer' => $this->chatId,
'reply_to' => [ '_' => 'inputReplyToMessage', 'reply_to_msg_id' => 0 ],
]
);
return $this->getClient()->wrapMessage($this->getClient()->extractMessage($result));
}
}

View File

@ -19,6 +19,7 @@ namespace danog\MadelineProto\EventHandler\Media;
use danog\MadelineProto\EventHandler\Media;
use danog\MadelineProto\MTProto;
use danog\MadelineProto\TL\Types\Bytes;
/**
* Represents a photo.
@ -26,15 +27,34 @@ use danog\MadelineProto\MTProto;
final class Photo extends Media
{
/** If true; the current media has attached mask stickers. */
public readonly bool $hasStickers;
public readonly ?bool $hasStickers;
/** Content of thumbnail file (JPEGfile, quality 55, set in a square 90x90) only for secret chats. */
public readonly string $thumb;
/** Thumbnail height only for secret chats. */
public readonly string $thumbHeight;
/** Thumbnail width only for secret chats. */
public readonly string $thumbWidth;
/** @internal */
public function __construct(
MTProto $API,
array $rawMedia,
bool $protected,
array $rawMedia,
bool $protected,
) {
parent::__construct($API, $rawMedia, $protected);
$this->hasStickers = $rawMedia['photo']['has_stickers'];
$this->hasStickers = $rawMedia['photo']['has_stickers'] ?? null;
$this->thumb = (string) $rawMedia['thumb'] ?? null;
$this->thumbHeight = (string) $rawMedia['thumb_h'] ?? null;
$this->thumbWidth = (string) $rawMedia['thumb_w'] ?? null;
}
/** @internal */
public function jsonSerialize(): mixed
{
$v = \get_object_vars($this);
unset($v['API'], $v['session'], $v['location']);
$v['_'] = static::class;
$v['thumb'] = new Bytes($v['thumb']);
return $v;
}
}

View File

@ -16,27 +16,11 @@
namespace danog\MadelineProto\EventHandler\Message;
use danog\MadelineProto\EventHandler\Message;
use danog\MadelineProto\EventHandler\Message\Service\DialogScreenshotTaken;
use danog\MadelineProto\EventHandler\AbstractPrivateMessage;
/**
* Represents an incoming or outgoing private message.
*/
final class PrivateMessage extends Message
final class PrivateMessage extends AbstractPrivateMessage
{
/**
* Notify the other user in a private chat that a screenshot of the chat was taken.
*
*/
public function screenShot(): DialogScreenshotTaken
{
$result = $this->getClient()->methodCallAsyncRead(
'messages.sendScreenshotNotification',
[
'peer' => $this->chatId,
'reply_to' => [ '_' => 'inputReplyToMessage', 'reply_to_msg_id' => 0 ],
]
);
return $this->getClient()->wrapMessage($this->getClient()->extractMessage($result));
}
}

View File

@ -0,0 +1,41 @@
<?php declare(strict_types=1);
/**
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Mahdi <mahdi.talaee1379@gmail.com>
* @copyright 2016-2023 Mahdi <mahdi.talaee1379@gmail.com>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto\EventHandler\Message;
use danog\MadelineProto\EventHandler\AbstractPrivateMessage;
use danog\MadelineProto\MTProto;
/**
* Represents New encrypted message.
*/
class SecretMessage extends AbstractPrivateMessage
{
/** Whether the webpage preview is disabled */
public readonly ?bool $noWebpage;
/** Random message ID of the message this message replies to (parameter added in layer 45) */
public readonly ?int $replyToRandomId;
/** Specifies the ID of the inline bot that generated the message (parameter added in layer 45) */
public readonly ?string $viaBotName;
/** @internal */
public function __construct(MTProto $API, array $rawMessage, array $info)
{
parent::__construct($API, $decryptedMessage = $rawMessage['decrypted_message'], $info);
$this->noWebpage = $decryptedMessage['no_webpage'] ?? null;
$this->replyToRandomId = $decryptedMessage['reply_to_random_id'] ?? null;
$this->viaBotName = $decryptedMessage['via_bot_name'] ?? null;
}
}

View File

@ -0,0 +1,37 @@
<?php declare(strict_types=1);
/**
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Mahdi <mahdi.talaee1379@gmail.com>
* @copyright 2016-2023 Mahdi <mahdi.talaee1379@gmail.com>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto\EventHandler\Message\Service\SecretChat;
use danog\MadelineProto\EventHandler\Message\ServiceMessage;
use danog\MadelineProto\MTProto;
/**
* Deleted messages.
*/
class ActionDeleteMessages extends ServiceMessage
{
public function __construct(
MTProto $API,
array $rawMessage,
array $info,
/** @var $randomIds list<int> List of deleted message IDs */
public readonly array $randomIds
) {
parent::__construct($API, $rawMessage, $info);
}
}

View File

@ -0,0 +1,31 @@
<?php declare(strict_types=1);
/**
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Mahdi <mahdi.talaee1379@gmail.com>
* @copyright 2016-2023 Mahdi <mahdi.talaee1379@gmail.com>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto\EventHandler\Message\Service\SecretChat;
use danog\MadelineProto\EventHandler\Message\ServiceMessage;
use danog\MadelineProto\MTProto;
/**
* The entire message history has been deleted.
*/
class ActionFlushHistory extends ServiceMessage
{
public function __construct(MTProto $API, array $rawMessage, array $info)
{
parent::__construct($API, $rawMessage, $info);
}
}

View File

@ -0,0 +1,37 @@
<?php declare(strict_types=1);
/**
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Mahdi <mahdi.talaee1379@gmail.com>
* @copyright 2016-2023 Mahdi <mahdi.talaee1379@gmail.com>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto\EventHandler\Message\Service\SecretChat;
use danog\MadelineProto\EventHandler\Message\ServiceMessage;
use danog\MadelineProto\MTProto;
/**
* Messages marked as read.
*/
class ActionReadMessages extends ServiceMessage
{
public function __construct(
MTProto $API,
array $rawMessage,
array $info,
/** @var $randomIds list<int> List of message IDs */
public readonly array $randomIds
) {
parent::__construct($API, $rawMessage, $info);
}
}

View File

@ -0,0 +1,37 @@
<?php declare(strict_types=1);
/**
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Mahdi <mahdi.talaee1379@gmail.com>
* @copyright 2016-2023 Mahdi <mahdi.talaee1379@gmail.com>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto\EventHandler\Message\Service\SecretChat;
use danog\MadelineProto\EventHandler\Message\ServiceMessage;
use danog\MadelineProto\MTProto;
/**
* A screenshot was taken.
*/
class ActionScreenshotMessages extends ServiceMessage
{
public function __construct(
MTProto $API,
array $rawMessage,
array $info,
/** @var $randomIds list<int> List of affected message ids (that appeared on the screenshot) */
public readonly array $randomIds
) {
parent::__construct($API, $rawMessage, $info);
}
}

View File

@ -0,0 +1,40 @@
<?php declare(strict_types=1);
/**
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Mahdi <mahdi.talaee1379@gmail.com>
* @copyright 2016-2023 Mahdi <mahdi.talaee1379@gmail.com>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto\EventHandler\Message\Service\SecretChat;
use danog\MadelineProto\EventHandler\Message\ServiceMessage;
use danog\MadelineProto\MTProto;
/**
*
* Setting of a message lifetime after reading.
*
* Upon receiving such message the client shall start deleting of all messages of an encrypted chat ttl_seconds seconds after the messages were read by user.
*/
class ActionSetMessageTTL extends ServiceMessage
{
public function __construct(
MTProto $API,
array $rawMessage,
array $info,
/** Lifetime in seconds */
public readonly int $ttlSeconds
) {
parent::__construct($API, $rawMessage, $info);
}
}

View File

@ -0,0 +1,38 @@
<?php declare(strict_types=1);
/**
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Mahdi <mahdi.talaee1379@gmail.com>
* @copyright 2016-2023 Mahdi <mahdi.talaee1379@gmail.com>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto\EventHandler\Message\Service\SecretChat;
use danog\MadelineProto\EventHandler\Action;
use danog\MadelineProto\EventHandler\Message\ServiceMessage;
use danog\MadelineProto\MTProto;
/**
* User is preparing a message: typing, recording, uploading, etc.
*/
class ActionTyping extends ServiceMessage
{
public function __construct(
MTProto $API,
array $rawMessage,
array $info,
/** Type of action */
public readonly Action $action
) {
parent::__construct($API, $rawMessage, $info);
}
}

View File

@ -58,18 +58,21 @@ trait FilesAbstraction
*/
public function wrapMedia(array $media, bool $protected = false): ?Media
{
if ($media['_'] === 'messageMediaPhoto') {
if ($media['_'] === 'messageMediaPhoto' || $media['_'] == 'decryptedMessageMediaPhoto') {
if (!isset($media['photo'])) {
return null;
}
return new Photo($this, $media, $protected);
}
if ($media['_'] !== 'messageMediaDocument') {
if ($media['_'] !== 'messageMediaDocument' && $media['_'] !== 'decryptedMessageMediaExternalDocument') {
return null;
}
if (!isset($media['document'])) {
if (!isset($media['document']) && $media['_'] !== 'messageMediaDocument') {
return null;
}
if ($media['_'] !== 'decryptedMessageMediaExternalDocument') {
$media['document']['attributes'] = $media['attributes'];
}
$has_video = null;
$has_document_photo = null;
$has_animated = false;

View File

@ -27,6 +27,7 @@ use Amp\Http\Client\Response;
use Amp\TimeoutException;
use danog\MadelineProto\API;
use danog\MadelineProto\EventHandler\AbstractMessage;
use danog\MadelineProto\EventHandler\Action;
use danog\MadelineProto\EventHandler\InlineQuery;
use danog\MadelineProto\EventHandler\Message;
use danog\MadelineProto\EventHandler\Message\ChannelMessage;
@ -60,6 +61,12 @@ use danog\MadelineProto\EventHandler\Message\Service\DialogTitleChanged;
use danog\MadelineProto\EventHandler\Message\Service\DialogTopicCreated;
use danog\MadelineProto\EventHandler\Message\Service\DialogTopicEdited;
use danog\MadelineProto\EventHandler\Message\Service\DialogWebView;
use danog\MadelineProto\EventHandler\Message\Service\SecretChat\ActionDeleteMessages;
use danog\MadelineProto\EventHandler\Message\Service\SecretChat\ActionFlushHistory;
use danog\MadelineProto\EventHandler\Message\Service\SecretChat\ActionReadMessages;
use danog\MadelineProto\EventHandler\Message\Service\SecretChat\ActionScreenshotMessages;
use danog\MadelineProto\EventHandler\Message\Service\SecretChat\ActionSetMessageTTL;
use danog\MadelineProto\EventHandler\Message\Service\SecretChat\ActionTyping;
use danog\MadelineProto\EventHandler\Privacy;
use danog\MadelineProto\EventHandler\Query\ChatButtonQuery;
use danog\MadelineProto\EventHandler\Query\ChatGameQuery;
@ -373,7 +380,7 @@ trait UpdateHandler
{
try {
return match ($update['_']) {
'updateNewChannelMessage', 'updateNewMessage', 'updateNewScheduledMessage', 'updateEditMessage', 'updateEditChannelMessage' => $this->wrapMessage($update['message']),
'updateNewChannelMessage', 'updateNewMessage', 'updateNewScheduledMessage', 'updateEditMessage', 'updateEditChannelMessage','updateNewEncryptedMessage' => $this->wrapMessage($update['message']),
'updateBotCallbackQuery' => isset($update['game_short_name'])
? new ChatGameQuery($this, $update)
: new ChatButtonQuery($this, $update),
@ -620,6 +627,41 @@ trait UpdateHandler
$message['action']['button_id'],
$this->getIdInternal($message['action']['peer']),
),
'decryptedMessageActionSetMessageTTL' => new ActionSetMessageTTL(
$this,
$message,
$info,
$message['action']['ttl_seconds']
),
'decryptedMessageActionReadMessages' => new ActionReadMessages(
$this,
$message,
$info,
$message['action']['random_ids']
),
'decryptedMessageActionDeleteMessages' => new ActionDeleteMessages(
$this,
$message,
$info,
$message['action']['random_ids']
),
'decryptedMessageActionScreenshotMessages' => new ActionScreenshotMessages(
$this,
$message,
$info,
$message['action']['random_ids']
),
'decryptedMessageActionFlushHistory' => new ActionFlushHistory(
$this,
$message,
$info
),
'decryptedMessageActionTyping' => new ActionTyping(
$this,
$message,
$info,
Action::fromRawAction($message['action']['action'])
),
default => null
};
}