Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
Giraffaman committed Apr 12, 2024
2 parents e2a4e11 + 426f30e commit 29cf5ff
Show file tree
Hide file tree
Showing 43 changed files with 984 additions and 204 deletions.
31 changes: 31 additions & 0 deletions core/event.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Shimmie2;

use MicroHTML\HTMLElement;

/**
* Generic parent class for all events.
*
Expand Down Expand Up @@ -346,3 +348,32 @@ public function __construct(string $section, int $priority, string $message)
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;
}
}
2 changes: 1 addition & 1 deletion ext/comment/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public function onUserPageBuilding(UserPageBuildingEvent $event): void
$i_days_old = ((time() - \Safe\strtotime($event->display_user->join_date)) / 86400) + 1;
$i_comment_count = Comment::count_comments_by_user($event->display_user);
$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);
$this->theme->display_recent_user_comments($recent, $event->display_user);
Expand Down
2 changes: 1 addition & 1 deletion ext/favorites/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function onUserPageBuilding(UserPageBuildingEvent $event): void
$i_days_old = ((time() - \Safe\strtotime($event->display_user->join_date)) / 86400) + 1;
$h_favorites_rate = sprintf("%.1f", ($i_favorites_count / $i_days_old));
$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
Expand Down
4 changes: 2 additions & 2 deletions ext/featured/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public function onInitExt(InitExtEvent $event): void
public function onPageRequest(PageRequestEvent $event): void
{
global $config, $page, $user;
if ($event->page_matches("featured_image/set", method: "POST", permission: Permissions::EDIT_FEATURE)) {
$id = int_escape($event->req_POST('image_id'));
if ($event->page_matches("featured_image/set/{image_id}", method: "POST", permission: Permissions::EDIT_FEATURE)) {
$id = $event->get_iarg('image_id');
$config->set_int("featured_id", $id);
log_info("featured", "Featured post set to >>$id", "Featured post set");
$page->set_mode(PageMode::REDIRECT);
Expand Down
7 changes: 3 additions & 4 deletions ext/featured/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ public function testFeatured(): void
# FIXME: test that regular users can't feature things

// Admin can feature things
// FIXME: use Event rather than modifying database
// $this->log_in_as_admin();
// send_event(new SetFeaturedEvent($image_id));
$config->set_int("featured_id", $image_id);
$this->log_in_as_admin();
$page = $this->post_page("featured_image/set/$image_id");
$this->assertEquals(302, $page->code);

$this->get_page("post/list");
$this->assert_text("Featured Post");
Expand Down
17 changes: 17 additions & 0 deletions ext/filter/info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Shimmie2;

class FilterInfo extends ExtensionInfo
{
public const KEY = "filter";

public string $key = self::KEY;
public string $name = "Filter Tags";
public array $authors = ["Danbooru Project" => "", "Discomrade" => ""];
public string $license = "WTFPL";
public string $description = "Allow users to filter out tags.";
public ?string $documentation = "Admins can set default filters and users can override them in user settings. This is derived from Danbooru's blacklist code, it works in the user's browser with JavaScript, and will hide posts until the filter runs.";
}
49 changes: 49 additions & 0 deletions ext/filter/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Shimmie2;

class Filter extends Extension
{
/** @var FilterTheme */
protected Themelet $theme;

public function onInitExt(InitExtEvent $event): void
{
global $config;
$config->set_default_string("filter_tags", "spoilers\nguro\nscat\nfurry -rating:s\n");
}

public function onPageRequest(PageRequestEvent $event): void
{
global $page;
$this->theme->addFilterBox();
$page->add_html_header("<script>
Array.from(document.getElementsByClassName('thumb')).forEach(function(post) {
post.style.display='none';
});</script>");
}

public function onSetupBuilding(SetupBuildingEvent $event): void
{
$sb = $event->panel->create_new_block("Filters");
$sb->add_longtext_option("filter_tags", 'Default filtered tags');
$sb->add_label("This controls the tags which are hidden by default. This feature currently requires JavaScript. Separate filters by line, or by commas. You can enter multiple tags per filter, as well as negative tags.");
}

public function onInitUserConfig(InitUserConfigEvent $event): void
{
global $config;
$event->user_config->set_default_string("filter_tags", $config->get_string("filter_tags"));
}

public function onUserOptionsBuilding(UserOptionsBuildingEvent $event): void
{
global $user;

$sb = $event->panel->create_new_block("Filters");
$sb->add_longtext_option("filter_tags", 'Default filtered tags');
$sb->add_label("This controls the tags which are hidden by default. This feature currently requires JavaScript. Separate filters by line, or by commas. You can enter multiple tags per filter, as well as negative tags.");
}
}
Loading

0 comments on commit 29cf5ff

Please sign in to comment.