Have a common PartListBuildingEvent, fixes #1124

This commit is contained in:
Shish 2024-03-28 12:42:39 +00:00 committed by Shish
parent 8d489b4f90
commit b59fe4c694
16 changed files with 90 additions and 91 deletions

View file

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace Shimmie2; namespace Shimmie2;
use MicroHTML\HTMLElement;
/** /**
* Generic parent class for all events. * Generic parent class for all events.
* *
@ -346,3 +348,32 @@ class LogEvent extends Event
class DatabaseUpgradeEvent extends Event class DatabaseUpgradeEvent extends Event
{ {
} }
/**
* @template T
*/
abstract class PartListBuildingEvent extends Event
{
/** @var T[] */
private array $parts = [];
/**
* @param T $html
*/
public function add_part(mixed $html, int $position = 50): void
{
while (isset($this->parts[$position])) {
$position++;
}
$this->parts[$position] = $html;
}
/**
* @return array<T>
*/
public function get_parts(): array
{
ksort($this->parts);
return $this->parts;
}
}

View file

@ -323,7 +323,7 @@ class CommentList extends Extension
$i_days_old = ((time() - \Safe\strtotime($event->display_user->join_date)) / 86400) + 1; $i_days_old = ((time() - \Safe\strtotime($event->display_user->join_date)) / 86400) + 1;
$i_comment_count = Comment::count_comments_by_user($event->display_user); $i_comment_count = Comment::count_comments_by_user($event->display_user);
$h_comment_rate = sprintf("%.1f", ($i_comment_count / $i_days_old)); $h_comment_rate = sprintf("%.1f", ($i_comment_count / $i_days_old));
$event->add_stats("Comments made: $i_comment_count, $h_comment_rate per day"); $event->add_part("Comments made: $i_comment_count, $h_comment_rate per day");
$recent = $this->get_user_comments($event->display_user->id, 10); $recent = $this->get_user_comments($event->display_user->id, 10);
$this->theme->display_recent_user_comments($recent, $event->display_user); $this->theme->display_recent_user_comments($recent, $event->display_user);

View file

@ -87,7 +87,7 @@ class Favorites extends Extension
$i_days_old = ((time() - \Safe\strtotime($event->display_user->join_date)) / 86400) + 1; $i_days_old = ((time() - \Safe\strtotime($event->display_user->join_date)) / 86400) + 1;
$h_favorites_rate = sprintf("%.1f", ($i_favorites_count / $i_days_old)); $h_favorites_rate = sprintf("%.1f", ($i_favorites_count / $i_days_old));
$favorites_link = search_link(["favorited_by={$event->display_user->name}"]); $favorites_link = search_link(["favorited_by={$event->display_user->name}"]);
$event->add_stats("<a href='$favorites_link'>Posts favorited</a>: $i_favorites_count, $h_favorites_rate per day"); $event->add_part("<a href='$favorites_link'>Posts favorited</a>: $i_favorites_count, $h_favorites_rate per day");
} }
public function onImageInfoSet(ImageInfoSetEvent $event): void public function onImageInfoSet(ImageInfoSetEvent $event): void

View file

@ -87,8 +87,8 @@ class Forum extends Extension
$threads_rate = sprintf("%.1f", ($threads_count / $days_old)); $threads_rate = sprintf("%.1f", ($threads_count / $days_old));
$posts_rate = sprintf("%.1f", ($posts_count / $days_old)); $posts_rate = sprintf("%.1f", ($posts_count / $days_old));
$event->add_stats("Forum threads: $threads_count, $threads_rate per day"); $event->add_part("Forum threads: $threads_count, $threads_rate per day");
$event->add_stats("Forum posts: $posts_count, $posts_rate per day"); $event->add_part("Forum posts: $posts_count, $posts_rate per day");
} }
public function onPageNavBuilding(PageNavBuildingEvent $event): void public function onPageNavBuilding(PageNavBuildingEvent $event): void

View file

