Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ VITE_SHOW_LOGOUT="${USE_ZANICHELLI_IDP}"

IDP_BASE_URL=
IDP_COOKIE_NAME=
SUBSCRIBER_PAUSED_TTL=

TELESCOPE_ENABLED=true
TELESCOPE_CACHE_WATCHER=true
Expand Down
1 change: 1 addition & 0 deletions ansible/roles/deploy-buzzer/templates/.env.j2
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ VITE_SHOW_LOGOUT="${USE_ZANICHELLI_IDP}"

IDP_BASE_URL=https://{{ idp_url }}
IDP_COOKIE_NAME={{ idp_cookie_name }}
SUBSCRIBER_PAUSED_TTL=3600

TELESCOPE_ENABLED=false
TELESCOPE_CACHE_WATCHER=true
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/PublisherController.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public function destroy(int $id)
*/
public function getPublisher($id)
{
$publisher = $this->publisherRepository->find($id);;
$publisher = $this->publisherRepository->find($id);
if (!$publisher) {
return response()->error404(__('messages.Publisher') . $id);
}
Expand Down
90 changes: 89 additions & 1 deletion app/Http/Controllers/SubscriberController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use App\Http\Requests\SubscriberRequest;
use App\Http\Resources\SubscriberResource;
use App\Http\Repositories\RepositoryInterface;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;

class SubscriberController extends Controller
{
Expand Down Expand Up @@ -226,10 +228,96 @@ public function destroy(int $id)
*/
public function getSubscriber($id)
{
$subscriber = $this->subscriberRepository->find($id);;
$subscriber = $this->subscriberRepository->find($id);
if (!$subscriber) {
return response()->error404(__('messages.Subscriber') . $id);
}
return new SubscriberResource($subscriber);
}

/**
* @OA\Post(
* path="/api/subscribers/{id}/pause",
* summary="Pauses a subscriber from receiving messages",
* tags={"subscribers"},
* security={{"passport":{}}},
* description="Use to pauses a subscriber from receiving messages for 1 hour",
* operationId="SubscriberController.pauseSubscriber",
* @OA\Parameter(
* in="path",
* required=true,
* description="Subscriber id",
* name="id",
* @OA\Schema(
* type="integer",
* minimum=1
* )
* ),
* @OA\Response(
* response=200,
* ref="#/components/responses/Success200"
* ),
* @OA\Response(
* response=404,
* ref="#/components/responses/Error404"
* )
* )
*
* Response to route /api/subscribers/{id}/pause
*
* @param int $id
*
*/
public function pauseSubscriber($id)
{
$subscriber = $this->subscriberRepository->find($id);
if (!$subscriber) {
return response()->error404(__('messages.Subscriber') . $id);
}
Cache::put(config('cache.subscriber_paused_key_prefix') . $id, true, config('cache.subscriber_paused_ttl'));
return response()->success204();
}

/**
* @OA\Post(
* path="/api/subscribers/{id}/restore",
* summary="Restore a subscriber from receiving messages",
* tags={"subscribers"},
* security={{"passport":{}}},
* description="Use to restore a subscriber from receiving messages",
* operationId="SubscriberController.restoreSubscriber",
* @OA\Parameter(
* in="path",
* required=true,
* description="Subscriber id",
* name="id",
* @OA\Schema(
* type="integer",
* minimum=1
* )
* ),
* @OA\Response(
* response=200,
* ref="#/components/responses/Success200"
* ),
* @OA\Response(
* response=404,
* ref="#/components/responses/Error404"
* )
* )
*
* Response to route /api/subscribers/{id}/restore
*
* @param int $id
*
*/
public function restoreSubscriber($id)
{
$subscriber = $this->subscriberRepository->find($id);
if (!$subscriber) {
return response()->error404(__('messages.Subscriber') . $id);
}
Cache::forget(Config::get('cache.subscriber_paused_key_prefix') . $id);
return response()->success204();
}
}
8 changes: 8 additions & 0 deletions app/Jobs/SendMessageJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Cache;

class SendMessageJob implements ShouldQueue
{
Expand Down Expand Up @@ -75,6 +76,13 @@ public function handle(GuzzleService $guzzleService)
"authentication" => $this->event->channelSubscribe->authentication,
]);

$pausedKey = config('cache.subscriber_paused_key_prefix') . $this->event->channelSubscribe->subscriber_id;
if (Cache::has($pausedKey)) {

@vpasquino vpasquino Apr 30, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Se lo vuoi scrivere più carino e più corto if (Cache::has(config('cache.publisher_paused_key_prefix') . $this->event->channelSubscribe->subscriber_id))

Log::warning("Message paused");
$this->fail("Subscriber Paused");
return;
}

$this->startTime = microtime(true);
switch ($this->event->channelSubscribe->authentication) {
case Authentication::BASIC:
Expand Down
4 changes: 3 additions & 1 deletion config/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@
*/

'prefix' => env('CACHE_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_') . '_cache:'),
'channel_key_prefix' => "channel."
'channel_key_prefix' => "channel.",
'subscriber_paused_key_prefix' => "subscriber_paused.",
'subscriber_paused_ttl' => env('SUBSCRIBER_PAUSED_TTL', 3600)

];
2 changes: 1 addition & 1 deletion resources/js/store/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const useChannelsStore = defineStore("channels", {
return { meta, links };
} catch (error) {
this.loadingObject = false;
throw error.response.data;;
throw error.response.data;
}
},

