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

Simplify structure

This commit is contained in:
Daniil Gentili 2023-07-06 21:39:20 +02:00
parent f1c28fabba
commit 5e1b6cda10
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
23 changed files with 143 additions and 365 deletions

View File

@ -1,89 +0,0 @@
<?php declare(strict_types=1);
namespace danog\MadelineProto\EventHandler;
use danog\MadelineProto\MTProto;
/**
* Represents an incoming or outgoing message.
*/
abstract class AbstractMessage extends Update
{
/** Message ID */
public readonly int $id;
/** ID of the chat where the message was sent */
public readonly int $chatId;
/** ID of the message to which this message is replying */
public readonly ?int $replyToMsgId;
/** When was the message sent */
public readonly int $date;
/** ID of the forum topic where the message was sent */
public readonly ?int $topicId;
/** ID of the message thread where the message was sent */
public readonly ?int $threadId;
/** Whether this is a reply to a scheduled message */
public readonly bool $replyToScheduled;
/** Whether we were mentioned in this message */
public readonly bool $mentioned;
/** Whether this message was sent without any notification (silently) */
public readonly bool $silent;
/** Time-to-live of the message */
public readonly ?int $ttlPeriod;
// Todo media (waveform, photosizes, thumbs), albums, reactions, replies, games eventually, service messages
/** @internal */
public function __construct(
MTProto $API,
array $rawMessage,
/** Whether the message is outgoing */
public readonly bool $out
) {
parent::__construct($API);
$info = $this->API->getInfo($rawMessage);
$this->id = $rawMessage['id'];
$this->chatId = $info['bot_api_id'];
$this->date = $rawMessage['date'];
$this->mentioned = $rawMessage['mentioned'];
$this->silent = $rawMessage['silent'];
$this->ttlPeriod = $rawMessage['ttl_period'] ?? null;
if (isset($rawMessage['reply_to'])) {
$replyTo = $rawMessage['reply_to'];
$this->replyToScheduled = $replyTo['reply_to_scheduled'];
if ($replyTo['forum_topic']) {
if (isset($replyTo['reply_to_top_id'])) {
$this->topicId = $replyTo['reply_to_top_id'];
$this->replyToMsgId = $replyTo['reply_to_msg_id'];
} else {
$this->topicId = $replyTo['reply_to_msg_id'];
$this->replyToMsgId = null;
}
$this->threadId = null;
} elseif ($info['Chat']['forum'] ?? false) {
$this->topicId = 1;
$this->replyToMsgId = $replyTo['reply_to_msg_id'];
$this->threadId = $replyTo['reply_to_top_id'] ?? null;
} else {
$this->topicId = null;
$this->replyToMsgId = $replyTo['reply_to_msg_id'];
$this->threadId = $replyTo['reply_to_top_id'] ?? null;
}
} elseif ($info['Chat']['forum'] ?? false) {
$this->topicId = 1;
$this->replyToMsgId = null;
$this->threadId = null;
$this->replyToScheduled = false;
} else {
$this->topicId = null;
$this->replyToMsgId = null;
$this->threadId = null;
$this->replyToScheduled = false;
}
}
}

View File

@ -1,19 +0,0 @@
<?php declare(strict_types=1);
namespace danog\MadelineProto\EventHandler\Filter\Media;
use danog\MadelineProto\EventHandler\Filter\Filter;
use danog\MadelineProto\EventHandler\MessageService;
use danog\MadelineProto\EventHandler\Service\DialogMemberLeft;
use danog\MadelineProto\EventHandler\Update;
/**
* Filter that only matches service messages about left users.
*/
final class FilterMemberLeft extends Filter
{
public function apply(Update $update): bool
{
return $update instanceof MessageService && $update->serviceInfo instanceof DialogMemberLeft;
}
}

View File

@ -1,19 +0,0 @@
<?php declare(strict_types=1);
namespace danog\MadelineProto\EventHandler\Filter\Media;
use danog\MadelineProto\EventHandler\Filter\Filter;
use danog\MadelineProto\EventHandler\MessageService;
use danog\MadelineProto\EventHandler\Service\DialogMembersJoined;
use danog\MadelineProto\EventHandler\Update;
/**
* Filter that only matches service messages about joined users.
*/
final class FilterMembersJoined extends Filter
{
public function apply(Update $update): bool
{
return $update instanceof MessageService && $update->serviceInfo instanceof DialogMembersJoined;
}
}

