Skip to content

Commit

Permalink
Optionally include tags in Webhook (#72)
Browse files Browse the repository at this point in the history
* New migration to add include_tags column

* Added include tags on frontend

* backend update to handle include tags setting

* Tags working for Discord

* Tags working for Slack

* removed todo

* hide include tags if it is microsoft teams

* Small fix

* removed double spaces

* added missing comma

* Some formatting tweaks

* simplified appliedTags
  • Loading branch information
MikeLundahl committed Sep 17, 2024
1 parent 448ab85 commit 9cd06a7
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 6 deletions.
16 changes: 12 additions & 4 deletions js/src/admin/components/WebhookEditModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export default class WebhookEditModal extends Modal {
this.extraText = Stream(this.webhook.extraText() || '');
this.usePlainText = Stream(this.webhook.usePlainText());
this.maxPostContentLength = Stream(this.webhook.maxPostContentLength());
this.includeTags = Stream(this.webhook.includeTags());

this.events = groupBy(
events.reduce(
Expand Down Expand Up @@ -136,9 +137,12 @@ export default class WebhookEditModal extends Modal {

<div className="Form-group Webhook-events">
<label className="label">{app.translator.trans('fof-webhooks.admin.settings.modal.events_label')}</label>

{app.translator.trans('fof-webhooks.admin.settings.modal.description')}

<p className="helpText">{app.translator.trans('fof-webhooks.admin.settings.modal.description')}</p>
{
this.webhook.service() !== 'microsoft-teams' && <div style={{display: "block", marginTop: "30px"}}>
<Switch state={this.includeTags()} onchange={this.includeTags}>{"Include tags"}</Switch>
</div>
}
{Object.entries(this.events).map(([, events]) => (
<div>
{Object.entries(events)
Expand All @@ -148,7 +152,8 @@ export default class WebhookEditModal extends Modal {
<div>
<h3>{this.translate(group)}</h3>
{events.map((event) => (
<Switch state={this.webhook.events().includes(event.full)} onchange={this.onchange.bind(this, event.full)}>
<Switch state={this.webhook.events().includes(event.full)}
onchange={this.onchange.bind(this, event.full)}>
{this.translate(group, event.name.toLowerCase())}
</Switch>
))}
Expand Down Expand Up @@ -178,10 +183,12 @@ export default class WebhookEditModal extends Modal {
this.extraText() != this.webhook.extraText() ||
this.groupId() !== this.webhook.groupId() ||
this.usePlainText() !== this.webhook.usePlainText() ||
this.includeTags() !== this.webhook.includeTags() ||
this.maxPostContentLength() != this.webhook.maxPostContentLength()
);
}


onsubmit(e) {
e.preventDefault();

Expand All @@ -192,6 +199,7 @@ export default class WebhookEditModal extends Modal {
extraText: this.extraText(),
group_id: this.groupId(),
use_plain_text: this.usePlainText(),
include_tags: this.includeTags(),
max_post_content_length: this.maxPostContentLength() || 0,
})
.then(() => {
Expand Down
2 changes: 2 additions & 0 deletions js/src/admin/models/Webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export default class Webhook extends Model {
usePlainText = Model.attribute('use_plain_text', Boolean);
maxPostContentLength = Model.attribute('max_post_content_length');

includeTags = Model.attribute('include_tags', Boolean);

apiEndpoint() {
return `/fof/webhooks${this.exists ? `/${this.data.id}` : ''}`;
}
Expand Down
26 changes: 26 additions & 0 deletions migrations/2024_09_12_add_include_tags_columns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of fof/webhooks.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;

return [
'up' => function (Builder $schema) {
$schema->table('webhooks', function (Blueprint $table) {
$table->boolean('include_tags')->default(false);
});
},
'down' => function (Builder $schema) {
$schema->table('webhooks', function (Blueprint $table) {
$table->dropColumn('include_tags');
});
},
];
8 changes: 7 additions & 1 deletion src/Adapters/Discord/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ public function toArray(Response $response): array
'url' => $response->getAuthorUrl(),
'icon_url' => $response->author->avatar_url,
] : null,
'color' => $response->getColor(),
'color' => $response->getColor(),
'fields' => $response->getIncludeTags() ? [
[
'name' => 'Tags',
'value' => implode(', ', $response->getTags()),
],
] : null,
'timestamp' => $response->timestamp,
'type' => 'rich',
];
Expand Down
7 changes: 7 additions & 0 deletions src/Adapters/Slack/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ public function toArray(Response $response): array
'title_link' => $response->url,
'text' => $response->description,
'footer' => $this->settings->get('forum_title'),
'fields' => $response->getIncludeTags() ? [
[
'title' => 'Tags',
'value' => implode(', ', $response->getTags()),
'short' => false,
],
] : null,
];

if ($response->author->exists) {
Expand Down
1 change: 1 addition & 0 deletions src/Api/Serializer/WebhookSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ protected function getDefaultAttributes($webhook): array
'extra_text' => $webhook->extra_text ?: '',

'use_plain_text' => (bool) $webhook->use_plain_text,
'include_tags' => (bool) $webhook->include_tags,
'max_post_content_length' => ((int) $webhook->max_post_content_length) ?: null,

'is_valid' => $webhook->isValid(),
Expand Down
5 changes: 5 additions & 0 deletions src/Command/UpdateWebhookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function handle(UpdateWebhook $command): Webhook
$events = Arr::get($data, 'attributes.events');
$groupId = Arr::get($data, 'attributes.group_id');
$usePlainText = Arr::get($data, 'attributes.use_plain_text');
$includeTags = Arr::get($data, 'attributes.include_tags');
$maxPostContentLength = Arr::get($data, 'attributes.max_post_content_length');

if (isset($service)) {
Expand Down Expand Up @@ -87,6 +88,10 @@ public function handle(UpdateWebhook $command): Webhook
$webhook->use_plain_text = $usePlainText;
}

if (isset($includeTags)) {
$webhook->include_tags = $includeTags;
}

if (isset($maxPostContentLength)) {
$webhook->max_post_content_length = $maxPostContentLength == 0 ? null : $maxPostContentLength;
}
Expand Down
13 changes: 12 additions & 1 deletion src/Models/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
* @property string|null $error
* @property string $events
* @property number $group_id
* @property number $tag_id
* @property array|null $tag_id
* @property string $extra_text
* @property Group|null $group
* @property Tag|null $tag
* @property bool $use_plain_text
* @property bool $include_tags
* @property ?int $max_post_content_length
*/
class Webhook extends AbstractModel
Expand Down Expand Up @@ -79,6 +80,16 @@ public function tags()
return Tag::whereIn('id', $this->tag_id)->get();
}

public function appliedTags()
{
return Tag::select('name')->whereIn('id', $this->tag_id)->pluck('name')->toArray();
}

public function getIncludeTags(): bool
{
return $this->include_tags;
}

public function asGuest(): bool
{
$group = $this->group;
Expand Down
15 changes: 15 additions & 0 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ class Response
*/
public $color;

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

/**
* @var string
*/
Expand Down Expand Up @@ -141,6 +146,16 @@ public function getExtraText(): ?string
return $this->webhook->extra_text;
}

public function getIncludeTags(): bool
{
return $this->webhook->include_tags;
}

public function getTags(): ?array
{
return $this->webhook->appliedTags();
}

public function withWebhook(Webhook $webhook): self
{
$this->setWebhook($webhook);
Expand Down

0 comments on commit 9cd06a7

Please sign in to comment.