Skip to content

Commit

Permalink
Create HasWebhookParams interface (#3281)
Browse files Browse the repository at this point in the history
Co-authored-by: Partydragen <[email protected]>
  • Loading branch information
tadhgboyle and partydragen authored Apr 10, 2023
1 parent 8423bc7 commit 3966a8c
Show file tree
Hide file tree
Showing 30 changed files with 360 additions and 82 deletions.
2 changes: 1 addition & 1 deletion core/classes/Events/DiscordDispatchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ interface DiscordDispatchable {
*
* @return DiscordWebhookBuilder The webhook builder to send the event as an embed
*/
public function toDiscordWebook(): DiscordWebhookBuilder;
public function toDiscordWebhook(): DiscordWebhookBuilder;

}
23 changes: 18 additions & 5 deletions core/classes/Events/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public static function registerEvent(
if (class_exists($event) && is_subclass_of($event, AbstractEvent::class)) {
$class_name = $event;
$name = $event::name();
$description = $event::description();
// We lazy load descriptions for class-based events to avoid loading new Language instances unnecessarily
$description = fn () => $event::description();
$return = $event::return();
$internal = $event::internal();
} else {
Expand Down Expand Up @@ -202,16 +203,28 @@ public static function executeEvent($event, array $params = []): ?array {
/**
* Get a list of events to display on the StaffCP webhooks page.
*
* @param bool $internal Whether to include internal events or not
* @return array List of all currently registered events
*/
public static function getEvents(bool $internal = false): array {
public static function getEvents(bool $showInternal = false): array {
$return = [];

foreach (self::$_events as $name => $meta) {
if (!$meta['internal'] || $internal) {
$return[$name] = $meta['description'];
if ($meta['internal'] && !$showInternal) {
continue;
}

if (is_callable($meta['description'])) {
$description = $meta['description']();
} else {
$description = $meta['description'];
}

$class = $meta['class_name'];
$return[$name] = [
'description' => $description,
'supports_discord' => $class !== null && is_subclass_of($class, DiscordDispatchable::class),
'supports_normal' => $class !== null && is_subclass_of($class, HasWebhookParams::class),
];
}

return $return;
Expand Down
17 changes: 17 additions & 0 deletions core/classes/Events/HasWebhookParams.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Represents an event which has specific parameters to send to a webhook.
*
* @package NamelessMC\Events
* @author Aberdeener
* @version 2.2.0
* @license MIT
*/
interface HasWebhookParams {

/**
* @return array Array of parameters to send to the webhook
*/
public function webhookParams(): array;

}
11 changes: 11 additions & 0 deletions core/classes/Events/WebhookDispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

interface WebhookDispatcher {

/**
* @param AbstractEvent|array $event Event to execute, or array of params if event is not object based
* @param string $webhook_url Webhook URL to use, if not provided in event
*/
public static function execute($event, string $webhook_url = '');

}
3 changes: 3 additions & 0 deletions custom/languages/en_UK.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
"admin/details": "Details",
"admin/disable": "Disable",
"admin/disabled": "Disabled",
"admin/discord_hook": "Discord",
"admin/display_field_on_forum": "Display field on forum?",
"admin/download": "Download",
"admin/download_sitemap": "Download Sitemap",
Expand Down Expand Up @@ -225,6 +226,8 @@
"admin/query_type": "Query type",
"admin/internal": "Internal",
"admin/external": "External",
"admin/event_supports_discord": "This event supports Discord webhooks and has a custom embed.",
"admin/event_supports_normal": "This event supports normal webhooks.",
"admin/plugin": "Plugin",
"admin/query_type_help": "If the default internal server query does not work, try another option.",
"admin/face": "Face",
Expand Down
24 changes: 16 additions & 8 deletions custom/panel_templates/Default/core/hooks_edit.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,26 @@
<div class="form-group">
<label for="link_location">{$HOOK_TYPE}</label>
<select class="form-control" id="hook_type" name="hook_type">
<option value="2" {if $HOOK_TYPE_VALUE eq 2} selected{/if}>Discord</option>
<option value="1" {if $HOOK_TYPE_VALUE eq 1} selected{/if}>{$NORMAL}</option>
<option value="2" {if $HOOK_TYPE_VALUE eq 2} selected{/if}>
{$DISCORD}
</option>
<option value="1" {if $HOOK_TYPE_VALUE eq 1} selected{/if}>
{$NORMAL}
</option>
</select>
</div>
<label for="InputName">{$HOOK_EVENTS}</label>
{foreach from=$ALL_EVENTS key=key item=item}
{foreach from=$ALL_EVENTS key=key item=meta}
<div class="form-group custom-control custom-switch">
<input type="checkbox" id="inputevents[{$key|escape}]" name="events[{$key|escape}]"
class="custom-control-input" value="1" {if in_array($key|escape,
$ENABLED_HOOKS)} checked{/if}>
<input type="checkbox" id="inputevents[{$key|escape}]" name="events[{$key|escape}]" class="custom-control-input" value="1" {if in_array($key|escape, $ENABLED_HOOKS)} checked{/if}>
<label class="custom-control-label" for="inputevents[{$key|escape}]">
{$item|escape}
{$meta.description|escape}
{if $meta.supports_discord}
<i class="fab fa-discord"></i>
{/if}
{if $meta.supports_normal}
<i class="fas fa-globe"></i>
{/if}
</label>
</div>
{/foreach}
Expand Down Expand Up @@ -109,4 +117,4 @@

</body>

</html>
</html>
29 changes: 19 additions & 10 deletions custom/panel_templates/Default/core/hooks_new.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,28 @@
<div class="form-group">
<label for="link_location">{$HOOK_TYPE}</label>
<select class="form-control" id="hook_type" name="hook_type">
<option value="2">Discord</option>
<option value="2">{$DISCORD}</option>
<option value="1">{$NORMAL}</option>
</select>
</div>
<label for="InputName">{$HOOK_EVENTS}</label>
{foreach from=$ALL_EVENTS key=key item=item}
<div class="form-group custom-control custom-switch">
<input type="checkbox" id="inputevents[{$key|escape}]" name="events[{$key|escape}]"
class="custom-control-input">
<label class="custom-control-label" for="inputevents[{$key|escape}]">
{$item|escape}
</label>
</div>
{foreach from=$ALL_EVENTS key=key item=meta}
<div class="form-group custom-control custom-switch">
<input type="checkbox" id="inputevents[{$key|escape}]" name="events[{$key|escape}]" class="custom-control-input">
<label class="custom-control-label" for="inputevents[{$key|escape}]">
{$meta.description|escape}
{if $meta.supports_discord}
<span data-toggle="tooltip" data-original-title="{$SUPPORTS_DISCORD}">
<i class="fab fa-discord"></i>
</span>
{/if}
{if $meta.supports_normal}
<span data-toggle="tooltip" data-original-title="{$SUPPORTS_NORMAL}">
<i class="fas fa-globe"></i>
</span>
{/if}
</label>
</div>
{/foreach}
<div class="form-group">
<input type="hidden" name="token" value="{$TOKEN}">
Expand Down Expand Up @@ -108,4 +117,4 @@

</body>

</html>
</html>
2 changes: 2 additions & 0 deletions dev/scripts/cli_install.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ function getEnvVar(string $name, string $fallback = null, array $valid_values =

DatabaseInitialiser::runPostUser();

Config::set('core.installed', true);

print(PHP_EOL . '✅ Installation complete! (Took ' . round(microtime(true) - $start, 2) . ' seconds)' . PHP_EOL);
print(PHP_EOL . '🖥 URL: http://' . $conf['core']['hostname'] . $conf['core']['path']);
print(PHP_EOL . '🔑 Admin username: ' . $username);
Expand Down
15 changes: 12 additions & 3 deletions modules/Core/classes/Events/AnnouncementCreatedEvent.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class AnnouncementCreatedEvent extends AbstractEvent implements DiscordDispatchable {
class AnnouncementCreatedEvent extends AbstractEvent implements HasWebhookParams, DiscordDispatchable {

public User $creator;
public string $header;
Expand All @@ -17,10 +17,19 @@ public static function name(): string {
}

public static function description(): string {
return (new Language(ROOT_PATH . '/modules/Forum/language'))->get('forum', 'new_topic_hook_info');
return (new Language())->get('admin', 'announcement_hook_info');
}

public function toDiscordWebook(): DiscordWebhookBuilder {
public function webhookParams(): array {
return [
'user_id' => $this->creator->data()->id,
'username' => $this->creator->getDisplayname(),
'header' => $this->header,
'message' => $this->message
];
}

public function toDiscordWebhook(): DiscordWebhookBuilder {
$language = new Language('core', DEFAULT_LANGUAGE);

return DiscordWebhookBuilder::make()
Expand Down
14 changes: 12 additions & 2 deletions modules/Core/classes/Events/ReportCreatedEvent.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class ReportCreatedEvent extends AbstractEvent implements DiscordDispatchable {
class ReportCreatedEvent extends AbstractEvent implements HasWebhookParams, DiscordDispatchable {

public string $username;
public string $content;
Expand Down Expand Up @@ -34,7 +34,17 @@ public static function description(): string {
return (new Language())->get('admin', 'report_hook_info');
}

public function toDiscordWebook(): DiscordWebhookBuilder {
public function webhookParams(): array {
return [
'username' => $this->username,
'title' => $this->title,
'content' => $this->content,
'content_full' => $this->content_full,
'url' => $this->url
];
}

public function toDiscordWebhook(): DiscordWebhookBuilder {
return DiscordWebhookBuilder::make()
->setUsername($this->username . ' | ' . SITE_NAME)
->setAvatarUrl($this->avatar_url)
Expand Down
19 changes: 17 additions & 2 deletions modules/Core/classes/Events/UserBannedEvent.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class UserBannedEvent extends AbstractEvent implements DiscordDispatchable {
class UserBannedEvent extends AbstractEvent implements HasWebhookParams, DiscordDispatchable {

public User $punished;
public User $punisher;
Expand All @@ -18,7 +18,22 @@ public static function description(): string {
return (new Language())->get('admin', 'ban_hook_info');
}

public function toDiscordWebook(): DiscordWebhookBuilder {
public function webhookParams(): array {
return [
'punished' => [
'user_id' => $this->punished->data()->id,
'username' => $this->punished->getDisplayname(),
],
'punisher' => [
'user_id' => $this->punisher->data()->id,
'username' => $this->punisher->getDisplayname(),
],
'reason' => $this->reason,
'ip_ban' => $this->ip_ban
];
}

public function toDiscordWebhook(): DiscordWebhookBuilder {
$language = new Language('core', DEFAULT_LANGUAGE);

return DiscordWebhookBuilder::make()
Expand Down
9 changes: 8 additions & 1 deletion modules/Core/classes/Events/UserDeletedEvent.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class UserDeletedEvent extends AbstractEvent {
class UserDeletedEvent extends AbstractEvent implements HasWebhookParams {

public User $user;

Expand All @@ -12,6 +12,13 @@ public static function name(): string {
return 'deleteUser';
}

public function webhookParams(): array {
return [
'user_id' => $this->user->data()->id,
'username' => $this->user->getDisplayname(),
];
}

public static function description(): string {
return (new Language())->get('admin', 'delete_hook_info');
}
Expand Down
15 changes: 13 additions & 2 deletions modules/Core/classes/Events/UserGroupAddedEvent.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class UserGroupAddedEvent extends AbstractEvent implements DiscordDispatchable {
class UserGroupAddedEvent extends AbstractEvent implements HasWebhookParams, DiscordDispatchable {

public User $user;
public Group $group;
Expand All @@ -14,7 +14,18 @@ public static function description(): string {
return (new Language())->get('admin', 'user_group_added_hook_info');
}

public function toDiscordWebook(): DiscordWebhookBuilder {
public function webhookParams(): array {
return [
'user_id' => $this->user->data()->id,
'username' => $this->user->getDisplayname(),
'group' => [
'id' => $this->group->id,
'name' => $this->group->name
]
];
}

public function toDiscordWebhook(): DiscordWebhookBuilder {
$language = new Language('core', DEFAULT_LANGUAGE);

return DiscordWebhookBuilder::make()
Expand Down
15 changes: 13 additions & 2 deletions modules/Core/classes/Events/UserGroupRemovedEvent.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class UserGroupRemovedEvent extends AbstractEvent implements DiscordDispatchable {
class UserGroupRemovedEvent extends AbstractEvent implements HasWebhookParams, DiscordDispatchable {

public User $user;
public Group $group;
Expand All @@ -14,7 +14,18 @@ public static function description(): string {
return (new Language())->get('admin', 'user_group_removed_hook_info');
}

public function toDiscordWebook(): DiscordWebhookBuilder {
public function webhookParams(): array {
return [
'user_id' => $this->user->data()->id,
'username' => $this->user->getDisplayname(),
'group' => [
'id' => $this->group->id,
'name' => $this->group->name
]
];
}

public function toDiscordWebhook(): DiscordWebhookBuilder {
$language = new Language('core', DEFAULT_LANGUAGE);

return DiscordWebhookBuilder::make()
Expand Down
17 changes: 15 additions & 2 deletions modules/Core/classes/Events/UserIntegrationLinkedEvent.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class UserIntegrationLinkedEvent extends AbstractEvent implements DiscordDispatchable {
class UserIntegrationLinkedEvent extends AbstractEvent implements HasWebhookParams, DiscordDispatchable {

public User $user;
public IntegrationBase $integration;
Expand All @@ -20,7 +20,20 @@ public static function description(): string {
return (new Language())->get('admin', 'user_link_integration_hook_info');
}

public function toDiscordWebook(): DiscordWebhookBuilder {
public function webhookParams(): array {
return [
'user_id' => $this->user->data()->id,
'username' => $this->user->getDisplayname(),
'integration' => [
'integration' => $this->integration->getName(),
'username' => $this->integration_user->data()->username,
'identifier' => $this->integration_user->data()->identifier,
'verified' => $this->integration_user->isVerified()
]
];
}

public function toDiscordWebhook(): DiscordWebhookBuilder {
$language = new Language('core', DEFAULT_LANGUAGE);

return DiscordWebhookBuilder::make()
Expand Down
Loading

0 comments on commit 3966a8c

Please sign in to comment.