From 3966a8c3433437f290a5d623d9d611598bb1ae68 Mon Sep 17 00:00:00 2001 From: Tadhg Boyle Date: Mon, 10 Apr 2023 13:34:29 -0700 Subject: [PATCH] Create `HasWebhookParams` interface (#3281) Co-authored-by: Partydragen --- core/classes/Events/DiscordDispatchable.php | 2 +- core/classes/Events/EventHandler.php | 23 ++++++++++---- core/classes/Events/HasWebhookParams.php | 17 +++++++++++ core/classes/Events/WebhookDispatcher.php | 11 +++++++ custom/languages/en_UK.json | 3 ++ .../Default/core/hooks_edit.tpl | 24 ++++++++++----- .../Default/core/hooks_new.tpl | 29 +++++++++++------- dev/scripts/cli_install.php | 2 ++ .../Events/AnnouncementCreatedEvent.php | 15 ++++++++-- .../classes/Events/ReportCreatedEvent.php | 14 +++++++-- .../Core/classes/Events/UserBannedEvent.php | 19 ++++++++++-- .../Core/classes/Events/UserDeletedEvent.php | 9 +++++- .../classes/Events/UserGroupAddedEvent.php | 15 ++++++++-- .../classes/Events/UserGroupRemovedEvent.php | 15 ++++++++-- .../Events/UserIntegrationLinkedEvent.php | 17 +++++++++-- .../Events/UserIntegrationUnlinkedEvent.php | 17 +++++++++-- .../Events/UserIntegrationVerifiedEvent.php | 17 +++++++++-- .../Events/UserProfilePostCreatedEvent.php | 19 ++++++++++-- .../UserProfilePostReplyCreatedEvent.php | 19 ++++++++++-- .../classes/Events/UserRegisteredEvent.php | 12 ++++++-- .../classes/Events/UserValidatedEvent.php | 12 ++++++-- .../Core/classes/Events/UserWarnedEvent.php | 18 +++++++++-- modules/Core/hooks/WebHook.php | 30 ++++++++++++++----- modules/Core/pages/panel/hooks.php | 8 ++++- .../Discord Integration/hooks/DiscordHook.php | 23 +++++++------- .../classes/Events/TopicCreatedEvent.php | 24 +++++++++++++-- .../classes/Events/TopicReplyCreatedEvent.php | 19 ++++++++++-- modules/Forum/language/en_UK.json | 1 - modules/Forum/pages/forum/new_topic.php | 4 +-- modules/Forum/pages/forum/view_topic.php | 4 +-- 30 files changed, 360 insertions(+), 82 deletions(-) create mode 100644 core/classes/Events/HasWebhookParams.php create mode 100644 core/classes/Events/WebhookDispatcher.php diff --git a/core/classes/Events/DiscordDispatchable.php b/core/classes/Events/DiscordDispatchable.php index a2d88e8915..56dcccb17d 100644 --- a/core/classes/Events/DiscordDispatchable.php +++ b/core/classes/Events/DiscordDispatchable.php @@ -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; } diff --git a/core/classes/Events/EventHandler.php b/core/classes/Events/EventHandler.php index 7db4216af9..85b97dc65a 100644 --- a/core/classes/Events/EventHandler.php +++ b/core/classes/Events/EventHandler.php @@ -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 { @@ -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; diff --git a/core/classes/Events/HasWebhookParams.php b/core/classes/Events/HasWebhookParams.php new file mode 100644 index 0000000000..0569224bcf --- /dev/null +++ b/core/classes/Events/HasWebhookParams.php @@ -0,0 +1,17 @@ + - {foreach from=$ALL_EVENTS key=key item=item} + {foreach from=$ALL_EVENTS key=key item=meta}
- +
{/foreach} @@ -109,4 +117,4 @@ - \ No newline at end of file + diff --git a/custom/panel_templates/Default/core/hooks_new.tpl b/custom/panel_templates/Default/core/hooks_new.tpl index 8ec3f24fbb..6cc9779498 100644 --- a/custom/panel_templates/Default/core/hooks_new.tpl +++ b/custom/panel_templates/Default/core/hooks_new.tpl @@ -64,19 +64,28 @@
- {foreach from=$ALL_EVENTS key=key item=item} -
- - -
+ {foreach from=$ALL_EVENTS key=key item=meta} +
+ + +
{/foreach}
@@ -108,4 +117,4 @@ - \ No newline at end of file + diff --git a/dev/scripts/cli_install.php b/dev/scripts/cli_install.php index 3b04a7be12..aae987bafb 100644 --- a/dev/scripts/cli_install.php +++ b/dev/scripts/cli_install.php @@ -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); diff --git a/modules/Core/classes/Events/AnnouncementCreatedEvent.php b/modules/Core/classes/Events/AnnouncementCreatedEvent.php index 886d6666b3..1546c6e29a 100644 --- a/modules/Core/classes/Events/AnnouncementCreatedEvent.php +++ b/modules/Core/classes/Events/AnnouncementCreatedEvent.php @@ -1,6 +1,6 @@ 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() diff --git a/modules/Core/classes/Events/ReportCreatedEvent.php b/modules/Core/classes/Events/ReportCreatedEvent.php index 994c59a685..3e83341205 100644 --- a/modules/Core/classes/Events/ReportCreatedEvent.php +++ b/modules/Core/classes/Events/ReportCreatedEvent.php @@ -1,6 +1,6 @@ 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) diff --git a/modules/Core/classes/Events/UserBannedEvent.php b/modules/Core/classes/Events/UserBannedEvent.php index d8ad22038f..8a9860d610 100644 --- a/modules/Core/classes/Events/UserBannedEvent.php +++ b/modules/Core/classes/Events/UserBannedEvent.php @@ -1,6 +1,6 @@ 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() diff --git a/modules/Core/classes/Events/UserDeletedEvent.php b/modules/Core/classes/Events/UserDeletedEvent.php index ff1f962178..0bed8c5db5 100644 --- a/modules/Core/classes/Events/UserDeletedEvent.php +++ b/modules/Core/classes/Events/UserDeletedEvent.php @@ -1,6 +1,6 @@ $this->user->data()->id, + 'username' => $this->user->getDisplayname(), + ]; + } + public static function description(): string { return (new Language())->get('admin', 'delete_hook_info'); } diff --git a/modules/Core/classes/Events/UserGroupAddedEvent.php b/modules/Core/classes/Events/UserGroupAddedEvent.php index 2bcffec819..0c15b44976 100644 --- a/modules/Core/classes/Events/UserGroupAddedEvent.php +++ b/modules/Core/classes/Events/UserGroupAddedEvent.php @@ -1,6 +1,6 @@ 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() diff --git a/modules/Core/classes/Events/UserGroupRemovedEvent.php b/modules/Core/classes/Events/UserGroupRemovedEvent.php index a7a2858bbd..6746b070c1 100644 --- a/modules/Core/classes/Events/UserGroupRemovedEvent.php +++ b/modules/Core/classes/Events/UserGroupRemovedEvent.php @@ -1,6 +1,6 @@ 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() diff --git a/modules/Core/classes/Events/UserIntegrationLinkedEvent.php b/modules/Core/classes/Events/UserIntegrationLinkedEvent.php index 032cac74a9..61becc6713 100644 --- a/modules/Core/classes/Events/UserIntegrationLinkedEvent.php +++ b/modules/Core/classes/Events/UserIntegrationLinkedEvent.php @@ -1,6 +1,6 @@ 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() diff --git a/modules/Core/classes/Events/UserIntegrationUnlinkedEvent.php b/modules/Core/classes/Events/UserIntegrationUnlinkedEvent.php index b9bd173bab..3f6338a575 100644 --- a/modules/Core/classes/Events/UserIntegrationUnlinkedEvent.php +++ b/modules/Core/classes/Events/UserIntegrationUnlinkedEvent.php @@ -1,6 +1,6 @@ get('admin', 'user_unlink_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() diff --git a/modules/Core/classes/Events/UserIntegrationVerifiedEvent.php b/modules/Core/classes/Events/UserIntegrationVerifiedEvent.php index b9a4e3daae..2793e036c2 100644 --- a/modules/Core/classes/Events/UserIntegrationVerifiedEvent.php +++ b/modules/Core/classes/Events/UserIntegrationVerifiedEvent.php @@ -1,6 +1,6 @@ get('admin', 'user_verify_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() diff --git a/modules/Core/classes/Events/UserProfilePostCreatedEvent.php b/modules/Core/classes/Events/UserProfilePostCreatedEvent.php index 04d007287a..68de604982 100644 --- a/modules/Core/classes/Events/UserProfilePostCreatedEvent.php +++ b/modules/Core/classes/Events/UserProfilePostCreatedEvent.php @@ -1,6 +1,6 @@ get('admin', 'user_new_profile_post_hook_info'); } - public function toDiscordWebook(): DiscordWebhookBuilder { + public function webhookParams(): array { + return [ + 'poster' => [ + 'user_id' => $this->poster->data()->id, + 'username' => $this->poster->getDisplayname() + ], + 'profile_user' => [ + 'user_id' => $this->profile_user->data()->id, + 'username' => $this->profile_user->getDisplayname() + ], + 'content' => $this->content, + 'url' => URL::getSelfURL() . ltrim(URL::build('/profile/' . urlencode($this->profile_user->getDisplayname(true)) . '/#post-' . urlencode(DB::getInstance()->lastId())), '/') + ]; + } + + public function toDiscordWebhook(): DiscordWebhookBuilder { $language = new Language('core', DEFAULT_LANGUAGE); return DiscordWebhookBuilder::make() diff --git a/modules/Core/classes/Events/UserProfilePostReplyCreatedEvent.php b/modules/Core/classes/Events/UserProfilePostReplyCreatedEvent.php index 83af324a1b..8f4216f242 100644 --- a/modules/Core/classes/Events/UserProfilePostReplyCreatedEvent.php +++ b/modules/Core/classes/Events/UserProfilePostReplyCreatedEvent.php @@ -1,6 +1,6 @@ get('admin', 'user_profile_post_reply_hook_info'); } - public function toDiscordWebook(): DiscordWebhookBuilder { + public function webhookParams(): array { + return [ + 'poster' => [ + 'user_id' => $this->poster->data()->id, + 'username' => $this->poster->getDisplayname() + ], + 'profile_user' => [ + 'user_id' => $this->profile_user->data()->id, + 'username' => $this->profile_user->getDisplayname() + ], + 'content' => $this->content, + 'url' => URL::getSelfURL() . ltrim(URL::build('/profile/' . urlencode($this->profile_user->getDisplayname(true)) . '/#post-' . urlencode(DB::getInstance()->lastId())), '/') + ]; + } + + public function toDiscordWebhook(): DiscordWebhookBuilder { $language = new Language('core', DEFAULT_LANGUAGE); return DiscordWebhookBuilder::make() diff --git a/modules/Core/classes/Events/UserRegisteredEvent.php b/modules/Core/classes/Events/UserRegisteredEvent.php index e12568848a..11052d008c 100644 --- a/modules/Core/classes/Events/UserRegisteredEvent.php +++ b/modules/Core/classes/Events/UserRegisteredEvent.php @@ -1,6 +1,6 @@ get('admin', 'register_hook_info'); } - public function toDiscordWebook(): DiscordWebhookBuilder { + public function webhookParams(): array { + return [ + 'user_id' => $this->user->data()->id, + 'username' => $this->user->getDisplayname(), + 'profile_url' => URL::getSelfURL() . ltrim($this->user->getProfileURL(), '/') + ]; + } + + public function toDiscordWebhook(): DiscordWebhookBuilder { $language = new Language('core', DEFAULT_LANGUAGE); return DiscordWebhookBuilder::make() diff --git a/modules/Core/classes/Events/UserValidatedEvent.php b/modules/Core/classes/Events/UserValidatedEvent.php index 07b3427603..a7d3b4ed61 100644 --- a/modules/Core/classes/Events/UserValidatedEvent.php +++ b/modules/Core/classes/Events/UserValidatedEvent.php @@ -1,6 +1,6 @@ get('admin', 'validate_hook_info'); } - public function toDiscordWebook(): DiscordWebhookBuilder { + public function webhookParams(): array { + return [ + 'user_id' => $this->user->data()->id, + 'username' => $this->user->getDisplayname(), + 'profile_url' => URL::getSelfURL() . ltrim($this->user->getProfileURL(), '/') + ]; + } + + public function toDiscordWebhook(): DiscordWebhookBuilder { $language = new Language('core', DEFAULT_LANGUAGE); return DiscordWebhookBuilder::make() diff --git a/modules/Core/classes/Events/UserWarnedEvent.php b/modules/Core/classes/Events/UserWarnedEvent.php index 5e733eb4c4..5b5861d34f 100644 --- a/modules/Core/classes/Events/UserWarnedEvent.php +++ b/modules/Core/classes/Events/UserWarnedEvent.php @@ -1,6 +1,6 @@ get('admin', 'warning_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 + ]; + } + + public function toDiscordWebhook(): DiscordWebhookBuilder { $language = new Language('core', DEFAULT_LANGUAGE); return DiscordWebhookBuilder::make() diff --git a/modules/Core/hooks/WebHook.php b/modules/Core/hooks/WebHook.php index 2da0bec089..4007c487c1 100644 --- a/modules/Core/hooks/WebHook.php +++ b/modules/Core/hooks/WebHook.php @@ -1,20 +1,34 @@ webhookParams(); + } else if ($event instanceof AbstractEvent) { + ErrorHandler::logWarning('Event ' . $event::name() . ' does not implement HasWebhookParams, using `params()` instead'); + $params = $event->params(); + } else { + $params = $event; + } + + $webhook_url = $event instanceof AbstractEvent + ? $webhook_url + : $params['webhook']; - public static function execute(array $params = []): void { $return = $params; unset($return['webhook']); + $json = json_encode($return, JSON_UNESCAPED_SLASHES); - $httpClient = HttpClient::post($params['webhook'], $json, [ + $httpClient = HttpClient::post($webhook_url, $json, [ 'headers' => [ 'Content-Type' => 'application/json', ], diff --git a/modules/Core/pages/panel/hooks.php b/modules/Core/pages/panel/hooks.php index 8d2bbda362..64b8939713 100644 --- a/modules/Core/pages/panel/hooks.php +++ b/modules/Core/pages/panel/hooks.php @@ -114,8 +114,11 @@ 'HOOK_EVENTS' => $language->get('admin', 'hook_events'), 'BACK' => $language->get('general', 'back'), 'BACK_LINK' => URL::build('/panel/core/hooks'), + 'DISCORD' => $language->get('admin', 'discord_hook'), 'NORMAL' => $language->get('general', 'normal'), 'ALL_EVENTS' => EventHandler::getEvents(), + 'SUPPORTS_DISCORD' => $language->get('admin', 'event_supports_discord'), + 'SUPPORTS_NORMAL' => $language->get('admin', 'event_supports_normal'), ]); $template_file = 'core/hooks_new.tpl'; @@ -200,9 +203,12 @@ 'HOOK_EVENTS' => $language->get('admin', 'hook_events'), 'BACK' => $language->get('general', 'back'), 'BACK_LINK' => URL::build('/panel/core/hooks'), + 'DISCORD' => $language->get('admin', 'discord_hook'), 'NORMAL' => $language->get('general', 'normal'), 'ALL_EVENTS' => EventHandler::getEvents(), - 'ENABLED_HOOKS' => json_decode($hook->events, true) + 'ENABLED_HOOKS' => json_decode($hook->events, true), + 'SUPPORTS_DISCORD' => $language->get('admin', 'event_supports_discord'), + 'SUPPORTS_NORMAL' => $language->get('admin', 'event_supports_normal'), ]); $template_file = 'core/hooks_edit.tpl'; diff --git a/modules/Discord Integration/hooks/DiscordHook.php b/modules/Discord Integration/hooks/DiscordHook.php index 0964c26f4b..bf2a7601e1 100644 --- a/modules/Discord Integration/hooks/DiscordHook.php +++ b/modules/Discord Integration/hooks/DiscordHook.php @@ -1,17 +1,14 @@ params() @@ -26,7 +23,7 @@ public static function execute($event, string $webhook_url = ''): void { : $params['event']; $format = $event instanceof DiscordDispatchable - ? $event->toDiscordWebook() + ? $event->toDiscordWebhook() : []; $return = EventHandler::executeEvent(new DiscordWebhookFormatterEvent( @@ -35,6 +32,10 @@ public static function execute($event, string $webhook_url = ''): void { $params, ))['format']; + if (is_array($return) && isset($return['webhook'])) { + unset($return['webhook']); + } + if ($return instanceof DiscordWebhookBuilder) { $return = $return->toArray(); } diff --git a/modules/Forum/classes/Events/TopicCreatedEvent.php b/modules/Forum/classes/Events/TopicCreatedEvent.php index 4536cb2e3d..6fcfb21c3c 100644 --- a/modules/Forum/classes/Events/TopicCreatedEvent.php +++ b/modules/Forum/classes/Events/TopicCreatedEvent.php @@ -1,6 +1,6 @@ get('admin', 'announcement_hook_info'); + return (new Language(ROOT_PATH . '/modules/Forum/language'))->get('forum', 'new_topic'); } - public function toDiscordWebook(): DiscordWebhookBuilder { + public function webhookParams(): array { + $forum = new Forum(); + + return [ + 'user_id' => $this->creator->data()->id, + 'username' => $this->creator->getDisplayname(), + 'forum' => [ + 'title' => $this->forum_title + ], + 'topic' => [ + 'id' => $this->topic_id, + 'title' => $this->topic_title + ], + 'content' => $this->content, + 'url' => URL::getSelfURL() . ltrim(URL::build('/forum/topic/' . urlencode($this->topic_id) . '-' . $forum->titleToURL($this->topic_title)), '/') + ]; + } + + public function toDiscordWebhook(): DiscordWebhookBuilder { $language = new Language(ROOT_PATH . '/modules/Forum/language', DEFAULT_LANGUAGE); $forum = new Forum(); diff --git a/modules/Forum/classes/Events/TopicReplyCreatedEvent.php b/modules/Forum/classes/Events/TopicReplyCreatedEvent.php index b526355cdb..ca623a6117 100644 --- a/modules/Forum/classes/Events/TopicReplyCreatedEvent.php +++ b/modules/Forum/classes/Events/TopicReplyCreatedEvent.php @@ -1,6 +1,6 @@ get('forum', 'topic_reply'); } - public function toDiscordWebook(): DiscordWebhookBuilder { + public function webhookParams(): array { + $forum = new Forum(); + + return [ + 'user_id' => $this->creator->data()->id, + 'username' => $this->creator->getDisplayname(), + 'topic' => [ + 'id' => $this->topic_id, + 'title' => $this->topic_title + ], + 'content' => $this->content, + 'url' => URL::getSelfURL() . ltrim(URL::build('/forum/topic/' . urlencode($this->topic_id) . '-' . $forum->titleToURL($this->topic_title)), '/') + ]; + } + + public function toDiscordWebhook(): DiscordWebhookBuilder { $language = new Language(ROOT_PATH . '/modules/Forum/language', DEFAULT_LANGUAGE); $forum = new Forum(); diff --git a/modules/Forum/language/en_UK.json b/modules/Forum/language/en_UK.json index eac70563d7..851d7332b6 100644 --- a/modules/Forum/language/en_UK.json +++ b/modules/Forum/language/en_UK.json @@ -103,7 +103,6 @@ "forum/new_reply_in_topic": "{{author}} has replied to topic {{topic}}", "forum/new_search": "New Search", "forum/new_topic": "New Topic", - "forum/new_topic_hook_info": "New topic", "forum/new_topic_text": "Topic created in {{forum}} by {{author}}", "forum/news_items_front_page_limit": "Number of news items to display on front page", "forum/news_items_max": "Number of news items should be at least {{max}}", diff --git a/modules/Forum/pages/forum/new_topic.php b/modules/Forum/pages/forum/new_topic.php index 5360d24305..85d6733bf3 100644 --- a/modules/Forum/pages/forum/new_topic.php +++ b/modules/Forum/pages/forum/new_topic.php @@ -2,7 +2,7 @@ /* * Made by Samerton * https://github.com/NamelessMC/Nameless/ - * NamelessMC version 2.0.0-pr13 + * NamelessMC version 2.1.0 * * License: MIT * @@ -201,7 +201,7 @@ // Execute hooks and pass $available_hooks $available_hooks = DB::getInstance()->get('forums', ['id', $fid])->first(); - $available_hooks = json_decode($available_hooks->hooks); + $available_hooks = json_decode($available_hooks->hooks) ?? []; EventHandler::executeEvent(new TopicCreatedEvent( $user, $forum_title, diff --git a/modules/Forum/pages/forum/view_topic.php b/modules/Forum/pages/forum/view_topic.php index d379656b8d..223cf7b962 100644 --- a/modules/Forum/pages/forum/view_topic.php +++ b/modules/Forum/pages/forum/view_topic.php @@ -2,7 +2,7 @@ /* * Made by Samerton * https://github.com/NamelessMC/Nameless/ - * NamelessMC version 2.0.0-pr13 + * NamelessMC version 2.1.0 * * License: MIT * @@ -297,7 +297,7 @@ // Execute hooks and pass $available_hooks // TODO: This gets hooks only for this specific forum, not any of its parents... $available_hooks = DB::getInstance()->get('forums', ['id', $topic->forum_id])->first(); - $available_hooks = json_decode($available_hooks->hooks); + $available_hooks = json_decode($available_hooks->hooks) ?? []; EventHandler::executeEvent(new TopicReplyCreatedEvent( $user, $topic->topic_title,