Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Some Bounded methods #1395

Merged
merged 27 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/EventHandler/Media/Gif.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,36 @@ public function __construct(
}
$this->hasStickers = $hasStickers;
}

/**
* Add GIF to saved gifs list
*
* @return bool
*/
public function save(): bool
{
return $this->getClient()->methodCallAsyncRead(
'messages.saveGif',
[
'id' => $this->botApiFileId,
'unsave' => false
]
);
}

/**
* Remove GIF from saved gifs list
*
* @return bool
*/
public function unsave(): bool
{
return $this->getClient()->methodCallAsyncRead(
'messages.saveGif',
[
'id' => $this->botApiFileId,
'unsave' => true
]
);
}
}
72 changes: 72 additions & 0 deletions src/EventHandler/Message/ChannelMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@

namespace danog\MadelineProto\EventHandler\Message;

use AssertionError;
use danog\MadelineProto\EventHandler\Message;
use danog\MadelineProto\MTProto;
use danog\MadelineProto\EventHandler\Participant;
use danog\MadelineProto\EventHandler\Participant\Left;
use danog\MadelineProto\EventHandler\Participant\Admin;
use danog\MadelineProto\EventHandler\Participant\Member;
use danog\MadelineProto\EventHandler\Participant\MySelf;
use danog\MadelineProto\EventHandler\Participant\Banned;
use danog\MadelineProto\EventHandler\Participant\Creator;

/**
* Represents an incoming or outgoing channel message.
Expand All @@ -32,4 +40,68 @@ public function __construct(
) {
parent::__construct($API, $rawMessage, $info);
}

/**
* Disable message signatures in channels
*
* @return boolean
*/
public function disableSignatures(): bool
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can return void in both

{
$this->getClient()->methodCallAsyncRead(
'channels.toggleSignatures',
[
'channel' => $this->chatId,
'enabled' => false,
]
);
return true;
}

/**
* Enable message signatures in channels
*
* @return boolean
*/
public function enableSignatures(): bool
{
$this->getClient()->methodCallAsyncRead(
'channels.toggleSignatures',
[
'channel' => $this->chatId,
'enabled' => true,
]
);
return true;
}

/**
* Get info about a [channel/supergroup](https://core.telegram.org/api/channel) participant
*
* @param string|integer $member Participant to get info about.
* @return Participant
* @throws AssertionError
*/
public function getMember(string|int $member): Participant
{
$client = $this->getClient();
$member = $client->getId($member);
$result = $client->methodCallAsyncRead(
'channels.getParticipant',
[
'channel' => $this->chatId,
'participant' => $member,
]
)['participant'];

return match ($result['_']) {
'channelParticipant' => new Member($result),
'channelParticipantLeft' => new Left($client, $result),
'channelParticipantSelf' => new MySelf($result),
'channelParticipantAdmin' => new Admin($result),
'channelParticipantBanned' => new Banned($client, $result),
'channelParticipantCreator' => new Creator($result),
default => throw new AssertionError("undefined Participant type: {$result['_']}")
};
}
}
39 changes: 38 additions & 1 deletion src/EventHandler/Message/GroupMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,48 @@

namespace danog\MadelineProto\EventHandler\Message;

use AssertionError;
use danog\MadelineProto\EventHandler\Message;

use danog\MadelineProto\EventHandler\Participant;
use danog\MadelineProto\EventHandler\Participant\Left;
use danog\MadelineProto\EventHandler\Participant\Admin;
use danog\MadelineProto\EventHandler\Participant\Member;
use danog\MadelineProto\EventHandler\Participant\MySelf;
use danog\MadelineProto\EventHandler\Participant\Banned;
use danog\MadelineProto\EventHandler\Participant\Creator;
/**
* Represents an incoming or outgoing group message.
*/
final class GroupMessage extends Message
{
/**
* Get info about a [channel/supergroup](https://core.telegram.org/api/channel) participant
*
* @param string|integer|null $member Participant to get info about; can be empty or null to get info about the sender of the message.
* @return Participant
* @throws AssertionError
*/
public function getMember(string|int|null $member = null): Participant
{
$client = $this->getClient();
$member ??= $this->senderId;
$member = $client->getId($member);
$result = $client->methodCallAsyncRead(
'channels.getParticipant',
[
'channel' => $this->chatId,
'participant' => $member,
]
)['participant'];

return match ($result['_']) {
'channelParticipant' => new Member($result),
'channelParticipantLeft' => new Left($client, $result),
'channelParticipantSelf' => new MySelf($result),
'channelParticipantAdmin' => new Admin($result),
'channelParticipantBanned' => new Banned($client, $result),
'channelParticipantCreator' => new Creator($result),
default => throw new AssertionError("undefined Participant type: {$result['_']}")
};
}
}
17 changes: 17 additions & 0 deletions src/EventHandler/Message/PrivateMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,27 @@
namespace danog\MadelineProto\EventHandler\Message;