@ -15,11 +15,12 @@ class HelpPageListBuildingEvent extends Event
} }
} }
class HelpPageBuildingEvent extends Event /**
* @extends PartListBuildingEvent<Block>
*/
class HelpPageBuildingEvent extends PartListBuildingEvent
{ {
public string $key; public string $key;
/** @var array<int,Block> */
public array $blocks = [];
public function __construct(string $key) public function __construct(string $key)
{ {
@ -29,10 +30,7 @@ class HelpPageBuildingEvent extends Event
public function add_block(Block $block, int $position = 50): void public function add_block(Block $block, int $position = 50): void
{ {
while (array_key_exists($position, $this->blocks)) { $this->add_part($block, $position);
$position++;
}
$this->blocks[$position] = $block;
} }
} }
@ -58,8 +56,7 @@ class HelpPages extends Extension
$this->theme->display_help_page($title); $this->theme->display_help_page($title);
$hpbe = send_event(new HelpPageBuildingEvent($name)); $hpbe = send_event(new HelpPageBuildingEvent($name));
ksort($hpbe->blocks); foreach ($hpbe->get_parts() as $block) {
foreach ($hpbe->blocks as $block) {
$page->add_block($block); $page->add_block($block);
} }
} elseif ($event->page_matches("help")) { } elseif ($event->page_matches("help")) {

View file

@ -155,7 +155,7 @@ class ImageIO extends Extension
$i_days_old = ((time() - \Safe\strtotime($event->display_user->join_date)) / 86400) + 1; $i_days_old = ((time() - \Safe\strtotime($event->display_user->join_date)) / 86400) + 1;
$h_image_rate = sprintf("%.1f", ($i_image_count / $i_days_old)); $h_image_rate = sprintf("%.1f", ($i_image_count / $i_days_old));
$images_link = search_link(["user=$u_name"]); $images_link = search_link(["user=$u_name"]);
$event->add_stats("<a href='$images_link'>Posts uploaded</a>: $i_image_count, $h_image_rate per day"); $event->add_part("<a href='$images_link'>Posts uploaded</a>: $i_image_count, $h_image_rate per day");
} }
public function onSetupBuilding(SetupBuildingEvent $event): void public function onSetupBuilding(SetupBuildingEvent $event): void

View file

@ -131,7 +131,7 @@ class NumericScore extends Extension
$link_up = search_link(["upvoted_by={$event->display_user->name}"]); $link_up = search_link(["upvoted_by={$event->display_user->name}"]);
$n_down = Search::count_images(["downvoted_by={$event->display_user->name}"]); $n_down = Search::count_images(["downvoted_by={$event->display_user->name}"]);
$link_down = search_link(["downvoted_by={$event->display_user->name}"]); $link_down = search_link(["downvoted_by={$event->display_user->name}"]);
$event->add_stats("<a href='$link_up'>$n_up Upvotes</a> / <a href='$link_down'>$n_down Downvotes</a>"); $event->add_part("<a href='$link_up'>$n_up Upvotes</a> / <a href='$link_down'>$n_down Downvotes</a>");
} }
public function onPageRequest(PageRequestEvent $event): void public function onPageRequest(PageRequestEvent $event): void

View file

@ -28,8 +28,7 @@ class ReportImageTheme extends Themelet
$userlink = "<a href='".make_link("user/$reporter_name")."'>$reporter_name</a>"; $userlink = "<a href='".make_link("user/$reporter_name")."'>$reporter_name</a>";
$iabbe = send_event(new ImageAdminBlockBuildingEvent($image, $user, "report")); $iabbe = send_event(new ImageAdminBlockBuildingEvent($image, $user, "report"));
ksort($iabbe->parts); $actions = join("", $iabbe->get_parts());
$actions = join("", $iabbe->parts);
$h_reportedimages .= " $h_reportedimages .= "
<tr> <tr>

View file

