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 4 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 add(): bool
danog marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->getClient()->methodCallAsyncRead(
'messages.saveGif',
[
'id' => $this->botApiFileId,
'unsave' => false
]
);
}

/**
* Remove GIF from saved gifs list
*
* @return bool
*/
public function remove(): bool
{
return $this->getClient()->methodCallAsyncRead(
'messages.saveGif',
[
'id' => $this->botApiFileId,
'unsave' => true
]
);
}
}
73 changes: 73 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,69 @@ 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|null $member
danog marked this conversation as resolved.
Show resolved Hide resolved
* @return Participant
* @throws AssertionError
*/
public function getMember(string|int $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['_']}")
};
}
}
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
* @return Participant
* @throws AssertionError
*/
public function getMember(string|int $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['_']}")
};
}
}
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;

/**
* The channel participant
Copy link
Owner

Choose a reason for hiding this comment

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

Info about a channel participant

*/
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;
}
}
63 changes: 63 additions & 0 deletions src/EventHandler/Participant/Admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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;

/**
* Admin
*/
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 bool $adminRights; //!
Copy link
Owner

Choose a reason for hiding this comment

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

Not bool :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh some drug XD


/** 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'] ?? '';
Copy link
Owner

Choose a reason for hiding this comment

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

I think we can use ?? null and make the rank nullable

Copy link
Owner

Choose a reason for hiding this comment

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

Or maybe do ?? 'admin', since that's how the GUI clients do it...

$this->adminRights = new Rights\Admin($rawParticipant['admin_rights']);
}
}
53 changes: 53 additions & 0 deletions src/EventHandler/Participant/Banned.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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\MTProto;

/**
* Banned/kicked user
*/
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 bool $bannedRights; //!
Copy link
Owner

Choose a reason for hiding this comment

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

Not bool :)


/** @internal */
public function __construct(
MTProto $API,
array $rawParticipant
) {
$this->left = $rawParticipant['left'];
$this->peer = $API->getId($rawParticipant['peer']);
Copy link
Owner

Choose a reason for hiding this comment

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

You can use getIdInternal here, it's faster

$this->kickedBy = $rawParticipant['kicked_by'];
$this->date = $rawParticipant['date'];
$this->bannedRights = new Rights\Banned($rawParticipant['banned_rights']);
}
}
43 changes: 43 additions & 0 deletions src/EventHandler/Participant/Creator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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;

/**
* Channel/supergroup creator
*/
class Creator extends Participant
{
/** User ID */
public readonly int $userId;

/** Creator admin rights */
public readonly int $adminRights; //!
Copy link
Owner

Choose a reason for hiding this comment

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

Not an int :)


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

/** @internal */
public function __construct(
array $rawParticipant
) {
$this->userId = $rawParticipant['user_id'];
$this->adminRights = new Rights\Admin($rawParticipant['adminRights']);
$this->rank = $rawParticipant['rank'] ?? '';
Copy link
Owner

Choose a reason for hiding this comment

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

?? 'admin', probably

}
}
Loading