View File

@ -20,14 +20,28 @@ use danog\MadelineProto\StrTools;
/**
* Represents an incoming or outgoing message.
*/
abstract class Message extends AbstractMessage
abstract class Message extends Update
{
/** Message ID */
public readonly int $id;
/** Content of the message */
public readonly string $message;
/** ID of the chat where the message was sent */
public readonly int $chatId;
/** ID of the message to which this message is replying */
public readonly ?int $replyToMsgId;
/** When was the message sent */
public readonly int $date;
/** Info about a forwarded message */
public readonly ?ForwardedInfo $fwdInfo;
/** ID of the forum topic where the message was sent */
public readonly ?int $topicId;
/** ID of the message thread where the message was sent */
public readonly ?int $threadId;
/** Bot command (if present) */
public readonly ?string $command;
/** @var list<string> Bot command arguments (if present) */
@ -47,6 +61,12 @@ abstract class Message extends AbstractMessage
*/
public readonly ?Media $media;
/** Whether this is a reply to a scheduled message */
public readonly bool $replyToScheduled;
/** Whether we were mentioned in this message */
public readonly bool $mentioned;
/** Whether this message was sent without any notification (silently) */
public readonly bool $silent;
/** Whether this message is a sent scheduled message */
public readonly bool $fromScheduled;
/** Whether this message is a pinned message */
@ -59,6 +79,9 @@ abstract class Message extends AbstractMessage
/** Last edit date of the message */
public readonly ?int $editDate;
/** Time-to-live of the message */
public readonly ?int $ttlPeriod;
/** Inline or reply keyboard. */
public readonly InlineKeyboard|ReplyKeyboard|null $keyboard;
@ -74,22 +97,63 @@ abstract class Message extends AbstractMessage
public function __construct(
MTProto $API,
array $rawMessage,
bool $out
/** Whether the message is outgoing */
public readonly bool $out
) {
parent::__construct($API, $rawMessage, $out);
parent::__construct($API);
$info = $this->API->getInfo($rawMessage);
$this->entities = $rawMessage['entities'] ?? null;
$this->id = $rawMessage['id'];
$this->message = $rawMessage['message'] ?? '';
$this->chatId = $info['bot_api_id'];
$this->date = $rawMessage['date'];
$this->mentioned = $rawMessage['mentioned'];
$this->silent = $rawMessage['silent'];
$this->fromScheduled = $rawMessage['from_scheduled'];
$this->pinned = $rawMessage['pinned'] ?? false;
$this->protected = $rawMessage['noforwards'] ?? false;
$this->viaBotId = $rawMessage['via_bot_id'] ?? null;
$this->editDate = $rawMessage['edit_date'] ?? null;
$this->ttlPeriod = $rawMessage['ttl_period'] ?? null;
$this->keyboard = isset($rawMessage['reply_markup'])
? Keyboard::fromRawReplyMarkup($rawMessage['reply_markup'])
: null;
if (isset($rawMessage['reply_to'])) {
$replyTo = $rawMessage['reply_to'];
$this->replyToScheduled = $replyTo['reply_to_scheduled'];
if ($replyTo['forum_topic']) {
if (isset($replyTo['reply_to_top_id'])) {
$this->topicId = $replyTo['reply_to_top_id'];
$this->replyToMsgId = $replyTo['reply_to_msg_id'];
} else {
$this->topicId = $replyTo['reply_to_msg_id'];
$this->replyToMsgId = null;
}
$this->threadId = null;
} elseif ($info['Chat']['forum'] ?? false) {
$this->topicId = 1;
$this->replyToMsgId = $replyTo['reply_to_msg_id'];
$this->threadId = $replyTo['reply_to_top_id'] ?? null;
} else {
$this->topicId = null;
$this->replyToMsgId = $replyTo['reply_to_msg_id'];
$this->threadId = $replyTo['reply_to_top_id'] ?? null;
}
} elseif ($info['Chat']['forum'] ?? false) {
$this->topicId = 1;
$this->replyToMsgId = null;
$this->threadId = null;
$this->replyToScheduled = false;
} else {
$this->topicId = null;
$this->replyToMsgId = null;
$this->threadId = null;
$this->replyToScheduled = false;
}
if (isset($rawMessage['fwd_from'])) {
$fwdFrom = $rawMessage['fwd_from'];
$this->fwdInfo = new ForwardedInfo(

View File

@ -1,22 +0,0 @@
<?php declare(strict_types=1);
namespace danog\MadelineProto\EventHandler;
use danog\MadelineProto\MTProto;
/**
* Represents an incoming or outgoing service message.
*/
abstract class MessageService extends AbstractMessage
{
/** @internal */
public function __construct(
MTProto $API,
array $rawMessage,
/** Service message information */
public readonly Service $serviceInfo,
bool $out
) {
parent::__construct($API, $rawMessage, $out);
}
}

View File

@ -1,21 +0,0 @@
<?php declare(strict_types=1);
namespace danog\MadelineProto\EventHandler\MessageService;
use danog\MadelineProto\EventHandler\MessageService;
use danog\MadelineProto\MTProto;
/**
* Represents an incoming or outgoing channel service message.
*/
abstract class ChannelMessageService extends MessageService
{
/** @internal */
protected function __construct(
MTProto $API,
array $rawMessage,
bool $out
) {
parent::__construct($API, $rawMessage, $out);
}
}

View File

@ -1,28 +0,0 @@
<?php declare(strict_types=1);
namespace danog\MadelineProto\EventHandler\MessageService;
use danog\MadelineProto\EventHandler\MessageService;
use danog\MadelineProto\MTProto;
/**
* Represents an incoming or outgoing group service message.
*/
abstract class GroupMessageService extends MessageService
{
/** ID of the sender of the message (equal to the chatId for anonymous admins) */
public readonly int $senderId;
/** @internal */
protected function __construct(
MTProto $API,
array $rawMessage,
bool $outgoing
) {
parent::__construct($API, $rawMessage, $outgoing);
$this->senderId = isset($rawMessage['from_id'])
? $this->API->getIdInternal($rawMessage['from_id'])
: $this->chatId;
}
}

View File

@ -1,20 +0,0 @@
<?php declare(strict_types=1);
namespace danog\MadelineProto\EventHandler\MessageService;
use danog\MadelineProto\EventHandler\BasicFilter\Incoming;
use danog\MadelineProto\MTProto;
/**
* Represents an incoming channel service message.
*/
final class IncomingChannelMessageService extends ChannelMessageService implements Incoming
{
/** @internal */
public function __construct(
MTProto $API,
array $rawMessage
) {
parent::__construct($API, $rawMessage, false);
}
}

View File

@ -1,20 +0,0 @@
<?php declare(strict_types=1);
namespace danog\MadelineProto\EventHandler\MessageService;
use danog\MadelineProto\EventHandler\BasicFilter\Incoming;
use danog\MadelineProto\MTProto;
/**
* Represents an incoming group service message.
*/
final class IncomingGroupMessageService extends GroupMessageService implements Incoming
{
/** @internal */
public function __construct(
MTProto $API,
array $rawMessage
) {
parent::__construct($API, $rawMessage, false);
}
}

View File

@ -1,20 +0,0 @@
<?php declare(strict_types=1);
namespace danog\MadelineProto\EventHandler\MessageService;
use danog\MadelineProto\EventHandler\BasicFilter\Incoming;
use danog\MadelineProto\MTProto;
/**
* Represents an incoming private service message.
*/
final class IncomingPrivateMessageService extends PrivateMessageService implements Incoming
{
/** @internal */
public function __construct(
MTProto $API,
array $rawMessage
) {
parent::__construct($API, $rawMessage, false);
}
}

View File

@ -1,20 +0,0 @@
<?php declare(strict_types=1);
namespace danog\MadelineProto\EventHandler\MessageService;
use danog\MadelineProto\EventHandler\BasicFilter\Outgoing;
use danog\MadelineProto\MTProto;
/**
* Represents an outgoing channel service message.
*/
final class OutgoingChannelMessageService extends ChannelMessageService implements Outgoing
{
/** @internal */
public function __construct(
MTProto $API,
array $rawMessage
) {
parent::__construct($API, $rawMessage, true);
}
}

View File

@ -1,20 +0,0 @@
<?php declare(strict_types=1);
namespace danog\MadelineProto\EventHandler\MessageService;
use danog\MadelineProto\EventHandler\BasicFilter\Outgoing;
use danog\MadelineProto\MTProto;
/**
* Represents an outgoing group service message.
*/
final class OutgoingGroupMessageService extends GroupMessageService implements Outgoing
{
/** @internal */
public function __construct(
MTProto $API,
array $rawMessage
) {
parent::__construct($API, $rawMessage, true);
}
}

View File

@ -1,20 +0,0 @@
<?php declare(strict_types=1);
namespace danog\MadelineProto\EventHandler\MessageService;
use danog\MadelineProto\EventHandler\BasicFilter\Outgoing;
use danog\MadelineProto\MTProto;
/**
* Represents an outgoing private service message.
*/
final class OutgoingPrivateMessageService extends PrivateMessageService implements Outgoing
{
/** @internal */
public function __construct(
MTProto $API,
array $rawMessage
) {
parent::__construct($API, $rawMessage, true);
}
}

View File

@ -1,12 +0,0 @@
<?php declare(strict_types=1);
namespace danog\MadelineProto\EventHandler\MessageService;
use danog\MadelineProto\EventHandler\MessageService;
/**
* Represents an incoming or outgoing private service message.
*/
abstract class PrivateMessageService extends MessageService
{
}

View File

@ -5,6 +5,6 @@ namespace danog\MadelineProto\EventHandler;
/**
* Represents info about a service message.
*/
abstract class Service
abstract class Service extends Update
{
}

View File

@ -9,9 +9,11 @@ use danog\MadelineProto\EventHandler\Service;
*/
final class DialogCreated extends Service
{
/** Title of the created chat or channel */
public readonly string $title;
/** @var list<int> List of group members */
public readonly array $users;
public function __construct(
/** Title of the created chat or channel */
public readonly string $title,
/** @var list<int> List of group members */
public readonly array $users,
) {
}
}

View File

@ -9,6 +9,9 @@ use danog\MadelineProto\EventHandler\Service;
*/
final class DialogMemberLeft extends Service
{
/** ID of the user that left the channel */
public readonly int $left;
public function __construct(
/** ID of the user that left the channel */
public readonly int $left,
) {
}
}

View File

@ -9,6 +9,9 @@ use danog\MadelineProto\EventHandler\Service;
*/
final class DialogMembersJoined extends Service
{
/** @var list<int> List of IDs of the user that joined the chat or channel. */
public readonly array $joined;
public function __construct(
/** @var list<int> List of IDs of the user that joined the chat or channel. */
public readonly array $joined
) {
}
}

View File

@ -10,6 +10,9 @@ use danog\MadelineProto\EventHandler\Service;
*/
final class DialogPhotoChanged extends Service
{
/** New photo (or no photo if it was deleted) */
public readonly ?Photo $photo;
public function __construct(
/** New photo (or no photo if it was deleted) */
public readonly ?Photo $photo
) {
}
}

View File

@ -9,6 +9,9 @@ use danog\MadelineProto\EventHandler\Service;
*/
final class DialogTitleChanged extends Service
{
/** New title */
public readonly string $title;
public function __construct(
/** New title */
public readonly string $title
) {
}
}

View File

@ -1652,7 +1652,9 @@ final class MTProto implements TLCallback, LoggerGetter
$file = null;
if ($this->settings->getLogger()->getType() === Logger::FILE_LOGGER
&& $path = $this->settings->getLogger()->getExtra()) {
\touch($path);
$temp = \tempnam(\sys_get_temp_dir(), 'madelinelog');
\copy($path, $temp);
$path = $temp;
if (!getSize($path)) {
$message = "!!! WARNING !!!\nThe logfile is empty, please DO NOT delete the logfile to avoid errors in MadelineProto!\n\n$message";
} else {
@ -1669,6 +1671,7 @@ final class MTProto implements TLCallback, LoggerGetter
],
],
);
\unlink($path);
}
}
$sent = false;

View File

@ -151,6 +151,7 @@ trait Files
$part_size = 512 * 1024;
$parallel_chunks = $this->settings->getFiles()->getUploadParallelChunks();
$part_total_num = (int) \ceil($size / $part_size);
Assert::notEq($part_total_num, 0);
$part_num = 0;
$method = $size > 10 * 1024 * 1024 ? 'upload.saveBigFilePart' : 'upload.saveFilePart';
$constructor = 'input'.($encrypted === true ? 'Encrypted' : '').($size > 10 * 1024 * 1024 ? 'FileBig' : 'File').($encrypted === true ? 'Uploaded' : '');

View File

@ -26,6 +26,7 @@ use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use Amp\TimeoutCancellation;
use Amp\TimeoutException;
use danog\MadelineProto\EventHandler\AbstractMessage;
use danog\MadelineProto\EventHandler\Message;
use danog\MadelineProto\EventHandler\Message\IncomingChannelMessage;
use danog\MadelineProto\EventHandler\Message\IncomingGroupMessage;
@ -33,6 +34,11 @@ use danog\MadelineProto\EventHandler\Message\IncomingPrivateMessage;
use danog\MadelineProto\EventHandler\Message\OutgoingChannelMessage;
use danog\MadelineProto\EventHandler\Message\OutgoingGroupMessage;
use danog\MadelineProto\EventHandler\Message\OutgoingPrivateMessage;
use danog\MadelineProto\EventHandler\Service\DialogCreated;
use danog\MadelineProto\EventHandler\Service\DialogMemberLeft;
use danog\MadelineProto\EventHandler\Service\DialogMembersJoined;
use danog\MadelineProto\EventHandler\Service\DialogPhotoChanged;
use danog\MadelineProto\EventHandler\Service\DialogTitleChanged;
use danog\MadelineProto\EventHandler\Update;
use danog\MadelineProto\Exception;
use danog\MadelineProto\Lang;
@ -324,26 +330,46 @@ trait UpdateHandler
/**
* Wrap a Message constructor into an abstract Message object.
*/
public function wrapMessage(array $message): ?Message
public function wrapMessage(array $message): ?AbstractMessage
{
if ($message['_'] !== 'message') {
return null;
if ($message['_'] === 'message') {
$id = $this->getIdInternal($message);
if (($this->chats[$id]['username'] ?? '') === 'replies') {
return null;
}
return match ($this->getType($id)) {
\danog\MadelineProto\API::PEER_TYPE_BOT, \danog\MadelineProto\API::PEER_TYPE_USER => $message['out']
? new OutgoingPrivateMessage($this, $message)
: new IncomingPrivateMessage($this, $message),
\danog\MadelineProto\API::PEER_TYPE_GROUP, \danog\MadelineProto\API::PEER_TYPE_SUPERGROUP => $message['out']
? new OutgoingGroupMessage($this, $message)
: new IncomingGroupMessage($this, $message),
\danog\MadelineProto\API::PEER_TYPE_CHANNEL => $message['out']
? new OutgoingChannelMessage($this, $message)
: new IncomingChannelMessage($this, $message),
};
}
$id = $this->getIdInternal($message);
if (($this->chats[$id]['username'] ?? '') === 'replies') {
return null;
if ($message['_'] === 'messageService') {
return match ($message['action']['_']) {
'messageActionChatCreate' => new DialogCreated(
$message['action']['title'],
$message['action']['users'],
),
'messageActionChatEditTitle' => new DialogTitleChanged(
$message['action']['title']
),
'messageActionChatEditPhoto' => new DialogPhotoChanged(
$this->wrapMedia($message['action']['photo'])
),
'messageActionChatDeletePhoto' => new DialogPhotoChanged(null),
'messageActionChatAddUser' => new DialogMembersJoined(
$message['action']['users']
),
'messageActionChatDeleteUser' => new DialogMemberLeft($message['action']['user_id']),
default => null
};
}
return match ($this->getType($id)) {
\danog\MadelineProto\API::PEER_TYPE_BOT, \danog\MadelineProto\API::PEER_TYPE_USER => $message['out']
? new OutgoingPrivateMessage($this, $message)
: new IncomingPrivateMessage($this, $message),
\danog\MadelineProto\API::PEER_TYPE_GROUP, \danog\MadelineProto\API::PEER_TYPE_SUPERGROUP => $message['out']
? new OutgoingGroupMessage($this, $message)
: new IncomingGroupMessage($this, $message),
\danog\MadelineProto\API::PEER_TYPE_CHANNEL => $message['out']
? new OutgoingChannelMessage($this, $message)
: new IncomingChannelMessage($this, $message),
};
return null;
}
/**
* Extract a message ID from an Updates constructor.