diff --git a/app/External/Slack/Api/ConversationsApi.php b/app/External/Slack/Api/ConversationsApi.php index e59fa1ea..c7d10a4f 100644 --- a/app/External/Slack/Api/ConversationsApi.php +++ b/app/External/Slack/Api/ConversationsApi.php @@ -3,6 +3,7 @@ namespace App\External\Slack\Api; use App\External\Slack\SlackApi; +use App\External\Slack\SlackRateLimit; use GuzzleHttp\RequestOptions; use Illuminate\Support\Arr; use Illuminate\Support\Collection; @@ -58,6 +59,8 @@ public function list(...$types): Collection $typeString = implode(',', $types); return $this->paginate('channels', function ($cursor) use ($typeString) { + SlackRateLimit::conversations_list()->hit(); + return $this->clients->managementApiClient ->get('https://denhac.slack.com/api/conversations.list', [ RequestOptions::QUERY => [ diff --git a/app/External/Slack/Api/UsersProfileApi.php b/app/External/Slack/Api/UsersProfileApi.php index 1370c464..fc5d473e 100644 --- a/app/External/Slack/Api/UsersProfileApi.php +++ b/app/External/Slack/Api/UsersProfileApi.php @@ -2,6 +2,7 @@ namespace App\External\Slack\Api; +use App\External\Slack\SlackRateLimit; use GuzzleHttp\RequestOptions; class UsersProfileApi @@ -17,6 +18,8 @@ public function __construct(SlackClients $clients) public function set($user_id, $profile) { + SlackRateLimit::users_profile_set()->hit(); + return $this->clients->adminClient ->post('https://denhac.slack.com/api/users.profile.set', [ RequestOptions::JSON => [ @@ -28,6 +31,8 @@ public function set($user_id, $profile) public function get($user_id) { + SlackRateLimit::users_profile_get()->hit(); + $response = $this->clients->adminClient ->get('https://denhac.slack.com/api/users.profile.get', [ RequestOptions::QUERY => [ diff --git a/app/External/Slack/Api/ViewsApi.php b/app/External/Slack/Api/ViewsApi.php index c4c114a5..747f946a 100644 --- a/app/External/Slack/Api/ViewsApi.php +++ b/app/External/Slack/Api/ViewsApi.php @@ -2,6 +2,7 @@ namespace App\External\Slack\Api; +use App\External\Slack\SlackRateLimit; use GuzzleHttp\RequestOptions; use Illuminate\Support\Facades\Log; @@ -18,6 +19,8 @@ public function __construct(SlackClients $clients) public function open($trigger_id, $view) { + SlackRateLimit::views_open()->hit(); + return $this->clients->spaceBotApiClient ->post('https://denhac.slack.com/api/views.open', [ RequestOptions::JSON => [ @@ -29,6 +32,8 @@ public function open($trigger_id, $view) public function publish($user_id, $view) { + SlackRateLimit::views_publish()->hit(); + $this->clients->spaceBotApiClient ->post('https://denhac.slack.com/api/views.publish', [ RequestOptions::JSON => [ @@ -40,6 +45,8 @@ public function publish($user_id, $view) public function update($view_id, $view, $hash = null) { + SlackRateLimit::views_update()->hit(); + $data = [ 'view_id' => $view_id, 'view' => json_encode($view), diff --git a/app/External/Slack/SlackRateLimit.php b/app/External/Slack/SlackRateLimit.php index 081a24bf..a1531c52 100644 --- a/app/External/Slack/SlackRateLimit.php +++ b/app/External/Slack/SlackRateLimit.php @@ -10,18 +10,20 @@ /** * @method static SlackPostMessageRateLimit chat_postMessage() * - * @method static RateLimited conversations_invite() - * @method static RateLimited conversations_join() - * @method static RateLimited conversations_kick() - * @method static RateLimited conversations_list() + * @method static SlackRateLimited conversations_invite() + * @method static SlackRateLimited conversations_join() + * @method static SlackRateLimited conversations_kick() + * @method static SlackRateLimited conversations_list() * - * @method static RateLimited usergroups_list() - * @method static RateLimited usergroups_update() + * @method static SlackRateLimited usergroups_list() + * @method static SlackRateLimited usergroups_update() * - * @method static RateLimited users_profile_get() - * @method static RateLimited users_profile_set() + * @method static SlackRateLimited users_profile_get() + * @method static SlackRateLimited users_profile_set() * - * @method static RateLimited views_publish() + * @method static SlackRateLimited views_open(); + * @method static SlackRateLimited views_publish() + * @method static SlackRateLimited views_update() */ class SlackRateLimit { @@ -48,7 +50,9 @@ class SlackRateLimit 'usergroups_update' => self::TIER_2, 'users_profile_get' => self::TIER_4, 'users_profile_set' => self::TIER_3, + 'views_open' => self::TIER_4, 'views_publish' => self::TIER_4, + 'views_update' => self::TIER_4, ]; public static function __callStatic(string $name, array $arguments) @@ -70,7 +74,7 @@ public static function __callStatic(string $name, array $arguments) return self::limit($tier, $name); } - private static function limit($tier, $name) + private static function limit($tier, $name): SlackRateLimited { if (is_null($name)) { $limit_key = "slack-$tier"; @@ -85,6 +89,6 @@ private static function limit($tier, $name) }); } - return new RateLimited($limit_key); + return new SlackRateLimited($limit_key); } } diff --git a/app/External/Slack/SlackRateLimited.php b/app/External/Slack/SlackRateLimited.php new file mode 100644 index 00000000..daa28e07 --- /dev/null +++ b/app/External/Slack/SlackRateLimited.php @@ -0,0 +1,20 @@ +make(RateLimiter::class); + /** @var Limit $limit */ + $limit = $limiter->limiter($this->limiterName)(); + $decayMinutes = $limit->decayMinutes; + $limiter->hit($this->limiterName, $decayMinutes); + } +}