|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is a part of the DiscordPHP project. |
| 7 | + * |
| 8 | + * Copyright (c) 2015-present David Cole <[email protected]> |
| 9 | + * |
| 10 | + * This file is subject to the MIT license that is bundled |
| 11 | + * with this source code in the LICENSE.md file. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Discord\Repository\Guild; |
| 15 | + |
| 16 | +use Discord\Http\Endpoint; |
| 17 | +use Discord\Parts\Channel\Message; |
| 18 | +use Discord\Parts\Guild\GuildSearch; |
| 19 | +use Discord\Repository\AbstractRepository; |
| 20 | +use React\Promise\PromiseInterface; |
| 21 | + |
| 22 | +use function React\Promise\resolve; |
| 23 | + |
| 24 | +/** |
| 25 | + * Used only to search messages sent in a guild. |
| 26 | + * |
| 27 | + * All bots with the message content intent can use it, but it's not considered stable yet, and Discord might make changes or remove bot access if necessary. |
| 28 | + * |
| 29 | + * @see \Discord\Parts\Guild\GuildSearch |
| 30 | + * |
| 31 | + * @since 10.19.0 |
| 32 | + * |
| 33 | + * @method Message|null get(string $discrim, $key) |
| 34 | + * @method Message|null pull(string|int $key, $default = null) |
| 35 | + * @method Message|null first() |
| 36 | + * @method Message|null last() |
| 37 | + * @method Message|null find(callable $callback) |
| 38 | + * |
| 39 | + * @return PromiseInterface<static> |
| 40 | + * @throws \Exception |
| 41 | + */ |
| 42 | +class MessageRepository extends AbstractRepository |
| 43 | +{ |
| 44 | + /** |
| 45 | + * The collection discriminator. |
| 46 | + * |
| 47 | + * @var string Discriminator. |
| 48 | + */ |
| 49 | + protected $discrim = 'analytics_id'; |
| 50 | + |
| 51 | + /** |
| 52 | + * @inheritDoc |
| 53 | + */ |
| 54 | + protected $endpoints = [ |
| 55 | + 'all' => Endpoint::GUILD_MESSAGES_SEARCH, |
| 56 | + ]; |
| 57 | + |
| 58 | + /** |
| 59 | + * @inheritDoc |
| 60 | + */ |
| 61 | + protected $class = GuildSearch::class; |
| 62 | + |
| 63 | + /** |
| 64 | + * Freshens the repository cache. |
| 65 | + * |
| 66 | + * @param array $queryparams Query string params to add to the request (no validation). |
| 67 | + * Supported parameters: |
| 68 | + * - sort_by: Sorting mode. See SortingMode schema. |
| 69 | + * - sort_order: Sorting order. See SortingOrder schema. |
| 70 | + * - content: Message content to search for (string, max 1024 chars). |
| 71 | + * - slop: Integer, minimum 0, maximum 100. |
| 72 | + * - contents: Array of message contents to search for (string|null, max 1024 chars each, up to 100 items). |
| 73 | + * - author_id: Author ID (SnowflakeType|null, up to 1521 unique items). |
| 74 | + * - author_type: Author type (AuthorType, up to 1521 unique items). |
| 75 | + * - mentions: Mentioned user ID (SnowflakeType|null, up to 1521 unique items). |
| 76 | + * - mention_everyone: Boolean, whether to include messages mentioning everyone. |
| 77 | + * - min_id: Minimum message ID (SnowflakeType). |
| 78 | + * - max_id: Maximum message ID (SnowflakeType). |
| 79 | + * - limit: Integer, minimum 1, maximum 25. |
| 80 | + * - offset: Integer, minimum 0, maximum 9975. |
| 81 | + * - cursor: Cursor for pagination (ScoreCursor or TimestampCursor). |
| 82 | + * - has: Message feature (HasOption, up to 1521 unique items). |
| 83 | + * - link_hostname: Link hostname (string|null, max 152133 chars each, up to 1521 unique items). |
| 84 | + * - embed_provider: Embed providers (string|null, max 256 chars each, up to 1521 unique items). |
| 85 | + * - embed_type: Embed type (SearchableEmbedType|null, up to 1521 unique items). |
| 86 | + * - attachment_extension: Attachment extension (string|null, max 152133 chars each, up to 1521 unique items). |
| 87 | + * - attachment_filename: Attachment filename (string, max 1024 chars). |
| 88 | + * - pinned: Boolean, whether to include pinned messages. |
| 89 | + * - command_id: Command ID (SnowflakeType). |
| 90 | + * - command_name: Command name (string, max 32 chars). |
| 91 | + * - include_nsfw: Boolean, whether to include NSFW messages. |
| 92 | + * - channel_id: Channel IDs (SnowflakeType, up to 500 unique items). |
| 93 | + * |
| 94 | + * @return PromiseInterface<static> |
| 95 | + * |
| 96 | + * @throws \Exception |
| 97 | + */ |
| 98 | + public function freshen(array $queryparams = []): PromiseInterface |
| 99 | + { |
| 100 | + if (empty($queryparams)) { |
| 101 | + return resolve($this); |
| 102 | + } |
| 103 | + |
| 104 | + $endpoint = new Endpoint($this->endpoints['all']); |
| 105 | + $endpoint->bindAssoc($this->vars); |
| 106 | + |
| 107 | + foreach ($queryparams as $query => $param) { |
| 108 | + $endpoint->addQuery($query, $param); |
| 109 | + } |
| 110 | + |
| 111 | + return $this->http->get($endpoint)->then(function ($response) { |
| 112 | + $part = $this->factory->create($this->class, (array) $response, true); |
| 113 | + return $this->cacheFreshen($part); |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @param object $response |
| 119 | + * |
| 120 | + * @return PromiseInterface<static> |
| 121 | + */ |
| 122 | + protected function cacheFreshen($response): PromiseInterface |
| 123 | + { |
| 124 | + return $this->cache->set($response->{$this->discrim}, (array) $response)->then(fn ($success) => $this); |
| 125 | + } |
| 126 | +} |
0 commit comments