Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tombstone support #900

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions ext/tombstones/info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

class TombstonesInfo extends ExtensionInfo
{
public const KEY = "tombstones";

public $key = self::KEY;
public $name = "Tombstones";
public $url = self::SHIMMIE_URL;
public $authors = self::SHISH_AUTHOR;
public $license = self::LICENSE_GPLV2;
public $description = "Provide a marker listing some details about deleted posts";
}
72 changes: 72 additions & 0 deletions ext/tombstones/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php declare(strict_types=1);

use MicroCRUD\ActionColumn;
use MicroCRUD\StringColumn;
use MicroCRUD\DateColumn;
use MicroCRUD\TextColumn;
use MicroCRUD\Table;

class TombstoneTable extends Table
{
public function __construct(\FFSPHP\PDO $db)
{
parent::__construct($db);
$this->table = "tombstones";
$this->base_query = "SELECT * FROM tombstones";
$this->primary_key = "post_id";
$this->size = 100;
$this->limit = 1000000;
$this->set_columns([
new IntColumn("post_id", "Post"),
new StringColumn("hash"),
new DateColumn("date", "Date"),
new TextColumn("message", "Message"),
]);
$this->order_by = ["date DESC", "post_id"];
$this->table_attrs = ["class" => "zebra"];
}
}

class Tombstones extends Extension
{
/** @var TombstoneTheme */
protected $theme;

public function onDatabaseUpgrade(DatabaseUpgradeEvent $event)
{
global $database;
if ($this->get_version("ext_tombstone_version") < 1) {
$database->create_table("tombstones", "
post_id INTEGER NOT NULL,
hash CHAR(32) NOT NULL,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
message TEXT NOT NULL
");
$this->set_version("ext_tombstone_version", 1);
}
}

public function onPageRequest(PageRequestEvent $event)
{
global $database, $page, $user;

if ($event->page_matches("post/view")) {
$post_id = int_escape($event->get_arg(0));

$tombstone = $database->get_one("SELECT * FROM tombstones WHERE post_id=:post_id", ["post_id"=>$post_id]);

if (!is_null($tombstone)) {
$this->theme->display_tombstone($tombstone);
}
}
}

public function onAddImageHashBan(AddImageHashBanEvent $event)
{
global $database;
$database->execute(
"UPDATE tombstones SET message=message || :reason WHERE hash=:hash",
["hash"=>$event->hash, "reason"=>"\n" . $event->reason]
);
}
}
52 changes: 52 additions & 0 deletions ext/tombstones/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types=1);
class TombstonesTest extends ShimmiePHPUnitTestCase
{
private $hash = "feb01bab5698a11dd87416724c7a89e3";

public function testPages()
{
$this->log_in_as_admin();
$page = $this->get_page("image_hash_ban/list");
$this->assertEquals(200, $page->code);
}

public function testBan()
{
$this->log_in_as_admin();

// Post image
$image_id = $this->post_image("tests/pbx_screenshot.jpg", "pbx");
$page = $this->get_page("post/view/$image_id");
$this->assertEquals(200, $page->code);

// Ban & delete
send_event(new AddImageHashBanEvent($this->hash, "test hash ban"));
send_event(new ImageDeletionEvent(Image::by_id($image_id), true));

// Check deleted
$page = $this->get_page("post/view/$image_id");
$this->assertEquals(404, $page->code);

// Can't repost
try {
$this->post_image("tests/pbx_screenshot.jpg", "pbx");
$this->assertTrue(false);
} catch (UploadException $e) {
$this->assertTrue(true);
}

// Remove ban
send_event(new RemoveImageHashBanEvent($this->hash));

// Can repost
$image_id = $this->post_image("tests/pbx_screenshot.jpg", "pbx");
$page = $this->get_page("post/view/$image_id");
$this->assertEquals(200, $page->code);
}

public function onNotSuccessfulTest(Throwable $t): void
{
send_event(new RemoveImageHashBanEvent($this->hash));
parent::onNotSuccessfulTest($t); // TODO: Change the autogenerated stub
}
}
30 changes: 30 additions & 0 deletions ext/tombstones/theme.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);
use function MicroHTML\INPUT;

class TombstonesTheme extends Themelet
{
/*
* Show all the bans
*/
public function display_bans(Page $page, $table, $paginator)
{
$page->set_title("Post Bans");
$page->set_heading("Post Bans");
$page->add_block(new NavBlock());
$page->add_block(new Block("Edit Post Bans", $table . $paginator));
}

/*
* Display a link to delete an image
*/
public function get_buttons_html(Image $image)
{
return (string)SHM_SIMPLE_FORM(
"image_hash_ban/add",
INPUT(["type"=>'hidden', "name"=>'c_hash', "value"=>$image->hash]),
INPUT(["type"=>'hidden', "name"=>'c_image_id', "value"=>$image->id]),
INPUT(["type"=>'text', "name"=>'c_reason']),
INPUT(["type"=>'submit', "value"=>'Ban Hash and Delete Post']),
);
}
}