From ac6a59914b0c1a20f084f9ae9a3136b28c89493e Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 7 Mar 2021 13:22:38 +0300 Subject: nsfw: support API clients --- plugins/nsfw/init.js | 14 ++++++++++---- plugins/nsfw/init.php | 46 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 46 insertions(+), 14 deletions(-) (limited to 'plugins/nsfw') diff --git a/plugins/nsfw/init.js b/plugins/nsfw/init.js index 4bc2443e8..71fe4747b 100644 --- a/plugins/nsfw/init.js +++ b/plugins/nsfw/init.js @@ -2,11 +2,17 @@ Plugins.NSFW = { toggle: function(elem) { - const content = elem.domNode.parentNode.querySelector(".nswf.content"); + elem = elem.domNode || elem; - if (content) { - Element.toggle(content); - } + const content = elem.closest(".nsfw-wrapper").querySelector('.nsfw-content'); + + // we can't use .toggle() here because this script could be invoked by the api client + // so it's back to vanilla js + + if (content.style.display == 'none') + content.style.display = ''; + else + content.style.display = 'none'; } } diff --git a/plugins/nsfw/init.php b/plugins/nsfw/init.php index 112c7d7c2..4279a397b 100644 --- a/plugins/nsfw/init.php +++ b/plugins/nsfw/init.php @@ -12,9 +12,11 @@ class NSFW extends Plugin { function init($host) { $this->host = $host; - $host->add_hook($host::HOOK_RENDER_ARTICLE, $this); - $host->add_hook($host::HOOK_RENDER_ARTICLE_CDM, $this); - $host->add_hook($host::HOOK_PREFS_TAB, $this); + $host->add_hook(PluginHost::HOOK_RENDER_ARTICLE, $this); + $host->add_hook(PluginHost::HOOK_RENDER_ARTICLE_CDM, $this); + $host->add_hook(PluginHost::HOOK_RENDER_ARTICLE_API, $this); + $host->add_hook(PluginHost::HOOK_ARTICLE_IMAGE, $this); + $host->add_hook(PluginHost::HOOK_PREFS_TAB, $this); } @@ -22,22 +24,46 @@ class NSFW extends Plugin { return file_get_contents(__DIR__ . "/init.js"); } - function hook_render_article($article) { - $tags = array_map("trim", explode(",", $this->host->get($this, "tags"))); - $a_tags = array_map("trim", explode(",", $article["tag_cache"])); + function hook_article_image($enclosures, $content, $site_url, $article) { + $tags = explode(",", $this->host->get($this, "tags")); + $article_tags = $article["tags"]; + + if (count(array_intersect($tags, $article_tags)) > 0) { + return [Config::get_self_url() . "/plugins/nsfw/nsfw.png", "", "nsfw", []]; + } else { + return ["", "", $content]; + } + } + + private function rewrite_contents($article, bool $add_api_js = false) { + $tags = explode(",", $this->host->get($this, "tags")); + $article_tags = $article["tags"]; - if (count(array_intersect($tags, $a_tags)) > 0) { - $article["content"] = "
". + if (count(array_intersect($tags, $article_tags)) > 0) { + $article["content"] = "
". \Controls\button_tag(__("Not work safe (click to toggle)"), '', ['onclick' => 'Plugins.NSFW.toggle(this)']). - " + "
"; + + if ($add_api_js) { + $article["content"] .= ""; + } } return $article; } + function hook_render_article_api($row) { + $article = isset($row['headline']) ? $row['headline'] : $row['article']; + return $this->rewrite_contents($article, true); + } + + function hook_render_article($article) { + return $this->rewrite_contents($article); + } + function hook_render_article_cdm($article) { - return $this->hook_render_article($article); + return $this->rewrite_contents($article); } function hook_prefs_tab($args) { -- cgit v1.2.3