2020-08-04 11:25:22 +00:00
|
|
|
<?php
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
namespace Config;
|
2020-05-27 18:46:16 +02:00
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
use App\Entities\Actor;
|
2021-08-13 11:07:29 +00:00
|
|
|
use App\Entities\Post;
|
2020-05-27 18:46:16 +02:00
|
|
|
use CodeIgniter\Events\Events;
|
2021-04-02 17:20:02 +00:00
|
|
|
use CodeIgniter\Exceptions\FrameworkException;
|
2021-08-23 11:05:16 +00:00
|
|
|
use Modules\Auth\Entities\User;
|
2020-05-27 18:46:16 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* --------------------------------------------------------------------
|
|
|
|
* Application Events
|
|
|
|
* --------------------------------------------------------------------
|
|
|
|
* Events allow you to tap into the execution of the program without
|
|
|
|
* modifying or extending core files. This file provides a central
|
|
|
|
* location to define your events, though they can always be added
|
|
|
|
* at run-time, also, if needed.
|
|
|
|
*
|
|
|
|
* You create code that can execute by subscribing to events with
|
|
|
|
* the 'on()' method. This accepts any form of callable, including
|
|
|
|
* Closures, that will be executed when the event is triggered.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* Events::on('create', [$myInstance, 'myMethod']);
|
|
|
|
*/
|
|
|
|
|
|
|
|
Events::on('pre_system', function () {
|
2021-05-12 14:00:25 +00:00
|
|
|
// @phpstan-ignore-next-line
|
2020-06-10 15:00:12 +00:00
|
|
|
if (ENVIRONMENT !== 'testing') {
|
2021-04-02 17:20:02 +00:00
|
|
|
if (ini_get('zlib.output_compression')) {
|
|
|
|
throw FrameworkException::forEnabledZlibOutputCompression();
|
2020-06-10 15:00:12 +00:00
|
|
|
}
|
2020-05-27 18:46:16 +02:00
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
while (ob_get_level() > 0) {
|
|
|
|
ob_end_flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
ob_start(function ($buffer) {
|
2020-06-10 15:00:12 +00:00
|
|
|
return $buffer;
|
|
|
|
});
|
|
|
|
}
|
2020-05-27 18:46:16 +02:00
|
|
|
|
2020-06-10 15:00:12 +00:00
|
|
|
/*
|
|
|
|
* --------------------------------------------------------------------
|
|
|
|
* Debug Toolbar Listeners.
|
|
|
|
* --------------------------------------------------------------------
|
|
|
|
* If you delete, they will no longer be collected.
|
2021-05-12 14:00:25 +00:00
|
|
|
*
|
|
|
|
* @phpstan-ignore-next-line
|
2020-06-10 15:00:12 +00:00
|
|
|
*/
|
2021-05-25 10:40:22 +00:00
|
|
|
if (CI_DEBUG && ! is_cli()) {
|
2021-06-08 09:52:11 +00:00
|
|
|
Events::on('DBQuery', 'CodeIgniter\Debug\Toolbar\Collectors\Database::collect');
|
2020-06-10 15:00:12 +00:00
|
|
|
Services::toolbar()->respond();
|
|
|
|
}
|
2020-05-27 18:46:16 +02:00
|
|
|
});
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
Events::on('login', function (User $user): void {
|
2021-04-02 17:20:02 +00:00
|
|
|
helper('auth');
|
|
|
|
|
|
|
|
// set interact_as_actor_id value
|
|
|
|
$userPodcasts = $user->podcasts;
|
|
|
|
if ($userPodcasts = $user->podcasts) {
|
2021-04-14 10:43:43 +00:00
|
|
|
set_interact_as_actor($userPodcasts[0]->actor_id);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
Events::on('logout', function (User $user): void {
|
2021-04-02 17:20:02 +00:00
|
|
|
helper('auth');
|
|
|
|
|
|
|
|
// remove user's interact_as_actor session
|
2021-04-14 10:43:43 +00:00
|
|
|
remove_interact_as_actor();
|
2021-04-02 17:20:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
* --------------------------------------------------------------------
|
2021-08-27 10:58:22 +00:00
|
|
|
* Fediverse events
|
2021-04-02 17:20:02 +00:00
|
|
|
* --------------------------------------------------------------------
|
|
|
|
*/
|
2021-06-08 09:52:11 +00:00
|
|
|
/**
|
|
|
|
* @param Actor $actor
|
|
|
|
* @param Actor $targetActor
|
|
|
|
*/
|
|
|
|
Events::on('on_follow', function ($actor, $targetActor): void {
|
|
|
|
if ($actor->is_podcast) {
|
|
|
|
cache()
|
|
|
|
->deleteMatching("podcast#{$actor->podcast->id}*");
|
|
|
|
cache()
|
|
|
|
->deleteMatching("page_podcast#{$actor->podcast->id}*");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($targetActor->is_podcast) {
|
|
|
|
cache()
|
|
|
|
->deleteMatching("podcast#{$targetActor->podcast->id}*");
|
|
|
|
cache()
|
|
|
|
->deleteMatching("page_podcast#{$targetActor->podcast->id}*");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Actor $actor
|
|
|
|
* @param Actor $targetActor
|
|
|
|
*/
|
|
|
|
Events::on('on_undo_follow', function ($actor, $targetActor): void {
|
|
|
|
if ($actor->is_podcast) {
|
|
|
|
cache()
|
|
|
|
->deleteMatching("podcast#{$actor->podcast->id}*");
|
|
|
|
cache()
|
|
|
|
->deleteMatching("page_podcast#{$actor->podcast->id}*");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($targetActor->is_podcast) {
|
|
|
|
cache()
|
|
|
|
->deleteMatching("podcast#{$targetActor->podcast->id}*");
|
|
|
|
cache()
|
|
|
|
->deleteMatching("page_podcast#{$targetActor->podcast->id}*");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2021-08-13 11:07:29 +00:00
|
|
|
* @param Post $post
|
2021-06-08 09:52:11 +00:00
|
|
|
*/
|
2021-08-13 11:07:29 +00:00
|
|
|
Events::on('on_post_add', function ($post): void {
|
|
|
|
$isReply = $post->in_reply_to_id !== null;
|
|
|
|
|
|
|
|
if ($isReply) {
|
|
|
|
$post = $post->reply_to_post;
|
2021-06-08 09:52:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 11:07:29 +00:00
|
|
|
if ($post->episode_id !== null) {
|
|
|
|
if ($isReply) {
|
|
|
|
model('EpisodeModel', false)
|
|
|
|
->where('id', $post->episode_id)
|
|
|
|
->increment('comments_count');
|
|
|
|
} else {
|
|
|
|
model('EpisodeModel', false)
|
|
|
|
->where('id', $post->episode_id)
|
|
|
|
->increment('posts_count');
|
|
|
|
}
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
2021-04-22 17:20:28 +00:00
|
|
|
|
2021-08-13 11:07:29 +00:00
|
|
|
if ($post->actor->is_podcast) {
|
2021-06-08 09:52:11 +00:00
|
|
|
// Removing all of the podcast pages is a bit overkill, but works to avoid caching bugs
|
|
|
|
// same for other events below
|
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("podcast#{$post->actor->podcast->id}*");
|
2021-06-08 09:52:11 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("page_podcast#{$post->actor->podcast->id}*");
|
2021-06-08 09:52:11 +00:00
|
|
|
}
|
2021-04-02 17:20:02 +00:00
|
|
|
});
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
/**
|
2021-08-13 11:07:29 +00:00
|
|
|
* @param Post $post
|
2021-06-08 09:52:11 +00:00
|
|
|
*/
|
2021-08-13 11:07:29 +00:00
|
|
|
Events::on('on_post_remove', function ($post): void {
|
|
|
|
if ($post->in_reply_to_id !== null) {
|
|
|
|
Events::trigger('on_post_remove', $post->reply_to_post);
|
2021-06-08 09:52:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 11:07:29 +00:00
|
|
|
if ($episodeId = $post->episode_id) {
|
|
|
|
model('EpisodeModel', false)
|
2021-06-08 09:52:11 +00:00
|
|
|
->where('id', $episodeId)
|
2021-08-13 11:07:29 +00:00
|
|
|
->decrement('posts_count');
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
2021-04-22 17:20:28 +00:00
|
|
|
|
2021-08-13 11:07:29 +00:00
|
|
|
if ($post->actor->is_podcast) {
|
2021-06-08 09:52:11 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("podcast#{$post->actor->podcast->id}*");
|
2021-06-08 09:52:11 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("page_podcast#{$post->actor->podcast->id}*");
|
2021-06-08 09:52:11 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("page_post#{$post->id}*");
|
2021-04-02 17:20:02 +00:00
|
|
|
});
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
/**
|
|
|
|
* @param Actor $actor
|
2021-08-13 11:07:29 +00:00
|
|
|
* @param Post $post
|
2021-06-08 09:52:11 +00:00
|
|
|
*/
|
2021-08-13 11:07:29 +00:00
|
|
|
Events::on('on_post_reblog', function ($actor, $post): void {
|
|
|
|
if ($post->actor->is_podcast) {
|
2021-06-08 09:52:11 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("podcast#{$post->actor->podcast->id}*");
|
2021-06-08 09:52:11 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("page_podcast#{$post->actor->podcast->id}*");
|
2021-06-08 09:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($actor->is_podcast) {
|
|
|
|
cache()->deleteMatching("podcast#{$actor->podcast->id}*");
|
|
|
|
cache()
|
|
|
|
->deleteMatching("page_podcast#{$actor->podcast->id}*");
|
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("page_post#{$post->id}*");
|
2021-04-22 17:20:28 +00:00
|
|
|
|
2021-08-13 11:07:29 +00:00
|
|
|
if ($post->in_reply_to_id !== null) {
|
|
|
|
cache()->deleteMatching("page_post#{$post->in_reply_to_id}");
|
2021-04-22 17:20:28 +00:00
|
|
|
}
|
2021-04-02 17:20:02 +00:00
|
|
|
});
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
/**
|
2021-08-13 11:07:29 +00:00
|
|
|
* @param Post $reblogPost
|
2021-06-08 09:52:11 +00:00
|
|
|
*/
|
2021-08-13 11:07:29 +00:00
|
|
|
Events::on('on_post_undo_reblog', function ($reblogPost): void {
|
|
|
|
$post = $reblogPost->reblog_of_post;
|
2021-04-22 17:20:28 +00:00
|
|
|
|
2021-08-13 11:07:29 +00:00
|
|
|
if ($post->actor->is_podcast) {
|
2021-06-08 09:52:11 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("podcast#{$post->actor->podcast->id}*");
|
2021-06-08 09:52:11 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("page_podcast#{$post->actor->podcast->id}*");
|
2021-06-08 09:52:11 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("page_post#{$post->id}*");
|
2021-06-08 09:52:11 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("page_post#{$reblogPost->id}*");
|
2021-06-08 09:52:11 +00:00
|
|
|
|
2021-08-13 11:07:29 +00:00
|
|
|
if ($post->in_reply_to_id !== null) {
|
|
|
|
cache()->deleteMatching("page_post#{$post->in_reply_to_id}");
|
2021-06-08 09:52:11 +00:00
|
|
|
}
|
2021-04-22 17:20:28 +00:00
|
|
|
|
2021-08-13 11:07:29 +00:00
|
|
|
if ($reblogPost->actor->is_podcast) {
|
2021-06-08 09:52:11 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("podcast#{$reblogPost->actor->podcast->id}*");
|
2021-06-08 09:52:11 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("page_podcast#{$reblogPost->actor->podcast->id}*");
|
2021-04-22 17:20:28 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
/**
|
2021-08-13 11:07:29 +00:00
|
|
|
* @param Post $reply
|
2021-06-08 09:52:11 +00:00
|
|
|
*/
|
2021-08-13 11:07:29 +00:00
|
|
|
Events::on('on_post_reply', function ($reply): void {
|
|
|
|
$post = $reply->reply_to_post;
|
2021-04-22 17:20:28 +00:00
|
|
|
|
2021-08-13 11:07:29 +00:00
|
|
|
if ($post->actor->is_podcast) {
|
2021-06-08 09:52:11 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("podcast#{$post->actor->podcast->id}*");
|
2021-06-08 09:52:11 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("page_podcast#{$post->actor->podcast->id}*");
|
2021-06-08 09:52:11 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("page_post#{$post->id}*");
|
2021-04-22 17:20:28 +00:00
|
|
|
});
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
/**
|
2021-08-13 11:07:29 +00:00
|
|
|
* @param Post $reply
|
2021-06-08 09:52:11 +00:00
|
|
|
*/
|
|
|
|
Events::on('on_reply_remove', function ($reply): void {
|
2021-08-13 11:07:29 +00:00
|
|
|
$post = $reply->reply_to_post;
|
2021-04-22 17:20:28 +00:00
|
|
|
|
2021-08-13 11:07:29 +00:00
|
|
|
if ($post->actor->is_podcast) {
|
2021-06-08 09:52:11 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("page_podcast#{$post->actor->podcast->id}*");
|
2021-06-08 09:52:11 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("podcast#{$post->actor->podcast->id}*");
|
2021-06-08 09:52:11 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("page_post#{$post->id}*");
|
2021-06-08 09:52:11 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("page_post#{$reply->id}*");
|
2021-04-02 17:20:02 +00:00
|
|
|
});
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
/**
|
|
|
|
* @param Actor $actor
|
2021-08-13 11:07:29 +00:00
|
|
|
* @param Post $post
|
2021-06-08 09:52:11 +00:00
|
|
|
*/
|
2021-08-13 11:07:29 +00:00
|
|
|
Events::on('on_post_favourite', function ($actor, $post): void {
|
|
|
|
if ($post->actor->is_podcast) {
|
2021-06-08 09:52:11 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("podcast#{$post->actor->podcast->id}*");
|
2021-06-08 09:52:11 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("page_podcast#{$post->actor->podcast->id}*");
|
2021-06-08 09:52:11 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("page_post#{$post->id}*");
|
2021-04-22 17:20:28 +00:00
|
|
|
|
2021-08-13 11:07:29 +00:00
|
|
|
if ($post->in_reply_to_id !== null) {
|
|
|
|
cache()->deleteMatching("page_post#{$post->in_reply_to_id}*");
|
2021-04-22 17:20:28 +00:00
|
|
|
}
|
2021-06-08 09:52:11 +00:00
|
|
|
|
|
|
|
if ($actor->is_podcast) {
|
|
|
|
cache()->deleteMatching("podcast#{$actor->podcast->id}*");
|
|
|
|
cache()
|
|
|
|
->deleteMatching("page_podcast#{$actor->podcast->id}*");
|
|
|
|
}
|
2021-04-02 17:20:02 +00:00
|
|
|
});
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
/**
|
|
|
|
* @param Actor $actor
|
2021-08-13 11:07:29 +00:00
|
|
|
* @param Post $post
|
2021-06-08 09:52:11 +00:00
|
|
|
*/
|
2021-08-13 11:07:29 +00:00
|
|
|
Events::on('on_post_undo_favourite', function ($actor, $post): void {
|
|
|
|
if ($post->actor->is_podcast) {
|
2021-06-08 09:52:11 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("podcast#{$post->actor->podcast->id}*");
|
2021-06-08 09:52:11 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("page_podcast#{$post->actor->podcast->id}*");
|
2021-06-08 09:52:11 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching("page_post#{$post->id}*");
|
2021-04-22 17:20:28 +00:00
|
|
|
|
2021-08-13 11:07:29 +00:00
|
|
|
if ($post->in_reply_to_id !== null) {
|
|
|
|
cache()->deleteMatching("page_post#{$post->in_reply_to_id}*");
|
2021-04-22 17:20:28 +00:00
|
|
|
}
|
2021-06-08 09:52:11 +00:00
|
|
|
|
|
|
|
if ($actor->is_podcast) {
|
|
|
|
cache()->deleteMatching("podcast#{$actor->podcast->id}*");
|
|
|
|
cache()
|
|
|
|
->deleteMatching("page_podcast#{$actor->podcast->id}*");
|
|
|
|
}
|
2021-04-22 17:20:28 +00:00
|
|
|
});
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
Events::on('on_block_actor', function (int $actorId): void {
|
2021-04-22 17:20:28 +00:00
|
|
|
cache()->deleteMatching('page_podcast*');
|
2021-05-25 18:00:09 +00:00
|
|
|
cache()
|
|
|
|
->deleteMatching('podcast*');
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching('page_post*');
|
2021-04-22 17:20:28 +00:00
|
|
|
});
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
Events::on('on_unblock_actor', function (int $actorId): void {
|
2021-04-22 17:20:28 +00:00
|
|
|
cache()->deleteMatching('page_podcast*');
|
2021-05-25 18:00:09 +00:00
|
|
|
cache()
|
|
|
|
->deleteMatching('podcast*');
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching('page_post*');
|
2021-04-22 17:20:28 +00:00
|
|
|
});
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
Events::on('on_block_domain', function (string $domainName): void {
|
2021-04-22 17:20:28 +00:00
|
|
|
cache()->deleteMatching('page_podcast*');
|
2021-05-25 18:00:09 +00:00
|
|
|
cache()
|
|
|
|
->deleteMatching('podcast*');
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching('page_post*');
|
2021-04-22 17:20:28 +00:00
|
|
|
});
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
Events::on('on_unblock_domain', function (string $domainName): void {
|
2021-04-22 17:20:28 +00:00
|
|
|
cache()->deleteMatching('page_podcast*');
|
2021-05-25 18:00:09 +00:00
|
|
|
cache()
|
|
|
|
->deleteMatching('podcast*');
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
2021-08-13 11:07:29 +00:00
|
|
|
->deleteMatching('page_post*');
|
2021-04-02 17:20:02 +00:00
|
|
|
});
|