@ -6,53 +6,40 @@ namespace Shimmie2;
use MicroHTML\HTMLElement; use MicroHTML\HTMLElement;
class UserBlockBuildingEvent extends Event /**
* @extends PartListBuildingEvent<array{name: string|HTMLElement, link: string}>
*/
class UserBlockBuildingEvent extends PartListBuildingEvent
{ {
/** @var array<int, array{name: string|HTMLElement, link: string}> */
public array $parts = [];
public function add_link(string|HTMLElement $name, string $link, int $position = 50): void public function add_link(string|HTMLElement $name, string $link, int $position = 50): void
{ {
while (isset($this->parts[$position])) { $this->add_part(["name" => $name, "link" => $link], $position);
$position++;
}
$this->parts[$position] = ["name" => $name, "link" => $link];
} }
} }
class UserOperationsBuildingEvent extends Event /**
* @extends PartListBuildingEvent<HTMLElement>
*/
class UserOperationsBuildingEvent extends PartListBuildingEvent
{ {
/** @var string[] */ public function __construct(
public array $parts = []; public User $user,
public BaseConfig $user_config,
public function __construct(public User $user, public BaseConfig $user_config) ) {
{
parent::__construct(); parent::__construct();
} }
public function add_html(string $html): void
{
$this->parts[] = $html;
}
} }
class UserPageBuildingEvent extends Event /**
* @extends PartListBuildingEvent<string>
*/
class UserPageBuildingEvent extends PartListBuildingEvent
{ {
/** @var array<int, string> */ public function __construct(
public array $stats = []; public User $display_user,
) {
public function __construct(public User $display_user)
{
parent::__construct(); parent::__construct();
} }
public function add_stats(string $html, int $position = 50): void
{
while (isset($this->stats[$position])) {
$position++;
}
$this->stats[$position] = $html;
}
} }
class UserCreationEvent extends Event class UserCreationEvent extends Event

View file

@ -340,22 +340,22 @@ class UserPage extends Extension
$h_class = $event->display_user->class->name; $h_class = $event->display_user->class->name;
} }
$event->add_stats("Joined: $h_join_date", 10); $event->add_part("Joined: $h_join_date", 10);
if ($user->name == $event->display_user->name) { if ($user->name == $event->display_user->name) {
$event->add_stats("Current IP: " . get_real_ip(), 80); $event->add_part("Current IP: " . get_real_ip(), 80);
} }
$event->add_stats("Class: $h_class", 90); $event->add_part("Class: $h_class", 90);
$av = $event->display_user->get_avatar_html(); $av = $event->display_user->get_avatar_html();
if ($av) { if ($av) {
$event->add_stats($av, 0); $event->add_part($av, 0);
} elseif ( } elseif (
( (
$config->get_string("avatar_host") == "gravatar" $config->get_string("avatar_host") == "gravatar"
) && ) &&
($user->id == $event->display_user->id) ($user->id == $event->display_user->id)
) { ) {
$event->add_stats( $event->add_part(
"No avatar? This gallery uses <a href='https://gravatar.com'>Gravatar</a> for avatar hosting, use the" . "No avatar? This gallery uses <a href='https://gravatar.com'>Gravatar</a> for avatar hosting, use the" .
"<br>same email address here and there to have your avatar synced<br>", "<br>same email address here and there to have your avatar synced<br>",
0 0
@ -377,8 +377,7 @@ class UserPage extends Extension
{ {
global $user, $page, $config; global $user, $page, $config;
ksort($event->stats); $this->theme->display_user_page($event->display_user, $event->get_parts());
$this->theme->display_user_page($event->display_user, $event->stats);
if (!$user->is_anonymous()) { if (!$user->is_anonymous()) {
if ($user->id == $event->display_user->id || $user->can("edit_user_info")) { if ($user->id == $event->display_user->id || $user->can("edit_user_info")) {
@ -391,8 +390,7 @@ class UserPage extends Extension
if ($user->id == $event->display_user->id) { if ($user->id == $event->display_user->id) {
$ubbe = send_event(new UserBlockBuildingEvent()); $ubbe = send_event(new UserBlockBuildingEvent());
ksort($ubbe->parts); $this->theme->display_user_links($page, $user, $ubbe->get_parts());
$this->theme->display_user_links($page, $user, $ubbe->parts);
} }
if ( if (
($user->can(Permissions::VIEW_IP) || ($user->is_logged_in() && $user->id == $event->display_user->id)) && # admin or self-user ($user->can(Permissions::VIEW_IP) || ($user->is_logged_in() && $user->id == $event->display_user->id)) && # admin or self-user
@ -606,8 +604,7 @@ class UserPage extends Extension
$this->theme->display_login_block($page); $this->theme->display_login_block($page);
} else { } else {
$ubbe = send_event(new UserBlockBuildingEvent()); $ubbe = send_event(new UserBlockBuildingEvent());
ksort($ubbe->parts); $this->theme->display_user_block($page, $user, $ubbe->get_parts());
$this->theme->display_user_block($page, $user, $ubbe->parts);
} }
} }

View file

@ -336,7 +336,7 @@ class UserPageTheme extends Themelet
)); ));
} }
foreach ($event->parts as $part) { foreach ($event->get_parts() as $part) {
$html .= $part; $html .= $part;
} }
} }

