Skip to content

Commit

Permalink
Added slack rate limiting within pagination for conversations list
Browse files Browse the repository at this point in the history
  • Loading branch information
Jnesselr committed Sep 29, 2024
1 parent 53e1287 commit d7f223e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
3 changes: 3 additions & 0 deletions app/External/Slack/Api/ConversationsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 => [
Expand Down
5 changes: 5 additions & 0 deletions app/External/Slack/Api/UsersProfileApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\External\Slack\Api;

use App\External\Slack\SlackRateLimit;
use GuzzleHttp\RequestOptions;

class UsersProfileApi
Expand All @@ -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 => [
Expand All @@ -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 => [
Expand Down
7 changes: 7 additions & 0 deletions app/External/Slack/Api/ViewsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\External\Slack\Api;

use App\External\Slack\SlackRateLimit;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Facades\Log;

Expand All @@ -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 => [
Expand All @@ -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 => [
Expand All @@ -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),
Expand Down
26 changes: 15 additions & 11 deletions app/External/Slack/SlackRateLimit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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)
Expand All @@ -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";
Expand All @@ -85,6 +89,6 @@ private static function limit($tier, $name)
});
}

return new RateLimited($limit_key);
return new SlackRateLimited($limit_key);
}
}
20 changes: 20 additions & 0 deletions app/External/Slack/SlackRateLimited.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\External\Slack;

use Illuminate\Cache\RateLimiter;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Queue\Middleware\RateLimited;

class SlackRateLimited extends RateLimited
{
public function hit(): void
{
/** @var \Illuminate\Cache\RateLimiter $limiter */
$limiter = app()->make(RateLimiter::class);
/** @var Limit $limit */
$limit = $limiter->limiter($this->limiterName)();
$decayMinutes = $limit->decayMinutes;
$limiter->hit($this->limiterName, $decayMinutes);
}
}

0 comments on commit d7f223e

Please sign in to comment.