Skip to content

Commit

Permalink
Improve code, fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ilestis committed Apr 12, 2024
1 parent b6e48dc commit 00d09ec
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 96 deletions.
34 changes: 7 additions & 27 deletions app/Http/Controllers/Campaign/WebhookController.php
Expand Up @@ -13,10 +13,6 @@ class WebhookController extends Controller
{
protected string $view = 'webhooks';

/**
* Create a new controller instance.
* @return void
*/
public function __construct()
{
$this->middleware('auth');
Expand All @@ -33,15 +29,13 @@ public function index(Campaign $campaign)

$this->authorize('webhooks', $campaign);

$webhooks = $campaign->webhooks()
$rows = $campaign->webhooks()
->sort(request()->only(['o', 'k']))
//->with(['users', 'permissions', 'campaign'])
->orderBy('updated_at', 'DESC')
//->orderBy('name')
->paginate();

$rows = $webhooks;

// Ajax Datagrid
if (request()->ajax()) {
$html = view('layouts.datagrid._table')->with('rows', $rows)->with('campaign', $campaign)->render();
Expand All @@ -53,7 +47,7 @@ public function index(Campaign $campaign)
]);
}

return view('campaigns.webhooks', compact('campaign', 'rows', 'webhooks'));
return view('campaigns.webhooks', compact('campaign', 'rows'));
}

/**
Expand All @@ -75,8 +69,9 @@ public function store(StoreWebhook $request, Campaign $campaign)
return response()->json();
}

$data = $request->all() + ['campaign_id' => $campaign->id];
Webhook::create($data);
$new = new Webhook($request->all());
$new->campaign_id = $campaign->id;
$new->save();

return redirect()->route('webhooks.index', $campaign)
->with('success', __('campaigns/webhooks.create.success'));
Expand Down Expand Up @@ -106,21 +101,10 @@ public function destroy(Campaign $campaign, Webhook $webhook)
$this->authorize('webhooks', $campaign);

$webhook->delete();

return redirect()->route('webhooks.index', $campaign)
->with('success', __('campaigns/webhooks.destroy.success'));
}

public function status(Campaign $campaign, Webhook $webhook)
{
$this->authorize('webhooks', $campaign);

return view('campaigns.webhooks.status', [
'campaign' => $campaign,
'webhook' => $webhook,
]);
}

/**
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
Expand Down Expand Up @@ -161,14 +145,10 @@ public function bulk(Campaign $campaign)
if ($action === 'delete') {
$webhook->delete();
$count++;
}

if ($action === 'disable' && $webhook->status) {
} elseif ($action === 'disable' && $webhook->status) {
$webhook->update(['status' => 0]);
$count++;
}

if ($action === 'enable' && !$webhook->status) {
} elseif ($action === 'enable' && !$webhook->status) {
$webhook->update(['status' => 1]);
$count++;
}
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/EntityWebhookJob.php
Expand Up @@ -58,7 +58,7 @@ public function handle()
return;
}

$webhooks = Webhook::Active($this->campaign->id, $this->action)->with('tags')->get();
$webhooks = Webhook::active($this->campaign->id, $this->action)->with('tags')->get();
$entityTags = $this->entity->tags()->pluck('tags.id')->all();
foreach ($webhooks as $webhook) {
if ($this->isInvalid($webhook, $entityTags)) {
Expand Down
27 changes: 9 additions & 18 deletions app/Jobs/TestWebhookJob.php
Expand Up @@ -21,25 +21,13 @@ class TestWebhookJob implements ShouldQueue
use Queueable;
use SerializesModels;

/**
* @var int
*/
public $campaignId;
public int $campaignId;

/**
* @var int
*/
public $action;
public int $action;

/**
* @var string
*/
public $username;
public string $username;

/**
* @var Webhook
*/
public $webhook;
public Webhook $webhook;