View file

@ -166,7 +166,7 @@ class UserConfig extends Extension
$key = generate_key(); $key = generate_key();
$event->user_config->set_string(self::API_KEY, $key); $event->user_config->set_string(self::API_KEY, $key);
} }
$event->add_html($this->theme->get_user_operations($key)); $event->add_part($this->theme->get_user_operations($key));
} }
} }

View file

@ -4,9 +4,13 @@ declare(strict_types=1);
namespace Shimmie2; namespace Shimmie2;
use MicroHTML\HTMLElement;
use function MicroHTML\rawHTML;
class UserConfigTheme extends Themelet class UserConfigTheme extends Themelet
{ {
public function get_user_operations(string $key): string public function get_user_operations(string $key): HTMLElement
{ {
$html = " $html = "
<p>".make_form(make_link("user_admin/reset_api_key"))." <p>".make_form(make_link("user_admin/reset_api_key"))."
@ -24,7 +28,7 @@ class UserConfigTheme extends Themelet
</table> </table>
</form> </form>
"; ";
return $html; return rawHTML($html);
} }

View file

@ -8,10 +8,11 @@ use MicroHTML\HTMLElement;
use function MicroHTML\{FORM,INPUT}; use function MicroHTML\{FORM,INPUT};
class ImageAdminBlockBuildingEvent extends Event /**
* @extends PartListBuildingEvent<HTMLElement>
*/
class ImageAdminBlockBuildingEvent extends PartListBuildingEvent
{ {
/** @var HTMLElement[] */
public array $parts = [];
public Image $image; public Image $image;
public User $user; public User $user;
public string $context; public string $context;
@ -24,14 +25,6 @@ class ImageAdminBlockBuildingEvent extends Event
$this->context = $context; $this->context = $context;
} }
public function add_part(HTMLElement $html, int $position = 50): void
{
while (isset($this->parts[$position])) {
$position++;
}
$this->parts[$position] = $html;
}
public function add_button(string $name, string $path, int $position = 50): void public function add_button(string $name, string $path, int $position = 50): void
{ {
$this->add_part( $this->add_part(

View file

@ -6,10 +6,11 @@ namespace Shimmie2;
use MicroHTML\HTMLElement; use MicroHTML\HTMLElement;
class ImageInfoBoxBuildingEvent extends Event /**
* @extends PartListBuildingEvent<HTMLElement>
*/
class ImageInfoBoxBuildingEvent extends PartListBuildingEvent
{ {
/** @var HTMLElement[] */
public array $parts = [];
public Image $image; public Image $image;
public User $user; public User $user;
@ -19,12 +20,4 @@ class ImageInfoBoxBuildingEvent extends Event
$this->image = $image; $this->image = $image;
$this->user = $user; $this->user = $user;
} }
public function add_part(HTMLElement $html, int $position = 50): void
{
while (isset($this->parts[$position])) {
$position++;
}
$this->parts[$position] = $html;
}
} }

View file

@ -97,12 +97,10 @@ class ViewPost extends Extension
$this->theme->display_meta_headers($image); $this->theme->display_meta_headers($image);
$iibbe = send_event(new ImageInfoBoxBuildingEvent($image, $user)); $iibbe = send_event(new ImageInfoBoxBuildingEvent($image, $user));
ksort($iibbe->parts); $this->theme->display_page($image, $iibbe->get_parts());
$this->theme->display_page($image, $iibbe->parts);
$iabbe = send_event(new ImageAdminBlockBuildingEvent($image, $user, "view")); $iabbe = send_event(new ImageAdminBlockBuildingEvent($image, $user, "view"));
ksort($iabbe->parts); $this->theme->display_admin_block($page, $iabbe->get_parts());
$this->theme->display_admin_block($page, $iabbe->parts);
} }
public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event): void public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event): void