SensitiveContentPlugin.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Adds a setting to allow a user to hide #NSFW-hashtagged notices behind a
  18. * blocker image until clicked.
  19. *
  20. * @package GNUsocial
  21. * @author MoonMan
  22. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Class SensitiveContentPlugin
  28. * Handles the main UI stuff
  29. *
  30. * @package GNUsocial
  31. * @author MoonMan
  32. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  33. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  34. */
  35. class SensitiveContentPlugin extends Plugin
  36. {
  37. public $blockerimage = 'img/blocker.png';
  38. public $hideforvisitors = false;
  39. const PLUGIN_VERSION = '0.1.0';
  40. public function onPluginVersion(array &$versions): bool
  41. {
  42. $versions[] = [
  43. 'name' => 'Sensitive Content',
  44. 'version' => self::PLUGIN_VERSION,
  45. 'author' => 'MoonMan',
  46. 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/SensitiveContent/',
  47. 'description' => _m('Mark, hide/show sensitive notices like on Twitter.'),
  48. ];
  49. return true;
  50. }
  51. /**
  52. * What blocker image to use?
  53. *
  54. * @return string
  55. */
  56. public function getBlockerImage(): string
  57. {
  58. return Plugin::staticPath('SensitiveContent', '') . $this->blockerimage;
  59. }
  60. /**
  61. * @param $profile
  62. * @return bool
  63. */
  64. public function shouldHide($profile): bool
  65. {
  66. if (isset($profile) && $profile instanceof Profile) {
  67. return $this->getHideSensitive($profile);
  68. }
  69. return $this->hideforvisitors;
  70. }
  71. public function getHideSensitive(Profile $profile): bool
  72. {
  73. $c = Cache::instance();
  74. // if (!empty($c)) {
  75. // $hidesensitive = $c->get(Cache::key('profile:hide_sensitive:'.$profile->id));
  76. // if (is_numeric($hidesensitive)) {
  77. // return (bool) $hidesensitive;
  78. // } else {
  79. // return false;
  80. // }
  81. // }
  82. $hidesensitive = $profile->getPref('MoonMan', 'hide_sensitive', '0');
  83. if (!empty($c)) {
  84. //not using it yet.
  85. $c->set(Cache::key('profile:hide_sensitive:' . $profile->id), $hidesensitive);
  86. }
  87. //common_log(LOG_DEBUG, "SENSITIVECONTENT hidesensitive? id " . $profile->id . " value " . (bool) $hidesensitive );
  88. if (is_null($hidesensitive)) {
  89. return false;
  90. }
  91. if (is_numeric($hidesensitive)) {
  92. return (bool)$hidesensitive;
  93. }
  94. return false;
  95. }
  96. /* GNU social EVENTS */
  97. public function onNoticeSimpleStatusArray($notice, &$twitter_status, $scoped)
  98. {
  99. $twitter_status['tags'] = $notice->getTags();
  100. }
  101. public function onTwitterUserArray($profile, &$twitter_user, $scoped)
  102. {
  103. if ($scoped instanceof Profile && $scoped->sameAs($profile)) {
  104. $twitter_user['hide_sensitive'] = $this->getHideSensitive($scoped);
  105. }
  106. }
  107. public function onRouterInitialized(URLMapper $m)
  108. {
  109. $m->connect('settings/sensitivecontent',
  110. ['action' => 'sensitivecontentsettings']);
  111. }
  112. public function onEndAccountSettingsNav($action)
  113. {
  114. $action->menuItem(common_local_url('sensitivecontentsettings'),
  115. _m('MENU', 'Sensitive Content'),
  116. _m('Settings for display of sensitive content.'));
  117. return true;
  118. }
  119. public function onEndShowStyles(Action $action)
  120. {
  121. $blocker = $this->getBlockerImage();
  122. $styles = <<<EOB
  123. /* default no show */
  124. html .tagcontainer > footer > .attachments > .inline-attachment > .attachment-wrapper > .sensitive-blocker {
  125. display: none;
  126. }
  127. html[data-hidesensitive='true'] .tagcontainer.data-tag-nsfw > footer > .attachments > .inline-attachment > .attachment-wrapper > .sensitive-blocker {
  128. display: block;
  129. /* hugh, two magic numbers below, sorry :( */
  130. width: 98%;
  131. height: 90%;
  132. position: absolute;
  133. /* z-index: 100; */
  134. /* background-color: #d4baba; */
  135. background-color: black;
  136. background-image: url({$blocker});
  137. background-repeat: no-repeat;
  138. background-position: center center;
  139. background-size: contain;
  140. transition: opacity 1s ease-in-out;
  141. }
  142. html[data-hidesensitive='true'] .tagcontainer.data-tag-nsfw > footer > .attachments > .inline-attachment > .attachment-wrapper > .sensitive-blocker.reveal {
  143. opacity: 0;
  144. }
  145. EOB;
  146. $action->style($styles);
  147. }
  148. public function onStartShowAttachmentRepresentation($out, $file)
  149. {
  150. $classes = 'sensitive-blocker'; //'sensitive-blocker';
  151. $thumbnail = null;
  152. try {
  153. $thumbnail = $file->getThumbnail();
  154. } catch (Exception $e) {
  155. $thumbnail = null;
  156. }
  157. // $thumb_width_css = $thumbnail ? $thumbnail->width . 'px' : '100%';
  158. // $thumb_height_css = $thumbnail ? $thumbnail->height . 'px' : '100%';
  159. $out->elementStart('div', [
  160. 'class' => 'attachment-wrapper',
  161. /* 'style' => "height: {$thumb_height_css}; width: {$thumb_width_css};", */
  162. ]); // needs height of thumb
  163. $out->elementStart('div', [
  164. 'class' => $classes,
  165. 'onclick' => 'toggleSpoiler(event)',
  166. /* 'style' => "height: {$thumb_height_css}; width: {$thumb_width_css};", */
  167. ]);
  168. $out->raw('&nbsp;');
  169. $out->elementEnd('div');
  170. }
  171. public function onEndShowAttachmentRepresentation($out, $file)
  172. {
  173. $out->elementEnd('div');
  174. }
  175. public function onEndShowScripts(Action $action)
  176. {
  177. $profile = $action->getScoped();
  178. $hidesensitive = $this->shouldHide($profile);
  179. $hidesensitive_string = $hidesensitive ? 'true' : 'false';
  180. $inline = <<<EOB
  181. window.hidesensitive = {$hidesensitive_string};
  182. function toggleSpoiler(evt) {
  183. if (window.hidesensitive) evt.target.classList.toggle('reveal');
  184. }
  185. EOB;
  186. $action->inlineScript($inline);
  187. }
  188. public function onEndOpenNoticeListItemElement(NoticeListItem $nli)
  189. {
  190. $rawtags = $nli->getNotice()->getTags();
  191. $classes = 'tagcontainer';
  192. foreach ($rawtags as $tag) {
  193. $classes = $classes . ' data-tag-' . $tag;
  194. }
  195. $nli->elementStart('span', ['class' => $classes]);
  196. //$nli->elementEnd('span');
  197. }
  198. public function onStartCloseNoticeListItemElement(NoticeListItem $nli)
  199. {
  200. $nli->elementEnd('span');
  201. }
  202. public function onStartHtmlElement($action, &$attrs)
  203. {
  204. $profile = Profile::current();
  205. $hidesensitive = $this->shouldHide($profile);
  206. $attrs = array_merge($attrs,
  207. ['data-hidesensitive' => ($hidesensitive ? 'true' : 'false')]
  208. );
  209. }
  210. /* Qvitter's EVENTS */
  211. public function onQvitterEndShowHeadElements(Action $action)
  212. {
  213. $blocker = $this->getBlockerImage();
  214. common_log(LOG_DEBUG, 'SENSITIVECONTENT ' . $blocker);
  215. $styles = <<<EOB
  216. .sensitive-blocker {
  217. display: none;
  218. }
  219. div.stream-item.notice.sensitive-notice .sensitive-blocker {
  220. display: block;
  221. width: 100%;
  222. height: 100%;
  223. position: absolute;
  224. z-index: 100;
  225. background-color: #d4baba;
  226. background-image: url({$blocker});
  227. background-repeat: no-repeat;
  228. background-position: center center;
  229. background-size: contain;
  230. transition: opacity 1s ease-in-out;
  231. }
  232. .sensitive-blocker:hover {
  233. opacity: .5;
  234. }
  235. div.stream-item.notice.expanded.sensitive-notice .sensitive-blocker {
  236. display: none;
  237. background-color: transparent;
  238. background-image: none;
  239. }
  240. EOB;
  241. $action->style($styles);
  242. }
  243. public function onQvitterEndShowScripts(Action $action)
  244. {
  245. $action->script(Plugin::staticPath('SensitiveContent', '') . 'js/sensitivecontent.js');
  246. }
  247. }