/**
* The number of times the job may be attempted.
Expand Down Expand Up @@ -70,10 +58,13 @@ public function __construct(Campaign $campaign, User $user, Webhook $webhook, in
*/
public function handle()
{
/** @var Campaign|null $campaign */
$campaign = Campaign::find($this->campaignId);

if ($this->webhook->type == 1) {
$data = Str::replace(
['{name}', '{who}', '{url}'],
['Thaelia', $this->username, route('locations.index', [$this->campaignId])],
['Thaelia', $this->username, route('locations.index', [$campaign])],
$this->webhook->message
);

Expand Down Expand Up @@ -119,7 +110,7 @@ public function handle()
'title' => 'Thaelia',
'description' => strval($data),
'color' => config('discord.color'),
'url' => route('locations.index', [$this->campaignId]),
'url' => route('locations.index', [$campaign]),
'author' => [
'name' => 'Kanka Webhooks',
],
Expand Down
20 changes: 9 additions & 11 deletions app/Models/Webhook.php
Expand Up @@ -9,28 +9,28 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use App\Facades\CampaignLocalization;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
* @property int $id
* @property int $feature_id
* @property int $campaign_id
* @property int $status
* @property string $path
* @property Feature $feature
* @property \App\Enums\Webhook $status_id
* @property WebhookAction $action
*/
class Webhook extends Model
{
use Paginatable;
use SortableTrait;

public $fillable = [
'campaign_id',
'action',
'url',
'type',
'message',
'status',
'created_by',
'updated_by',
];

protected array $sortable = [
Expand All @@ -39,10 +39,7 @@ class Webhook extends Model
'action',
];

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function tags()
public function tags(): BelongsToMany
{
return $this->belongsToMany(
'App\Models\Tag',
Expand Down Expand Up @@ -91,10 +88,11 @@ public function shortUrl(): string
return $this->url;
}

/**
*/
public function scopeActive(Builder $query, int $campaignId, int $action): Builder
{
return $query->where('campaign_id', $campaignId)->where('action', $action)->where('status', 1);
return $query
->where('campaign_id', $campaignId)
->where('action', $action)
->where('status', 1);
}
}
9 changes: 7 additions & 2 deletions app/Renderers/Layouts/Campaign/Webhook.php
Expand Up @@ -3,6 +3,7 @@
namespace App\Renderers\Layouts\Campaign;

use App\Renderers\Layouts\Layout;
use Illuminate\Support\Str;

class Webhook extends Layout
{
Expand Down Expand Up @@ -32,13 +33,17 @@ public function columns(): array
'label' => 'campaigns/webhooks.fields.message',
'render' => function ($model) {
/** @var \App\Models\Webhook $model */
return $model->message;
return '<div data-toggle="tooltip" data-title="' . nl2br($model->message) . '" data-html="1">'
. Str::limit(strip_tags($model->message ?? ''), 30)
. '</div>';
},
],
'url' => [
'label' => 'campaigns/webhooks.fields.url',
'render' => function ($model) {
return '<div href="#" data-toggle="tooltip" title="' . $model->url . '">' . $model->shortUrl() . '</div>';
return '<div data-toggle="tooltip" data-title="' . $model->url . '">'
. $model->shortUrl()
. '</div>';
},
],

Expand Down
10 changes: 5 additions & 5 deletions lang/en/campaigns/webhooks.php
Expand Up @@ -3,9 +3,9 @@
return [
'title' => 'Webhooks',
'destroy' => [
'success' => 'Webhook deleted succesfully',
'success' => 'Webhook deleted successfully',
],
'pitch' => 'Create custom webhooks to recieve custom updates whenever an entity from the campaign is updated.',
'pitch' => 'Create custom webhooks to receive custom updates whenever an entity from the campaign is updated.',
'actions' => [
'add' => 'Create webhook',
'update' => 'Update webhook',
Expand All @@ -21,11 +21,11 @@
],
'create' => [
'title' => 'Add new webhook',
'success' => 'Webhook created succesfully',
'success' => 'Webhook created successfully',
],
'edit' => [
'title' => 'Update webhook',
'success' => 'Webhook updated succesfully',
'success' => 'Webhook updated successfully',
],
'test' => [
'success' => 'Test request sent',
Expand Down Expand Up @@ -53,6 +53,6 @@
],
'placeholders' => [
'url' => 'Target webhook\'s url',
'message' => 'Message to append to the webhook',
'message' => '{who} made changes to {name}, check it out at {url}',
],
];
1 change: 1 addition & 0 deletions public/build/assets/app-5da4beae.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion public/build/assets/app-d8eaa37c.js

This file was deleted.

2 changes: 1 addition & 1 deletion public/build/manifest.json
Expand Up @@ -80,7 +80,7 @@
"src": "resources/js/api.js"
},
"resources/js/app.js": {
"file": "assets/app-d8eaa37c.js",
"file": "assets/app-5da4beae.js",
"imports": [
"_tippy.esm-24ef6cb2.js",
"_mention-2d258b1d.js",
Expand Down
9 changes: 6 additions & 3 deletions resources/views/campaigns/webhooks/_form.blade.php
Expand Up @@ -5,7 +5,10 @@
:required="true"
:label="__('campaigns/webhooks.fields.event')"
>
{!! Form::select('action', [1 => __('campaigns/webhooks.fields.events.new'), 2 => __('campaigns/webhooks.fields.events.edited'), 3 => __('campaigns/webhooks.fields.events.deleted')]) !!}
{!! Form::select('action', [
\App\Enums\WebhookAction::CREATED->value => __('campaigns/webhooks.fields.events.new'),
\App\Enums\WebhookAction::EDITED->value => __('campaigns/webhooks.fields.events.edited'),
\App\Enums\WebhookAction::DELETED->value => __('campaigns/webhooks.fields.events.deleted')]) !!}
</x-forms.field>

<x-forms.field
Expand Down Expand Up @@ -37,14 +40,14 @@
:helper="__('campaigns/webhooks.helper.message')"
link="https://docs.kanka.io/en/latest/features/campaigns/webhooks.html#mappings"
>
{!! Form::text('message', null, ['placeholder' => __('campaigns/webhooks.placeholders.message'), 'class' => '', 'maxlength' => 400, 'required' => false]) !!}
{!! Form::textarea('message', null, ['placeholder' => __('campaigns/webhooks.placeholders.message'), 'class' => '', 'maxlength' => 400, 'rows' => 4]) !!}
</x-forms.field>
</div>

<x-forms.field
field="status"
:label="__('campaigns/webhooks.fields.enabled')">
{!! Form::hidden('status', 1) !!}
{!! Form::hidden('status', 0) !!}
<x-checkbox :text="__('campaigns/webhooks.helper.active')">
{!! Form::checkbox('status', 1, isset($webhook) ? $webhook->status : 1) !!}
</x-checkbox>
Expand Down
5 changes: 0 additions & 5 deletions resources/views/campaigns/webhooks/_status.blade.php

This file was deleted.

22 changes: 0 additions & 22 deletions resources/views/campaigns/webhooks/status.blade.php

This file was deleted.

0 comments on commit 00d09ec

Please sign in to comment.