use danog\MadelineProto\EventHandler\Message;
use danog\MadelineProto\EventHandler\Message\Service\DialogScreenshotTaken;

/**
* Represents an incoming or outgoing private message.
*/
final class PrivateMessage extends Message
{
/**
* Notify the other user in a private chat that a screenshot of the chat was taken
*
* @return DialogScreenshotTaken
*/
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));
}
}
34 changes: 34 additions & 0 deletions src/EventHandler/Message/Service/DialogScreenshotTaken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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 Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2023 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/

namespace danog\MadelineProto\EventHandler\Message\Service;

use danog\MadelineProto\EventHandler\Message\ServiceMessage;
use danog\MadelineProto\MTProto;

/**
* A message was pinned in a chat.
*/
final class DialogScreenshotTaken extends ServiceMessage
{
public function __construct(
MTProto $API,
array $rawMessage,
array $info,
) {
parent::__construct($API, $rawMessage, $info);
}
}
39 changes: 39 additions & 0 deletions src/EventHandler/Participant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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 Daniil Gentili <daniil@daniil.it>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW you can put yourself as author on the files you made :)

* @copyright 2016-2023 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/

namespace danog\MadelineProto\EventHandler;

use danog\MadelineProto\MTProto;
use JsonSerializable;
use ReflectionClass;
use ReflectionProperty;

/**
* Info about a channel participant
*/
abstract class Participant implements JsonSerializable
{
/** @internal */
public function jsonSerialize(): mixed
{
$res = ['_' => static::class];
$refl = new ReflectionClass($this);
foreach ($refl->getProperties(ReflectionProperty::IS_PUBLIC) as $prop) {
$res[$prop->getName()] = $prop->getValue($this);
}
return $res;
}
}
64 changes: 64 additions & 0 deletions src/EventHandler/Participant/Admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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 Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2023 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/

namespace danog\MadelineProto\EventHandler\Participant;

use danog\MadelineProto\EventHandler\Participant;
use danog\MadelineProto\EventHandler\Participant\Rights\Admin as AdminRights;

/**
* Admin
*/
final class Admin extends Participant
{
/** Can this admin promote other admins with the same permissions? */
public readonly bool $canEdit;

/** Is this the current user */
public readonly bool $self;

/** Admin user ID */
public readonly int $userId;

/** User that invited the admin to the channel/group */
public readonly ?int $inviterId;

/** User that promoted the user to admin */
public readonly int $promotedBy;

/** When did the user join */
public readonly int $date;

/** Admin [rights](https://core.telegram.org/api/rights) */
public readonly AdminRights $adminRights;

/** The role (rank) of the admin in the group: just an arbitrary string, `admin` by default */
public readonly string $rank;

/** @internal */
public function __construct(
array $rawParticipant
) {
$this->canEdit = $rawParticipant['can_edit'];
$this->self = $rawParticipant['self'];
$this->userId = $rawParticipant['user_id'];
$this->inviterId = $rawParticipant['inviter_id'] ?? null;
$this->promotedBy = $rawParticipant['promoted_by'];
$this->date = $rawParticipant['date'];
$this->rank = $rawParticipant['rank'] ?? 'admin';
$this->adminRights = new AdminRights($rawParticipant['admin_rights']);
}
}
54 changes: 54 additions & 0 deletions src/EventHandler/Participant/Banned.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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 Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2023 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/

namespace danog\MadelineProto\EventHandler\Participant;

use danog\MadelineProto\EventHandler\Participant;
use danog\MadelineProto\EventHandler\Participant\Rights\Banned as BannedRights;
use danog\MadelineProto\MTProto;

/**
* Banned/kicked user
*/
final class Banned extends Participant
{
/** Whether the user has left the group */
public readonly bool $left;

/** The banned peer */
public readonly int $peer;

/** User was kicked by the specified admin */
public readonly int $kickedBy;

/** When did the user join the group */
public readonly ?int $date;

/** Banned [rights](https://core.telegram.org/api/rights) */
public readonly BannedRights $bannedRights;

/** @internal */
public function __construct(
MTProto $API,
array $rawParticipant
) {
$this->left = $rawParticipant['left'];
$this->peer = $API->getIdInternal ($rawParticipant['peer']);
$this->kickedBy = $rawParticipant['kicked_by'];
$this->date = $rawParticipant['date'];
$this->bannedRights = new BannedRights($rawParticipant['banned_rights']);
}
}
Loading