Expand Down
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
Route::get('/', 'SubscriberController@getList');
Route::post('/', 'SubscriberController@store');
Route::get('{id}', 'SubscriberController@getSubscriber')->where('id', '[0-9]+');
Route::post('{id}/pause', 'SubscriberController@pauseSubscriber')->where('id', '[0-9]+');
Route::post('{id}/restore', 'SubscriberController@restoreSubscriber')->where('id', '[0-9]+');
Route::delete('{id}', 'SubscriberController@destroy')->where('id', '[0-9]+');

Route::get('{id}/channels', 'ChannelSubscribeController@getChannelSubscribe')->where('id', '[0-9]+');
Expand Down
67 changes: 67 additions & 0 deletions tests/Feature/SubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use App\Models\Subscriber;
use App\Http\Repositories\SubscriberRepository;
use Tests\TestCaseWithoutMiddleware;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;

class SubscriberTest extends TestCaseWithoutMiddleware
{
Expand Down Expand Up @@ -169,4 +172,68 @@ public function testDestroy()
$response = $this->json('DELETE', '/api/subscribers/1');
$response->assertStatus(200);
}

public function testPause()
{
Cache::spy();
$subscriber = factory(Subscriber::class)->make();
$subscriber->id = 1;
$mock = Mockery::mock(SubscriberRepository::class)->makePartial()
->shouldReceive([
"find" => $subscriber,
])
->withAnyArgs()
->once()
->getMock();
$this->app->instance('App\Http\Repositories\SubscriberRepository', $mock);
$response = $this->json('POST', '/api/subscribers/1/pause');
$response->assertStatus(Response::HTTP_NO_CONTENT);
Cache::shouldHaveReceived('put')->with(Config::get('cache.subscriber_paused_key_prefix') . 1, true, 3600);
}

public function testPauseNotFound()
{
$mock = Mockery::mock(SubscriberRepository::class)->makePartial()
->shouldReceive([
"find" => null,
])
->withAnyArgs()
->once()
->getMock();
$this->app->instance('App\Http\Repositories\SubscriberRepository', $mock);
$response = $this->json('POST', '/api/subscribers/1/pause');
$response->assertStatus(Response::HTTP_NOT_FOUND);
}

public function testRestore()
{
Cache::spy();
$subscriber = factory(Subscriber::class)->make();
$subscriber->id = 1;
$mock = Mockery::mock(SubscriberRepository::class)->makePartial()
->shouldReceive([
"find" => $subscriber,
])
->withAnyArgs()
->once()
->getMock();
$this->app->instance('App\Http\Repositories\SubscriberRepository', $mock);
$response = $this->json('POST', '/api/subscribers/1/restore');
$response->assertStatus(Response::HTTP_NO_CONTENT);
Cache::shouldHaveReceived('forget')->with(Config::get('cache.subscriber_paused_key_prefix') . 1);
}

public function testRestoreNotFound()
{
$mock = Mockery::mock(SubscriberRepository::class)->makePartial()
->shouldReceive([
"find" => null,
])
->withAnyArgs()
->once()
->getMock();
$this->app->instance('App\Http\Repositories\SubscriberRepository', $mock);
$response = $this->json('POST', '/api/subscribers/1/restore');
$response->assertStatus(Response::HTTP_NOT_FOUND);
}
}
17 changes: 17 additions & 0 deletions tests/Integration/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Tests\Integration;

use App\Events\SendMessageEvent;
use Mockery;
use App\Models\Message;
use App\Models\Channel;
use App\Models\Publisher;
use Illuminate\Support\Str;
Expand All @@ -13,6 +15,8 @@
use Illuminate\Foundation\Testing\TestCase;
use Illuminate\Support\Facades\Queue;
use Tests\CreatesApplication;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;

class MessageTest extends TestCase
{
Expand Down Expand Up @@ -191,4 +195,17 @@ public function testBasicAuthFailedAuth()
$response = $this->json('POST', '/api/sendMessage/' . Str::random(10), [], $authorization);
$response->assertStatus(401);
}

public function testSendMessagePaused()
{
$channel = factory(Channel::class)->create();
$publisher = factory(Publisher::class)->create(['password' => bcrypt(self::PUBLISHER_PASSWORD)]);
factory(ChannelPublish::class)->create(['channel_id' => $channel->id, 'publisher_id' => $publisher->id]);
$channelSubscribe = factory(ChannelSubscribe::class)->create(['channel_id' => $channel->id]);
Cache::put(Config::get('cache.subscriber_paused_key_prefix') . $channelSubscribe->subscriber_id, true, 3600);
$job = (new SendMessageJob(new SendMessageEvent(new Message("hello"), "http://prova.com", $channelSubscribe, "low", "test", "test")))->withFakeQueueInteractions();

$job->handle(new GuzzleService());
$job->assertFailed();
}
}